Kubernetes Basics
Kubernetes runs your containers across many machines by comparing what you declared with what is actually running, and closing the gap. Learn to read and write the core objects, follow a request from the internet to a container, and debug a Pod that will not start.
- Intermediate
- 4 units, 12 lessons, 148 exercises
- YAML, shell
- Builds on Docker & Containers, Networking helps
Unit 01 · 3 lessons
Desired state
Declare what should run and read how controllers make it so
- 01Why an orchestrator
Desired state, actual state, and the loop that closes the gap.
- 02Pods and manifests
The four parts of a manifest, what a Pod shares, and three commands to read one.
- BOSSA Deployment for the shop
Replicas, labels and selectors, and a ReplicaSet that replaces what you delete.
Read the unit guide
Docker runs a container on one machine because you typed a command. Kubernetes runs containers across many machines, called nodes, because you described what should exist. You hand it the desired state. Controllers, which are loops that never finish, compare that with the actual state and act on the difference. This is reconciliation, and it is the one idea the rest of the roadmap keeps returning to.
A cluster has a control plane and nodes. In the control plane, the API server is the only door: every read and write goes through it, and it keeps everything in a store called etcd. The scheduler picks a node for each new Pod. Controllers run the loops. On every node an agent called the kubelet starts the containers it has been assigned and keeps them running.
Everything you create is an object, described by a manifest with four parts: apiVersion and kind (what type of thing), metadata (its name and labels) and spec (the state you want). The cluster adds status, which is what is actually true. The smallest object that runs anything is the Pod: one or more containers started together on one node, sharing one IP address.
You rarely create a Pod yourself, because nothing would replace it. You create a Deployment: a count, a label selector and a Pod template. The Deployment creates a ReplicaSet, and the ReplicaSet keeps that many Pods matching the selector alive. Delete a Pod, lose a node, and the count is restored from the template.
Three commands carry most of the reading. kubectl get gives one line per object, kubectl describe gives one object's details and events, and kubectl logs gives what the container printed.
Unit 02 · 3 lessons
Networking
Give changing Pods a stable name and trace a request to a container
- 01Services
A stable name and address over Pods that keep changing.
- 02Ingress
One entry point that routes by host and path, with TLS at the edge.
- BOSSFollow one request
From the internet to a container hop by hop, and a selector that matches nothing.
Read the unit guide
Pods come and go, and every replacement has a new IP address. A Service puts one stable address in front of them: a virtual IP that lasts as long as the Service, and a DNS name equal to the Service's name. Its selector is a label query, and traffic is spread over the Pods that match it and are ready right now. The list of those Pods is kept up to date for you.
Service types are nested. ClusterIP, the default, is reachable only inside the cluster. NodePort adds the same high port on every node. LoadBalancer adds an external load balancer from the cloud provider. Inside the cluster, a Service is called by its name from the same namespace, and by name.namespace from another.
An Ingress is a set of HTTP rules: for this host and this path, send the request to that Service. It lets one public entry point serve many apps, and it is where TLS usually ends. The rules do nothing without an ingress controller, a reverse proxy running in the cluster that reads them. The Ingress API is stable but frozen, and its successor, the Gateway API, routes on the same idea.
A public request crosses several hops: public DNS, a load balancer, the ingress controller, a Service, a Pod. Each hop chooses the next by one thing: a domain, a host and path, a label. When a request fails, test hop by hop. A Service whose list of endpoints is empty has a selector that matches no ready Pod, and that is the most common fault of all.
Unit 03 · 3 lessons
Configuration and resources
Run one image with different settings, storage and limits
- 01ConfigMaps and Secrets
Settings as env vars or files, and why base64 is not encryption.
- 02Storage
Volumes, claims that bind to them, and StatefulSets in outline.
- BOSSMake the api production-ready
Requests, limits and the scheduler, OOMKilled, three probes, and namespaces.
Read the unit guide
One image should run everywhere, with its settings supplied from outside. A ConfigMap holds plain key-value settings and a Secret holds sensitive ones. A container takes either as environment variables or as files in a mounted volume. Environment variables are fixed when the container starts, so a changed ConfigMap reaches them only after a restart. Mounted files are refreshed in place after a short delay.
The values of a Secret are base64-encoded, which is an encoding and not encryption: anyone can reverse it without a key. By default a Secret is stored unencrypted, and what protects it is who may read it. A Secret manifest in a git repository is a leaked password.
A container's own filesystem is thrown away with the container. A volume is a directory supplied by the Pod: an emptyDir lasts as long as the Pod, while a PersistentVolumeClaim asks for storage that outlives it. A claim binds one-to-one to a volume, which exists already or is created on demand by a StorageClass. When several copies each need their own data and a fixed name, a StatefulSet gives every Pod a number and a claim of its own.
Every container can state requests and limits for CPU and memory. The scheduler places Pods by their requests, never by real usage, and a Pod must fit on one node. Limits are enforced on the node: over its CPU limit a container is throttled, and over its memory limit it is killed, which shows up as OOMKilled.
Probes tell the kubelet how a container is doing. A failed liveness probe restarts the container. A failed readiness probe only takes the Pod out of its Services until it passes again. A startup probe holds the other two back while a slow app boots. Namespaces divide a cluster into named areas, and names need only be unique inside one.
Unit 04 · 3 lessons
Operating
Change a running app without downtime and find out why a Pod is stuck
- 01Rolling updates
maxSurge and maxUnavailable as arithmetic, then watching and undoing a rollout.
- 02Autoscaling
The autoscaler's formula on real numbers, and why requests must be set.
- BOSSRelease day goes wrong
Pending, ImagePullBackOff and CrashLoopBackOff, read to the cause.
Read the unit guide
Changing a Deployment's Pod template starts a rolling update. The Deployment creates a new ReplicaSet and trades Pods between the old and the new in steps. Two settings bound each step: maxSurge is how many Pods above the replica count may exist, and maxUnavailable is how many below it may be missing. Both default to 25%. A percentage that is not a whole number is rounded up for surge and down for unavailable, and the two cannot both be 0. The old ReplicaSet is kept at 0 Pods, which is what kubectl rollout undo scales back up.
A HorizontalPodAutoscaler changes the replica count for you. Its formula is desired = ceil(current × measured / target), kept between a minimum and a maximum, and skipped when the ratio is within 10% of 1. CPU utilization is measured against each Pod's request, so a container without a request cannot be autoscaled on CPU. When new Pods fit on no node they wait as Pending, and a node autoscaler, where one is installed, adds machines for them.
A Pod that will not start is stuck at one of three stages. Pending means no node was chosen: read the scheduler's events in kubectl describe. ImagePullBackOff means the node cannot get the image: check the name, the tag and the registry credentials. CrashLoopBackOff means the container starts and exits repeatedly: read kubectl logs, with --previous for the run that crashed. BackOff means retries with growing waits, capped at five minutes.
The diagnosis always narrows the same way: get for the status, describe for the events, logs for the app's own words, then a fix made in the manifest. When the same manifests serve several environments, keep one base and a small file of differences per environment, with Kustomize overlays or a Helm chart's values.
Learn Kubernetes Basics on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.