Part 12 · Kubernetes · 12.1.10

Events, status, logs và production debugging

Troubleshoot theo reconciliation chain: desired object → controller/status → scheduling → kubelet/runtime → network/storage → application SLI. Mục tiêu không phải “chạy nhiều lệnh”, mà là thu hẹp failure domain bằng evidence và bảo toàn khả năng rollback.

Troubleshooting

Kubernetes là hệ thống reconciliation: object khai báo desired state, nhiều controller và node component cùng tiến dần actual state về desired state. Vì vậy debug hiệu quả phải trả lời tuần tự: object có đúng không, controller đã quan sát chưa, Pod có được schedule không, kubelet/runtime có chạy container không, dependency network/storage có sẵn sàng không, và cuối cùng user-facing SLI có đạt không.

Nguyên tắc: tìm “lớp đầu tiên bị sai” trong chuỗi thay vì sửa triệu chứng ở lớp sau. Ví dụ Pod Running nhưng không nhận traffic chưa đủ để kết luận application healthy; readiness, Service selector, EndpointSlice, port, policy và gateway vẫn có thể sai.

Systematic flow

  1. Get: status, readiness, restart count, age, node, owner và rollout generation.
  2. Describe: Conditions, Events, scheduling decisions, probes, mounts, image pulls và container states.
  3. Logs: current logs; với container restart dùng thêm --previous. Correlate timestamp với deploy/config change.
  4. Resources: requests/limits, actual usage, QoS, node pressure, eviction signal và throttling/OOM evidence.
  5. Network: Service selectors, EndpointSlices, DNS, ports/targetPort, listen address, NetworkPolicy và ingress/gateway path.
  6. Storage: PVC/PV binding, attach/mount, access mode, topology và CSI events.
  7. Controller/release: rollout history, image/config diff, desired vs observed generation, canary/SLI và rollback path.
kubectl get pod -n <ns> -o wide
kubectl describe pod <pod> -n <ns>
kubectl logs <pod> -n <ns> -c <container> --previous
kubectl get events -n <ns> --sort-by=.lastTimestamp
kubectl get svc,endpointslice -n <ns>

Đây là triage sequence, không phải checklist cứng. Nếu symptom chỉ xuất hiện ở một Availability Zone, một node pool hoặc một version, hãy phân nhóm evidence theo dimension đó trước khi mở rộng phạm vi.

Common symptoms

Triệu chứngFailure domain thường gặpEvidence ưu tiênAction an toàn đầu tiên
PendingThiếu CPU/memory; taint/toleration; affinity/topology; PVC; image pull secretPod Conditions, scheduler Events, PVC/PV statusKhông nới constraint mù quáng; xác nhận bottleneck và capacity/headroom
CrashLoopBackOffCommand/config/permission; probe; dependency; app exitContainer lastState/exit code, current + --previous logsFreeze config/image diff; tránh restart thủ công làm mất evidence
OOMKilledMemory limit thấp; heap/native growth; burst traffic; leakLast termination reason, memory working set/RSS, traffic rate, limitGiảm tải hoặc rollback trước; chỉ tăng limit khi node capacity cho phép
Running nhưng không có trafficReadiness; selector; EndpointSlice; targetPort/listen; NetworkPolicy; gatewayReady condition, endpoints, port mapping, request pathTest từng hop từ Service đến Pod trước khi thay gateway/routing
Latency/error tăng sau deployRegression; cold cache; dependency saturation; rollout overlapVersion-sliced SLI, saturation, traces, rollout timelineStop/pause rollout hoặc rollback nếu error budget đang burn

Observability

Quan sát theo nhiều lớp: API/control-plane latency và audit; scheduler attempts; controller workqueue/reconcile errors; kubelet/runtime/CNI/CSI; node CPU/memory/disk/PID; workload RED metrics (rate, errors, duration) và dependency saturation. Không có một signal đơn lẻ nào đủ để mô tả toàn incident.

LớpSignals cần giữCâu hỏi chẩn đoán
Control planeAPI latency/errors, admission failures, audit, scheduler/controller queueDesired state có được accepted và reconciled kịp không?
Node/runtimeNode conditions, CPU/memory/disk/PID pressure, kubelet/runtime logsNode có đủ tài nguyên và runtime có ổn định không?
Network/storageCNI/CSI errors, EndpointSlices, DNS, attach/mount latencyPod có reach dependency và data path có sẵn sàng không?
ApplicationRequest rate, error rate, latency, queue depth, dependency metricsUser-visible impact bắt đầu lúc nào và tập trung ở version/tenant/region nào?

Events hữu ích nhưng có tính tạm thời và có thể bị rate-limit; nếu incident evidence quan trọng, phải export hoặc gom về hệ thống quan sát tập trung. Tương tự, node/pod/container có lifecycle ngắn nên cluster-level logging cần backend độc lập với node và workload lifecycle.

Cardinality và retention là capacity concern. Label theo pod UID, request ID hoặc unbounded user value có thể làm metrics/logging backend quá tải đúng lúc incident. Định nghĩa budget cho ingestion, retention, query latency và high-cardinality dimensions trước production.

Debug safely

Dùng ephemeral containers khi runtime image tối giản và cần công cụ debug; tránh sửa trực tiếp production container/image chỉ để “thử”. Mọi exec, port-forward, debug container và quyền đọc log/secret cần tuân thủ RBAC, audit và incident procedure.

Failure window: restart, reschedule, autoscaling hoặc rollback có thể xóa trạng thái tạm thời và thay đổi topology. Nếu incident cần root cause sâu, thu evidence cần thiết trước hành động phục hồi — nhưng không trì hoãn mitigation khi SLO đang bị ảnh hưởng nghiêm trọng.

Release validation

Controller báo rollout complete chỉ xác nhận trạng thái Kubernetes ở mức controller; nó không chứng minh request thực tế thành công. Validation production phải kết hợp rollout condition với user SLI, canary metrics, error-budget burn, dependency saturation và business signal phù hợp.

Rollback / forward-fix gate

kubectl rollout status deployment/<name> -n <ns>
kubectl rollout history deployment/<name> -n <ns>
# rollback chỉ khi compatibility và runbook cho phép
kubectl rollout undo deployment/<name> -n <ns>
Production closeout: lưu timeline, symptom, evidence, mitigation, root cause, detection gap và corrective action. Một incident đã phục hồi nhưng không cải thiện signal/runbook/capacity guardrail sẽ dễ lặp lại.
Tài liệu: Troubleshooting Kubernetes · Debug Running Pods · Observability · Logging Architecture · System Logs