Kubectl run --restart command magic

While recently looking to creating Kubernetes manifests quickly for pods and deployments, I came across a really effective way of making either a job , deployment or pod manifest using the kubectl command.
The pivot on which type of resource is created is based around the "restart" value passed to kubectl command.

Lets me show you what I mean.

The kubectl run function is described in the kubernetes.io documentation  as being able to run a specified image on the cluster.

Check this command out first using the help function


This produces an extensive list of examples and options for running resources with kubectl, and so is a very useful way to remember kubectl commands.

But, lets specifically get information on the restart option. We can do this by filtering the output related to this using grep.


Now we can see the information related to the "restart" option and how different parameters for this will give us very different outcomes. The three options are:

1. Always
2. OnFailure
3. Never

As we can see from the kubectl --help output:
If set to 'Always' a deployment is created, if set to 'OnFailure' a job is created, if set to 'Never', a regular pod is created.

This is great news for speedily creating these very different resources by just changing the "restart" option

Let summarise these options and their sample use:

1. kubectl run optest --image=nginx  ---> creates a deployment called "optest" with 1 pod called "optest-<uniqueID>" , running an nginx container. Default restart=Always

2. kubectl run optest --image=nginx --restart=OnFailure  ---> creates a job called "optest" with 1 pod called "optest-<uniqueID>" , running an nginx container.

3. kubectl run optest --image=nginx --restart=Never  ---> creates a pod called "optest", running an nginx container. Default restart=Always


So, with a small bit of magic with the right options for the kubectl run command, we can create very different Kubernetes resources, at speed. Handy to know.

Comments