The kubectl exec
command in Kubernetes allows you to execute commands inside a running container of a pod. This is particularly useful for troubleshooting, debugging, or interacting with applications directly within the Kubernetes cluster.
Here are several examples demonstrating the usage of kubectl exec
:
Example 1: Execute a shell (/bin/sh) in a pod named my-pod
:
kubectl exec -it my-pod -- /bin/sh
This command opens an interactive shell session within the my-pod
pod, allowing you to run commands as if you were logged into the container.
Example 2: Run a command directly in a pod without opening an interactive shell:
kubectl exec my-pod -- ls /var/log
Here, kubectl exec
runs the ls /var/log
command inside my-pod
, listing files and directories in the /var/log
directory of the container.
Example 3: Execute a command using a specific container within a multi-container pod:
kubectl exec -it my-pod -c my-container -- /bin/bash
This example runs an interactive bash shell session inside the container named my-container
within the pod my-pod
.
Example 4: Send input to a specific container’s stdin:
echo "Hello, Kubernetes!" | kubectl exec -i my-pod -c my-container -- /usr/bin/env tee /tmp/greeting.txt
Here, the echo
command output is redirected to the stdin of the container my-container
and written to /tmp/greeting.txt
.
Example 5: Execute a command in a pod using a specific namespace:
kubectl exec -n my-namespace my-pod -- ps aux
This command runs ps aux
inside my-pod
in the namespace my-namespace
, showing processes running in the container.
Example 6: Run a command with custom environment variables:
kubectl exec my-pod -- env VAR=value bash
Here, VAR=value
sets an environment variable VAR
with value value
before running bash
in the container.
Example 7: Execute a command with different working directory:
kubectl exec -it my-pod -- /bin/sh -c "cd /path/to/directory && ls"
This command changes the directory to /path/to/directory
and lists its contents.
Example 8: Get output of a command executed inside a pod:
kubectl exec my-pod -- cat /var/log/app.log
Here, kubectl exec
retrieves the contents of /var/log/app.log
from my-pod
and outputs it to the console.
Example 9: Execute a command as a specific user in a pod:
kubectl exec -it my-pod -- /bin/bash -u 1000
This command runs an interactive bash shell as user with UID 1000 inside my-pod
.
Example 10: Execute a command with resource limitations:
kubectl exec -it my-pod --limits=cpu=100m,memory=256Mi -- /bin/bash
Here, the command sets CPU and memory limits before starting an interactive bash shell session in my-pod
.
To verify if a kubectl exec
command executed successfully, you can check for the expected output or changes within the pod or container. For commands that interactively open a shell, ensure the shell prompt appears, indicating the session has started. For non-interactive commands, verify the output matches the expected result.
Also check similar articles.
Attach to Running Containers with kubectl attach
Retrieve Container Logs Using kubectl logs
Detailed Resource Inspection with kubectl describe
Update Node Taints with kubectl taint
Prepare Nodes for Maintenance with kubectl drain
Discussion about this post