Bài 11: Debug Pod Với kubectl describe Và exec

Sau khi đã học cách giám sát logs và events ở Bài 10, chúng ta sẽ tiếp tục domain Observability (chiếm 10% trọng số trong kỳ thi CKAD) bằng việc tìm hiểu hai lệnh mạnh mẽ nhất để debug Pods: kubectl describekubectl exec (sau này đa phần thấy mọi người dùng giao diện hơi nhiều nhưng mình khuyến nghị dùng command nhé như vậy thì trong môi trường không có giao diện vẫn chiến tốt). Đây là công cụ cốt lõi giúp bạn kiểm tra trạng thái chi tiết, xác định vấn đề, và tương tác trực tiếp bên trong containers để troubleshoot runtime issues.

Bài viết này thuộc domain Observability, CKAD yêu cầu bạn sử dụng các lệnh này để debug hands-on. Trong kỳ thi, bạn thường gặp task như describe Pod để tìm lý do Pending, hoặc exec vào container để check files/processes gây CrashLoopBackOff. Giờ chúng ta vào luôn xem describeexec là gì, cách sử dụng, kết hợp với logs/events, và áp dụng trong debug thực tế.

kubectl describe Là Gì Và Cách Sử Dụng Để Debug

kubectl describe là lệnh cung cấp thông tin chi tiết về một resource (như Pod, Node, Deployment), bao gồm metadata, spec, status, conditions, và events liên quan. Đây là “first stop” cho debug, vì nó aggregate data từ API server mà không cần nhiều lệnh riêng lẻ.

Cấu trúc output của describe pod:

  • Name/Namespace/Labels/Annotations: Metadata cơ bản.
  • Status: Phase (Pending/Running/Succeeded/Failed/Unknown), Conditions (Ready, Initialized, etc.), IP/Node.
  • Containers/Init Containers: Image, State (Running/Waiting/Terminated), Ready, Restart Count, Probes.
  • Volumes/Mounts: Chi tiết storage.
  • Events: Last events liên quan đến Pod (rất hữu ích cho debug).

Use cases debug:

  • Pod Pending: Check events cho FailedScheduling hoặc Insufficient resources.
  • CrashLoopBackOff: Xem Restart Count cao, Last State Terminated với exit code/reason.
  • Not Ready: Check Conditions False (e.g., ContainersReady False).

Syntax: kubectl describe pod [-n ].

Options:

  • --show-events=false: Ẩn events nếu không cần.
  • Output chỉ phần: Kết hợp với | grep Events để filter.

Dưới đây là ví dụ output kubectl describe pod minh họa các phần chi tiết như Status và Events.

https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-11-13-kubeclt-describe/4.png

kubectl exec Là Gì Và Cách Sử Dụng Để Debug

kubectl exec cho phép chạy lệnh bên trong container của Pod, tương tự SSH vào máy ảo. Đây là công cụ interactive để check files, processes, env vars, hoặc test network từ góc nhìn container.

Syntax: kubectl exec [-c ] [-n ] [-it] --.

Options:

  • -i: Interactive (stdin).
  • -t: TTY (pseudo-terminal cho shell).
  • -c: Chỉ định container (default đầu tiên).
  • Ví dụ shell: kubectl exec -it my-pod -- /bin/sh (mở shell).

Use cases debug:

  • Check files: ls /app, cat /etc/config.
  • Processes: ps aux, top.
  • Network: curl localhost:80, ping google.com.
  • Env: env | grep DB.

Lưu ý: Exec yêu cầu container running; nếu crash, dùng ephemeral debug container (nâng cao, không trong CKAD cơ bản).

Kết Hợp describeexec Với Các Công Cụ Khác Để Debug Toàn Diện

Debug hiệu quả thường kết hợp:

  • get pods: Xem overview status.
  • describe pod: Xem details và events.
  • logs: Xem output.
  • exec: Interact nội bộ.

Ví dụ workflow debug Pod không Running:

  1. kubectl get pods: Thấy Pending/CrashLoop.
  2. kubectl describe pod: Check events (e.g., ImagePull fail).
  3. Nếu Running nhưng not Ready: exec check probes fail (e.g., curl health endpoint).
  4. Nếu crash: logs --previous + exec check config files.

https://static.learnkube.com/7168d73d007a65dc2fae9686d539e015.png

Bảng Cheat Sheet Lệnh Debug Với describeexec

Lệnh Mô tả Ví dụ
kubectl describe pod Chi tiết Pod, events kubectl describe pod nginx-pod
kubectl describe node Debug node issues kubectl describe node minikube
kubectl exec -- Chạy lệnh non-interactive kubectl exec nginx-pod -- ls /app
kubectl exec -it -- sh Mở shell interactive kubectl exec -it nginx-pod -c nginx -- /bin/sh
kubectl exec -c Chỉ định container kubectl exec -c sidecar my-pod -- tail log
kubectl describe pod \| grep Events Filter events kubectl describe pod my-pod \| grep Events

Dưới đây là cheat sheet debug Pod, tóm tắt commands cho observability.

https://miro.medium.com/v2/resize:fit:1400/1*7pnrBHJn_fcyG0JZzCpVvQ.png

Liên Hệ Với Chứng Chỉ CKAD

Domain Observability (10%) nhấn mạnh debug với describe/exec, chiếm phần lớn task. Các nhiệm vụ điển hình:

  • Describe Pod để tìm lý do FailedMount hoặc OOMKilled.
  • Exec vào container kiểm tra env vars hoặc files gây error.
  • Kết hợp với logs debug multi-container Pod.

Mẹo thi:

  • Describe luôn đầu tiên cho overview; exec cho deep dive.
  • Thời gian: 4-6 phút/task, dùng autocomplete (kubectl d p).
  • Nhớ -c cho multi-container, -it cho shell.
  • Luyện trên Katacoda: Tạo Pod fail và debug.
  • Kết hợp get events từ Bài 10 cho full picture.

Thực Hành Thực Tế Trên Minikube

  1. Tạo Pod healthy:

    kubectl run debug-pod --image=nginx
  2. Describe Pod:

    kubectl describe pod debug-pod  # Xem status Running, events Pulled/Scheduled
  3. Exec vào Pod:

    kubectl exec -it debug-pod -- /bin/bash
    # Bên trong: ls /usr/share/nginx/html, ps aux, env | grep PATH, exit
  4. Tạo Pod fail (ví dụ sai image):

    kubectl run fail-pod --image=nonexistent:image
    kubectl describe pod fail-pod  # Thấy events ImagePullBackOff
  5. Debug multi-container (từ Bài 8):

    kubectl describe pod sidecar-pod  # Xem containers status
    kubectl exec -it sidecar-pod -c app-container -- curl localhost:80
  6. Cleanup: kubectl delete pod debug-pod fail-pod.

Nếu exec fail (Pod not Running), check describe trước.

Kết Luận

Bài này mình đã hướng dẫn chi tiết debug Pod với kubectl describeexec, từ lệnh cơ bản đến kết hợp debug. Bạn giờ có thể troubleshoot Pods hiệu quả, hoàn tất domain Observability cho CKAD. Tiếp theo, ở Bài 12: Tối Ưu Pod Với Resource Limits Và Requests, chúng ta sẽ bước vào domain Pod Design để học tối ưu hóa.

Điều hướng chuỗi bài viết<< Bài 10: Giám Sát Log Và Events Với kubectl logs
>> Bài 12: Tối Ưu Pod Với Resource Limits Và Requests

Thông tin nổi bật

Sự kiện phát trực tiếp​

Event Thumbnail

Báo cáo quan trọng

Article Thumbnail
Article Thumbnail
Chia sẻ bài viết:
Theo dõi
Thông báo của
0 Góp ý
Được bỏ phiếu nhiều nhất
Mới nhất Cũ nhất
Phản hồi nội tuyến
Xem tất cả bình luận

Tiêu điểm chuyên gia