Tag 5: Volumes
Ziel: Du gibst Pods flüchtigen und dauerhaften Speicher und wählst dafür den passenden Volume-Typ. · Dauer: ca. 60 min · Voraussetzung: Tag 1
Briefing
CKAD: Application Design and Build (20 %) – „Utilize persistent and ephemeral volumes”.
flüchtig (lebt mit dem Pod) dauerhaft (lebt ohne den Pod)
emptyDir PVC --> PV (dynamisch per StorageClass
projected / configMap / secret oder statisch von Hand)
ephemeral (PVC pro Pod)
Im Labor ist local-path die Standard-StorageClass: Sie
legt Volumes als Verzeichnis auf dem Node an (nur RWO/RWOP) und bindet
erst, wenn ein Pod den PVC nutzt.
mkdir -p ~/ckad/tag05 && cd ~/ckad/tag05
k create ns tag05
k config set-context --current --namespace=tag05Drill 1: emptyDir im RAM
Zwei Container teilen sich ein Verzeichnis. Der Leser bindet es nur lesend ein.
cat > cache.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: cache
spec:
containers:
- name: writer
image: busybox:1.37
command: ["sh", "-c", "while true; do date >> /cache/log; sleep 5; done"]
volumeMounts:
- name: cache
mountPath: /cache
- name: reader
image: busybox:1.37
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
readOnly: true
volumes:
- name: cache
emptyDir:
medium: Memory
sizeLimit: 64Mi
EOF
k apply -f cache.yaml
k wait --for=condition=Ready pod/cache
sleep 10
k exec cache -c reader -- tail -2 /cache/log
k exec cache -c reader -- df -h /cache
k exec cache -c reader -- touch /cache/xErwartet:
Wed Nov 18 15:26:10 UTC 2026
Wed Nov 18 15:26:15 UTC 2026
Filesystem Size Used Available Use% Mounted on
tmpfs 64.0M 4.0K 64.0M 0% /cache
touch: /cache/x: Read-only file system
medium: Memory macht aus dem Volume ein tmpfs,
sizeLimit setzt seine Größe. Was dort liegt, zählt zum
Speicherverbrauch des Containers. Probier, mehr zu schreiben:
k exec cache -c writer -- dd if=/dev/zero of=/cache/big bs=1M count=100Erwartet (gekürzt):
dd: error writing '/cache/big': No space left on device
67104768 bytes (64.0MB) copied, 0.180261 seconds, 355.0MB/s
Ohne medium liegt das emptyDir auf der Node-Platte; dort
überwacht der Kubelet das sizeLimit und räumt den Pod bei
Überschreitung per Eviction ab.
Fortschritt speichern? Kostenlos registrieren
Drill 2: hostPath – nur mit Vorsicht
k delete pod cache --now
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
name: nodelog
spec:
containers:
- name: c
image: busybox:1.37
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: varlog
mountPath: /host/var/log
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/log
type: Directory
EOF
k wait --for=condition=Ready pod/nodelog
k exec nodelog -- ls /host/var/log/pods | head -2Erwartet (gekürzt):
calico-system_calico-node-zjlh8_2382af01-417c-4b72-bd87-6f45d3ed34ab
calico-system_csi-node-driver-pd2g4_02bed54d-7eef-47bc-ace0-424b1247cf08
Achtung: hostPath öffnet das Dateisystem des Nodes. Ein Pod mit Schreibzugriff auf
/oder/var/runkann den Node übernehmen. Für Anwendungen fast nie richtig; Pod-Security-Standards (Tag 9) verbieten es abbaseline.
k delete pod nodelog --nowFortschritt speichern? Kostenlos registrieren
Drill 3: PVC mit StorageClass
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: notes-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
EOF
k get pvc notes-data
k describe pvc notes-data | tail -1Erwartet (gekürzt):
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS ...
notes-data Pending local-path ...
Normal WaitForFirstConsumer 1s persistentvolume-controller waiting for first
consumer to be created before binding
Kein storageClassName → die Standardklasse greift.
Pending ist hier kein Fehler:
WaitForFirstConsumer wartet, bis der Scheduler weiß, auf
welchem Node der Pod landet.
Fortschritt speichern? Kostenlos registrieren
Drill 4: Daten überleben den Pod
cat > store.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: store
spec:
containers:
- name: c
image: busybox:1.37
command: ["sh", "-c", "echo start $(hostname) >> /data/log; sleep 3600"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: notes-data
EOF
k apply -f store.yaml
k wait --for=condition=Ready pod/store --timeout=90s
k get pvc notes-data
k delete pod store --now
k apply -f store.yaml
k wait --for=condition=Ready pod/store
k exec store -- cat /data/logErwartet (gekürzt):
NAME STATUS VOLUME CAPACITY ACCESS MODES
notes-data Bound pvc-454231cd-5f97-452b-9b90-aeda506caa82 1Gi RWO
start store
start store
Zwei Zeilen: Der zweite Pod hat die Datei des ersten
weitergeschrieben. Das PV von local-path hängt per
nodeAffinity an einem Node – der neue Pod landet zwingend
dort.
Jetzt der Gegenversuch mit ReadWriteMany:
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: shared
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 1Gi
EOF
sed 's/notes-data/shared/; s/name: store/name: rwx/' store.yaml | k apply -f -
k describe pvc shared | grep ProvisioningFailedErwartet (gekürzt):
Warning ProvisioningFailed 0s (x2 over 15s) rancher.io/local-path_... failed to
provision volume with StorageClass "local-path": NodePath only supports ReadWriteOnce
and ReadWriteOncePod (1.22+) access modes
Welche Access Modes gehen, bestimmt der Provisioner, nicht Kubernetes.
k delete pod rwx --now
k delete pvc sharedFortschritt speichern? Kostenlos registrieren
Drill 5: Statisches PV mit ReadWriteOncePod
PVs sind clusterweit, darum ein eindeutiger Name.
storageClassName: "" heißt „keine Klasse” – sonst würde die
Standardklasse den PVC an sich reißen.
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
name: tag05-static
spec:
capacity:
storage: 200Mi
accessModes: ["ReadWriteOncePod"]
persistentVolumeReclaimPolicy: Retain
storageClassName: ""
hostPath:
path: /tmp/tag05-static
type: DirectoryOrCreate
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: static-claim
spec:
storageClassName: ""
volumeName: tag05-static
accessModes: ["ReadWriteOncePod"]
resources:
requests:
storage: 100Mi
EOF
k get pvc static-claimErwartet (gekürzt):
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS
static-claim Bound tag05-static 200Mi RWOP
volumeName bindet gezielt an dieses PV. 100Mi angefragt,
200Mi bekommen: Der PVC belegt das ganze PV. Jetzt zwei Pods auf
denselben Claim:
sed 's/notes-data/static-claim/; s/name: store/name: one/' store.yaml | k apply -f -
sed 's/notes-data/static-claim/; s/name: store/name: two/' store.yaml | k apply -f -
k get pods one two
k describe pod two | tail -1Erwartet (nach einigen Sekunden, gekürzt):
NAME READY STATUS RESTARTS AGE
one 1/1 Running 0 9s
two 0/1 Pending 0 9s
... 2 node(s) unavailable due to PersistentVolumeClaim with ReadWriteOncePod access mode
already in-use by another pod. ...
RWO erlaubt mehrere Pods auf einem Node, RWOP genau einen Pod.
k delete pod one two --nowFortschritt speichern? Kostenlos registrieren
Drill 6: Projected Volume
Vier Quellen, ein Verzeichnis:
k create configmap app-cfg --from-literal=LOG_LEVEL=debug
k create secret generic app-key --from-literal=api-key=lab-key-123
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
name: proj
labels:
app: notes
spec:
containers:
- name: c
image: busybox:1.37
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: all
mountPath: /etc/app
readOnly: true
volumes:
- name: all
projected:
sources:
- configMap:
name: app-cfg
- secret:
name: app-key
- downwardAPI:
items:
- path: labels
fieldRef:
fieldPath: metadata.labels
- serviceAccountToken:
path: token
audience: notes-api
expirationSeconds: 3600
EOF
k wait --for=condition=Ready pod/proj
k exec proj -- ls /etc/app
k exec proj -- cat /etc/app/labels; echoErwartet:
LOG_LEVEL
api-key
labels
token
app="notes"
Das Token ist ein JWT mit eigener audience
(aud: ["notes-api"]) und läuft nach einer Stunde ab; der
Kubelet erneuert es rechtzeitig. Mehr dazu an Tag 8.
Fortschritt speichern? Kostenlos registrieren
Drill 7: Generisches ephemeral Volume
Wie emptyDir, aber vom Provisioner – mit Größe, StorageClass und eigener Platte.
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
name: scratch
spec:
containers:
- name: c
image: busybox:1.37
command: ["sh", "-c", "sleep 3600"]
volumeMounts:
- name: work
mountPath: /work
volumes:
- name: work
ephemeral:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: local-path
resources:
requests:
storage: 500Mi
EOF
k wait --for=condition=Ready pod/scratch --timeout=90s
k get pvc scratch-work -o jsonpath='{.metadata.ownerReferences[0].kind}{"\n"}'
k delete pod scratch --now
k get pvcErwartet (gekürzt):
Pod
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS
notes-data Bound pvc-4542... 1Gi RWO local-path
static-claim Bound tag05-static 200Mi RWOP
Der PVC heißt <pod>-<volume> und gehört dem
Pod. Mit dem Pod verschwindet er.
Fortschritt speichern? Kostenlos registrieren
Drill 8: subPath – eine Datei statt eines Ordners
k create configmap nginx-extra \
--from-literal=extra.conf='server { listen 8080; return 200 "tag05\n"; }'
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
containers:
- name: nginx
image: nginx:1.29
volumeMounts:
- name: extra
mountPath: /etc/nginx/conf.d/extra.conf
subPath: extra.conf
volumes:
- name: extra
configMap:
name: nginx-extra
EOF
k wait --for=condition=Ready pod/web
k exec web -- ls /etc/nginx/conf.d
k exec web -- curl -s localhost:8080Erwartet:
default.conf
extra.conf
tag05
Ohne subPath (mountPath /etc/nginx/conf.d)
überdeckt das Volume den Ordner, und default.conf ist weg.
Haken: Über subPath eingebundene Dateien erhalten keine
Aktualisierungen der ConfigMap.
Fortschritt speichern? Kostenlos registrieren
Übung: Speed-Drills
- Pod
tmp(busybox:1.37,sleep 3600) mit emptyDirscratchunter/scratch, höchstens 100Mi. Zeitziel: 2 min - PVC
logs(100Mi, RWO) und Podlogger, der ihn unter/var/log/appeinbindet. Zeitziel: 3 min - Pod
infomit Labeltier=backend, der seine Labels unter/etc/podinfo/labelslesen kann. Zeitziel: 2 min
Lösung
Gerüst immer mit k run … $do, dann volumes
und volumeMounts ergänzen:
k run tmp --image=busybox:1.37 $do --command -- sleep 3600 > tmp.yamlspec:
containers:
- name: tmp
image: busybox:1.37
command: ["sleep", "3600"]
volumeMounts:
- name: scratch
mountPath: /scratch
volumes:
- name: scratch
emptyDir:
sizeLimit: 100MiapiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: logs
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 100Mi
---
apiVersion: v1
kind: Pod
metadata:
name: logger
spec:
containers:
- name: logger
image: busybox:1.37
command: ["sh", "-c", "while true; do date >> /var/log/app/out; sleep 10; done"]
volumeMounts:
- name: logs
mountPath: /var/log/app
volumes:
- name: logs
persistentVolumeClaim:
claimName: logsmetadata:
name: info
labels:
tier: backend
spec:
containers:
- name: info
image: busybox:1.37
command: ["sleep", "3600"]
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
volumes:
- name: podinfo
downwardAPI:
items:
- path: labels
fieldRef:
fieldPath: metadata.labelsFortschritt speichern? Kostenlos registrieren
Kontrollpunkt
k get pvc logs -o jsonpath='{.status.phase}{"\n"}'Erwartet: Bound
k exec info -- cat /etc/podinfo/labels; echoErwartet: tier="backend"
k get pv tag05-static -o jsonpath='{.spec.accessModes}{"\n"}'Erwartet: ["ReadWriteOncePod"]
Challenge
Ein Kollege legt einen PVC reports (1Gi, RWO) mit
storageClassName: "" an. Er bleibt für immer
Pending. Finde den Grund und repariere ihn so, dass
local-path ihn bedient.
cat <<'EOF' | k apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: reports
spec:
storageClassName: ""
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
EOFHinweis: Die Spec eines PVC ist fast vollständig unveränderlich.
Lösung
k describe pvc reports | tail -1Erwartet (gekürzt):
Normal FailedBinding 3s persistentvolume-controller no persistent volumes available
for this claim and no storage class is set
"" schaltet die dynamische Bereitstellung ab, und es
gibt kein passendes PV. Ein Patch scheitert an
spec is immutable after creation. Also neu anlegen:
k get pvc reports -o yaml > reports.yaml
sed -i '/storageClassName: ""/d' reports.yaml
k replace --force -f reports.yaml
k get pvc reportsErwartet: reports mit
STORAGECLASS local-path, Status
Pending (WaitForFirstConsumer) – jetzt aus dem richtigen
Grund.
Aufräumen (das PV ist clusterweit; wegen Retain bleibt
das Verzeichnis unter /tmp auf dem Node, bis er neu
startet):
k config set-context --current --namespace=default
k delete ns tag05
k delete pv tag05-staticFortschritt speichern? Kostenlos registrieren
Prüfungstipp
- Volumes eines laufenden Pods sind unveränderlich:
k get pod x -o yaml > x.yaml, bearbeiten,k replace --force -f x.yaml. - PVC
Pending?k describe pvclesen: WaitForFirstConsumer ist normal, „no storage class is set” oder ein falscher Access Mode nicht. - Doku: kubernetes.io → Concepts → Storage → Volumes (alle Typen mit YAML-Beispiel).
Enterprise-Notiz
local-pathist Laborspeicher: Fällt der Node aus, sind die Daten weg. Produktion nutzt CSI-Treiber (Ceph/Rook, Longhorn, Cloud-Disks) mit Replikation und Snapshots.- Für Datenbanken StatefulSets mit
volumeClaimTemplatesstatt einzelner PVCs. - hostPath per Pod Security Admission oder Policy-Engine (Kyverno, Gatekeeper) sperren.
- RWX (NFS, CephFS) nur, wenn die Anwendung parallele Schreiber wirklich verträgt.