Silly Kubectl Trick #5: Waiting for Kubernetes
To wait for a single pod to do something, we can use the wait command. For deployments, you can use kubectl wait or kubectl rollout status.
Join the DZone community and get the full member experience.Join For Free
When you deploy stuff for a living, you find yourself waiting around, a lot. Wait for Terraform to spin up the AWS VPCs. Wait for the Kubernetes cluster node VMs to boot. Wait for the Kubernetes cluster to coalesce. Wait for the CNI pods, DNS pods, and kube-proxy bits to be happy. Wait, wait, wait.
kubectl feels your pain there, and has some nifty ways of helping you wait (or not!) for the things you deploy or delete to coalesce.
To wait for a single pod to do something, we can use the
wait command, and pause until a condition is met on the pod:
Here's a more rigorous shell session, with
date bounding so you can better visualize the passage of time:
You'll probably want to wait for either ready, initialized, or containersReady. If you want, you can even define your own readiness gates, which will delay the ready condition until your custom gates resolve to true.
You can find out more, here, in the official Kubernetes documentation.
Bigger Things Than Pods
Chances are you aren't dealing directly with individual pods, but are concerned chiefly with Deployments, StatefulSets, or DaemonSets. For deployments, you can use
kubectl rollout status
This command will block while the entirety of the deployment is incrementally "rolled out" and comes up to a state of readiness. As the pods in the current live replica set spin up.
kubectl rollout status prints helpful progress updates, to keep you abreast of what's happening while you wait.
This works for StatefulSets and DaemonSets, too.
You can also use
kubectl wait for deployments:
Note that the condition for a deployment is available, not ready.
kubectl wait to be a useful tool for change automation, properly ordering things outside of Kubernetes, and general Zen-like meditative states. We hope you will too!
Published at DZone with permission of . See the original article here.
Opinions expressed by DZone contributors are their own.