如何让 bash 脚本等到 kubernetes pods 重启?

How do I make bash script wait until kubernetes pods restart?

我正在写一个脚本,我想用scale-down scale-up的方式重启kubernetespods

kubectl scale --replicas=0 myPod -n myNamespace
kubectl scale --replicas=3 myPod -n myNamespace

我希望脚本等到 pods 为 Running - 所以我想像

while kubectl get pods --field-selector=status.phase=Running -n myNameSpace | grep -c myPod = 3;
    do
        sleep 1
        echo "."
    done

可以工作 - 但没有骰子。 = 3 部分不起作用。 我不能只使用

while kubectl get pods --field-selector=status.phase!=Running -n myNameSpace | grep -c myPod > /dev/null

因为 pods 按顺序启动,我可能会因为一个 pod 部署而查询而感到不走运,而其他 pod 甚至没有启动。

如何确保脚本仅在 pods 中的所有 3 个都为 Running 后才继续?

在 [ ] 中写下你的条件,并用 ` 或 $ 获取命令的值。例如你的情况:

while [ "$(kubectl get pods --field-selector=status.phase=Running -n myNameSpace | grep -c myPod)" != 3 ]

  do
    sleep 1
    echo "wait"
  done

echo "All three pods is running and continue your script"