0
0
KubernetesHow-ToBeginner · 3 min read

How to Exec into Pod in Kubernetes: Simple Steps

Use the kubectl exec command to run a command inside a Kubernetes pod. The basic syntax is kubectl exec -it <pod-name> -- <command>, where -it allows interactive terminal access.
📐

Syntax

The command kubectl exec lets you run commands inside a pod. The -i flag keeps input open, and -t allocates a terminal for interaction. You specify the pod name and the command to run after --.

  • kubectl exec: The base command to execute inside a pod.
  • -it: Combines -i (interactive) and -t (terminal) for interactive sessions.
  • <pod-name>: The name of the pod you want to access.
  • --: Separates kubectl options from the command you want to run inside the pod.
  • <command>: The command to run inside the pod, e.g., bash or sh.
bash
kubectl exec -it <pod-name> -- <command>
💻

Example

This example shows how to open an interactive bash shell inside a pod named my-pod. This lets you run commands inside the pod as if you were logged into its terminal.

bash
kubectl exec -it my-pod -- bash
Output
root@my-pod:/#
⚠️

Common Pitfalls

Common mistakes include:

  • Not using -it flags, which prevents interactive terminal access.
  • Forgetting the -- separator, causing kubectl to misinterpret the command.
  • Using the wrong pod name or a pod that is not running.
  • Trying to exec into pods without a shell installed (some minimal containers lack bash or sh).

Always check pod status with kubectl get pods before exec.

bash
Wrong: kubectl exec my-pod bash
Right: kubectl exec -it my-pod -- bash
📊

Quick Reference

CommandDescription
kubectl exec -it -- bashOpen interactive bash shell inside pod
kubectl exec -it -- shOpen interactive sh shell if bash not available
kubectl exec -- ls /Run ls command inside pod without interactive shell
kubectl get podsList pods to find pod names and status

Key Takeaways

Use kubectl exec with -it flags for interactive terminal access inside pods.
Always include -- before the command to separate kubectl options from the command.
Verify the pod name and status before trying to exec into it.
Some pods may not have bash; try sh as an alternative shell.
Use kubectl get pods to list pods and check their readiness.