Lesson 1 of 8 in Kubernetes

Why Kubernetes exists and how it is built

Core12 min

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

The parts of a cluster and who talks to whom
The parts of a cluster and who talks to whomYou 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.1 send arequest2 save thestate3 assign a node4 fetch my PodsYoukubectlAPI serverfront door of the clusteretcdthe saved cluster stateSchedulerpicks a node for a PodNode: kubeletstarts the containers
  • 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.

  1. You to API server: 1 send a request
  2. API server to etcd: 2 save the state
  3. Scheduler to API server: 3 assign a node
  4. Node: kubelet to API server: 4 fetch my Pods
The reconcile loop that never stops
The reconcile loop that never stopsA controller reads the wanted state and the observed state, creates or removes things to close the gap, then observes again.1 read whatyou want2 read whatexists3 act on the gap4 observe againWanted state3 PodsControllercompares the twoObserved state2 Pods runningCreate 1 Podcloses the gap
  • 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.

  1. Wanted state to Controller: 1 read what you want
  2. Observed state to Controller: 2 read what exists
  3. Controller to Create 1 Pod: 3 act on the gap
  4. 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.

  1. 1
    Create 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.

  2. 2
    Meet the node
    kubectl get nodes -o wide

    You should see one node named learn-control-plane with status Ready. In kind, one node plays both control plane and worker.

  3. 3
    Find the control plane parts
    kubectl get pods -n kube-system

    Look for pods whose names start with etcd, kube-apiserver, kube-scheduler and kube-controller-manager. The control plane runs as Pods too.

  4. 4
    Ask the API server directly
    kubectl get --raw /readyz?verbose | head -20

    kubectl is 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 ps to see if the kind container is running, then kubectl config get-contexts to see which cluster kubectl is using. Switch with kubectl config use-context kind-learn.
  • kind create cluster fails 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 run kind delete cluster --name learn and 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.

Lesson 70 of 92 in the DevOps path

Search

Search cheat sheets and projects