Issue
I am writing a script, where I want to restart kubernetes pods with the scale-down scale-up method
kubectl scale --replicas=0 myPod -n myNamespace
kubectl scale --replicas=3 myPod -n myNamespace
I would like the script to wait until the pods are Running
- so I thought something like
while kubectl get pods --field-selector=status.phase=Running -n myNameSpace | grep -c myPod = 3;
do
sleep 1
echo "."
done
could work - but no dice. The = 3
part doesn't work.
I can't just use
while kubectl get pods --field-selector=status.phase!=Running -n myNameSpace | grep -c myPod > /dev/null
since the pods start in sequence, and I could get unlucky by querying just as one pod deployed, and others didn't even start.
How can I ensure that the script continues only after all 3 of the pods are Running
?
Solution
Write your condition in [ ] and get the value of command with ` or $. for example in your case:
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"
Answered By - Hamid Ostadvali