Kubectl Exec Command

The kubectl exec command makes it easy to manage arbitrary tasks from afar inside a pod’s existing container. When you need to inspect the contents, condition, and/or surroundings of a container, this makes things easier. For all of us who interact with Kubernetes containerized applications regularly, the kubectl exec command is a lifesaver. By executing commands within our containers, we may analyze and debug our apps. kubectl is a command-line tool for executing Kubernetes cluster commands. This article covers the kubectl syntax, the command actions, and frequent examples. For your process to operate successfully, you may need to set up the environment in your container. In this instance, the ideal method is to have the container start with a shell script. The main objective of this shell script is to set up the environment and start the main process.

If you use this method, the shell script will have PID 1, not your process. That is why you have to use the shell script’s built-in exec command to start the process. The exec command substitutes the desired application for the script. Then, PID 1 is sent down to your process. The guide will explain the usage of the kubectl exec command in detail.


You should have the minikube cluster configured on your system to execute the exec command with kubectl. To access your system, you will also need sudo privileges. While implementing this article, we have used Ubuntu 20.04 LTS. For implementation, you can utilize your preferred operating system. We have already installed kubectl on our system. You need to install it before implementing this guide.

Method to Use the Kubectl Exec Command

To utilize the exec command with kubectl, you must first install a minikube cluster. We will need to run the command line shell in Ubuntu 20.04 LTS. Two simple techniques are available for launching the terminal window. One such way is to use your operating system’s application bar to access it. Another approach is to use the simplest shortcut key combination of “Ctrl+Alt+T.” The command-line terminal will open as soon as you use either of these ways.

First, you must start the minikube cluster that has already been installed on your Ubuntu 20.04 LTS operating system. In the command line, type the following affixed command to run minikube:

$ minikube start

To get started with minikube, you must wait for a few moments after running this command. You can also look at the minikube version displayed after the command has been successfully executed. Using the kubectl get pods command, display the pods and choose one to execute with the exec command:

$ kubectl get pods

We are using a “shell-demo” pod for this purpose. You can use the -i and -t parameters for the kubectl exec to launch a shell linked to your terminal. For instance, use the following affixed command:

$ kubectl exec -it shell-demo -- sh

You are now ready to utilize the shell. The following command can help you to list the root directory:

# ls

The output will be similar to the one that is displayed in the image. You can update the shell by using the following command:

# apt-get update

Depending on your internet speed, the update will be completed:

$ kubectl exec shell-demo -- curl -s

You will also need to know your service’s cluster IP. We have added our pod name and service IP. However, you can use your desired pod name and service IP.


Kubernetes is a complicated system, but sometimes the quickest and most obvious approach to finding an issue is to run commands directly on a running pod. Fortunately, the Kubernetes command-line tool, kubectl, has a built-in exec function, particularly for this purpose. We explained the usage of the exec command in detail. After reading this guide, I guarantee you will have no doubts about the kubectl exec command.

About the author

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content