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:
-
Reading and parsing the manifest:
kubectlreads thedeployment.yamlfile and parses it into a Kubernetes API object. -
Determining the current state:
kubectlcontacts 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). -
Calculating the difference (diff):
kubectlcompares the current state of the object with the desired state described indeployment.yaml. -
Applying changes: If there are differences,
kubectlsends a PATCH or PUT request to the API server to update the object. If the object does not exist, it is created. -
Updating the state in etcd: The API server saves the new state of the object in the etcd storage.
-
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.