Computer Notes/Kubernetes/Kubernetes Troubleshooting Cheat Sheet: Difference between revisions
No edit summary |
|||
| (2 intermediate revisions by the same user not shown) | |||
| Line 8: | Line 8: | ||
== 1. Context & First-Look Triage == | == 1. Context & First-Look Triage == | ||
Run these commands first to orient yourself and spot broken workloads across the cluster | Run these commands first to orient yourself and spot broken workloads across the cluster | ||
Verify which cluster and namespace you are targeting | |||
kubectl config current-context | |||
kubectl config set-context --current --namespace=<ns> | |||
kubectl config current-context | |||
kubectl config set-context --current --namespace=<ns> | |||
Fast scan: show every pod that is NOT Running | |||
kubectl get pods -A --field-selector=status.phase!=Running | |||
kubectl get pods - | |||
Detailed pod view (READY, STATUS, RESTARTS, IP, NODE) | |||
kubectl get nodes -o wide | |||
kubectl top nodes | kubectl get pods -n <ns> -o wide | ||
kubectl top pods -n <ns> --sort-by=memory | |||
Check node health (NotReady, MemoryPressure, DiskPressure) | |||
kubectl get nodes -o wide | |||
kubectl top nodes | |||
kubectl top pods -n <ns> --sort-by=memory | |||
| Line 32: | Line 33: | ||
Follow the standard debugging flow: <code>get</code> → <code>describe</code> → <code>logs</code> → <code>events</code>. | Follow the standard debugging flow: <code>get</code> → <code>describe</code> → <code>logs</code> → <code>events</code>. | ||
Read the bottom "Events" table (scheduling, image pull, probes, OOMKilled) | |||
<blockquote> | |||
kubectl describe pod <pod> -n <ns> | kubectl describe pod <pod> -n <ns> | ||
</blockquote> | |||
View logs from the PREVIOUS crashed container (essential for CrashLoopBackOff) | |||
<blockquote> | |||
kubectl logs <pod> -n <ns> --previous | |||
</blockquote> | |||
Stream live logs or grab the last 100 lines across all replicas of an app | |||
<blockquote> | |||
kubectl logs -f <pod> -n <ns> --tail=100 | |||
kubectl logs -l app=<label> -n <ns> --tail=100 | |||
</blockquote> | |||
Multi-container pod (specify container name) | |||
<blockquote> | |||
kubectl logs <pod> -c <container> -n <ns> | |||
</blockquote> | |||
Cluster-wide warning events sorted chronologically | |||
<blockquote> | |||
kubectl get events -n <ns> --field-selector type=Warning --sort-by=.lastTimestamp | |||
</blockquote> | |||
== 3. Immediate Mitigation & Rollbacks == | |||
Unblock production SLAs and isolate faulty nodes before digging into root cause. | |||
Check deployment rollout history and undo a bad release | |||
kubectl rollout status deployment/<name> -n <ns> | |||
kubectl | kubectl rollout undo deployment/<name> -n <ns> | ||
< | |||
Scale healthy replicas up (or scale a broken worker down to 0) | |||
kubectl scale deployment/<name> --replicas=<count> -n <ns> | |||
== | Isolate a bad node (stop new pods from scheduling on it) | ||
kubectl cordon <node> | |||
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data | |||
== 4. Live Shell, Networking & DNS Checks == | |||
Use these when a pod or worker is timing out or failing to reach internal services or external APIs. | |||
Shell into a running container | |||
kubectl exec -it <pod> -n <ns> -- /bin/sh | |||
kubectl | |||
Attach an ephemeral debug container (works on distroless images with no shell) | |||
kubectl debug -it <pod> -n <ns> --image=busybox --target=<container> | |||
Spin up a temporary network debug pod (dig, nslookup, curl, tcpdump) | |||
= | kubectl run netdebug --rm -it --image=nicolaka/netshoot -n <ns> -- /bin/bash | ||
Verify Service-to-Pod selector matching (if <none>, labels don't match) | |||
kubectl get svc,endpoints -n <ns> | |||
kubectl | |||
Test CoreDNS and external egress from inside a pod | |||
nslookup kubernetes.default | |||
nslookup s3.amazonaws.com | |||
Forward a local port to test a pod/service directly | |||
kubectl port-forward svc/<svc> 8080:80 -n <ns> | |||
kubectl port-forward svc/<svc> 8080:80 -n <ns | |||
| Line 99: | Line 109: | ||
Commands for inspecting autoscaling, configuration objects, RBAC, and the Kubernetes-to-AWS IAM boundary (IRSA / EKS Pod Identity). | Commands for inspecting autoscaling, configuration objects, RBAC, and the Kubernetes-to-AWS IAM boundary (IRSA / EKS Pod Identity). | ||
Check Horizontal Pod Autoscaler (if <unknown>, pod is missing resources.requests) | |||
kubectl get hpa -n <ns> | |||
kubectl | kubectl describe hpa <hpa-name> -n <ns> | ||
Verify ConfigMaps, Secrets, and Storage | |||
kubectl get cm,secret,pvc -n <ns> | |||
kubectl | |||
Check which ServiceAccount a pod is using (for IRSA / EKS Pod Identity) | |||
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.serviceAccountName}' | |||
Verify the IRSA IAM role annotation on the ServiceAccount | |||
kubectl describe sa <sa-name> -n <ns> | |||
Check Kubernetes RBAC permissions for a ServiceAccount | |||
kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa-name> -n <ns> | |||
== 6. Quick Status-to-Command Lookup == | == 6. Quick Status-to-Command Lookup == | ||
| Line 132: | Line 143: | ||
|- | |- | ||
| '''AccessDenied (AWS)''' || <code>kubectl describe sa <sa></code> || Missing <code>eks.amazonaws.com/role-arn</code> annotation, OIDC trust policy mismatch, or KMS key policy block. | | '''AccessDenied (AWS)''' || <code>kubectl describe sa <sa></code> || Missing <code>eks.amazonaws.com/role-arn</code> annotation, OIDC trust policy mismatch, or KMS key policy block. | ||
|} | |||
== 7. Pod Statuses (<code>kubectl get pods</code>) == | |||
These reflect the lifecycle phase of the pod or the state of its containers. | |||
{| class="wikitable" | |||
! Pod Status !! What It Means !! Common Cause / First Check | |||
|- | |||
| '''Pending''' || Accepted by the cluster, but not yet scheduled onto a node (or still waiting on volumes/images). || Insufficient CPU/RAM, node taint/affinity mismatch, or unbound PVC. Check <code>kubectl describe pod</code> for <code>FailedScheduling</code>. | |||
|- | |||
| '''ContainerCreating''' || Scheduled to a node; kubelet is pulling images, mounting volumes, or attaching networking. || Normal briefly. If stuck, check for volume mount failures or VPC/CNI IP exhaustion. | |||
|- | |||
| '''Running''' || Bound to a node and at least one container is running. || Check the <code>READY</code> column (e.g., <code>0/1</code> means the container is up, but failing its <code>readinessProbe</code> and receiving no traffic). | |||
|- | |||
| '''CrashLoopBackOff''' || Container starts, crashes, and Kubernetes waits with exponential backoff before restarting it again. || App startup crash, missing env var/Secret, or aggressive <code>livenessProbe</code>. Run <code>kubectl logs <pod> --previous</code>. | |||
|- | |||
| '''OOMKilled (Exit 137)''' || Container exceeded its memory <code>limits</code> and was killed by the Linux kernel. || Memory leak or heavy workload (e.g., large file scan). Raise memory <code>limits</code> or reduce worker batch size. | |||
|- | |||
| '''ImagePullBackOff''' || Kubelet cannot pull the container image from the registry. || Wrong image tag, missing <code>imagePullSecrets</code>, or EKS node missing ECR read permissions / NAT egress. | |||
|- | |||
| '''CreateContainerConfigError''' || Kubelet cannot assemble the container config before starting it. || Referenced <code>ConfigMap</code> or <code>Secret</code> does not exist in that namespace. | |||
|- | |||
| '''Init:0/1 / Init:Error''' || An init container has not completed or is failing, blocking the main container from starting. || Inspect the init container specifically with <code>kubectl logs <pod> -c <init-container></code>. | |||
|- | |||
| '''Evicted''' || Kubelet forcibly terminated the pod to free up node resources. || Usually a symptom of '''Node''' <code>MemoryPressure</code> or <code>DiskPressure</code>. | |||
|- | |||
| '''Terminating''' || Pod is shutting down gracefully. || If stuck, check for hung shutdown hooks, stuck finalizers, or an unreachable node. | |||
|- | |||
| '''Completed''' || All containers exited cleanly with exit code <code>0</code>. || Normal end state for <code>Job</code> and <code>CronJob</code> pods. | |||
|- | |||
| '''Unknown''' || Control plane lost contact with the node hosting the pod. || Node crashed, lost network connectivity, or <code>kubelet</code> stopped running. | |||
|} | |||
== 8. Node Conditions (<code>kubectl get nodes</code> / <code>kubectl describe node</code>) == | |||
These reflect the health of the underlying worker machine (e.g., EC2 instance) hosting your pods. | |||
{| class="wikitable" | |||
! Node Condition !! What It Means !! Impact on Pods | |||
|- | |||
| '''Ready''' || Node is healthy and <code>kubelet</code> is reporting normally to the control plane. || Accepts new pod scheduling normally. | |||
|- | |||
| '''NotReady''' || Node is unhealthy or <code>kubelet</code> stopped sending heartbeats. || No new pods schedule here; after the eviction timeout (default 5 min), existing pods are rescheduled elsewhere. | |||
|- | |||
| '''MemoryPressure''' || Available RAM on the node dropped below the eviction threshold. || Kubelet blocks new BestEffort pods and starts evicting existing pods (leaving them in <code>Evicted</code> state). | |||
|- | |||
| '''DiskPressure''' || Root disk, container filesystem, or inodes are critically low (often from unrotated logs or cached images). || Kubelet triggers garbage collection and evicts pods to prevent the node from locking up. | |||
|- | |||
| '''PIDPressure''' || Too many active processes are running on the node, nearing the OS process ID limit. || New containers fail to start and existing workloads may be evicted. | |||
|- | |||
| '''NetworkUnavailable''' || The node's network plugin (CNI, such as AWS VPC CNI) is not configured or healthy. || Pods on the node cannot get IPs or route cluster/external traffic. | |||
|- | |||
| '''SchedulingDisabled''' || Node was manually cordoned (<code>kubectl cordon <node></code>). || Existing pods keep running, but the scheduler will not place any new pods on this node. | |||
|} | |} | ||
Latest revision as of 01:50, 2 December 2026
Kubernetes Troubleshooting Cheat Sheet
A rapid triage reference for debugging Kubernetes clusters, pods, networking, and AWS EKS IAM permissions. Follow the standard incident cadence: mitigate first to restore service, then deep-dive into root cause.
1. Context & First-Look Triage
Run these commands first to orient yourself and spot broken workloads across the cluster
Verify which cluster and namespace you are targeting
kubectl config current-context kubectl config set-context --current --namespace=<ns>
Fast scan: show every pod that is NOT Running
kubectl get pods -A --field-selector=status.phase!=Running
Detailed pod view (READY, STATUS, RESTARTS, IP, NODE)
kubectl get pods -n <ns> -o wide
Check node health (NotReady, MemoryPressure, DiskPressure)
kubectl get nodes -o wide kubectl top nodes kubectl top pods -n <ns> --sort-by=memory
2. Inspecting Failures (Events & Logs)
Follow the standard debugging flow: get → describe → logs → events.
Read the bottom "Events" table (scheduling, image pull, probes, OOMKilled)
kubectl describe pod <pod> -n <ns>
View logs from the PREVIOUS crashed container (essential for CrashLoopBackOff)
kubectl logs <pod> -n <ns> --previous
Stream live logs or grab the last 100 lines across all replicas of an app
kubectl logs -f <pod> -n <ns> --tail=100 kubectl logs -l app=<label> -n <ns> --tail=100
Multi-container pod (specify container name)
kubectl logs <pod> -c <container> -n <ns>
Cluster-wide warning events sorted chronologically
kubectl get events -n <ns> --field-selector type=Warning --sort-by=.lastTimestamp
3. Immediate Mitigation & Rollbacks
Unblock production SLAs and isolate faulty nodes before digging into root cause.
Check deployment rollout history and undo a bad release
kubectl rollout status deployment/<name> -n <ns> kubectl rollout undo deployment/<name> -n <ns>
Scale healthy replicas up (or scale a broken worker down to 0)
kubectl scale deployment/<name> --replicas=<count> -n <ns>
Isolate a bad node (stop new pods from scheduling on it)
kubectl cordon <node> kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
4. Live Shell, Networking & DNS Checks
Use these when a pod or worker is timing out or failing to reach internal services or external APIs.
Shell into a running container
kubectl exec -it <pod> -n <ns> -- /bin/sh
Attach an ephemeral debug container (works on distroless images with no shell)
kubectl debug -it <pod> -n <ns> --image=busybox --target=<container>
Spin up a temporary network debug pod (dig, nslookup, curl, tcpdump)
kubectl run netdebug --rm -it --image=nicolaka/netshoot -n <ns> -- /bin/bash
Verify Service-to-Pod selector matching (if <none>, labels don't match)
kubectl get svc,endpoints -n <ns>
Test CoreDNS and external egress from inside a pod
nslookup kubernetes.default nslookup s3.amazonaws.com
Forward a local port to test a pod/service directly
kubectl port-forward svc/<svc> 8080:80 -n <ns>
5. Scaling, Configs & AWS EKS IAM
Commands for inspecting autoscaling, configuration objects, RBAC, and the Kubernetes-to-AWS IAM boundary (IRSA / EKS Pod Identity).
Check Horizontal Pod Autoscaler (if <unknown>, pod is missing resources.requests)
kubectl get hpa -n <ns> kubectl describe hpa <hpa-name> -n <ns>
Verify ConfigMaps, Secrets, and Storage
kubectl get cm,secret,pvc -n <ns>
Check which ServiceAccount a pod is using (for IRSA / EKS Pod Identity)
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.serviceAccountName}'
Verify the IRSA IAM role annotation on the ServiceAccount
kubectl describe sa <sa-name> -n <ns>
Check Kubernetes RBAC permissions for a ServiceAccount
kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa-name> -n <ns>
6. Quick Status-to-Command Lookup
| Status You See | First Command to Run | What to Look For |
|---|---|---|
| Pending | kubectl describe pod <pod> |
FailedScheduling, insufficient CPU/RAM, taint mismatch, or unbound PVC.
|
| CrashLoopBackOff | kubectl logs <pod> --previous |
App startup error, missing env var/Secret, or aggressive livenessProbe.
|
| OOMKilled (Exit 137) | kubectl describe pod <pod> |
Last State: Terminated (Reason: OOMKilled). Raise memory limits or reduce worker batch size.
|
| ImagePullBackOff | kubectl describe pod <pod> |
Bad image tag, missing imagePullSecrets, or EKS node missing ECR read permissions / NAT egress.
|
| AccessDenied (AWS) | kubectl describe sa <sa> |
Missing eks.amazonaws.com/role-arn annotation, OIDC trust policy mismatch, or KMS key policy block.
|
7. Pod Statuses (kubectl get pods)
These reflect the lifecycle phase of the pod or the state of its containers.
| Pod Status | What It Means | Common Cause / First Check |
|---|---|---|
| Pending | Accepted by the cluster, but not yet scheduled onto a node (or still waiting on volumes/images). | Insufficient CPU/RAM, node taint/affinity mismatch, or unbound PVC. Check kubectl describe pod for FailedScheduling.
|
| ContainerCreating | Scheduled to a node; kubelet is pulling images, mounting volumes, or attaching networking. | Normal briefly. If stuck, check for volume mount failures or VPC/CNI IP exhaustion. |
| Running | Bound to a node and at least one container is running. | Check the READY column (e.g., 0/1 means the container is up, but failing its readinessProbe and receiving no traffic).
|
| CrashLoopBackOff | Container starts, crashes, and Kubernetes waits with exponential backoff before restarting it again. | App startup crash, missing env var/Secret, or aggressive livenessProbe. Run kubectl logs <pod> --previous.
|
| OOMKilled (Exit 137) | Container exceeded its memory limits and was killed by the Linux kernel. |
Memory leak or heavy workload (e.g., large file scan). Raise memory limits or reduce worker batch size.
|
| ImagePullBackOff | Kubelet cannot pull the container image from the registry. | Wrong image tag, missing imagePullSecrets, or EKS node missing ECR read permissions / NAT egress.
|
| CreateContainerConfigError | Kubelet cannot assemble the container config before starting it. | Referenced ConfigMap or Secret does not exist in that namespace.
|
| Init:0/1 / Init:Error | An init container has not completed or is failing, blocking the main container from starting. | Inspect the init container specifically with kubectl logs <pod> -c <init-container>.
|
| Evicted | Kubelet forcibly terminated the pod to free up node resources. | Usually a symptom of Node MemoryPressure or DiskPressure.
|
| Terminating | Pod is shutting down gracefully. | If stuck, check for hung shutdown hooks, stuck finalizers, or an unreachable node. |
| Completed | All containers exited cleanly with exit code 0. |
Normal end state for Job and CronJob pods.
|
| Unknown | Control plane lost contact with the node hosting the pod. | Node crashed, lost network connectivity, or kubelet stopped running.
|
8. Node Conditions (kubectl get nodes / kubectl describe node)
These reflect the health of the underlying worker machine (e.g., EC2 instance) hosting your pods.
| Node Condition | What It Means | Impact on Pods |
|---|---|---|
| Ready | Node is healthy and kubelet is reporting normally to the control plane. |
Accepts new pod scheduling normally. |
| NotReady | Node is unhealthy or kubelet stopped sending heartbeats. |
No new pods schedule here; after the eviction timeout (default 5 min), existing pods are rescheduled elsewhere. |
| MemoryPressure | Available RAM on the node dropped below the eviction threshold. | Kubelet blocks new BestEffort pods and starts evicting existing pods (leaving them in Evicted state).
|
| DiskPressure | Root disk, container filesystem, or inodes are critically low (often from unrotated logs or cached images). | Kubelet triggers garbage collection and evicts pods to prevent the node from locking up. |
| PIDPressure | Too many active processes are running on the node, nearing the OS process ID limit. | New containers fail to start and existing workloads may be evicted. |
| NetworkUnavailable | The node's network plugin (CNI, such as AWS VPC CNI) is not configured or healthy. | Pods on the node cannot get IPs or route cluster/external traffic. |
| SchedulingDisabled | Node was manually cordoned (kubectl cordon <node>). |
Existing pods keep running, but the scheduler will not place any new pods on this node. |