A kubectl
command can be used to create, delete or updated resources on a Kubernetes cluster.
With the --dry-run=(client|server)
flag, the kubectl
command can be used to only preview an object, without really submitting it to the Kubernetes cluster.
The dry-run mode is useful to see what will the kubectl
command do without actually changing anything.
This short note shows different examples of how to run the kubectl
command in the dry-run mode.
Cool Tip: How to increase a verbosity of the kubectl
command! Read more →
Kubectl Dry Run
Use the --dry-run=client
or --dry-run=server
options with the kubectl
command to see what it will do, without actually changing anything, for example:
$ kubectl apply -f deployment.yaml --dry-run=client
Option | Description |
---|---|
--dry-run=client |
A local dry-run, without accessing the Kubernetes cluster. In this case the object won’t be validated by the Kubernetes API server. |
--dry-run=server |
The object will be sent to the Kubernetes cluster for validation, but as it is a dry-run mode, nothing actually will be changed. |
The dry-run can also be used with a Kustomize, for example:
$ kubectl apply -k <directory> --dry-run=server
The dry-run with the -o yaml
option can be used to print the object in YAML:
$ kubectl apply -k <directory> --dry-run=client -o yaml
To save it to a file, execute:
$ kubectl apply -k <directory> --dry-run=client -o yaml > manifest.yaml
Kubectl Diff
To see the actual differences between the current live configuration and the configuration as it would be if applied, use the kubectl diff
command.
For example, to shows the actual changes that will be performed by applying a given YAML file, execute:
$ kubectl diff -f <file>
To show the changes that will be performed by processing a given kustomization directory:
$ kubectl diff -k <kustomizationDirectory>
All the options that can be applied to the kubectl diff
command:
Option | Description |
---|---|
-f , --filename <path> |
Filename, directory, or URL to files identifying the resource. |
-k , --kustomize <path> |
Filename, directory, or URL to files identifying the resource. Process the kustomization directory. This flag can’t be used together with -f or -R . |
-R , --recursive |
Process the directory used in -f , --filename recursively. Useful when you want to manage related manifests organized within the same directory. |
--field-manager <fieldManager> |
Name of the manager used to track field ownership. |
--force-conflicts |
If set, server-side apply will force the changes against conflicts. |
--server-side |
If set, apply runs in the server instead of the client. |