kubectl-attach
Attach to running container's I/O
TLDR
Get output from a pod
Get output from a container in the specified pod
Get output from the first pod of a replica set
Create an interactive session with a pod
Create an interactive session with a specific container from a pod
SYNOPSIS
kubectl attach POD [-c CONTAINER] [-i] [-t] [-o json|yaml] [--attachable]
PARAMETERS
-c, --container=string
Target container name; defaults to first in pod
-i, --stdin[=false]
Enable stdin stream (implies -t)
-t, --tty[=false]
Allocate TTY for interactive shell (default true with stdin)
-o, --output=string
Output format: json or yaml
--attachable[=false]
Attach only if pod is attachable (default true)
DESCRIPTION
kubectl attach connects your local terminal to a running container in a Kubernetes pod, streaming stdin, stdout, and stderr bidirectionally. Ideal for live debugging, troubleshooting, interactive shells, or monitoring processes without restarting pods.
It requires the pod to be Running and the container attachable (via pod annotation or deployment spec). Specify a container with -c if multiple exist; otherwise, the first is chosen.
Enable stdin with -i for input (implies -t for TTY by default). Output appears in real-time; use Ctrl+C or Ctrl+P Ctrl+Q to detach cleanly, sending SIGTERM/INT to foreground processes.
Unlike kubectl exec, attach maintains streams persistently until disconnect. Supports JSON/YAML output for scripting. Requires read/write pod permissions (RBAC). Common in DevOps for container introspection without port-forwarding overhead.
CAVEATS
Pod/container must be running; no support for init containers. Detach sends SIGINT/SIGTERM. Needs pod/exec permissions. Fails on non-attachable pods without flag.
EXAMPLES
kubectl attach mypod
Attach to first container (read-only).
kubectl attach mypod -it
Interactive shell with stdin/TTY.
kubectl attach mypod -c nginx --attachable
Force attach to specific container.
DETACH SEQUENCES
Ctrl+C: Send SIGINT.
Ctrl+P Ctrl+Q: Clean detach without signal.
HISTORY
Introduced in Kubernetes v1.0 (2014) for core container interaction; evolved with streaming APIs in v1.2+. Default attachable behavior changed in v1.23+.


