kubectl exec: Run Commands Inside Kubernetes Pods
Use kubectl exec to run commands and open shells inside Kubernetes pods. Covers interactive sessions, multi-container pods, and debugging with ephemeral containers.
π‘ Quick Answer: Use kubectl exec to run commands and open shells inside Kubernetes pods. Covers interactive sessions, multi-container pods, and debugging with ephemeral containers.
The Problem
This is one of the most searched Kubernetes topics. Having a comprehensive, well-structured guide helps both beginners and experienced users quickly find what they need.
The Solution
Basic exec
# Run a single command
kubectl exec my-pod -- ls /app
kubectl exec my-pod -- cat /etc/config/app.yaml
kubectl exec my-pod -- env | grep DATABASE
# Interactive shell
kubectl exec -it my-pod -- /bin/bash
kubectl exec -it my-pod -- /bin/sh # If bash isn't available
# Specific container (multi-container pod)
kubectl exec -it my-pod -c sidecar -- sh
# Specific namespace
kubectl exec -it my-pod -n production -- bashCommon Debugging Commands Inside Pods
# Network
curl -s http://other-service:8080/health
wget -qO- http://other-service:8080/health
nslookup kubernetes.default
cat /etc/resolv.conf
# Files and config
cat /etc/config/app.yaml
ls -la /data/
df -h # Disk usage
# Process info
ps aux
top
# Environment
env | sort
printenv DATABASE_URLWhen exec Doesnβt Work
# Pod has no shell (distroless/scratch images)
# Use ephemeral debug containers instead:
kubectl debug my-pod -it --image=nicolaka/netshoot --target=my-container
# Pod is CrashLoopBackOff (keeps restarting)
# Override the entrypoint:
kubectl debug my-pod -it --copy-to=debug-pod --container=my-container -- /bin/sh
# Node-level debugging
kubectl debug node/worker-1 -it --image=ubuntu
# chroot /hostgraph LR
A[kubectl exec -it pod -- bash] --> B[K8s API Server]
B -->|WebSocket| C[kubelet on node]
C -->|nsenter into container| D[Shell session in pod]Frequently Asked Questions
Can I exec into an init container?
No β init containers have already completed by the time the pod is Running. You can check their logs with kubectl logs <pod> -c <init-container>.
Is kubectl exec secure?
It uses your RBAC permissions and goes through the API server. Restrict pods/exec permission in RBAC to prevent unauthorized access.
Best Practices
- Start simple β use the basic form first, add complexity as needed
- Be consistent β follow naming conventions across your cluster
- Document your choices β add annotations explaining why, not just what
- Monitor and iterate β review configurations regularly
Key Takeaways
- This is fundamental Kubernetes knowledge every engineer needs
- Start with the simplest approach that solves your problem
- Use
kubectl explainandkubectl describewhen unsure - Practice in a test cluster before applying to production

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses β