Computer Notes/Kubernetes/Kubernetes Troubleshooting Cheat Sheet: Difference between revisions

From Cramsession
Jump to navigationJump to search
✍️ Verified Author: Mflavell • Click to view professional profile & credentials
No edit summary
No edit summary
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


<syntaxhighlight lang="bash">
kubectl config current-context
# Verify which cluster and namespace you are targeting
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
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 -A --field-selector=status.phase!=Running
kubectl get pods -n <ns> -o wide


# Check node health (NotReady, MemoryPressure, DiskPressure)
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
 
</syntaxhighlight>
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>.


<syntaxhighlight lang="bash">
Read the bottom "Events" table (scheduling, image pull, probes, OOMKilled)
# 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.


# View logs from the PREVIOUS crashed container (essential for CrashLoopBackOff)
Check deployment rollout history and undo a bad release
kubectl logs <pod> -n <ns> --previous


# Stream live logs or grab the last 100 lines across all replicas of an app
kubectl rollout status deployment/<name> -n <ns>
kubectl logs -f <pod> -n <ns> --tail=100
kubectl rollout undo deployment/<name> -n <ns>
kubectl logs -l app=<label> -n <ns> --tail=100


# Multi-container pod (specify container name)
Scale healthy replicas up (or scale a broken worker down to 0)
kubectl logs <pod> -c <container> -n <ns>


# Cluster-wide warning events sorted chronologically
kubectl scale deployment/<name> --replicas=<count> -n <ns>
kubectl get events -n <ns> --field-selector type=Warning --sort-by=.lastTimestamp
</syntaxhighlight>


Isolate a bad node (stop new pods from scheduling on it)


kubectl cordon <node>
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data


== 3. Immediate Mitigation & Rollbacks ==
Unblock production SLAs and isolate faulty nodes before digging into root cause.


<syntaxhighlight lang="bash">
# 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)
== 4. Live Shell, Networking & DNS Checks ==
kubectl scale deployment/<name> --replicas=<count> -n <ns>
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


# Isolate a bad node (stop new pods from scheduling on it)
Attach an ephemeral debug container (works on distroless images with no shell)
kubectl cordon <node>
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
</syntaxhighlight>


kubectl debug -it <pod> -n <ns> --image=busybox --target=<container>


Spin up a temporary network debug pod (dig, nslookup, curl, tcpdump)


== 4. Live Shell, Networking & DNS Checks ==
kubectl run netdebug --rm -it --image=nicolaka/netshoot -n <ns> -- /bin/bash
Use these when a pod or worker is timing out or failing to reach internal services or external APIs.


<syntaxhighlight lang="bash">
Verify Service-to-Pod selector matching (if <none>, labels don't match)
# 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 get svc,endpoints -n <ns>
kubectl debug -it <pod> -n <ns> --image=busybox --target=<container>


# Spin up a temporary network debug pod (dig, nslookup, curl, tcpdump)
Test CoreDNS and external egress from inside a pod
kubectl run netdebug --rm -it --image=nicolaka/netshoot -n <ns> -- /bin/bash


# Verify Service-to-Pod selector matching (if <none>, labels don't match)
nslookup kubernetes.default
kubectl get svc,endpoints -n <ns>
nslookup s3.amazonaws.com


# Test CoreDNS and external egress from inside a pod
Forward a local port to test a pod/service directly
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>
</syntaxhighlight>




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).


<syntaxhighlight lang="bash">
Check Horizontal Pod Autoscaler (if <unknown>, pod is missing resources.requests)
# Check Horizontal Pod Autoscaler (if <unknown>, pod is missing resources.requests)
 
kubectl get hpa -n <ns>
kubectl get hpa -n <ns>
kubectl describe hpa <hpa-name> -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)


# Verify ConfigMaps, Secrets, and Storage
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.serviceAccountName}'
kubectl get cm,secret,pvc -n <ns>


# Check which ServiceAccount a pod is using (for IRSA / EKS Pod Identity)
Verify the IRSA IAM role annotation on the ServiceAccount
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>
kubectl describe sa <sa-name> -n <ns>


# Check Kubernetes RBAC permissions for a ServiceAccount
Check Kubernetes RBAC permissions for a ServiceAccount
kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa-name> -n <ns>
</syntaxhighlight>


kubectl auth can-i --list --as=system:serviceaccount:<ns>:<sa-name> -n <ns>


<br />


== 6. Quick Status-to-Command Lookup ==
== 6. Quick Status-to-Command Lookup ==

Revision as of 01:16, 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.