DEV Community

Cover image for βœ… Scenario #3: Debugging a Running Container in Kubernetes
Latchu@DevOps
Latchu@DevOps

Posted on

βœ… Scenario #3: Debugging a Running Container in Kubernetes

🟩 Step 1 β€” Create an NGINX Pod

Create a file named nginx-debug.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-debug
  labels:
    app: nginx-debug
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f nginx-debug.yaml
Enter fullscreen mode Exit fullscreen mode

🟩 Step 2 β€” Verify Pod Is Running

Check Pod status:

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

Expected output:

NAME          READY   STATUS    RESTARTS   AGE   IP
nginx-debug   1/1     Running   0          5s    10.x.x.x
Enter fullscreen mode Exit fullscreen mode

🟩 Step 3 β€” Wait Until Pod Is Ready (Recommended)

This avoids connection errors:

kubectl wait --for=condition=Ready pod/nginx-debug --timeout=60s
Enter fullscreen mode Exit fullscreen mode

🟩 Step 4 β€” Exec Into the NGINX Container

Try bash first; if not available, fall back to sh:

kubectl exec -it nginx-debug -- /bin/bash 2>/dev/null || \
kubectl exec -it nginx-debug -- /bin/sh
Enter fullscreen mode Exit fullscreen mode

Now you should be inside the container:

root@nginx-debug:/#
Enter fullscreen mode Exit fullscreen mode

1


🟩 Step 5 β€” Perform Debugging Inside the Container

Below is a list of useful real-time debugging actions.

πŸ”Ž 5.1 Check running processes

ps aux
Enter fullscreen mode Exit fullscreen mode

You will see nginx master + worker processes.


πŸ”Ž 5.2 Check NGINX config

cat /etc/nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode

Or check site configs:

ls -R /etc/nginx
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 5.3 Test local NGINX web server

apt update 2>/dev/null || true
apt install curl -y 2>/dev/null || true
curl http://localhost
Enter fullscreen mode Exit fullscreen mode

You should see the default NGINX welcome page HTML.


πŸ”Ž 5.4 Check environment variables

env
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 5.5 Inspect container filesystem

ls -l /
ls -l /usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 5.6 Check network connectivity from inside the container

ping -c 3 google.com
Enter fullscreen mode Exit fullscreen mode

Check internet DNS from the container:

nslookup google.com
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 5.7 View logs (inside container)

Check access and error logs:

ls -l /var/log/nginx
cat /var/log/nginx/access.log
cat /var/log/nginx/error.log
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž 5.8 Inspect listening ports

netstat -tulnp
Enter fullscreen mode Exit fullscreen mode

You should see:

tcp 0.0.0.0:80 β†’ nginx
Enter fullscreen mode Exit fullscreen mode

🟩 Step 6 β€” Exit the Container

exit
Enter fullscreen mode Exit fullscreen mode

🟩 Step 7 β€” Clean Up (Optional)

kubectl delete pod nginx-debug
Enter fullscreen mode Exit fullscreen mode

🌟 Thanks for reading! If this post added value, a like ❀️, follow, or share would encourage me to keep creating more content.


β€” Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | πŸ” Security | ⚑ Automation
πŸ“Œ Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)