kubectl exec

Execute commands inside a running Kubernetes container.

medium risk

interactive breakdown — edit the command and press Explain
$ 
⚠ Risks & cautions
What it does

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.

Common flags
FlagMeaning
-i --stdinPass stdin to container
-t --ttyAllocate a TTY
-c --containerTarget specific container
-n --namespaceNamespace
Examples
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 →
Pro tip

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.

Related
kubectl logs kubectl describe kubectl get
Open the full explainer →More in Kubernetes & Helm

Reference text is hand-written and may be incomplete — check man kubectl for your version. Test destructive commands on a non-production system first.