kubernetes/exec-pod

Exec Into Pod

Run a command or open a shell inside a pod.

pods
debugging

Command

kubectl exec -it <pod-name> -- /bin/sh

Explanation

The 'kubectl exec' command runs commands inside a running container, similar to SSH but within the Kubernetes environment. Using '-it' provides an interactive terminal. It’s commonly used for debugging, inspecting files, or running diagnostics. You can specify a particular container using '-c <container-name>' when multiple containers exist.

Common Use Cases

  • Run debugging commands directly inside a container
  • Inspect logs, environment variables, or configuration files
  • Manually test application connectivity from within the pod

Best Practices

  • Always use '-it' for an interactive shell session
  • Avoid making manual configuration changes inside pods — use ConfigMaps or deployments instead

Common Mistakes to Avoid

  • Using exec on pods that are not running
  • Forgetting to use '--' before the shell command

Troubleshooting

Problem: Error: 'container not found'

Solution: Specify the container explicitly using '-c <container-name>' if the pod has multiple containers.

Examples

Open an interactive shell inside the pod

kubectl exec -it my-app-pod-1234 -- /bin/sh