Sub topic 1
Declaring what you want instead of how to get it
Prerequisites
This chapter assumes you can:
- Run
kubectl get,apply, anddescribe. - Read a YAML manifest.
- Explain what a Pod and a Deployment are.
Go knowledge is not needed here. It becomes useful in later chapters.
Imperative and declarative styles
There are two ways to tell a system what to do.
- Imperative: you give commands that say how to reach a result. "Start a container. Start another. Start a third."
- Declarative: you describe the result you want and let the system work out the steps. "Three copies of this container should be running."
Kubernetes is built around the declarative style. An object you submit to the API server is a record of desired state, which is the condition you want the cluster to be in. You do not submit a list of steps.
A declarative example
This manifest asks for three replicas of an nginx container.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27Apply it and read it back:
kubectl apply -f web.yaml
kubectl get deployment webThe manifest does not say which node runs each Pod, in what order they start, or what to do if one crashes. Those decisions belong to the cluster. To scale up, you change replicas: 3 to replicas: 5 and apply the file again. You do not write "add two more."
What happens after you apply
kubectl apply does not create Pods. It sends the object to the API server, which stores it. Separate programs called controllers then notice the stored intent and work to make the cluster match it.
The arrow from the last step back to the controller matters. A controller keeps checking after it acts. The next chapters cover that loop in detail.
Why this design is useful
- Self-healing. If a Pod dies, the stored intent still says three replicas, so a controller creates a replacement. You do not have to write recovery steps.
- Repeatable. Applying the same file twice gives the same result. A manifest describes an end state, so repeating it is safe.
- Reviewable. The manifest is a record of what you want. You can keep it in version control and compare versions.
- Tolerant of delay. Intent is stored before anything acts on it. Work can start late or be retried without losing your request.
Common mistakes
- Treating
applyas "run now." The command returns when the API server accepts the object. The cluster may not have changed yet. - Editing the result by hand. If you delete a Pod that a Deployment manages, the Deployment creates a new one. Change the spec to change the intent.
- Assuming the spec describes reality. The spec is what you want. What exists is a separate question, covered in the next chapter.
Exercise
You apply a Deployment with replicas: 3, and afterwards one of its Pods is deleted by hand. Which statement describes what Kubernetes does, and why?
Show answer
A controller creates a replacement Pod. The stored desired state still says three replicas, and the controller works to make the cluster match it. Nothing in the original apply command says how to recover. The declared intent is enough.