Why Kubernetes
You state what should run; the cluster keeps it true
A node fails. Its work is started elsewhere, with nobody called.
Compose can start containers on one machine. But a single machine can fail, and nothing in Compose will notice. Kubernetes is built for that moment. Here are three machines, called nodes, and a standing request: keep three copies of the web server running. Node B dies, and its copy is lost. Two are running, and the request says three. So Kubernetes starts a new copy on a healthy node. The request is true again.
Why Kubernetes · Going deeper
Lab: make a cluster and try to break it
Create a cluster, run three pods, delete them, watch them return.
Desired state
Watch, compare, act: the loop that runs everything
A controller compares wanted with running and closes the gap.
How does it know what to do? By running one simple loop for ever. Purple is what you asked for, green is what is running. At ten o'clock they match, so nothing happens. A node fails. Three wanted, two running. The controller starts one. They match again. Later you edit your file and ask for five. From the controller's side this is the same situation: a gap. It starts two. And then it rests.
Inside a cluster
A control plane decides; worker nodes run the containers
API server, etcd, controllers, scheduler; and a kubelet on every node.
A cluster has two kinds of machine. The control plane is the management, and the worker nodes do the work. At the centre is the A P I server. Every request, yours included, goes through it. It keeps the cluster's state in a database called et cee dee. The controllers run the loops you just saw. The scheduler chooses a node for each new pod. On every worker, an agent called the kubelet starts the pods assigned to it, through the container runtime. And kube proxy sets up the network rules that lead traffic to them.
Where the parts run · Going deeper
Static pods, services and ports: where to find each part when it fails
The control plane is static pods. The kubelet is a systemd service.
One request
One kubectl apply, followed through the cluster
Six steps, and nothing is pushed: every part watches the API server.
Follow one request through those parts. You run kubectl apply, and your file goes to the A P I server. It is checked and saved in et cee dee. Your command returns now. A controller notices a new Deployment that wants three pods and has none, so it creates three pod records. The scheduler notices pods without a node, and assigns each one. The kubelet on that node notices a pod with its name on it, and starts the containers. Then it reports the result back.
Working with kubectl · Going deeper
Know your context, and let kubectl write the YAML
A context is a cluster, a user and a namespace. --dry-run=client -o yaml writes the draft.
A context is a cluster, a user and a namespace. --dry-run=client -o yaml writes the draft. Know where you are pointing. kubectl reads a kubeconfig file, by default ~/.kube/config. It lists clusters (an address and a certificate authority), users (a client certificate or a token) and contexts. A context is one cluster, one user and a default namespace. kubectl config get-contexts every context; the current one is starred. kubectl config use-context prod switch to another cluster. kubectl config set-context --current --namespace=shop make shop the default namespace. kubectl get pods -A every namespace at once. Let kubectl write the first draft. Add --dry-run=client -o yaml to a create command and it prints the manifest and creates nothing. Save it, edit it, apply it. kubectl create deployment web --image=nginx:1.27 --replicas=3 --dry-run=client -o yaml > web.yaml a complete Deployment. kubectl run tmp --image=busybox:1.36 --dry-run=client -o yaml -- sleep 3600 a Pod, with its command. kubectl expose deployment web --port=80 --dry-run=client -o yaml a Service whose selector matches (the Deployment must exist). kubectl create cronjob report --image=acme/report:1.0 --schedule="0 2 * * *" --dry-run=client -o yaml a CronJob. Check before you change. kubectl diff -f web.yaml what an apply would change in the cluster. kubectl apply -f web.yaml --dry-run=server the API server validates it fully and saves nothing. kubectl explain deployment.spec.strategy --recursive the field names, without leaving the terminal. Get this wrong The right command sent to the wrong cluster is the classic outage. Read the current context before anything that deletes, and in a hands-on exam switch to the cluster or host each task names before you type anything else.
Know where you are pointing
kubectl reads a kubeconfig file, by default ~/.kube/config. It lists clusters (an address and a certificate authority), users (a client certificate or a token) and contexts. A context is one cluster, one user and a default namespace.
- kubectl config get-contexts every context; the current one is starred.
- kubectl config use-context prod switch to another cluster.
- kubectl config set-context --current --namespace=shop make shop the default namespace.
- kubectl get pods -A every namespace at once.
Let kubectl write the first draft
Add --dry-run=client -o yaml to a create command and it prints the manifest and creates nothing. Save it, edit it, apply it.
- kubectl create deployment web --image=nginx:1.27 --replicas=3 --dry-run=client -o yaml > web.yaml a complete Deployment.
- kubectl run tmp --image=busybox:1.36 --dry-run=client -o yaml -- sleep 3600 a Pod, with its command.
- kubectl expose deployment web --port=80 --dry-run=client -o yaml a Service whose selector matches (the Deployment must exist).
- kubectl create cronjob report --image=acme/report:1.0 --schedule="0 2 * * *" --dry-run=client -o yaml a CronJob.
Check before you change
- kubectl diff -f web.yaml what an apply would change in the cluster.
- kubectl apply -f web.yaml --dry-run=server the API server validates it fully and saves nothing.
- kubectl explain deployment.spec.strategy --recursive the field names, without leaving the terminal.
Manifests
Every object has the same shape: you write the spec
apiVersion, kind, metadata, spec. The cluster writes status.
What you send is called a manifest: a short YAML file describing one object. Whatever the object, the shape is the same. The A P I version and the kind say what it is. The metadata names it. And the spec describes the state you want: here, three copies of a pod running nginx. You never say how to get there. The cluster records what really exists in a fifth part, the status.
Manifests · Going deeper · 1 of 2
Lab: apply a manifest and read the result
Apply it twice, scale it, then read the status the cluster wrote.
API groups · Going deeper · 2 of 2
The apiVersion depends on the kind: eighteen to know
Pods and Services are v1. Deployments are apps/v1. Jobs are batch/v1.
Lesson complete
You finished “Why Kubernetes, and what is in a cluster”
Nicely done.