Kubernetes Explained Simply: Waiting For Kubernetes [Part 5]

Written by jameshunt | Published 2020/12/12
Tech Story Tags: kubernetes | opensource | tutorial | kubectl | k8s | devops | containers | hackernoon-top-story

TLDRvia the TL;DR App

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:
$ kubectl wait pod/slow --for condition=ready
pod/slow condition met
Here's a more rigorous shell session, with 
date
 bounding so you can better visualize the passage of time:
$ kubectl apply -f trick5/pod.yml
namespace/trick5 created
configmap/opt created
pod/slow created

$ date
Tue Feb 11 10:58:31 EST 2020

$ kubectl get pods
NAME   READY   STATUS     RESTARTS   AGE
slow   0/1     Init:0/1   0          1s

$ kubectl wait pod/slow --for condition=ready
pod/slow condition met

$ date
Tue Feb 11 10:58:39 EST 2020        # ~8s later!

$ kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
slow   1/1     Running   0          9s
You'll probably want to wait for either readyinitialized, 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.

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
kubectl rollout status deploy/slow
Waiting for deployment "slow" rollout to finish: 0 of 5 updated replicas are available...
Waiting for deployment "slow" rollout to finish: 1 of 5 updated replicas are available...
Waiting for deployment "slow" rollout to finish: 2 of 5 updated replicas are available...
Waiting for deployment "slow" rollout to finish: 3 of 5 updated replicas are available...
Waiting for deployment "slow" rollout to finish: 4 of 5 updated replicas are available...
deployment "slowdep" successfully rolled out
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.
You can also use 
kubectl wait
 for deployments:
$ kubectl wait deploy/slow --for condition=available
deployment.apps/slow condition met
Note that the condition for a deployment is available, not ready.
We find 
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!

Written by jameshunt | R&D at Stark & Wayne, finding software solutions to customer problems and changing them into executable best practices.
Published by HackerNoon on 2020/12/12