Quy ước: -n <ns> là namespace, -A là tất cả namespace. Nên đặt alias alias k=kubectl và export ns=<namespace>.
Khoanh vùng nhanh
| Mục đích | Lệnh |
|---|---|
| Pod không Running/Completed trên toàn cluster | kubectl get pods -A --field-selector=status.phase!=Running,status.phase!=Succeeded |
| Pod restart nhiều nhất | kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount' | tail |
| Event mới nhất (thường là manh mối đầu tiên) | kubectl get events -n $ns --sort-by=.lastTimestamp | tail -30 |
| Chỉ xem Warning | kubectl get events -A --field-selector type=Warning |
| Mô tả chi tiết Pod (Events ở cuối) | kubectl describe pod <pod> -n $ns |
| Trạng thái node | kubectl get nodes -o wide |
| Tài nguyên đang dùng | kubectl top pods -n $ns --sort-by=memory / kubectl top nodes |
Theo trạng thái Pod
| Trạng thái | Nguyên nhân hay gặp | Lệnh kiểm tra |
|---|---|---|
Pending |
Thiếu CPU/RAM, nodeSelector/taint không khớp, PVC chưa bind | kubectl describe pod <pod> → xem FailedScheduling |
ImagePullBackOff |
Sai tên/tag image, thiếu imagePullSecrets, registry chặn |
kubectl describe pod <pod> | grep -A5 Events |
CrashLoopBackOff |
App thoát ngay, thiếu env/config, probe sai | kubectl logs <pod> --previous |
OOMKilled |
Vượt limits.memory |
kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[*].lastState.terminated.reason}' |
CreateContainerConfigError |
ConfigMap/Secret được tham chiếu không tồn tại | kubectl describe pod <pod> → tên ConfigMap/Secret bị thiếu |
Terminating mãi không xong |
Finalizer kẹt, node mất kết nối | kubectl get pod <pod> -o jsonpath='{.metadata.finalizers}' |
Running nhưng 0/1 Ready |
readinessProbe fail | kubectl describe pod <pod> | grep -i readiness |
Logs và exec
| Mục đích | Lệnh |
|---|---|
| Log container đang chạy | kubectl logs <pod> -n $ns -f --tail=200 |
| Log của lần chạy trước khi crash | kubectl logs <pod> -n $ns --previous |
| Log container cụ thể / tất cả container | kubectl logs <pod> -c <container> / kubectl logs <pod> --all-containers |
| Log mọi Pod theo label | kubectl logs -l app=<app> -n $ns --prefix --tail=50 |
| Log 1 giờ gần nhất | kubectl logs <pod> --since=1h --timestamps |
| Vào shell | kubectl exec -it <pod> -n $ns -- sh |
| Image không có shell: gắn container debug | kubectl debug -it <pod> --image=nicolaka/netshoot --target=<container> |
| Debug trên node | kubectl debug node/<node> -it --image=busybox (filesystem của host ở /host) |
Network, Service, DNS
| Mục đích | Lệnh |
|---|---|
| Service có endpoint không (rỗng = selector không khớp hoặc Pod chưa Ready) | kubectl get endpointslices -n $ns -l kubernetes.io/service-name=<svc> |
| So selector của Service với label của Pod | kubectl get svc <svc> -o jsonpath='{.spec.selector}' và kubectl get pods -n $ns --show-labels |
| Kiểm tra DNS trong cluster | kubectl run dns-test --rm -it --image=busybox:1.36 --restart=Never -- nslookup <svc>.<ns>.svc.cluster.local |
| Gọi thử Service từ trong cluster | kubectl run curl --rm -it --image=curlimages/curl --restart=Never -- curl -sv http://<svc>.<ns>:<port>/ |
| Truy cập Service từ máy local | kubectl port-forward svc/<svc> 8080:<port> -n $ns |
| Log CoreDNS | kubectl logs -n kube-system -l k8s-app=kube-dns --tail=100 |
| NetworkPolicy đang áp dụng | kubectl get networkpolicy -n $ns |
| Ingress và backend | kubectl describe ingress <ing> -n $ns |
Node
| Mục đích | Lệnh |
|---|---|
| Condition của node (MemoryPressure, DiskPressure, Ready) | kubectl describe node <node> | grep -A8 Conditions |
| Pod đang chạy trên node | kubectl get pods -A -o wide --field-selector spec.nodeName=<node> |
| Taint của node | kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints |
| Ngưng nhận Pod mới / đẩy Pod ra để bảo trì | kubectl cordon <node> / kubectl drain <node> --ignore-daemonsets --delete-emptydir-data |
| Cho node nhận lại Pod | kubectl uncordon <node> |
| Log kubelet (SSH vào node) | journalctl -u kubelet -f --since "10 min ago" |
| Container runtime trên node | crictl ps -a / crictl logs <container-id> |
Deployment và rollout
| Mục đích | Lệnh |
|---|---|
| Theo dõi rollout | kubectl rollout status deploy/<app> -n $ns |
| Lịch sử revision | kubectl rollout history deploy/<app> -n $ns |
| Quay về bản trước | kubectl rollout undo deploy/<app> -n $ns |
| Restart toàn bộ Pod | kubectl rollout restart deploy/<app> -n $ns |
| ReplicaSet nào đang giữ Pod | kubectl get rs -n $ns -l app=<app> |
| Image đang chạy thực tế | kubectl get deploy <app> -o jsonpath='{.spec.template.spec.containers[*].image}' |
| HPA không scale | kubectl describe hpa <app> -n $ns (xem ScalingActive, metrics-server) |
Storage
| Mục đích | Lệnh |
|---|---|
| PVC chưa Bound | kubectl get pvc -n $ns và kubectl describe pvc <pvc> |
| PV và StorageClass | kubectl get pv / kubectl get storageclass |
| Volume gắn vào Pod | kubectl get pod <pod> -o jsonpath='{.spec.volumes}' |
| Lỗi attach/mount | kubectl get events -n $ns --field-selector reason=FailedMount |
Cấu hình và quyền
| Mục đích | Lệnh |
|---|---|
| Context/namespace đang dùng | kubectl config current-context / kubectl config view --minify | grep namespace |
| Kiểm tra quyền RBAC | kubectl auth can-i list pods -n $ns --as=system:serviceaccount:$ns:<sa> |
| Xem env thực tế trong container | kubectl exec <pod> -n $ns -- env | sort |
| So manifest local với cluster | kubectl diff -f deploy.yaml |
| Xuất YAML đang chạy (bỏ field runtime) | kubectl get deploy <app> -n $ns -o yaml | kubectl neat (cần plugin neat) |
| API resource có trong cluster | kubectl api-resources --namespaced=true |
Thứ tự khoanh vùng nên đi: Events → describe → logs --previous → exec/debug. Phần lớn lỗi Pending và CrashLoopBackOff lộ ra ngay trong Events mà không cần vào container.
kubectl get secret -o yaml in ra giá trị base64 (chỉ là mã hoá, không phải bảo mật). Đừng dán output đó vào sổ tay hay ticket.
Xem thêm: Vite app deploy dưới URI prefix sau Nginx cho lỗi phía Ingress/reverse proxy.