【问题标题】:Shell script should wait until kubernetes pod is running [duplicate]Shell脚本应该等到kubernetes pod运行[重复]
【发布时间】:2021-12-17 01:21:23
【问题描述】:

在一个简单的 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

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

【问题讨论】:

  • stackoverflow.com/q/65938572/4957508 有帮助吗? (可能在问题中回答)
  • 我知道,@Aserre;我指的是问题,其中包括kubectl wait --for condition=Ready ... 作为潜在的解决方案。
  • 对不起,我并不是真的要关闭它;我没有意识到它有一个bash 标签——如果答案不能解决您的问题,请联系我,我很乐意重新打开。

标签: bash shell kubectl kubernetes-pod


【解决方案1】:

正如 cmets 中所述,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

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

<pod-name> condition met

到标准输出。


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

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

或按标签:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 2021-02-19
    • 2022-01-15
    • 2021-05-07
    • 1970-01-01
    • 2016-08-03
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多