2.1. Kubernetes Architecture
In this lab, you create the cluster you use for all labs and find the components of the Kubernetes architecture in it.
Step 1: Create the Cluster
Create a directory for the files of this lab and change into it:
Create the file registries.yaml in ~/labs/2.1. It makes the nodes download images from Docker Hub
through a mirror:
Create a k3s cluster named labs with one server node and two agent nodes:
Creating the cluster takes about 25 seconds. The output ends with:
Check that kubectl now talks to the new cluster:
The output is k3d-labs.
Explanation
k3d starts the nodes as Docker containers, installs k3s in them and connects them to one cluster. The flags define what the cluster looks like:
| Flag | Effect |
|---|---|
labs | Name of the cluster. k3d prefixes containers and the kubectl context with k3d- |
--agents 2 | Adds two agent nodes (worker nodes) to the server node |
--cluster-init | Stores the cluster state in an embedded etcd |
--disable=traefik | Starts k3s without its built-in Ingress controller |
--disable=servicelb | Starts k3s without its built-in load balancer |
--port | Makes the ports 30080 to 30089 and 31337 reachable from your machine |
--registry-config | Copies registries.yaml into the nodes |
--k3s-arg passes a flag on to k3s. The suffix after @ selects the nodes a flag applies to: server:0 is the
first server node, server:* every server node. k3d also added the context k3d-labs to your kubeconfig
(~/.kube/config) and switched kubectl to it.
Docker Hub mirror
Each node downloads the images of its containers itself. In the training room, all laptops share one internet address,
and Docker Hub allows only 100 downloads per address in 6 hours. registries.yaml sends these downloads to
mirror.gcr.io, Google’s copy of Docker Hub. The chart in 4.1 calls Docker Hub registry-1.docker.io, so the file
lists both names.
Stop the cluster after the training day
k3d cluster stop labs stops all node containers and frees memory on your machine.
k3d cluster start labs starts them again, and your resources are still there.
Port is already allocated
If k3d fails with port is already allocated, another program on your machine uses one of the ports 30080 to 30089
or 31337. Stop the program, delete the cluster with k3d cluster delete labs and create it again.
Step 2: Explore the Nodes
List the nodes of your cluster:
If a node isn’t Ready yet, wait a moment and repeat the command.
Explanation
Every machine in a cluster is represented by a Node. Your cluster has three of them:
k3d-labs-server-0carries the rolescontrol-planeandetcd: it runs the control plane and stores the cluster state.- The two agent nodes have no role: they are worker nodes. In your cluster, the server node runs workloads as well.
Ready means the kubelet on the node is healthy and accepts new Pods. VERSION is the version of the kubelet.
Step 3: Find the Control Plane
In many Kubernetes clusters, each control plane component runs in a Pod in the
Namespace kube-system. A namespace groups resources, and -n selects it. Check what runs there
in your cluster:
If a Pod isn’t Running with 1/1 yet, wait a moment and repeat the command. Right after the cluster is created,
the add-ons need about half a minute to start.
None of the control plane components shows up: k3s runs them inside the k3s server process. Its log shows which
components it starts:
Compare it with an agent node:
Explanation
You found the components of the Kubernetes architecture in your cluster:
| Component | Runs on |
|---|---|
kube-apiserver | Server node |
etcd | Server node |
kube-scheduler | Server node |
kube-controller-manager | Server node |
kubelet | Every node |
kube-proxy | Every node |
containerd | Every node |
containerd is the container runtime: the kubelet tells it which containers to start.
k3s packs all of these components into a single binary. That is what makes it so lightweight.
The Pods in kube-system are add-ons that k3s installs out of the box:
| Add-on | Purpose |
|---|---|
coredns | DNS inside the cluster, resolves Service names |
local-path-provisioner | Creates persistent volumes in a directory on the node |
metrics-server | Collects CPU and memory usage of Pods and nodes |
Step 4: Find the Nodes in Docker
k3d runs every node as a Docker container. List the containers of your cluster:
Show the address kubectl uses to reach the API server:
The port is chosen randomly when the cluster is created, so yours differs.
Explanation
Each node is a container based on the rancher/k3s image. k3d adds one helper container:
| Container | Purpose |
|---|---|
k3d-labs-server-0 | Server node: control plane, etcd and workloads |
k3d-labs-agent-0 | Agent node: workloads |
k3d-labs-agent-1 | Agent node: workloads |
k3d-labs-serverlb | Proxy of k3d: forwards the ports of your machine into the cluster |
Depending on your Docker setup, you may also see k3d-labs-tools, a short-lived helper that k3d uses for cluster
operations.
The random port on k3d-labs-serverlb leads to the API server on port 6443. So every kubectl command takes this
path:
Optional Tasks
Finished early? Try this task. The following labs don’t depend on it.
Task 1: Pull the Plug
Simulate a node failure: stop the container of an agent node and check what Kubernetes reports. It takes almost a minute to notice. Then start the container again: the following labs need all three nodes.
| Command | Effect |
|---|---|
docker stop <container> | Stops a container |
docker start <container> | Starts a stopped container |
Check the nodes with kubectl get nodes from Step 2.