Every component gets an identity.
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.
Kubernetes components prove who they are with TLS client certificates, all signed by one CA you create. Everything in this part runs on your machine, with openssl. It touches nothing in AWS.
Same folder. Open each file below and use its copy button: it creates the file for you.
$ cd ~/k8s-hard-way
scripts/40-certs.sh (84 lines)$ cat > scripts/40-certs.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 40 - the certificate authority and every certificate the cluster needs.
#
# Why so many? Kubernetes components prove who they are to each other with TLS client
# certificates. The API server trusts anything signed by our CA, and reads the identity
# from the certificate itself:
# CN (common name) = the user name
# O (organization) = the group e.g. system:masters = cluster admin
# That is why "admin" gets O=system:masters and each worker gets CN=system:node:<name>,
# O=system:nodes: RBAC rules are written against those names.
#
# Runs on your machine only. Needs nothing from AWS. Output: .state/pki/
source "$(dirname "$0")/../lib.sh"
PKI="$STATE/pki"; mkdir -p "$PKI"; cd "$PKI" || exit
DAYS=825
log "certificate authority (valid $DAYS days)"
# We always pass -config explicitly: some openssl builds (macOS LibreSSL, some corporate
# builds) have no default openssl.cnf and refuse to run without one.
cat > ca.cnf <<CNF
[req]
distinguished_name = dn
prompt = no
x509_extensions = ext
[dn]
CN = Kubernetes Hard Way CA
[ext]
basicConstraints = critical, CA:TRUE
keyUsage = critical, keyCertSign, cRLSign
subjectKeyIdentifier = hash
CNF
openssl genrsa -out ca.key 4096 2>/dev/null
openssl req -x509 -new -nodes -key ca.key -sha256 -days "$DAYS" -config ca.cnf -out ca.crt
# mkcert NAME CN ORG SAN EKU
# NAME file name CN user name ORG group (may be empty)
# SAN subjectAltName: every name or IP a client will use to reach this server, or ""
# EKU what the certificate may be used for: clientAuth, serverAuth, or both
mkcert() {
local name=$1 cn=$2 org=$3 san=$4 eku=$5
cat > "$name.cnf" <<CNF
[req]
distinguished_name = dn
prompt = no
[dn]
CN = $cn
${org:+O = $org}
[ext]
basicConstraints = CA:FALSE
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = $eku
${san:+subjectAltName = $san}
CNF
openssl genrsa -out "$name.key" 2048 2>/dev/null
openssl req -new -key "$name.key" -config "$name.cnf" -out "$name.csr"
openssl x509 -req -in "$name.csr" -CA ca.crt -CAkey ca.key -CAcreateserial \
-days "$DAYS" -sha256 -extfile "$name.cnf" -extensions ext -out "$name.crt" 2>/dev/null
log " $name.crt (CN=$cn${org:+, O=$org})"
}
log "client certificates"
mkcert admin admin system:masters "" clientAuth
mkcert kube-proxy system:kube-proxy system:node-proxier "" clientAuth
mkcert kube-controller-manager system:kube-controller-manager system:kube-controller-manager "" clientAuth
mkcert kube-scheduler system:kube-scheduler system:kube-scheduler "" clientAuth
log "worker certificates (the kubelet is both a client of the API server AND a server: logs, exec)"
mkcert node-0 system:node:node-0 system:nodes "DNS:node-0,IP:$NODE0_IP" "clientAuth, serverAuth"
mkcert node-1 system:node:node-1 system:nodes "DNS:node-1,IP:$NODE1_IP" "clientAuth, serverAuth"
log "API server certificate: every address anyone will use to reach it must be listed"
# 127.0.0.1 you, through the SSM tunnel
# $SERVER_IP the workers
# $KUBERNETES_SERVICE_IP Pods, via the built-in "kubernetes" Service
SAN="IP:127.0.0.1,IP:$SERVER_IP,IP:$KUBERNETES_SERVICE_IP,DNS:server,DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.cluster,DNS:kubernetes.default.svc.cluster.local"
mkcert kube-api-server kubernetes Kubernetes "$SAN" "serverAuth, clientAuth"
log "service-account key pair (the API server signs Pod tokens with it)"
mkcert service-accounts service-accounts "" "" clientAuth
rm -f ./*.csr ./*.cnf ./*.srl
chmod 600 ./*.key
log "done: $(ls ./*.crt | wc -l) certificates in $PKI"
SCRIPT_EOF
scripts/50-configs.sh (48 lines)$ cat > scripts/50-configs.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 50 - kubeconfig files and the encryption key.
#
# A kubeconfig is a small file saying: which API server, which CA to trust, and which
# client certificate to present. Each component gets its own, with its own identity.
# Runs on your machine only. Output: .state/kubeconfigs/ and .state/pki/encryption-config.yaml
source "$(dirname "$0")/../lib.sh"
PKI="$STATE/pki"; KC="$STATE/kubeconfigs"; mkdir -p "$KC"
[ -f "$PKI/ca.crt" ] || die "Run 40-certs.sh first."
# mkkc FILE USER CERT-NAME SERVER
mkkc() {
local file="$KC/$1.kubeconfig" user=$2 cert=$3 server=$4
rm -f "$file"
kubectl config set-cluster "$PROJECT" --certificate-authority="$PKI/ca.crt" --embed-certs=true \
--server="$server" --kubeconfig="$file" >/dev/null
kubectl config set-credentials "$user" --client-certificate="$PKI/$cert.crt" --client-key="$PKI/$cert.key" \
--embed-certs=true --kubeconfig="$file" >/dev/null
kubectl config set-context default --cluster="$PROJECT" --user="$user" --kubeconfig="$file" >/dev/null
kubectl config use-context default --kubeconfig="$file" >/dev/null
log " $1.kubeconfig -> $server as $user"
}
API="https://$SERVER_IP:6443" # how the workers reach the API server
LOCAL="https://127.0.0.1:6443" # how things on the server itself, and you (tunnel), reach it
mkkc node-0 system:node:node-0 node-0 "$API"
mkkc node-1 system:node:node-1 node-1 "$API"
mkkc kube-proxy system:kube-proxy kube-proxy "$API"
mkkc kube-controller-manager system:kube-controller-manager kube-controller-manager "$LOCAL"
mkkc kube-scheduler system:kube-scheduler kube-scheduler "$LOCAL"
mkkc admin admin admin "$LOCAL"
log "encryption key: Secrets are encrypted before the API server stores them in etcd"
KEY="$(head -c 32 /dev/urandom | base64)"
cat > "$PKI/encryption-config.yaml" <<YAML
kind: EncryptionConfiguration
apiVersion: apiserver.config.k8s.io/v1
resources:
- resources: ["secrets"]
providers:
- aescbc: # the first provider encrypts; the others can still read
keys:
- name: key1
secret: $KEY
- identity: {} # lets it read anything stored before encryption was on
YAML
chmod 600 "$PKI/encryption-config.yaml" "$KC"/*.kubeconfig
log "done"
SCRIPT_EOF
scripts/60-push-files.sh (23 lines)$ cat > scripts/60-push-files.sh <<'SCRIPT_EOF'
#!/usr/bin/env bash
# 60 - copy to each machine only what it needs. Least privilege applies to files too:
# workers never receive the CA private key, and nothing receives the admin kubeconfig.
source "$(dirname "$0")/../lib.sh"
PKI="$STATE/pki"; KC="$STATE/kubeconfigs"
[ -f "$KC/admin.kubeconfig" ] || die "Run 40-certs.sh and 50-configs.sh first."
push() { # push HOST FILE...
local host=$1; shift
kssh "$host" 'rm -rf ~/stage && mkdir -p ~/stage'
kscp "$@" "$host:stage/"
log "$host <- $(printf '%s ' "${@##*/}")"
}
# the control plane: CA (with key: it signs), API server cert, Pod-token key, encryption, its kubeconfigs
push server "$PKI/ca.crt" "$PKI/ca.key" "$PKI/kube-api-server.crt" "$PKI/kube-api-server.key" \
"$PKI/service-accounts.crt" "$PKI/service-accounts.key" "$PKI/encryption-config.yaml" \
"$KC/kube-controller-manager.kubeconfig" "$KC/kube-scheduler.kubeconfig"
# each worker: the CA certificate (no key), its own certificate, and two kubeconfigs
for n in node-0 node-1; do
push "$n" "$PKI/ca.crt" "$PKI/$n.crt" "$PKI/$n.key" "$KC/$n.kubeconfig" "$KC/kube-proxy.kubeconfig"
done
SCRIPT_EOF
$ chmod +x scripts/*.sh remote/*.sh 2>/dev/null; true
$ scripts/40-certs.sh # the CA and 9 certificates
$ scripts/50-configs.sh # 6 kubeconfigs and the encryption key
$ scripts/60-push-files.sh # copy each machine only what it needs
40-certs.sh: one CA, nine certificates50-configs.sh: a kubeconfig per component, and the Secrets encryption key60-push-files.sh: copies files over SSH: workers never get the CA key$ cd .state/pki
$ openssl verify -CAfile ca.crt kube-api-server.crt node-0.crt \
admin.crt kube-proxy.crt
kube-api-server.crt: OK
node-0.crt: OK
admin.crt: OK
kube-proxy.crt: OK
$ openssl x509 -in node-0.crt -noout -subject
subject=CN = system:node:node-0, O = system:nodes
That output is real, from running the scripts. Now look at what is inside the API server certificate:
$ openssl x509 -in kube-api-server.crt -noout -ext subjectAltName
IP Address:127.0.0.1, IP Address:10.240.0.10, IP Address:10.32.0.1, DNS:server,
DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, ...
| Certificate | CN (user) | O (group) |
|---|---|---|
admin | admin | system:masters |
node-0, node-1 | system:node:<name> | system:nodes |
kube-proxy | system:kube-proxy | system:node-proxier |
kube-controller-manager | system:kube-controller-manager | same |
kube-scheduler | system:kube-scheduler | same |
kube-api-server | kubernetes | Kubernetes |
system:masters is a built-in group with full admin rights. Anyone holding the admin key is cluster admin.127.0.0.1 (you, through the tunnel), 10.240.0.10 (the workers) and 10.32.0.1 (Pods, through the built-in kubernetes Service). A missing name means a TLS error.A client certificate is a name plus a group, signed by the CA. Whoever holds the CA key can mint any identity, so guard it.
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.
So each has only its own identity and permissions. If the scheduler is compromised, the attacker is the scheduler, not the admin.
The CA key can mint any identity. A worker only needs to verify certificates, which the public CA certificate does.
The API server encrypts Secrets with an aescbc key before writing to etcd. Stored data starts with k8s:enc:aescbc:v1:key1:, which I saw in a local run of the control plane.
Components stop talking to each other. These last 825 days. Real clusters rotate them, which is a big part of operating Kubernetes by hand.
The kubelet is a client of the API server, and a server too: the API server calls it for logs and exec.
Inspired by Kubernetes The Hard Way. Text and scripts: CC BY-NC-SA 4.0.