The memory, the door, and the two loops behind it.
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.
The control plane is four programs on the server: etcd (the memory), and three that never talk to each other, only to the API server.
Same folder. Open each file below and use its copy button: it creates the file for you.
$ cd ~/k8s-hard-way
remote/etcd.sh (44 lines)$ cat > remote/etcd.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# Runs ON THE SERVER, as root (scripts/70-etcd.sh sends it over SSH).
#
# etcd is where Kubernetes keeps all of its state. Only the API server talks to it.
# So it listens on loopback only: nothing else, not even other machines in the VPC, can.
# One member: no high availability (a 3-member cluster survives one failure; one does not).
set -euo pipefail
: "${ETCD_VERSION:?}"
cd /tmp
curl -fsSLO "https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-amd64.tar.gz"
tar -xzf "etcd-${ETCD_VERSION}-linux-amd64.tar.gz" -C /usr/local/bin --strip-components=1 \
"etcd-${ETCD_VERSION}-linux-amd64/etcd" "etcd-${ETCD_VERSION}-linux-amd64/etcdctl"
install -d -m 700 /var/lib/etcd
cat > /etc/systemd/system/etcd.service <<UNIT
[Unit]
Description=etcd
Documentation=https://etcd.io/docs
[Service]
ExecStart=/usr/local/bin/etcd \\
--name server \\
--data-dir /var/lib/etcd \\
--listen-client-urls http://127.0.0.1:2379 \\
--advertise-client-urls http://127.0.0.1:2379 \\
--listen-peer-urls http://127.0.0.1:2380 \\
--initial-advertise-peer-urls http://127.0.0.1:2380 \\
--initial-cluster server=http://127.0.0.1:2380 \\
--initial-cluster-token k8s-hard-way \\
--initial-cluster-state new
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now etcd
sleep 3
echo "--- etcd members:"
etcdctl member list
SCRIPT_EOF
scripts/70-etcd.sh (4 lines)$ cat > scripts/70-etcd.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 70 - install and start etcd on the server.
source "$(dirname "$0")/../lib.sh"
run_remote server etcd.sh "ETCD_VERSION=$ETCD_VERSION"
SCRIPT_EOF
remote/control-plane.sh (130 lines)$ cat > remote/control-plane.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# Runs ON THE SERVER, as root (scripts/80-control-plane.sh sends it over SSH).
#
# The control plane is three programs. They never talk to each other directly:
# kube-apiserver the only door to etcd, and to the cluster. Everything else
# (kubectl, kubelets, the other two) is a client of it.
# kube-controller-manager the loops that make reality match what you declared
# (a Deployment wants 3 Pods, so make 3 Pods)
# kube-scheduler decides which node each new Pod runs on
set -euo pipefail
: "${K8S_VERSION:?}" "${SERVER_IP:?}" "${SERVICE_CIDR:?}" "${POD_CLUSTER_CIDR:?}"
STAGE=/home/ec2-user/stage
BASE="https://dl.k8s.io/release/${K8S_VERSION}/bin/linux/amd64"
# --- 1. the binaries, each checked against the sha256 published next to it ------------
cd /tmp
for b in kube-apiserver kube-controller-manager kube-scheduler kubectl; do
curl -fsSLO "$BASE/$b"
echo "$(curl -fsSL "$BASE/$b.sha256") $b" | sha256sum -c -
install -m 755 "$b" /usr/local/bin/
done
# --- 2. certificates, keys and kubeconfigs, with the keys readable by root only -------
install -d -m 755 /var/lib/kubernetes /etc/kubernetes/config
cp "$STAGE"/{ca.crt,ca.key,kube-api-server.crt,kube-api-server.key,service-accounts.crt,service-accounts.key} /var/lib/kubernetes/
cp "$STAGE"/{encryption-config.yaml,kube-controller-manager.kubeconfig,kube-scheduler.kubeconfig} /var/lib/kubernetes/
chmod 600 /var/lib/kubernetes/*.key /var/lib/kubernetes/encryption-config.yaml
# --- 3. kube-apiserver ------------------------------------------------------------------
# --bind-address=0.0.0.0 looks wide, but the security group has no inbound rule from the
# internet: only the workers (and your SSM tunnel, which arrives on loopback) can reach it.
cat > /etc/systemd/system/kube-apiserver.service <<UNIT
[Unit]
Description=Kubernetes API Server
After=network.target etcd.service
[Service]
ExecStart=/usr/local/bin/kube-apiserver \\
--advertise-address=${SERVER_IP} \\
--bind-address=0.0.0.0 \\
--secure-port=6443 \\
--etcd-servers=http://127.0.0.1:2379 \\
--client-ca-file=/var/lib/kubernetes/ca.crt \\
--tls-cert-file=/var/lib/kubernetes/kube-api-server.crt \\
--tls-private-key-file=/var/lib/kubernetes/kube-api-server.key \\
--authorization-mode=Node,RBAC \\
--enable-admission-plugins=NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota \\
--encryption-provider-config=/var/lib/kubernetes/encryption-config.yaml \\
--kubelet-certificate-authority=/var/lib/kubernetes/ca.crt \\
--kubelet-client-certificate=/var/lib/kubernetes/kube-api-server.crt \\
--kubelet-client-key=/var/lib/kubernetes/kube-api-server.key \\
--kubelet-preferred-address-types=InternalIP \\
--service-account-key-file=/var/lib/kubernetes/service-accounts.crt \\
--service-account-signing-key-file=/var/lib/kubernetes/service-accounts.key \\
--service-account-issuer=https://${SERVER_IP}:6443 \\
--service-cluster-ip-range=${SERVICE_CIDR} \\
--service-node-port-range=30000-32767 \\
--allow-privileged=true \\
--event-ttl=1h \\
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
UNIT
# --- 4. kube-controller-manager ---------------------------------------------------------
cat > /etc/systemd/system/kube-controller-manager.service <<UNIT
[Unit]
Description=Kubernetes Controller Manager
After=kube-apiserver.service
[Service]
ExecStart=/usr/local/bin/kube-controller-manager \\
--kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig \\
--bind-address=127.0.0.1 \\
--cluster-name=${PROJECT:-k8s-hard-way} \\
--cluster-cidr=${POD_CLUSTER_CIDR} \\
--service-cluster-ip-range=${SERVICE_CIDR} \\
--root-ca-file=/var/lib/kubernetes/ca.crt \\
--cluster-signing-cert-file=/var/lib/kubernetes/ca.crt \\
--cluster-signing-key-file=/var/lib/kubernetes/ca.key \\
--service-account-private-key-file=/var/lib/kubernetes/service-accounts.key \\
--use-service-account-credentials=true \\
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
UNIT
# --- 5. kube-scheduler ------------------------------------------------------------------
cat > /etc/kubernetes/config/kube-scheduler.yaml <<YAML
apiVersion: kubescheduler.config.k8s.io/v1
kind: KubeSchedulerConfiguration
clientConnection:
kubeconfig: /var/lib/kubernetes/kube-scheduler.kubeconfig
leaderElection:
leaderElect: true
YAML
cat > /etc/systemd/system/kube-scheduler.service <<UNIT
[Unit]
Description=Kubernetes Scheduler
After=kube-apiserver.service
[Service]
ExecStart=/usr/local/bin/kube-scheduler \\
--config=/etc/kubernetes/config/kube-scheduler.yaml \\
--bind-address=127.0.0.1 \\
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now kube-apiserver kube-controller-manager kube-scheduler
# --- 6. wait until the API server answers (it needs a few seconds to start) -----------
for _ in $(seq 1 30); do
kubectl --kubeconfig /var/lib/kubernetes/kube-scheduler.kubeconfig get --raw /healthz >/dev/null 2>&1 && break
sleep 2
done
echo "--- API server health:"
kubectl --kubeconfig /var/lib/kubernetes/kube-scheduler.kubeconfig get --raw /healthz; echo
systemctl is-active kube-apiserver kube-controller-manager kube-scheduler
SCRIPT_EOF
scripts/80-control-plane.sh (5 lines)$ cat > scripts/80-control-plane.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 80 - install and start the three control plane programs on the server.
source "$(dirname "$0")/../lib.sh"
run_remote server control-plane.sh "K8S_VERSION=$K8S_VERSION" "SERVER_IP=$SERVER_IP" \
"SERVICE_CIDR=$SERVICE_CIDR" "POD_CLUSTER_CIDR=$POD_CLUSTER_CIDR" "PROJECT=$PROJECT"
SCRIPT_EOF
$ chmod +x scripts/*.sh remote/*.sh 2>/dev/null; true
$ scripts/70-etcd.sh
$ scripts/80-control-plane.sh
70-etcd.sh: sends remote/etcd.sh to the server80-control-plane.sh: sends remote/control-plane.sh: API server, controller manager, schedulerkubectl, kubelets and the other two programs are all its clients.The script ends by asking the API server for its health. You are looking for this:
--- etcd members:
<id>, started, server, http://127.0.0.1:2380, http://127.0.0.1:2379, false
--- API server health:
ok
active
active
active
I ran the same four programs, with the same flags and certificates, on a laptop first. This is what the API server said:
$ kubectl get --raw /healthz
ok
$ kubectl get lease -n kube-system
NAME HOLDER
kube-controller-manager <host>_<id>
kube-scheduler <host>_<id>
$ kubectl auth whoami
Username admin
Groups [system:masters system:authenticated]
--authorization-mode=Node,RBAC: kubelets are limited to their own node, everyone else goes through RBAC.--client-ca-file: trust any client certificate signed by our CA. That is how admin logs in.--encryption-provider-config: Secrets are encrypted before they reach etcd.--bind-address=0.0.0.0 looks wide, but the security group allows only the workers and your tunnel.Everything goes through the API server, and the API server is the only thing that touches etcd.
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.
One member has no failure tolerance. Three need a majority of two, so they survive one failure. Two members are worse than one: same tolerance, twice the machines that can fail.
Several copies can run for availability, but only one acts. They compete for a lease in the API; if the leader dies, another takes over.
The controller manager can sign certificates. In this guide we create all certificates by hand, so it is not needed. It is here for realism, and is a good thing to remove.
--service-cluster-ip-range do?It reserves the virtual addresses for Services. The first, 10.32.0.1, is the API server as seen from inside the cluster.
Inspired by Kubernetes The Hard Way. Text and scripts: CC BY-NC-SA 4.0.