Shell 脚本应等到 kubernetes pod 运行

use*_*695 5 bash shell kubectl kubernetes-pod

在一个简单的 bash 脚本中,我想运行多个kubectlhelm命令,例如:

helm install \
  cert-manager jetstack/cert-manager \
  --namespace cert-manager \
  --create-namespace \
  --version v1.5.4 \
  --set installCRDs=true
kubectl apply -f deploy/cert-manager/cluster-issuers.yaml
Run Code Online (Sandbox Code Playgroud)

我的问题是,在helm install命令之后我必须等到 cert-manager pod 运行,然后kubectl apply才能使用该命令。现在脚本调用它太早了,所以它会失败。

小智 9

正如评论中所述,kubectl wait这是要走的路。
示例来自kubectl wait --help

Examples:
  # Wait for the pod "busybox1" to contain the status condition of type "Ready"
  kubectl wait --for=condition=Ready pod/busybox1
Run Code Online (Sandbox Code Playgroud)

这样你的脚本将暂停,直到指定的 pod 正在运行,并将kubectl输出

<pod-name> condition met
Run Code Online (Sandbox Code Playgroud)

到标准输出。


kubectl wait仍处于实验阶段。如果您想避免实验性功能,您可以使用 bashwhile循环实现类似的结果。按 Pod 名称:

while [[ $(kubectl get pods <pod-name> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
   sleep 1
done
Run Code Online (Sandbox Code Playgroud)

或按标签:

while [[ $(kubectl get pods -l <label>=<label-value> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
   sleep 1
done
Run Code Online (Sandbox Code Playgroud)