使用 shell 脚本提取 kubernetes pod 名称

Var*_*ini 0 bash shell grep kubernetes kubectl

我想从 kubernetes 上可用的 pod 列表中提取 pod 名称。例如,对于以下命令

kubectl get pods -n 命名空间

NAME          READY   STATUS            RESTARTS   AGE
pod1          1/1     Running           2          46d
pod2          1/1     Running           0          46d
test-pod3-yy  0/1     ImagePullBackOff  0          338d
test-pod3-xx  1/1     Running           0          255d
Run Code Online (Sandbox Code Playgroud)

test-pod3-xx我想使用 shell 脚本提取 pod 。目前这是我正在使用的命令

POD_NAME=$(kubectl get pods -n namespace | grep testpod-3 | cut -d' ' -f1)
Run Code Online (Sandbox Code Playgroud)

这样我就得到了两个 Pod test-pod3-yytest-pod3-xx但我想提取处于运行状态的 Pod。我怎样才能做到这一点?

the*_*dog 5

您只能使用field-selector并检查是否正在运行:

--field-selector=status.phase=Running

您还可以使用该-o name标志来仅获取名称。这样你就会得到:

$ kubectl get pods -n namespace -o name --field-selector=status.phase=Running
pod/pod1
pod/pod2
pod/test-pod3-xx
Run Code Online (Sandbox Code Playgroud)