Anhang A: Spickzettel
Alles Wichtige aus Tag 0–15 auf wenigen Seiten. Die Zahl in Klammern
ist der Tag, an dem du es geübt hast. Im Zweifel gelten die
kubectl-Referenz unter
kubernetes.io/docs/reference/kubectl/,
kubectl explain <typ>.<feld> und die Helm-Doku
unter helm.sh/docs.
Tagesstart und Gelände
source ~/k8s-lab/setup/env.sh # (0)
mkdir -p ~/k8s-lab/tagNN && cd ~/k8s-lab/tagNN
cp -R "$LAB/code/notes/." . && cp -R "$LAB/code/tag(N-1)/notes/." .
for app in api worker web; do # (0, 2)
docker build -q --build-arg VERSION=0.2.0 -t "notes-$app:0.2.0" "$NOTES/notes-$app"
kind load docker-image "notes-$app:0.2.0" --name lab # in alle Nodes
done
kubectl config set-context --current --namespace notes # (1)
| Befehl |
Zweck |
Tag |
colima start --cpu 4 --memory 8 --disk 60 |
Docker-VM auf dem Mac |
0 |
kind get nodes --name lab |
Node-Container des Clusters |
0 |
kind load docker-image notes-api:0.1.0 --name lab |
Image ohne Registry ins Cluster |
0 |
kind create cluster --name lab --config ~/k8s-lab/setup/kind.yaml |
Cluster anlegen |
0 |
kind delete cluster --name lab |
Cluster weg |
0 |
kubectl config get-contexts,
cluster-info |
Kontext kind-lab, API-Server |
0, 1 |
Anschauen
| Befehl |
Zweck |
Tag |
kubectl get pods -o wide,
get deploy,rs,pods |
Zustand, Node, IP |
1, 2 |
get pods --show-labels,
-L app.kubernetes.io/name |
Labels |
2 |
get pods -l app.kubernetes.io/name=api -o name |
Auswahl per Selektor |
2 |
kubectl describe pod api (Abschnitt
Events:) |
Zustand und Events |
1 |
kubectl events --for pod/api |
Events gezielt |
1 |
get events --field-selector reason=Failed |
Fehler im Namespace |
4, 8 |
kubectl logs deploy/api, --previous,
--tail 2 |
Logs, auch vom Vorgänger |
1, 3, 5 |
kubectl explain pod.spec.containers.imagePullPolicy |
Felder nachschlagen |
1 |
kubectl api-resources |
Typen, Kurznamen, API-Gruppen |
1 |
get endpointslices -l kubernetes.io/service-name=api |
Wer hinter einem Service steht |
3 |
describe quota notes,
describe limitrange defaults |
Verbrauch und Standardwerte |
6 |
get pod oom -o jsonpath='{.status.qosClass}' |
QoS-Klasse |
6 |
Achtung: In zsh jsonpath und custom-columns mit
[*] oder [0] immer in einfache
Anführungszeichen setzen, sonst versucht die Shell ein Dateimuster:
-o jsonpath='{.items[*].status.podIP}',
-o custom-columns='NAME:.metadata.name,IP:.status.podIP'.
Anwenden und ändern
| Befehl |
Zweck |
Tag |
kubectl apply --dry-run=server -f k8s/ |
vom API-Server prüfen lassen |
1 |
kubectl apply -f k8s/,
delete -f k8s/api-pod.yaml |
Soll-Zustand setzen / entfernen |
1, 2 |
kubectl diff -f k8s/api.yaml |
Abweichung Cluster ↔︎ Datei; Exit 1 = Unterschied |
2 |
scale deploy api --replicas 5 |
skalieren (Drift zur Datei!) |
2 |
set image deploy/api api=notes-api:0.3.0 |
Image tauschen (Drift) |
2 |
label pod … app.kubernetes.io/name=x --overwrite |
Pod aus dem ReplicaSet lösen |
2 |
patch configmap notes-config --type merge -p '{…}' |
Feld direkt ändern |
4 |
create secret generic … --dry-run=client -o yaml,
weiter an apply -f - |
Secret idempotent |
4, 7 |
kubectl create configmap demo --from-literal=… |
ConfigMap ohne Datei |
4 |
expose deploy api --name api-intern --port 80 --target-port 8080 |
Service ohne Datei |
3 |
Achtung: Per patch
hinzugefügte Felder überlebt jedes spätere
apply, und kubectl diff zeigt sie nicht (5).
Ein Job ist unveränderlich: field is immutable →
kubectl delete job migrate, neu anlegen (8).
Rollouts und Warten
| Befehl |
Zweck |
Tag |
kubectl rollout status deploy/api --timeout 60s |
warten; Exit 1 bei Timeout |
2, 4 |
rollout history deploy/api,
--revision 1 |
Revisionen |
2 |
rollout undo deploy/api |
zurück zur vorigen Revision |
2 |
rollout restart deploy/worker |
neue Pods, z. B. nach ConfigMap-Änderung |
4 |
rollout status statefulset/db |
auch für StatefulSets |
7 |
wait --for=condition=Ready pod/db-0 --timeout 120s |
auf Bereitschaft warten |
1, 7 |
wait --for=condition=Complete job/migrate |
auf einen Job warten |
7 |
wait --for=delete pod/db-0,
--for=jsonpath='{.status.phase}'=Failed |
auf Löschen / Feld |
5, 6, 7 |
Ein Rollout startet nur, wenn sich das Pod-Template ändert (2). Eine
geänderte ConfigMap oder ein neues Secret erreicht laufende Pods als
Umgebungsvariable erst nach rollout restart (4).
Im Cluster
kubectl exec deploy/web -- wget -qO- http://api:8080/version # (3)
kubectl exec deploy/web -- nslookup api.notes.svc.cluster.local # (3)
kubectl exec deploy/web -- nc -z -w 3 db 5432; echo "Exit $?" # (11)
kubectl exec db-0 -- psql -U notes -d notes -tAc 'SELECT count(*) FROM notes' # (7)
kubectl port-forward pod/api 8080:8080 & # (1)
kubectl auth can-i list secrets --as=system:serviceaccount:notes:default # (8)
- Für einmalige Abrufe
kubectl exec deploy/web -- wget …
statt kubectl run --rm -i – dort geht die Ausgabe oft
verloren (3).
- DNS:
api im eigenen Namespace, api.notes
von außerhalb, api.notes.svc.cluster.local vollständig
(3).
- Services verteilen Verbindungen, nicht Anfragen (3).
kubectl debug
POD=$(kubectl get pods -l app.kubernetes.io/name=api -o name | head -1)
kubectl debug -it "$POD" --image=busybox:1.37 --target=api --profile=restricted \
--custom=debug-user.yaml -- sh # (11)
--target=api: gemeinsamer Prozess-Namespace; PID 1 ist
die App, ihr Dateisystem liegt unter /proc/1/root
(11).
- Unter PSA
restricted: ohne Profil Forbidden
(SYS_PTRACE), mit --profile=restricted allein
image will run as root; --custom mit
securityContext.runAsUser: 65532 behebt das (11).
- Ephemeral Container bleiben bis zum Ende des Pods:
rollout restart (11).
Helm
| Befehl |
Zweck |
Tag |
helm show chart oci://… --version 6.17.0,
show values |
Chart ansehen |
9 |
helm template notes chart/notes -n notes |
rendern, ohne Cluster |
9, 10 |
helm template …, weiter an
kubectl apply --dry-run=server -f - |
gerendert prüfen |
10, 11 |
helm lint chart/notes |
Chart und Schema prüfen |
10 |
helm install podinfo oci://… --version … -n podinfo --create-namespace -f v.yaml |
installieren |
9 |
helm upgrade notes chart/notes -n notes --wait |
aktualisieren |
10, 11 |
upgrade … --reset-values |
zurück zu values.yaml |
10 |
upgrade … --rollback-on-failure --timeout 60s |
bei Fehler automatisch zurück |
9 |
helm rollback notes 1 -n notes --wait |
auf Revision 1 |
9, 10 |
helm list -A,
helm history notes -n notes |
Releases, Revisionen |
9 |
helm get values notes -n notes --all,
get manifest |
was der Release enthält |
9 |
helm test notes -n notes --logs |
Test-Pods des Charts |
10 |
helm package chart/notes -d /tmp/pkg |
.tgz bauen |
10 |
helm uninstall podinfo -n podinfo |
entfernen (Namespace bleibt) |
9 |
Achtung: Ohne --set/-f
übernimmt helm upgrade die Values des letzten Release. Erst
--reset-values kehrt zu values.yaml zurück (9,
10).
- Hook-Job migrate:
post-install,pre-upgrade mit
before-hook-creation; ein Rollback startet
pre-upgrade-Hooks nicht (10).
- Ein Release pro Namespace, Objektnamen fest – so übernimmt das Chart
den PVC
data-db-0 (10).
Gateway und NetworkPolicy
helm install eg oci://docker.io/envoyproxy/gateway-helm --version v1.9.3 \
-n envoy-gateway-system --create-namespace --wait # (11)
kubectl get gatewayclass; kubectl get gateway,httproute # (11)
kubectl get httproute web \
-o jsonpath='{range .status.parents[0].conditions[*]}{.type}={.status} {end}'
curl -s http://notes.localhost:30080/healthz # (11)
curl -s -H 'Host: notes.localhost' localhost:30080/healthz # (11)
kubectl get networkpolicy; kubectl describe networkpolicy api-from-clients
| Objekt |
gehört zu |
Tag |
GatewayClass eg + EnvoyProxy nodeport
(k8s/gateway.yaml) |
Plattform |
11 |
Gateway notes (Listener, Hostname) |
App-Chart |
11 |
HTTPRoute web (Host → Service
web:3000) |
App-Chart |
11 |
| NetworkPolicies (default deny + Freigaben) |
App-Chart |
11 |
- NetworkPolicy:
podSelector: {} +
policyTypes: [Ingress] ohne Regeln = alles zu. Policies
addieren sich. Zwei Einträge unter from = Oder, Namespace-
und Pod-Selektor im selben Eintrag = Und (11).
- Verworfene Pakete zeigen sich als Timeout, nicht als
Connection refused (11).
Fehlerbilder
| Symptom |
Ursache |
Fix |
ImagePullBackOff, ErrImagePull |
Tag existiert nicht im Node |
Tag prüfen, kind load docker-image (0, 1) |
exec: "sh": executable file not found |
scratch-Image ohne Shell |
kubectl debug (1, 11) |
Connection refused auf ClusterIP |
EndpointSlice leer, Selektor passt nicht |
Labels prüfen (3) |
{"error":"text required"} |
POST ohne Content-Type |
-H 'Content-Type: application/json' (3) |
| GETs liefern wechselnde Listen |
Speicher pro Replik |
Postgres (3, 7) |
CreateContainerConfigError |
Secret oder Schlüssel fehlt |
secrets.sh, Schlüsselname (4) |
CrashLoopBackOff, Liveness probe failed,
Exit 0 |
Probe falsch, App gesund |
Probe-Pfad prüfen, logs --previous (5) |
OOMKilled |
Memory-Limit zu klein |
Limit erhöhen (6) |
exceeded quota bzw. Pending mit
Insufficient cpu |
Quota bzw. Node voll |
Requests senken (6) |
api RESTARTS zählt hoch, Exit 1 |
db noch nicht bereit |
abwarten (7) |
field is immutable beim Job |
Job geändert |
Job löschen, Helm-Hook (8, 10) |
violates PodSecurity "restricted:latest" |
securityContext fehlt |
Pod- und Container-Kontext (8) |
image has non-numeric user |
USER node im Image |
runAsUser setzen (8) |
Read-only file system |
readOnlyRootFilesystem |
emptyDir für /tmp (8) |
| Gateway: Verbindung steht, keine Antwort |
externalTrafficPolicy: Local |
Cluster im EnvoyProxy (11) |
Gateway: 404, leere Antwort |
falscher Host-Header |
notes.localhost (11) |
wget: download timed out im Cluster |
NetworkPolicy verwirft |
Freigabe ergänzen (11) |
kubectl debug: Forbidden /
will run as root |
PSA restricted |
--profile=restricted --custom (11) |
RKE2
source ~/k8s-lab/setup/env.sh && source ~/k8s-lab/setup/rke2.env # (12)
multipass shell rke2-server # auf den Node, dann sudo -i (12)
export PATH=$PATH:/var/lib/rancher/rke2/bin KUBECONFIG=/etc/rancher/rke2/rke2.yaml
export CONTAINER_RUNTIME_ENDPOINT=unix:///run/k3s/containerd/containerd.sock
| Befehl oder Datei |
Zweck |
Tag |
https://get.rke2.io mit
INSTALL_RKE2_VERSION=… per sh - |
Installer, Agents zusätzlich mit
INSTALL_RKE2_TYPE=agent |
12 |
systemctl enable --now rke2-server bzw.
rke2-agent |
Dienst starten |
12 |
/etc/rancher/rke2/config.yaml |
tls-san, ingress-controller,
server, token-file |
12 |
/var/lib/rancher/rke2/server/node-token |
Join-Token, nie ausschreiben |
12 |
/etc/rancher/rke2/rke2.yaml |
Admin-kubeconfig (127.0.0.1 ersetzen) |
12 |
/var/lib/rancher/rke2/agent/pod-manifests/ |
Static Pods der Control Plane |
12 |
kubectl get helmcharts -n kube-system |
mitgelieferte Dienste |
12 |
/etc/rancher/rke2/registries.yaml |
Login und CA für Registries, danach Dienst neu starten |
13 |
journalctl -u rke2-agent --since -5min |
warum ein Node nicht hochkommt |
15 |
rke2-killall.sh |
alle Container des Nodes beenden |
14 |
rke2 etcd-snapshot save --name X, ls,
delete |
etcd sichern |
14 |
rke2 server --cluster-reset --cluster-reset-restore-path=… |
etcd zurückspielen (Dienst vorher stoppen) |
14 |
etcd-snapshot-schedule-cron,
etcd-snapshot-retention |
automatische Snapshots |
14 |
kubectl drain NODE --ignore-daemonsets --delete-emptydir-data # (14)
kubectl uncordon NODE # (14)
kubectl get pdb -A # (14)
kubectl wait --for=condition=Ready node --all --timeout=300s # (12)
kubectl -n default run pulltest --image=REG/IMG:TAG --image-pull-policy=Always \
--overrides='{"spec":{"nodeName":"NODE"}}' # (13, 15)
helm upgrade notes chart/notes --reuse-values --set KEY=WERT # (13)
helm rollback notes [REVISION] # (15)
helm get values notes # (15)
| Symptom |
Ursache |
Fix |
x509: certificate signed by unknown authority beim
Pull |
Node kennt die CA nicht |
tls.ca_file in registries.yaml (13) |
401 Unauthorized beim Pull |
Pull-Zugangsdaten falsch |
auth in registries.yaml (15) |
db-0 Pending,
didn't match PersistentVolume's node affinity |
Volume liegt auf gesperrtem Node |
uncordon (14) |
Drain hängt,
would violate the pod's disruption budget |
ALLOWED DISRUPTIONS 0 |
Budget oder Repliken anpassen (15) |
Node NotReady, Dienst activating |
Agent erreicht den Server nicht |
Journal, config.yaml (15) |
Pods Running auf NotReady-Node |
kubelet tot, Umzug erst nach 300 s |
warten, Envoy doppelt (14) |
503 nach etcd-Restore |
Controller mit altem Stand |
rollout restart des Controllers (14) |
exceeded quota nur in Events |
ReplicaSet darf keine Pods anlegen |
FailedCreate lesen (15) |
lookup … :53: i/o timeout |
Egress-Policy sperrt DNS |
DNS freigeben (15) |
password authentication failed nach Rotation |
Secret neu, DB alt |
erst DB, dann Secret (15) |