Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Use the kubectl exec Command

Run commands and open a shell inside a running Kubernetes pod, with practical examples.

VPS and Cloud Servers4 min read24 screenshots

kubectl exec runs a command inside a container of a running pod. It is like docker exec for Kubernetes, and it is one of the main tools for troubleshooting, checking configuration and maintenance.

You need kubectl installed and access to a cluster.

Syntax

kubectl exec [OPTIONS] POD_NAME -- COMMAND [args...]
  • kubectl is the client for the cluster.
  • exec runs a command in a container of a pod.
  • OPTIONS are flags such as -it, -c <container> or --namespace.
  • POD_NAME is the pod.
  • -- separates the kubectl options from the command that runs in the container. It is required.
  • COMMAND is what to run, such as sh, bash or ps.
  • args are the arguments of that command.

1. Run a simple command

List the files in the working directory of the container:

kubectl exec my-pod -- ls -la
Listing the files in the pod

Print the current directory:

kubectl exec my-pod -- pwd
Showing the current directory
Note: The double dash is required. Without it kubectl may treat the options of your command as its own.

2. Open an interactive shell

kubectl exec -it my-pod -- /bin/bash
An interactive shell in the pod

-i keeps standard input open and -t allocates a terminal. You can then run many commands.

Minimal images such as Alpine do not have bash. Use sh:

kubectl exec -it my-pod -- /bin/sh
A shell in an Alpine-based container

To see which shells exist:

kubectl exec my-pod -- ls -la /bin/*sh
Checking the available shells

3. Choose the container in a multi-container pod

kubectl exec -it my-pod -c container-name -- /bin/bash
Selecting a container in a multi-container pod

If you skip -c, kubectl uses the first container (or the default one set by an annotation). List the containers:

kubectl describe pod my-pod
Listing the containers of the pod

4. Use another namespace

kubectl exec -it my-pod -n my-namespace -- /bin/bash
Running exec in a specific namespace

5. Show environment variables

kubectl exec my-pod -- env
Showing the environment variables

This helps when you debug configuration problems.

6. Work with files

Read a file:

kubectl exec my-pod -- cat /etc/hostname
Reading the hostname file

Create a file:

kubectl exec my-pod -- touch /tmp/testfile
Creating a file

Check disk usage:

kubectl exec my-pod -- df -h
Checking the disk usage

Find files:

kubectl exec my-pod -- find /var/log -name "*.log"
Finding log files

7. Network checks

Run a temporary pod and ping an address:

kubectl run pingbox --image=busybox -it --restart=Never -- ping 8.8.8.8
Pinging from a temporary pod

8. Read logs and config files

kubectl exec my-pod -- tail -f /var/log/apt/term.log
Following a log file
kubectl exec my-pod -- cat /etc/nginx/nginx.conf
Reading the Nginx configuration
kubectl exec my-pod -- find /var -name "*.log"
Searching for log files

9. Install tools for debugging

kubectl exec my-pod -- apt-get update
Updating the package lists
kubectl exec my-pod -- apt-get install -y curl wget
Installing debugging tools
Note: Packages you install this way vanish when the pod restarts. Do not rely on them for the real setup.

10. Reload and test a configuration

kubectl exec nginx-pod -- nginx -s reload
Reloading the Nginx configuration
kubectl exec nginx-pod -- nginx -t
Validating the Nginx configuration

11. Files and directories

kubectl exec my-pod -- mkdir -p /tmp/test
Creating a directory
kubectl exec my-pod -- chmod 755 /etc/nginx/nginx.conf
Changing file permissions
kubectl exec my-pod -- find /app -name "*.log"
Searching for files

FAQ

What is the difference between kubectl exec and docker exec? kubectl exec works on pods in a cluster. docker exec works on Docker containers on one host.

Can I use another namespace? Yes, with -n or --namespace.

Which container does it use in a multi-container pod? The default one. Use -c to choose.

What does "exit code 126" mean? The command or shell does not exist in the container. Try /bin/sh instead of /bin/bash.

Can I exec into a pod that is not running? No. Check with kubectl get pods.

Can I set a timeout? Use the --request-timeout option or wrap the command in timeout 30 ... inside the container.

Quick recap

  • kubectl exec POD -- COMMAND runs one command.
  • kubectl exec -it POD -- sh opens a shell.
  • Use -c for the container and -n for the namespace.