Sub topic 1
Declarative Configuration and Desired State
Prerequisites
This chapter assumes you can use a terminal, know what a container image is, and have a high-level idea of what a cluster, node, and Pod are. To follow the examples you need a cluster (a local one is fine) with kubectl configured.
Imperative and declarative styles
Kubernetes accepts changes in two styles.
- Imperative: you tell the cluster what to do, one step at a time. For example,
kubectl create deployment web --image=nginx:1.27orkubectl scale deployment web --replicas=3. - Declarative: you write down what you want the cluster to look like, in a file, and submit it. For example,
kubectl apply -f deployment.yaml.
Imperative commands are quick for experiments. Their weakness is that the commands leave no record. If you must rebuild the environment, you have to remember every step you ran and the order you ran them in.
A declarative file is the record. You can review it, keep it in version control, and apply it again to get the same result. The rest of this tutorial uses the declarative style.
Desired state and the reconciliation loop
A manifest describes desired state: the objects you want to exist and how they should be configured. The cluster also has observed state, which is what is actually running right now.
Controllers inside Kubernetes compare the two. When they differ, a controller acts to close the gap. This repeating cycle is called a reconciliation loop.
Suppose a Deployment asks for 2 replicas and one Pod crashes. You did not run any command, yet the controller notices only 1 Pod exists and starts another. You declared an outcome, and the cluster keeps working toward it.
A first example
Here is a minimal declaration of one object.
apiVersion: v1
kind: Namespace
metadata:
name: demoSave it as namespace.yaml and submit it:
kubectl apply -f namespace.yaml
kubectl get namespace demoThe file says "a Namespace named demo should exist." It does not say "create" or "update". Run the same apply a second time and the cluster reports that nothing changed, because the observed state already matches the desired state. This property is called idempotence: repeating the operation produces the same result.
Now edit a file that describes a Deployment, change replicas: 2 to replicas: 4, and apply it again. You do not write a "scale" command. You change the declaration, and the controller adds the Pods needed to reach 4.
Common mistakes
- Mixing styles on the same object. If you create a Deployment with
kubectl applyand later change it withkubectl scale, the live object no longer matches your file. The nextapplyof the file may silently revert the replica count. - Editing live objects with
kubectl edit. The change works, but your file is now out of date and the nextapplycan overwrite it. - Assuming apply is instant.
applyrecords the desired state. Controllers then do the work, so the cluster can take seconds to catch up. Check progress withkubectl getafterward. - Expecting a fix for bad desired state. The loop makes reality match the declaration. If the declaration is wrong, such as a mistyped image tag, the cluster faithfully tries to run the wrong thing.
What comes next
The next chapters cover the YAML syntax and the structure of a manifest, then Deployments, Services, and the kubectl workflow for previewing and applying changes.
Exercise
You applied a Deployment file with replicas: 2. A teammate then ran kubectl scale deployment web --replicas=5. What can happen the next time you run kubectl apply -f deployment.yaml, and how do you prevent the mismatch?
Show answer
The apply can set replicas back to 2, because your file still declares 2. To prevent this, make changes only in the file (set replicas: 5 and apply it) and keep that file in version control as the single source of truth.
Exercise
A Pod owned by a Deployment is deleted by hand. Without running any other command, what do you expect to happen, and which concept explains it?
Show answer
The Deployment's controller sees fewer Pods than the desired replica count and creates a replacement. This is the reconciliation loop comparing desired state with observed state.