2

我正在尝试创建一个 shell 脚本,该脚本将通过检查显示 1/1 的 READY 标题来验证某些 pod 是否已准备好。我尝试了两种方法。

1.

ready=$(oc get pods | awk '{print $2}' | tail -n +2) # prints 1/1 or 0/1 for each pod
until [[ ${ready} == "1/1" ]]
do
  echo "Waiting for pods to be ready."
  sleep 3
done

即使 pod 已准备好并在 READY 列中显示 1/1,上面的脚本也会一直说“等待 pod 准备好”。

2.

while true ; do
  for i in 1 2 3; do
  ready=`oc get pods | awk '{print $2}' | tail -n +2 | head -n $i` 

  if [[ "${ready}" == "1/1" ]]; then
    echo "pods are up and running"
  else
    echo "waiting for pods to be ready"
  sleep 10
  break
  fi
  done
done

上面的脚本只是不断地打印等待 pod 准备好并且 pod 启动并运行。

任何帮助将不胜感激,我从 Bash 开始,不太确定该怎么做。

4

3 回答 3

4

我很惊讶到目前为止没有人提到实验性的,但官方的kubectl wait
$ kubectl wait ([-f FILENAME] | resource.group/resource.name | resource.group [(-l label | --all)]) [--for=delete|--for condition=available]

于 2021-05-17T17:43:35.957 回答
4

以下解决方案对我有用

while [ "$(kubectl get pods -l=app='activemq' -o jsonpath='{.items[*].status.containerStatuses[0].ready}')" != "true" ]; do
   sleep 5
   echo "Waiting for Broker to be ready."
done
于 2020-10-14T13:19:13.587 回答
2

对于 pod 状态,唯一的答案(一个 pod 可以正在运行但尚未准备好!):

kubectl get pods -l <key=val> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}'

对于单个容器状态(需要为“true”):

kubectl get pod <pod_name> --output="jsonpath={.status.containerStatuses[*].ready}" | cut -d' ' -f2

如果要检查多个 pod 的状态,可以使用 -l 进行过滤,只需添加前缀.items[*]

kubectl get pods -l <key=val> --output="jsonpath={.items[*].status.conditions[*].status}"
于 2020-10-23T10:47:07.977 回答