Sobes.tech
Senior

What happens in Kubernetes when you run the command kubectl apply -f deployment.yaml?

sobes.tech AI

Answer from AI

When you run the command kubectl apply -f deployment.yaml, the following happens:

  1. Reading and parsing the manifest: kubectl reads the deployment.yaml file and parses it into a Kubernetes API object.

  2. Determining the current state: kubectl contacts the Kubernetes API server to get the current state of the object described in the manifest (for example, a Deployment with the same name and namespace).

  3. Calculating the difference (diff): kubectl compares the current state of the object with the desired state described in deployment.yaml.

  4. Applying changes: If there are differences, kubectl sends a PATCH or PUT request to the API server to update the object. If the object does not exist, it is created.

  5. Updating the state in etcd: The API server saves the new state of the object in the etcd storage.

  6. Controller reactions: Kubernetes controllers (for example, the Deployment Controller) notice the changes and start bringing the actual state in line with the desired state — creating/deleting/updating pods.

Thus, kubectl apply implements declarative cluster state management, allowing you to describe the desired state in manifests and automatically synchronize the cluster with this state.