The purpose of --dry-run

kubectl run nginx --image=nginx --dry-run=client -o yaml

From the above kubectl command I can understand that we are creating a pod with image nginx running on it “-o yaml” refers the output type .
why I need to append the " --dry-run"?
when I need to append it?

--dry-run=client checks the syntax of your command, but does not actually send it to the server. --dry-run=client -o yaml goes a step further: it generates the YAML that would do the job, but does not actually create anything on the server.

This is handy because if you do have YAML and want to check for bad YAML formatting or for illegal arguments, this is the fastest way to do it. Also, if you have a YAML file and want to check for errors in it,

kubectl apply -f myster-file.yaml

will do that error check.

Especially for pods, which you really can’t fix after you create them, --dry-run saves you time by helping you find errors efficiently.

1 Like

Not forgetting of course the main use of this for time saving in the exam!

Say you have a question that asks to create a pod of nginx, and you have to add some other things to it like an emptyDir volume. Then you do

kubectl run nginx --image nginx --dry-run=client -o yaml > pod.yaml

That has created most of the YAML you need for the pod. Then you edit pod.yaml in vi and add in the additional features requested (e.g. the emptyDir), and then

kubectl apply -f pod.yaml

This has saved you the time of

  1. Finding pod YAML in documentation
  2. Copy/paste that into vi

Probably up to 2 minutes depending on how fast you are. Time is everything in exam!

1 Like

Note that only some resources can be created imperatively like that. Resources like PersistentVolume you still have to do by pasting from documentation.

What can be templated out with --dry-run=client -o yaml are listed under the create section of this document, all via kubectl create ... --dry-run=client -o yaml and Pods using kubectl run

Thanks rob for such clear explanation

Thanks alistair for sharing this