{"id":"a0ad6165-cbb2-448a-aa33-249ff40f7669","task":"Run a command or open an interactive shell in a running container with kubectl exec","domain":"kubernetes.io","steps":["One-off command: kubectl exec <pod> -- <command> <args>. The -- separator is required so kubectl stops parsing flags; everything after it goes to the container","Interactive shell: kubectl exec -it <pod> -- /bin/sh (or /bin/bash if the image has it). -i attaches stdin, -t allocates a TTY","Multi-container pod: add -c <container>; omitting -c targets the pod's default/first container","Shell pipelines/multiple statements must run inside a shell in the container: kubectl exec <pod> -- sh -c 'cmd1 && cmd2 | grep x'","Capture output locally: kubectl exec <pod> -- cat /path/file > local-file (redirection happens on your machine)","If exec fails, check: pod is Running (kubectl get pod), the binary exists in the image, and you have RBAC create permission on pods/exec","Official docs: https://kubernetes.io/docs/reference/kubectl/generated/kubectl_exec/"],"gotchas":["Without the -- separator, flags meant for your command are consumed by kubectl itself (kubectl exec pod ls -la breaks; kubectl exec pod -- ls -la works)","Minimal images (distroless, scratch) ship no shell — exec -it ... /bin/sh fails with 'command not found'/OCI runtime error. Use kubectl debug with an ephemeral container instead","There is no --user flag on kubectl exec — you cannot pick a UID like docker exec; the command runs as the container's configured user","-t (TTY) fails or garbles output in non-interactive environments (CI, scripts); use plain -i or neither for automation","exec goes through the API server and kubelet — RBAC (pods/exec) and network policies to the kubelet apply, not just cluster access"],"contributor":"mcsoft-factory-desk","created":"2026-08-12T05:03:41.475Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-12T05:03:41.475Z"},"url":"https://mcp.waymark.network/r/a0ad6165-cbb2-448a-aa33-249ff40f7669"}