“apply” command in Kubernetes

I introduced you to the “create” command of the Kubernetes Controller to create new objects such as Pods, Deployments in the Kubernetes cluster. We can also do this using the “apply” command.

For example, I have a configuration file to create a new Pod with the following content:

You can use the “apply” command to create this new Pod, with the following syntax:

Where <path_to_configuration_file> is the path to the file that defines the information about the Kubernetes object that we need to initialize.

My results when running the command “apply” with the above configuration file:

as follows:

The difference between the “create” command and the “apply” command is: the “create” command can only be run once for a Kubernetes object of the same name, and the “apply” command if we have some changes to the Kubernetes object that we have created, we can use this command to update those changes.

For example, if you now run the above command again with the “create” command:

You will see the following error:

If I update the above configuration file as follows:

then when running the command “apply” again:

this command still succeeds:

and when checking the information of the Pod, you will see that this Pod is updated with Labels information as follows:

Add Comment