In this lab, you'll learn how to troubleshoot common issues in Kubernetes applications in a streamlined manner by:
- Identifying the problem
- Gathering relevant information
- Analyzing logs and events
- Diagnosing the root cause
- Applying fixes and verifying the solution
There's also a Talk at Cloud Native Zurich about this topic. Feel free to check it out in your own time.
Apply the following manifest to deploy an application that will crash:
apiVersion: v1
kind: Pod
metadata:
name: crashpod
spec:
containers:
- name: crashcontainer
image: busybox
command: ["sh", "-c", "exit 1"]Save this as crashpod.yaml and apply it:
kubectl apply -f crashpod.yamlCheck the pod status:
kubectl get podsRetrieve the logs to understand why the container is crashing:
kubectl logs crashpodSince the container exits immediately, you might need to check previous logs:
kubectl logs crashpod --previousThe container is exiting with status code 1. Modify the command to keep the container running:
command: ["sh", "-c", "while true; do echo Hello; sleep 10; done"]Update the pod:
kubectl delete pod crashpod
kubectl apply -f crashpod.yamlVerify the pod is running:
kubectl get podsApply a manifest with a non-existent image:
apiVersion: v1
kind: Pod
metadata:
name: badimagepod
spec:
containers:
- name: badimage
image: nonexistentrepo/nonexistimage:latestSave this as badimagepod.yaml and apply it:
kubectl apply -f badimagepod.yamlCheck the pod status:
kubectl get podsThe pod should be in an ImagePullBackOff state.
Get detailed information about the pod:
kubectl describe pod badimagepodLook for events indicating image pull errors.
Update the image to a valid one, such as nginx:latest. Modify the manifest accordingly and reapply:
kubectl delete pod badimagepod
kubectl apply -f badimagepod.yamlVerify the pod is running:
kubectl get podsApply the following manifest:
apiVersion: v1
kind: Pod
metadata:
name: highresourcepod
spec:
containers:
- name: highresource
image: nginx
resources:
requests:
memory: "10Gi"
cpu: "4"Save this as highresourcepod.yaml and apply it:
kubectl apply -f highresourcepod.yamlCheck the pod status:
kubectl get podsThe pod should be in a Pending state.
Get detailed information:
kubectl describe pod highresourcepodLook for messages indicating insufficient resources.
Reduce the resource requests to fit within the cluster's capacity:
resources:
requests:
memory: "256Mi"
cpu: "0.5"Update the manifest and reapply:
kubectl delete pod highresourcepod
kubectl apply