paint-brush
kubectl tip of the day: wait like a bossby@mhausenblas
25,695 reads
25,695 reads

kubectl tip of the day: wait like a boss

by Michael HausenblasOctober 27th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How do you wait for something to happen with <code class="markup--code markup--p-code">kubectl</code>? I used to use a <code class="markup--code markup--p-code">while true</code> <a href="https://hackernoon.com/tagged/loop" target="_blank">loop</a> in a shell <a href="https://hackernoon.com/tagged/script" target="_blank">script</a> and check with a complicated <code class="markup--code markup--p-code">kubectl get</code> command until I’d see a certain condition, such as <code class="markup--code markup--p-code">condition=Ready</code> would be met. No more!&nbsp;:)

Company Mentioned

Mention Thumbnail
featured image - kubectl tip of the day: wait like a boss
Michael Hausenblas HackerNoon profile picture

How do you wait for something to happen with kubectl? I used to use a while true loop in a shell script and check with a complicated kubectl get command until I’d see a certain condition, such as condition=Ready would be met. No more! :)

Meet the kubectl wait command and see it in action here.

First, let’s create a job called worker that does something utterly useless in itself (print the word blah to stdout and pause for 3 seconds ten times):



$ kubectl version --shortClient Version: v1.12.0Server Version: v1.11.0

$ kubectl create ns waitplayground





$ kubectl -n waitplayground \create job worker \--image centos:7 -- \sh -c \'for i in {1..10} ; do echo blah ; sleep 3; done'

You could keep an eye on the resources with:

$ kubectl -n waitplayground get job,po

But what if you’d like to kick off another job after worker has completed? Here you go:




$ kubectl -n waitplayground \wait --for=condition=complete --timeout=32s \job/workerjob.batch/worker condition met

Note that above I’ve set the timeout (32 sec) slightly higher than what I’d expect the worker job to take (ca. 10 * 3 sec). Once the kubectl wait command returns, you just need to inspect its output and you can then make a decision based on this to, for example, launch a dependent job or retry the original one.

That was it, happy weekend and keep kubecuddling ;)