Chrysolites Tech Academy

Containers with Docker and Kubernetes · Course book

Chapter 4: Orchestrating Containers

Pairs with Lesson 4: Kubernetes for Beginners

Docker runs containers on one machine. A real application may need dozens of containers spread across many machines, kept running when machines fail, scaled up when traffic rises and updated without downtime. Doing that by hand is impossible, which is why container orchestrators exist. Kubernetes, originally developed at Google and now maintained as an open-source project, is by far the most widely used. This chapter explains its core ideas: the cluster and its nodes, pods, deployments and services, the command-line tool used to control it, and how a custom application is packaged, deployed, scaled and exposed.

What Kubernetes does

The central idea of Kubernetes is desired state. You do not tell it "start three containers on these machines". You declare what you want, such as "three copies of this application should always be running", and Kubernetes works continuously to make reality match. If a container crashes, it starts a replacement. If a machine dies, it reschedules that machine's containers elsewhere. If you change the declaration to five copies, it starts two more.

This declarative approach is what makes Kubernetes so reliable at scale. A loop inside it, called the control loop, repeatedly compares what is running with what should be running and corrects any difference, without anyone having to notice the problem first.

ObjectWhat it isAnalogy
ClusterThe whole set of machines managed togetherA whole shipping company
NodeOne machine in the cluster, physical or virtualOne ship
PodThe smallest unit Kubernetes runs: one or more containers sharing an addressA single shipping container
DeploymentA declaration of how many identical pods to run, and of which imageAn order for "always keep three of these at sea"
ServiceA stable address that routes traffic to a changing set of podsA port office that knows where every relevant container is
kubectlThe command-line tool for talking to the clusterThe radio to head office

Clusters, nodes and pods

A cluster has two kinds of node. The control plane is the brain: it stores the desired state, decides which node each pod should run on and runs the control loops. Worker nodes do the actual work of running pods. Each worker runs an agent called the kubelet, which receives instructions from the control plane and starts and stops containers through a container runtime, the software that actually runs containers, and a component called kube-proxy, which handles networking.

youkubectl Control planestores desired state, schedules pods Worker node 1Worker node 2 kubelet, kube-proxykubelet, kube-proxy podweb apppodweb apppodweb app
Figure 4.1 — A small cluster. You talk only to the control plane; it decides where pods run and keeps them running.

A pod wraps one or more containers that must run together on the same node and share a network address and storage. Most pods contain a single container, with helper containers added only when they must share it. Pods are ephemeral: they are created and destroyed freely, and each new pod gets a new IP address. You therefore almost never create pods directly; you create higher-level objects that create pods for you.

For learning, a full cluster of many machines is unnecessary. Minikube creates a single-node cluster on your own computer, inside a virtual machine or a container, and minikube start brings it up. kubectl, once installed, is configured to talk to it automatically. Typing kubectl over and over is tedious, so many people create a short alias, k, for it.

Deployments: keeping pods running

A deployment declares which image to run and how many copies, called replicas, to keep. kubectl create deployment web --image=nginx creates one, with a single pod. kubectl get pods lists the pods it created, each named after the deployment with a random suffix, and kubectl describe pod shows the full details, including the node it landed on, its IP address and recent events.

Deployments make scaling a one-line change: kubectl scale deployment web --replicas=4 tells Kubernetes that four pods should exist, and it starts three more, spreading them across nodes where possible. Delete one of those pods by hand and the deployment immediately creates a replacement, because the desired state still says four.

Deployments also handle updates. Changing the image to a new version starts a rolling update: new pods are created and old ones removed gradually, so the application keeps serving throughout. If the new version fails to start, the rollout pauses, and kubectl rollout undo returns to the previous version.

Under the hood a deployment works through another object, a replica set, which is responsible for keeping exactly the requested number of pods alive. During a rolling update the deployment creates a new replica set for the new version and gradually shifts pods from the old replica set to the new one: up one, down one, until the old set is empty. The old replica set is kept, scaled to zero, which is what makes an instant rollback possible.

Kubernetes also needs to know whether each pod is healthy. A readiness probe, a check such as requesting a particular web address, tells Kubernetes when a new pod is ready to receive traffic, so a rolling update never sends users to a pod that is still starting. A liveness probe checks that a running pod still works, and Kubernetes restarts it if not. Requests and limits on processor and memory, set per container, tell the scheduler how much room each pod needs and stop any one pod from using more than its share.

In practice, deployments are described in YAML manifest files instead of commands. A manifest states the deployment's name, the number of replicas, a label used to identify its pods and a template describing each pod's container. kubectl apply -f deployment.yaml creates the deployment or brings an existing one into line with the file, which can then be kept in version control like any other code.

Services: a stable way in

Pods come and go and change their addresses, so other parts of the system cannot rely on any pod's IP address. A service solves this. It gives a group of pods, selected by their labels, one stable name and address, and spreads incoming traffic across whichever of those pods currently exist.

Service typeReachable fromTypical use
ClusterIPInside the cluster onlyA database or internal API used by other pods
NodePortOutside, on a fixed port of every nodeTesting and simple setups such as Minikube
LoadBalancerOutside, through a cloud provider's load balancerPublic-facing applications in the cloud

A ClusterIP service is the default. Exposing the web deployment with kubectl expose deployment web --port=8080 --target-port=80 creates a ClusterIP service listening on port 8080 and forwarding to port 80 in the pods. Other pods can reach it by its name, web, which the cluster's internal DNS resolves to the service's address. A NodePort service additionally opens a port, in a range starting at 30000, on every node, so the application can be reached from outside the cluster at the node's address and that port. With Minikube, the command minikube service followed by the service name opens it in a browser directly, which saves looking up the node's address and the assigned port by hand.

Deploying your own application

Running nginx is a demonstration; the real goal is your own code. The path is the one followed throughout this course, ending in Kubernetes. Write a small Node.js web application, add a Dockerfile, build an image, and push it to Docker Hub under your account name with a version tag. Then create a deployment from that image, scale it to, say, 4 replicas, and expose it with a NodePort service.

A simple test reveals the load balancing at work. If the application responds with the name of the pod handling each request, refreshing the page repeatedly shows different pod names, as the service spreads requests across all 4 replicas. Scale to 8 and new names appear; delete a pod and its name disappears while a fresh one takes its place, with no interruption to the service.

Declare, do not instruct. The most important shift in moving to Kubernetes is to stop thinking in commands ("start this container there") and start thinking in declarations ("this is what should exist"). Kubernetes handles the how.

Kubernetes has much more: configuration and secrets objects to supply settings and credentials to pods, persistent volumes for data, ingress rules for routing web traffic by address, and namespaces to divide one cluster among teams. But every one of them builds on the four ideas in this chapter: nodes, pods, deployments and services.

Check your understanding

  • What does "desired state" mean in Kubernetes, and what happens when a running pod crashes?
  • What is the difference between the control plane and a worker node?
  • Why are pods rarely created directly?
  • A deployment has 3 replicas and you run kubectl scale with --replicas=7. What does Kubernetes do? (Answer: it starts 4 more pods.)
  • Why is a service needed when pods already have IP addresses?
  • Which service type would you use for an internal database, and which for a public website in the cloud?
  • List the steps for deploying your own application, from code to a reachable service.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.