Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Kubernetes Basics

The whole lesson is here to read. Create a free account to run it in a live lab.

Virtual machine lab11 min read10 commands

Keep it running

A shop's website has to be there all day. If the computer running it breaks at three in the morning, somebody has to start it again somewhere else. Kubernetes does that job: you tell it what should be running, and it keeps it running, starting a new copy whenever one goes.

By the end of this lesson

Note

I can say what a Deployment does and what happens when one of its pods is deleted.

Key words

  • Cluster: the machines Kubernetes runs your programs on.
  • Node: one machine of the cluster.
  • Pod: one running copy of a program, made of one or more containers.
  • Deployment: your wish for a number of identical pods, which Kubernetes keeps true.
  • Namespace: a named folder that keeps one group's things apart from another's.
  • kubectl: the command that talks to the cluster.

Starter: what you already know

Note

Answer in your head. 1. What is a container? 2. What goes wrong if a program runs on one computer only, and that computer breaks? 3. What does it mean to scale a program? You need all three in a moment.

Before you start

Note

Press Start the lab on the bar at the top of this lesson. This lab is a whole Ubuntu machine of your own with a small Kubernetes cluster, k3s, installed in it, so it takes a few minutes to come up the first time. Each Run button types its command into the machine's terminal, one after another in the same session. If one says the cluster is not ready, wait a minute and press it again. To watch them, or to type your own, press Open terminal on the bar. Kubernetes needs 2 GB of memory: that is a college account's machine, and a free account's has 1 GB.

A cluster you can read

This cluster is one machine, so it has one node. kubectl get nodes lists it: its STATUS is Ready, its role is control-plane and the last column is the version of k3s. kubectl get pods -A lists the pods of every namespace. The ones in kube-system were started by Kubernetes itself: coredns gives the cluster names and local-path-provisioner gives pods a place to keep files.

Command
kubectl get nodes

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
kubectl get pods -A

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A Deployment keeps pods running

First a namespace of our own, demo, so nothing here gets in the way of the lab. Then a Deployment called hello: three copies of a small program, busybox running sleep. This machine reaches no image registry, so every pod in this lab uses the one image it has, busybox. kubectl get pods -n demo lists three pods whose names start with hello- and end in random letters. They are Running after a few seconds, and Pending or ContainerCreating until then.

Command
kubectl create namespace demo

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
kubectl create deployment hello --image=busybox:1.37.0 --replicas=3 -n demo -- sleep 3600

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
kubectl get pods -n demo

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think that deleting a pod stops the program. It does not. The Deployment still wants three pods, so it makes another. To stop the program you delete the Deployment, or scale it down.

Delete the pods, and they come back

Now delete all three pods at once. --grace-period=1 just stops them quickly. The command prints three lines, one for each pod it deleted. Then list the pods again: there are three, with new names and an AGE of a few seconds. Nobody told Kubernetes to start them. The Deployment noticed that it had none and made three.

Command
kubectl delete pods -n demo -l app=hello --grace-period=1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
kubectl get pods -n demo

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check

Tip

Why are there still three pods after you deleted all three? Answer: the Deployment is a wish for three pods, and Kubernetes keeps making the wish true.

Scale it, then tidy up

Scaling is changing the number in the wish. kubectl scale asks for five. The Deployment reports READY as 3/5 while the two new pods start, then 5/5. The last command deletes the namespace, and everything in it goes too, ready for the lab.

Command
kubectl scale deployment hello -n demo --replicas=5

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
kubectl get deployment hello -n demo

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Command
kubectl delete namespace demo

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Now do the lab

Tip

You have seen one Deployment keep three pods. Your lab asks for more: three web servers behind one Service name, a ConfigMap the pages are made from, and three Deployments that are broken for you to fix by reading what Kubernetes says. Open the README.md in your home folder with cat README.md in the terminal.

Exit check

Tip

Answer without looking back. 1. What does a Deployment do? 2. What happens when you delete one pod of a Deployment of three? 3. What does kubectl scale change? 4. What is a namespace for? Answers: it keeps a number of identical pods running; it makes a new one, so there are three again; the number of pods the Deployment wants; keeping one group's things apart from another's.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson is worked in a VM lab: each student has an Ubuntu machine of their own with a one-node Kubernetes cluster (k3s) in it, and a Run types into its terminal and shows what it printed. Key learning points: 1. A Deployment is a wish for a number of pods. 2. Kubernetes keeps the wish true, so a deleted pod comes back. 3. Scaling changes the number. Common mistake: believing that deleting a pod stops the program. Equipment: the lab and a browser, and a college account, because the cluster needs 2 GB. Tip: before deleting the pods, ask the class what will happen and why. The lab itself has a check that marks the cluster. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

About this lab and its source: Kubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

Teaching a class? Set these lessons for your students with marking and a lab for every student.