Bash script to check if openshift/kubernetes pods are ready
Asked Answered
F

3

6

I am trying to create a shell script that will validate that certain pods are ready by checking the READY heading showing 1/1. I have tried two ways.

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

Above script just keeps saying "Waiting for pods to be ready" even when the pods are ready and displaying 1/1 in the READY column.

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

Above script just continually prints waiting for pods to be ready and pods are up and running.

Any help would be appreciated, I am starting with Bash and not quite sure what to do.

Faradic answered 20/5, 2020 at 21:17 Comment(4)
why do you need the scripts? Are you using pod directly, or its sub resources of deployment? Using pod directly is not recommended. If you have a deployment, did you try oc rollout status ?Ode
Take a look at https://mcmap.net/q/1630985/-openshift-commands-to-catch-pod-name-programmatically-scripting. You really shouldn't be relying on 1/1, there are more and better ways to determine exactly what pods are ready.Horrify
@C Han, we use scripts to deploy our apps sort of like Helm as we don't yet have Helm.Faradic
I am trying another option to poll the readiness and here is what I did. status=$(oc get pods -o jsonpath='{.items[*].status.containerStatuses[?(@.ready)].ready}') for stat in $(oc get pods -o jsonpath='{.items[*].status.containerStatuses[?(@.ready)].ready}'); do until [[ "${stat}" == "true" ]]; do echo "waiting for pods to be ready" sleep 10 done done echo "pods are ready"Faradic
F
13

I'm surprised no one so far has mentioned the experimental, yet official kubectl wait:
$ kubectl wait ([-f FILENAME] | resource.group/resource.name | resource.group [(-l label | --all)]) [--for=delete|--for condition=available]

Feat answered 17/5, 2021 at 17:43 Comment(4)
This answer should be way on top! kubectl -n mynamespace wait pod --for=condition=Ready -l labelKey1=labelValue1,labelKey2=labelValue2Tot
After my initial excitement on this response: this condition will wait ONLY if the pod exists (i.e. managed to be created). In my script, after helm install pod still needs some time to appear hence the command fails with Error from server (NotFound): pods "proc-s1e2-0" not foundBioluminescence
I suppose this was resolved by the time, but for anyone wondering there is a wait flag on helm install command.Snowwhite
Command to wait for pod to be in "Ready" state with a timeout of 60 seconds: kubectl wait --for=condition=Ready pod/<name-of-the-pod> --timeout=60s -n <name-of-the-namespace> 1. If the pod is ready within the timeout period (60 seconds), it will exit with return code 0 and outputs a message pod/<name-of-the-pod> condition met 2. If the pod is not ready even after 60 seconds, timeout occurs and it will exit with return code 1 and outputs a message error: timed out waiting for the condition on pod/<name-of-the-pod>Malvia
T
6

Following solutions works for me

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
Trailblazer answered 14/10, 2020 at 13:19 Comment(0)
H
4

For pod status, the one and only answer (a pod can be Running but not yet ready!):

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

For individual container statuses (need to be "true"):

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

If you want to check the status of several pods, you can use -l to filter and just prefix .items[*]:

kubectl get pods -l <key=val> --output="jsonpath={.items[*].status.conditions[*].status}"
Huckaby answered 23/10, 2020 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.