Skip to content
Foundations · 10 min

Containers and Kubernetes, from nothing

Many interview questions mention containers, pods and Kubernetes as if everyone knows them. This lesson explains what they are and why they exist, and shows the three or four things you will be asked to read.

The problem containers solve

A program rarely runs alone. It needs a particular version of a language, some libraries, some settings. On the developer's laptop these are all present. On the server one is missing or is a different version, and the program fails. "It works on my machine" is the oldest complaint in software.

What a container is

A container is a sealed box holding a program together with everything it needs to run. The box is built once, as a file called an image, and then started wherever it is needed. Because everything travels inside the box, it behaves the same on a laptop and on a server.

The box also keeps programs apart. A program in one container cannot see the files or the other programs in another, even though they share one computer. And each box can be given a ceiling: this much memory, this much processor time, and no more.

A picture to hold on toThink of shipping containers. A crane does not care whether a box holds bananas or bicycles: every box is the same shape and is handled the same way. Software containers give servers the same convenience.

Why one is not enough

A real service runs many copies of its container on many servers, so that one failure does not stop it. Somebody has to decide which server each copy goes on, start a new copy when one dies, and send requests only to the copies that are healthy. Doing that by hand does not scale. Kubernetes is the most widely used program for doing it automatically. Its name is often shortened to k8s.

The words Kubernetes uses

  • Node: one server that containers run on.
  • Cluster: a group of nodes managed together.
  • Pod: the smallest thing Kubernetes runs. Usually one container. When people say "the pod crashed", they mean one copy of the program stopped.
  • Deployment: an instruction such as "keep five copies of this program running". If one dies, Kubernetes starts another to get back to five.
  • Service: one stable address in front of all the copies, so callers do not need to know which pods exist right now.
  • kubectl: the command you type to ask Kubernetes questions and give it instructions.

Limits, and what happens at them

Each container is normally given a memory limit. If the program tries to use more, it is stopped at once. Nothing is saved and no warning is given. This is called an OOM kill: OOM stands for "out of memory". Kubernetes then starts a fresh copy. If the fresh copy does the same thing, the pod goes round in a loop of starting and dying, which Kubernetes reports as CrashLoopBackOff.

The important thing to notice is whose limit was hit. The server may have plenty of memory free. The container ran into its own ceiling.

Reading a pod that keeps restarting

Three commands answer most first questions. You do not need to remember the exact output, only what each one is for.

$ kubectl get pods                      # which copies exist, and how often each has restarted
$ kubectl describe pod <name>           # why the last one stopped: look for "Last State" and "Reason"
$ kubectl logs <name> --previous        # what the program printed just before it died

In the output of describe, a reason of OOMKilled means the memory ceiling. A reason of Error with an exit code means the program itself gave up, and its last log lines usually say why. Next ask what changed recently: a new version, a new setting, more traffic. The answer to an incident is very often to undo that change.

In an interviewSay what you are trying to find out before naming a command: "I want to know why the last copy stopped, so I would describe the pod and read the last state." If you do not know a command, say what you would look for and ask. That is normal on the job too.