Execute commands inside a running Kubernetes container.
medium risk
kubectl exec runs commands inside a running container, similar to docker exec. The -- separator is required before the command. Use for live debugging — testing DNS resolution, checking env vars, verifying file contents — without redeploying.
| Flag | Meaning |
|---|---|
-i --stdin | Pass stdin to container |
-t --tty | Allocate a TTY |
-c --container | Target specific container |
-n --namespace | Namespace |
kubectl exec -it mypod -- bashInteractive bash shell in pod · explain & risk-check →kubectl exec mypod -- env | grep DBCheck environment variables · explain & risk-check →kubectl exec mypod -c sidecar -- cat /etc/config.yamlRead file from specific container · explain & risk-check →kubectl exec mypod -- curl -s http://other-service/healthTest service connectivity from inside cluster · explain & risk-check →Use `kubectl exec pod -- curl http://service.namespace.svc.cluster.local` to test service DNS and connectivity from within the cluster network — the most reliable way to verify network policies.
Reference text is hand-written and may be incomplete — check man kubectl for your version. Test destructive commands on a non-production system first.