8 lab Kubernetes failure và operations
Dùng local hoặc managed cluster phù hợp; mỗi lab phải tạo được failure có kiểm soát, debug theo evidence, phục hồi hệ thống và lưu lại manifests, events, conditions, metrics, timeline cùng recovery evidence.
Lab 01 · Reconciliation trace
Apply Deployment rồi quan sát chuỗi Deployment → ReplicaSet → Pod → scheduler → kubelet → EndpointSlice bằng get/watch/events/ownerReferences. Scale hoặc delete Pod và giải thích vì sao hệ thống converge về desired state.
Objective
Chứng minh Kubernetes vận hành theo reconciliation thay vì “chạy một lần”, và phân biệt desired state, observed state với controller ownership.
Prerequisites
- Namespace lab, một Deployment 2 replicas và Service chọn đúng label.
- Quyền đọc Deployment/ReplicaSet/Pod/Event/EndpointSlice.
Steps
- Apply Deployment + Service và lưu YAML thực tế bằng
kubectl get ... -o yaml. - Watch Deployment, ReplicaSet, Pod và EndpointSlice song song.
- Đọc
ownerReferencestừ Pod lên ReplicaSet rồi Deployment; ghi scheduler/node assignment và readiness. - Scale Deployment từ 2 lên 4 replicas, sau đó delete một Pod đang Ready và quan sát controller tạo replacement.
kubectl get deploy,rs,pod -w
kubectl get events --sort-by=.lastTimestamp
kubectl get pod <pod> -o jsonpath='{.metadata.ownerReferences}'
kubectl get endpointslice -l kubernetes.io/service-name=<service> -w
Failure injection & verification
Delete Pod thủ công. Verify replica count quay lại desired state, Pod mới có owner đúng và EndpointSlice chỉ publish endpoint đủ điều kiện theo implementation/readiness.
Lab 02 · Pod failure matrix
Tạo bad image, bad command, missing config, permission, liveness loop và CrashLoop. Debug current/previous logs, last state và backoff thay vì chỉ restart Pod.
Objective
Nhận diện failure class bằng status/conditions/events/logs và chọn đúng evidence cho container chưa start, đang crash hoặc bị probe kill.
Prerequisites
Một Deployment mẫu có ConfigMap/Secret test, probes và quyền đọc logs/events.
Steps
- Lần lượt inject image tag không tồn tại, command sai, missing ConfigMap/Secret key và filesystem/UID permission không phù hợp.
- Tạo container exit non-zero để quan sát CrashLoopBackOff; sau đó cấu hình liveness probe luôn fail.
- Với mỗi lỗi, ghi
status.containerStatuses,state,lastState, restart count, events, current logs và--previous.
kubectl describe pod <pod>
kubectl logs <pod> -c <container>
kubectl logs <pod> -c <container> --previous
kubectl get pod <pod> -o jsonpath='{.status.containerStatuses}'
Failure injection & verification
Không sửa nhiều biến cùng lúc. Với từng case, sửa đúng root cause rồi verify Pod Ready ổn định và restart count không tiếp tục tăng.
Lab 03 · Resources/OOM/Pending
Thực hành requests/limits/QoS, CPU throttle và memory OOM; tạo Pending bằng affinity/taint/insufficient resource. Không chỉ tăng limit — phải đo heap/native/traffic và xác định capacity bottleneck.
Objective
Liên hệ scheduler admission, runtime enforcement và application behavior với request/limit thay vì nhìn mỗi trạng thái Pod.
Prerequisites
- Metrics Server hoặc telemetry tương đương nếu muốn quan sát CPU/memory.
- Workload có thể tạo CPU/memory load có kiểm soát.
Steps
- Đặt requests/limits khác nhau, ghi QoS class và node allocatable/requested.
- Tạo memory pressure vượt limit để quan sát OOMKilled/lastState; tạo CPU-bound load và so latency/throttle signals nếu runtime/metrics hỗ trợ.
- Tạo Pending bằng request lớn hơn capacity hoặc constraint affinity/taint không thỏa; đọc scheduler events.
- Phân biệt application leak/working-set tăng, native/off-heap, traffic burst và cấu hình limit quá thấp.
Failure injection & verification
Fix bằng thay đổi có giả thuyết: giảm working set, điều chỉnh concurrency/cache, request/limit hoặc capacity. Verify latency, memory trend, OOM count và Pending reason đã ổn định.
Lab 04 · Zero-error rollout
Giữ continuous traffic trong rollout; kiểm tra startup/readiness/TERM, maxSurge/maxUnavailable/minReadySeconds và schema compatible. Đo errors/in-flight/rollout conditions rồi inject bad release và rollback.
Objective
Chứng minh “Deployment Complete” không đồng nghĩa zero-error; phối hợp lifecycle, readiness, termination, capacity và backward-compatible dependency.
Prerequisites
- Deployment nhiều replicas, Service và traffic generator liên tục.
- Hai image versions: good và bad release có lỗi dễ quan sát.
Steps
- Baseline request rate/error/latency và số Ready endpoints.
- Cấu hình startup/readiness phù hợp, graceful termination và rolling update strategy; rollout good version.
- Quan sát Deployment conditions, ReplicaSets, Pod readiness, in-flight requests và error rate.
- Inject bad image/config hoặc version làm readiness fail; xác định failure window và blast radius.
- Rollback về revision tốt, verify endpoint population và traffic hồi phục.
kubectl rollout status deployment/<name>
kubectl rollout history deployment/<name>
kubectl rollout undo deployment/<name>
kubectl get deploy,rs,pod -w
Lab 05 · Network forensic
Tạo lần lượt Service selector mismatch, wrong targetPort/listen, DNS, no endpoints, default-deny NetworkPolicy và Gateway route/TLS. Debug từng hop thay vì kết luận chung là “network lỗi”.
Objective
Khoanh vùng request path từ client → DNS → route/Gateway → Service → EndpointSlice → Pod listener/policy.
Prerequisites
- Workload HTTP đơn giản, Service và DNS hoạt động.
- Nếu cluster có Gateway API/controller thì dùng route/TLS; nếu không, ghi rõ giới hạn môi trường.
Steps
- Baseline direct Pod IP, Service DNS và Service ClusterIP.
- Làm sai selector rồi verify EndpointSlice rỗng/không match; sửa lại.
- Làm sai
targetPorthoặc app listen port; kiểm tra listener trong Pod. - Áp default-deny NetworkPolicy rồi thêm allow rule tối thiểu.
- Nếu có Gateway, inject hostname/path/backend/TLS mismatch và theo dõi status conditions của Gateway/Route.
Failure injection & verification
Mỗi lần chỉ thay một hop. Sau fix, verify từ cùng client, cùng hostname/path và ghi rõ DNS resolution, endpoints, policy và route status.
Lab 06 · Stateful storage
Dùng StatefulSet + headless Service + PVC. Delete/reschedule Pod, mô phỏng zone/attach failure khi môi trường hỗ trợ, thực hiện snapshot/restore và kiểm tra reclaim policy; ghi rõ RPO/RTO limitations.
Objective
Phân biệt Pod identity, volume identity, scheduling topology, data durability và backup/restore.
Prerequisites
- StorageClass có dynamic provisioning; VolumeSnapshot chỉ dùng nếu CSI driver/cluster hỗ trợ.
- Dữ liệu test có checksum/version marker để verify restore.
Steps
- Deploy StatefulSet với headless Service và
volumeClaimTemplates; ghi Pod ordinal, PVC/PV binding. - Write dữ liệu marker, delete Pod và verify Pod ordinal trở lại với đúng persistent data.
- Nếu có topology constraints, tạo tình huống Pod không attach/mount được và đọc events từ Pod/PVC/PV/VolumeAttachment.
- Tạo snapshot/backup theo capability, thay đổi dữ liệu, restore sang volume mới và verify checksum.
- Kiểm tra StorageClass/PV reclaim policy trước khi delete PVC/PV.
Failure injection & verification
Không coi snapshot là backup hoàn chỉnh nếu chưa test restore. Ghi recovery point thực tế, recovery time và dependency ngoài volume như Secret/config/schema.
Lab 07 · Security baseline
Áp dụng dedicated ServiceAccount/RBAC, non-root/read-only/drop capabilities/seccomp, Secret external injection, quotas và admission policy theo hướng audit → enforce. Phải chứng minh các action không được phép thực sự bị deny.
Objective
Xây baseline least-privilege có thể kiểm chứng cho workload và giảm blast radius khi container bị compromise.
Prerequisites
- Namespace lab; quyền tạo Role/RoleBinding và workload security context.
- Nếu có Pod Security Admission/policy engine, dùng namespace riêng để tránh ảnh hưởng workload khác.
Steps
- Tạo ServiceAccount riêng, Role chỉ có verbs/resources cần thiết và RoleBinding.
- Verify bằng
kubectl auth can-i --as=system:serviceaccount:...cho allowed và denied actions. - Cấu hình non-root, read-only root filesystem khi app hỗ trợ, drop capabilities và seccomp profile phù hợp.
- Inject Secret qua cơ chế cluster/external secret phù hợp môi trường; không in Secret vào logs/evidence.
- Thêm ResourceQuota/LimitRange nếu phù hợp; thử Pod vi phạm policy ở audit/warn rồi chuyển enforce sau khi remediation.
Failure injection & verification
Thử đọc Secret ngoài scope, tạo privileged Pod hoặc action RBAC không được grant. Verify deny xảy ra ở đúng control plane layer và workload hợp lệ vẫn chạy.
can-i/API request, Pod security context, admission warning/deny và checklist không leak credential.Lab 08 · Scaling/disruption game day
Chạy HPA theo CPU hoặc custom queue metric, đưa vào node provisioning delay, DB saturation, PDB/drain/node loss. Tune stabilization/max replicas và load shedding; cuối cùng viết incident runbook.
Objective
Chứng minh autoscaling chỉ hữu ích khi dependency và cluster capacity theo kịp; luyện phản ứng disruption mà không khuếch đại overload.
Prerequisites
- HPA metrics pipeline hoạt động; nếu custom metric không có thì dùng CPU và ghi rõ giới hạn.
- Traffic/load generator và dependency test có saturation signal.
Steps
- Baseline replicas, utilization, request rate, latency/error, queue age và DB saturation signal.
- Tăng load để HPA scale; ghi delay từ metric → desired replicas → scheduled/Ready capacity.
- Tạo node provisioning/scheduling delay hoặc cordon/drain node test; quan sát Pending và available replicas.
- Tạo downstream DB saturation để chứng minh tăng replicas có thể làm overload nặng hơn.
- Thử PDB với drain/node loss; tune stabilization window/max replicas và thêm load shedding/backpressure.
- Viết runbook: detection, immediate containment, capacity action, rollback/recovery và post-incident evidence.
Failure injection & verification
Không drain production node. Verify hệ thống có bounded recovery, không scale vô hạn và SLO phục hồi sau khi bỏ fault/load.
Rubric
| Mức | Tiêu chí | Evidence tối thiểu |
|---|---|---|
| 1 | Workload chạy happy path. | Manifest + baseline output chứng minh workload/Service hoạt động. |
| 2 | Có probes/resources/security và systematic debug. | Failure được inject có kiểm soát, root cause dựa trên events/logs/status/metrics, có fix + verification. |
| 3 · Senior | Chứng minh reconciliation, failure, rollout, scaling và recovery. | Có timeline, failure window/blast radius, capacity/security signal, rollback/recovery evidence và trade-off/RPO/RTO khi phù hợp. |