kubectl apply command and explain how Kubernetes manages object configurations declaratively. If you’re looking to understand how local configuration files interact with live objects in the Kubernetes cluster, read on.
Local Configuration Example
Consider the following local configuration file (nginx.yaml) that defines a simple Nginx Pod:
How Kubectl Apply Works
When you execute thekubectl apply command, Kubernetes considers three sources before making any changes:
- The local configuration file.
- The live object’s configuration present in the Kubernetes cluster.
- The last applied configuration stored on the live object as an annotation.
If the object does not exist in the cluster, Kubernetes creates it using the local configuration. The newly created object resembles the file you provided but includes additional fields (such as status information) added by the cluster.
The Last Applied Configuration
When you run:apply operations, Kubernetes compares the updated local configuration with the live configuration, using the stored annotation to compute any changes.
For instance, if you update the image version from 1.18 to 1.19 in your local file:
Merge Strategy for Changes
The following diagram explains how Kubernetes merges changes for both primitive and map fields. It details the actions taken based on the presence or absence of a field in the local, live, or last applied configuration:
Last Applied Configuration Storage
One crucial aspect of thekubectl apply process is that the last applied configuration is stored as an annotation within the live object’s metadata.
Consider the following updated local configuration file:
Only the
kubectl apply command stores the last applied configuration. Commands like kubectl create or kubectl replace do not maintain this history. To ensure accurate tracking of changes, always use the declarative approach with kubectl apply.Conclusion
This lesson has provided insight into the internal mechanics of thekubectl apply command and how Kubernetes manages changes to object configurations. By storing the last applied configuration as an annotation, Kubernetes simplifies the process of comparing and merging configuration changes. Use this knowledge to maintain a robust and declarative workflow in your Kubernetes deployments.
For more information on working with Kubernetes, be sure to explore the following links: