Lesson 1 of 8 in Kubernetes
Why Kubernetes exists and how it is built
Kubernetes is a system that keeps containers running across a group of machines according to a description you give it. You declare the state you want, and programs called controllers keep working until reality matches that description.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Containers with Docker, Networking for DevOps
The problem this solves
One container on one machine is easy. Ten services across five machines is not. Someone has to decide which machine runs what, restart things that crash, replace a machine that dies and let services find each other as addresses change.
Doing that by hand, or with scripts that run once and exit, breaks the first time something fails at 3am. Kubernetes takes the job over. You describe what should be running, and the system keeps checking and correcting for as long as the cluster lives.
How it works
- People and clients
- Control
- Data
- Runs code
You send requests to the API server, which stores state in etcd; the scheduler and each node's kubelet both talk to the API server and never to each other.
- You to API server: 1 send a request
- API server to etcd: 2 save the state
- Scheduler to API server: 3 assign a node
- Node: kubelet to API server: 4 fetch my Pods
- Data
- Control
- Runs code
A controller reads the wanted state and the observed state, creates or removes things to close the gap, then observes again.
- Wanted state to Controller: 1 read what you want
- Observed state to Controller: 2 read what exists
- Controller to Create 1 Pod: 3 act on the gap
- Create 1 Pod to Observed state: 4 observe again
A cluster has two kinds of machines. The control plane makes decisions. Worker nodes run your containers. In the first diagram, everything goes through the API server. It checks each request, saves it in etcd, which is a small, consistent key-value database, and tells anyone who is watching.
The scheduler watches for Pods that have no node yet and writes down which node should run each one. It looks at free CPU and memory and at any rules you set. The kubelet is an agent on every node. It watches the API server for Pods assigned to its node and asks the container runtime to start them.
The second diagram is the idea that matters most. Controllers are small programs, each responsible for one kind of object. A controller compares wanted state with observed state, acts on the difference, then looks again. That is why a deleted Pod comes back without anyone running a command.
Try it yourself
You need Docker running, plus the kind and kubectl tools from their official install pages. kind runs a whole cluster inside a Docker container, so it costs nothing and is easy to throw away.
- 1Create a local cluster
kind create cluster --name learn
This takes a minute or two the first time because it pulls a node image. kubectl is pointed at the new cluster automatically.
- 2Meet the node
kubectl get nodes -o wide
You should see one node named
learn-control-planewith statusReady. In kind, one node plays both control plane and worker. - 3Find the control plane parts
kubectl get pods -n kube-system
Look for pods whose names start with
etcd,kube-apiserver,kube-schedulerandkube-controller-manager. The control plane runs as Pods too. - 4Ask the API server directly
kubectl get --raw /readyz?verbose | head -20
kubectlis only a client. This prints the API server's own health checks.
What breaks, and where to look
The connection to the server localhost:8080 was refused - did you specify the right host or port?
- WHY IT HAPPENS
- kubectl has no cluster configured, or it points at a cluster that is not running. Docker may be stopped, so the kind cluster is down.
- CHECK FIRST
- Run
docker psto see if the kind container is running, thenkubectl config get-contextsto see which cluster kubectl is using. Switch withkubectl config use-context kind-learn.
kind create clusterfails or hangs with an error about Docker or the Docker daemon.- WHY IT HAPPENS
- kind needs a working container runtime. Docker Desktop or the Docker service is not running, or your user cannot talk to it.
- CHECK FIRST
- Run
docker info. If that fails, start Docker first, then runkind delete cluster --name learnand create the cluster again.
When not to use this
- If you run a handful of containers on one machine, Docker Compose is simpler and easier to debug. Kubernetes pays off when you need scheduling across machines, self-healing and rolling updates.
- Most teams use a managed Kubernetes from a cloud provider instead of running the control plane themselves. kind is for learning and testing, not for production traffic.
Check yourself
Which component do kubectl, the scheduler and the kubelet all talk to?
The API server. It is the only component that reads and writes etcd, and everything else goes through it.
What is a controller?
A loop that compares the wanted state with the observed state and creates or removes objects to close the gap, then checks again.
Why does a Pod you delete by hand get replaced?
A controller notices the observed state no longer matches the wanted state and creates a new Pod to fix it.
Keep going
Lesson 70 of 92 in the DevOps path