The hard way · Part 4

Workers

A kubelet, a runtime, a network plugin and kube-proxy.

Status

The scripts are checked (flags, local control plane run, AWS --dry-run) but have not yet been run end to end on AWS. The outputs below are real where marked; elsewhere the page says what to look for.

A worker runs Pods. It needs four things, and you install each one.

1Create the files

Same folder. Open each file below and use its copy button: it creates the file for you.

$ cd ~/k8s-hard-way
remote/worker.sh (186 lines)
$ cat > remote/worker.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# Runs ON A WORKER, as root (scripts/90-workers.sh sends it over SSH).
#
# A worker runs Pods. Four pieces:
#   containerd + runc   actually start containers (containerd manages them, runc makes them)
#   CNI plugins         give each Pod an IP and plug it into the network
#   kubelet             the agent: asks the API server "what should run here?" and makes it so
#   kube-proxy          turns Service addresses into rules that reach real Pods
set -euo pipefail
: "${NODE_NAME:?}" "${POD_CIDR:?}" "${K8S_VERSION:?}" "${CONTAINERD_VERSION:?}" "${RUNC_VERSION:?}"
: "${CNI_PLUGINS_VERSION:?}" "${CLUSTER_DNS_IP:?}" "${POD_CLUSTER_CIDR:?}"
STAGE=/home/ec2-user/stage
BASE="https://dl.k8s.io/release/${K8S_VERSION}/bin/linux/amd64"

# --- 1. operating system: packages, kernel modules, forwarding ------------------------
# kube-proxy writes iptables rules and needs conntrack, socat and ipset.
dnf install -y iptables-nft conntrack-tools socat ipset >/dev/null
# br_netfilter: lets iptables see traffic that crosses the Pod bridge. overlay: container layers.
cat > /etc/modules-load.d/k8s.conf <<CONF
overlay
br_netfilter
CONF
modprobe overlay; modprobe br_netfilter
cat > /etc/sysctl.d/99-k8s.conf <<CONF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
CONF
sysctl --system >/dev/null

# --- 2. binaries ----------------------------------------------------------------------
cd /tmp
for b in kubelet kube-proxy kubectl; do
  curl -fsSLO "$BASE/$b"
  echo "$(curl -fsSL "$BASE/$b.sha256")  $b" | sha256sum -c -
  install -m 755 "$b" /usr/local/bin/
done
curl -fsSLO "https://github.com/containerd/containerd/releases/download/v${CONTAINERD_VERSION}/containerd-${CONTAINERD_VERSION}-linux-amd64.tar.gz"
tar -xzf "containerd-${CONTAINERD_VERSION}-linux-amd64.tar.gz" -C /usr/local
curl -fsSL -o runc "https://github.com/opencontainers/runc/releases/download/${RUNC_VERSION}/runc.amd64"
install -m 755 runc /usr/local/sbin/runc
install -d /opt/cni/bin
curl -fsSL "https://github.com/containernetworking/plugins/releases/download/${CNI_PLUGINS_VERSION}/cni-plugins-linux-amd64-${CNI_PLUGINS_VERSION}.tgz" | tar -xz -C /opt/cni/bin

# --- 3. containerd ----------------------------------------------------------------------
# Start from containerd's own default config and change the one thing that must match the
# kubelet: use systemd to manage cgroups (this machine runs cgroup v2 under systemd).
install -d /etc/containerd
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
grep -q 'SystemdCgroup = true' /etc/containerd/config.toml || { echo "could not enable SystemdCgroup"; exit 1; }
cat > /etc/systemd/system/containerd.service <<UNIT
[Unit]
Description=containerd container runtime
After=network.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target
UNIT

# --- 4. pod networking (CNI) ------------------------------------------------------------
# "bridge": every Pod on this node plugs into one virtual switch (cni0) and gets an IP
# from THIS node's slice of the Pod range. ipMasq is off on purpose: Pod-to-Pod traffic
# between nodes must keep its Pod IP. Traffic to the internet is masqueraded below.
install -d /etc/cni/net.d
cat > /etc/cni/net.d/10-bridge.conf <<CONF
{
  "cniVersion": "1.0.0",
  "name": "bridge",
  "type": "bridge",
  "bridge": "cni0",
  "isGateway": true,
  "ipMasq": false,
  "ipam": {
    "type": "host-local",
    "ranges": [[{ "subnet": "${POD_CIDR}" }]],
    "routes": [{ "dst": "0.0.0.0/0" }]
  }
}
CONF
cat > /etc/cni/net.d/99-loopback.conf <<CONF
{ "cniVersion": "1.0.0", "name": "lo", "type": "loopback" }
CONF
# Pods reaching the internet need the node's IP as their source (the internet cannot route
# 10.200.x.x). So: masquerade anything leaving the Pod range for somewhere outside it.
cat > /etc/systemd/system/pod-masquerade.service <<UNIT
[Unit]
Description=Masquerade Pod traffic that leaves the Pod network
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh -c 'iptables -t nat -C POSTROUTING -s ${POD_CLUSTER_CIDR} ! -d ${POD_CLUSTER_CIDR} -j MASQUERADE 2>/dev/null || iptables -t nat -A POSTROUTING -s ${POD_CLUSTER_CIDR} ! -d ${POD_CLUSTER_CIDR} -j MASQUERADE'

[Install]
WantedBy=multi-user.target
UNIT

# --- 5. kubelet ------------------------------------------------------------------------
install -d /var/lib/kubelet /var/lib/kube-proxy /var/lib/kubernetes /var/run/kubernetes
cp "$STAGE/ca.crt" /var/lib/kubelet/ca.crt
cp "$STAGE/${NODE_NAME}.crt" /var/lib/kubelet/kubelet.crt
cp "$STAGE/${NODE_NAME}.key" /var/lib/kubelet/kubelet.key
cp "$STAGE/${NODE_NAME}.kubeconfig" /var/lib/kubelet/kubeconfig
cp "$STAGE/kube-proxy.kubeconfig" /var/lib/kube-proxy/kubeconfig
chmod 600 /var/lib/kubelet/kubelet.key
cat > /var/lib/kubelet/kubelet-config.yaml <<YAML
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false              # nobody gets in without a certificate
  webhook:
    enabled: true               # ...and tokens are checked with the API server
  x509:
    clientCAFile: /var/lib/kubelet/ca.crt
authorization:
  mode: Webhook                 # the API server decides whether a caller may use the kubelet
cgroupDriver: systemd
clusterDomain: cluster.local
clusterDNS:
  - ${CLUSTER_DNS_IP}
containerRuntimeEndpoint: unix:///var/run/containerd/containerd.sock
podCIDR: ${POD_CIDR}
resolvConf: /etc/resolv.conf
runtimeRequestTimeout: 15m
tlsCertFile: /var/lib/kubelet/kubelet.crt
tlsPrivateKeyFile: /var/lib/kubelet/kubelet.key
YAML
cat > /etc/systemd/system/kubelet.service <<UNIT
[Unit]
Description=Kubernetes Kubelet
After=containerd.service
Requires=containerd.service

[Service]
ExecStart=/usr/local/bin/kubelet \\
  --config=/var/lib/kubelet/kubelet-config.yaml \\
  --kubeconfig=/var/lib/kubelet/kubeconfig \\
  --v=2
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
UNIT

# --- 6. kube-proxy ---------------------------------------------------------------------
cat > /var/lib/kube-proxy/kube-proxy-config.yaml <<YAML
kind: KubeProxyConfiguration
apiVersion: kubeproxy.config.k8s.io/v1alpha1
clientConnection:
  kubeconfig: /var/lib/kube-proxy/kubeconfig
mode: iptables
clusterCIDR: ${POD_CLUSTER_CIDR}
YAML
cat > /etc/systemd/system/kube-proxy.service <<UNIT
[Unit]
Description=Kubernetes Kube Proxy
After=network.target

[Service]
ExecStart=/usr/local/bin/kube-proxy --config=/var/lib/kube-proxy/kube-proxy-config.yaml
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
UNIT

systemctl daemon-reload
systemctl enable --now containerd pod-masquerade kubelet kube-proxy
sleep 5
systemctl is-active containerd kubelet kube-proxy
SCRIPT_EOF
scripts/90-workers.sh (8 lines)
$ cat > scripts/90-workers.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 90 - install and start the worker programs on node-0 and node-1.
#      Each worker gets its own name and its own slice of the Pod range.
source "$(dirname "$0")/../lib.sh"
common=("K8S_VERSION=$K8S_VERSION" "CONTAINERD_VERSION=$CONTAINERD_VERSION" "RUNC_VERSION=$RUNC_VERSION"
        "CNI_PLUGINS_VERSION=$CNI_PLUGINS_VERSION" "CLUSTER_DNS_IP=$CLUSTER_DNS_IP" "POD_CLUSTER_CIDR=$POD_CLUSTER_CIDR")
log "node-0"; run_remote node-0 worker.sh "${common[@]}" NODE_NAME=node-0 "POD_CIDR=$NODE0_POD_CIDR"
log "node-1"; run_remote node-1 worker.sh "${common[@]}" NODE_NAME=node-1 "POD_CIDR=$NODE1_POD_CIDR"
SCRIPT_EOF
$ chmod +x scripts/*.sh remote/*.sh 2>/dev/null; true

2Run it

$ scripts/90-workers.sh        # installs on node-0 and node-1
The four pieces
  • containerd and runc start the containers. containerd manages them, runc makes each one.
  • CNI plugins give each Pod an IP, and plug it into the network.
  • kubelet is the agent. It asks the API server what should run here, and makes it so.
  • kube-proxy turns Service addresses into iptables rules that reach real Pods.
How Pods get an IP
  • Each worker owns a slice of the Pod range: node-0 gets 10.200.0.0/24, node-1 gets 10.200.1.0/24.
  • The bridge plugin plugs every Pod on the node into one virtual switch, cni0, and the host-local plugin hands out addresses from that slice.
  • Pod-to-Pod traffic keeps its Pod IP. Only traffic leaving for the internet is masqueraded to the node's IP.

3Check it

$ ssh -F .state/ssh_config node-0 systemctl is-active containerd kubelet kube-proxy
active
active
active

Nodes only show up in the cluster once you have a tunnel, in the next part. The kubelet config, its flags and kube-proxy's config were all accepted by the real binaries when I tested them locally.

Remember

A worker is a kubelet that asks the API server for work, a runtime that does it, a network plugin that wires it up, and kube-proxy for Services.

Dig deeper with your AI

Guess first, then click for the TL;DR. Ask your AI for the full answer, and check it on your cluster or with kubectl explain.

  • Why SystemdCgroup = true in containerd?

    The kubelet and containerd must agree on who manages cgroups. This machine uses systemd with cgroup v2, so both use systemd. A mismatch makes Pods fail to start.

  • Why load br_netfilter and set ip_forward?

    Without them, iptables never sees traffic that crosses the Pod bridge, and the node will not forward packets between interfaces.

  • What does the kubelet's authentication block do?

    It turns off anonymous access and makes the kubelet accept only certificates from our CA, or tokens the API server vouches for.

  • Why is the bridge CNI not what real clusters use?

    It has no policy, no cross-node networking of its own (we added routes), and no overlay. Real clusters use a plugin such as the AWS VPC CNI, Cilium or Calico.

Inspired by Kubernetes The Hard Way. Text and scripts: CC BY-NC-SA 4.0.