How to Run Shell Commands in Kubernetes Pods or Containers
Last Updated :
14 Feb, 2024
In Kubernetes, we create pods by adding an extra layer of information on containers. This Kubernetes in short is known as K8s, an open-source container orchestration tool developed by Google. It is used to orchestrate the containers for bringing Agility in software deployment through scaling, and management. Currently, it is being maintained by the cloud native computing foundation (CNCF).
Kubernetes Cluster Setup
Firstly ensure setting up the Kubernetes cluster, It can be done either Through an Orginal Setup with multiple nodes or with a simple single-node Kubernetes cluster setup using software such as Minikube. The major difference between Kubernetes and Minikube is that on Minikube you only have a single node instead of a node cluster. Due to this, we do not have to go through the trouble of setting up the entire Kubernetes configuration. To know more about Setting Up Minikube in Your Local System Read this – Article
Understanding Of Kubernetes Pods And Containers
Kubernetes pods are the smallest deployable units in the Kubernetes cluster. A pod comes representing one or more containers with tight bonding and sharing the same network namespace and storage. You can think as pods the extra layers on top of the containers with metadata information provide effective orchestration with tools like Kubernetes. These pods encapsulate the application’s code, runtime, libraries, and dependencies bringing consistency across diverse environments.
Now let us see the working of commands inside a K8s pod. A pod in K8s is a single instance of an application and it is the smallest entity inside K8s.
Execute Commands Inside The Kubernetes Pod
Here, we are using Minikube setup Kubernetes cluster. Firstly Download the software as per your supporting System Configurations and then follow the below steps to execute the commands inside the Kubernetes Pod.
Step 1: Start A Minikube.
Start the minikube software with the following command:
$ minikube start
- On running the above command `minikube start`, Minikube start the program and set up the single node kubernetes cluster, the below screenshot shows its execution flow.

Step 2: Verify whether the minikube is running or not with the following command `minikube status`
$ minikube status

- Now we have to deploy a pod, we are going to use a pod that will be running an image of NGINX inside it.
Step 3: Run the kubernetes pod on using nginx image with the kubectl run command as shown below:
$ kubectl run <pod_name> --image=<your_image_name>
- On Running the above command, kubectl will create a pod with nginx image extracting from the Docker hub. You can see its creation as a result after running the command on the below screenshot.

- The above command will create a pod inside your minikube.
Step 4: For viewing, list the all pods in the default namespace by the following command:
$ kubectl get pods
- The above command list out the all the pods in the kubernetes, with the you can know the status of your created pod and its age of creation. The below screenshot illustrates it clearly.

Thi
- Now that our pod is created, let us enter inside our pod and run some commands.
Step 5: Connecting to the Minikube environment with ssh protocol by using the following command:
$ minikube ssh
- On Executing this command, It will help you landing in minikube interactive environment from your local environment. You can understand it from seeing the hostnames in the below screenshot.

Step 6: Check the list of all the running containers inside the Minikube environment with the following command.
$ docker ps -a
- The above command list out with displaying all the containers that are created with kubectl from local environment to the Minikube.

- From here we will get access to inside the Nginx-pod and run some commands with the following steps.
Step 7: Get inside the nginx pod container with its container_name or container_id knowing from the above step command `docker ps -a`. The following command helps you in going inside the nginx pod container.
$ docker exec -it <Id_of_container> /bin/bash

- Now we are inside the Nginx-pod, let us run a few shell commands like ls, pwd, and cd.
Outputs Of Shell Commands Execution
- With Step 7 you are landed to inside the pod container, from the you can run the shell commands here the below screenshots shows its execution practically.

So, This is how we can use the minikube to deploy a pod and run a command inside it.
Best Practices Of Executing Shell Commands In Kubernetes
Use {kubectl exec} With Precision: Making sure that the `kubectl exec` command is performed in the appropriate environment by specifying the target pod and container.
Make Use Of The {-c} Flag For Multi-Container Pods: For accessing the shell of a particular container inside the pod, try to use the -c flag together with the `kubectl exec` command.
Sequential Execution With `&&` Operator: For executing multiple commands sequentially within a Kubernetes container using the `&&` Operator. It helps in maintaining a clear and efficient workflow.
Debugging With `kubectl debug`: This command `kubectl debug` facilitates starting the debugging session inside an active pod for troubleshooting and debugging purposes. It offers a wide setting for research and problem-solving.
Non-Interactive Commands With Flags: For using one-off non-interactive commands try on using the `kubectl exec` along with `–stdin=false` and `–tty=false` flags. It helps in the execution of commands without entering the Pod Internally providing enhanced automation and scripting capabilities.
Conclusion
In conclusion, Learning how to run shell commands inside of Kubernetes Pods is helps in effectively managing the containerized applications. As discussed above within the Minikube Setup, users can easily deploy, manage, and debug their apps with more effectiveness. Through this way, developers can confidently engage with their Kubernetes environments. With this learning, running the shell commands you can enhance agility of software development and administration sections.
Execute Shell Commands In Kubernetes Pods – FAQ’s
How To Execute A Shell Command In Kubernetes Pod?
On using the `kubectl exec` command with passing container name, and Pod name and followed by the desired shell command you can run them.
Is It Possible To Execute Multiple Shell Commands Within A Kubernetes Container?
Yes, You can execute multiple shell commands with a Kubernetes container by using the ‘&&’ Operator sequentially with shell commands in a single line.
How To Troubleshoot Or Debug In A Running Pod?
On Using the `kubectl debug` command you can start a debugging session with the Pod.
How To Access A Shell Of A Specific Container In A Multi-Container Pod?
Using the `kubectl exec -c` command helps in accessing the shell of a specific container in a multi-container pod.
How To Run A One-Off Command Without Entering The Pod Interactively?
You can run a One-off command using `kubectl exec` with the `stdin=false` and `–tty=false` flags for non-interactively.
Similar Reads
Kubernetes Tutorial
In this Kubernetes Tutorial youâll learn all the basics to advanced concepts like Kubernetes service , Kubernetes cluster construction , Kubernetes deployment , Kubernetes Architecture , etc. This free tutorial on Kubernetes will provide all the essential information needed to understand and work wi
7 min read
Introduction to Kubernetes
Application Deployment
What are Kubernetes Containers?
Kubernetes is an open-source container orchestration framework that was originally developed by Google. Container orchestration is automation. It can facilitate you to deploy the identical application across different environments like physical machines, virtual machines cloud environments, or perha
15 min read
Kubernetes - Introduction to Container Orchestration
In this article, we will look into Container Orchestration in Kubernetes. But first, let's explore the trends that gave rise to containers, the need for container orchestration, and how that it has created the space for Kubernetes to rise to dominance and growth. The growth of technology into every
4 min read
Kubernetes - Images
Pre-requisite:- Kubernetes A container image is used to represent binary data that is being used to encapsulate an application and all its software dependencies. Container images can be represented as executable software bundles that run standalone and make very defined assumptions about their runti
3 min read
Kubernetes - Jobs
Pre-requisite: Kubernetes In the Kubernetes world, jobs are considered an object to act as a supervisor or controllers of a task. The Kubernetes job will create a pod, monitor the task, and recreate another one if that pod fails for some reason. Upon completion of the task, it will terminate the pod
4 min read
Kubernetes - Labels & Selectors
An open-source container management platform called Kubernetes automates the deployment, scaling, descaling, and load balancing of containers (also called a container orchestration tool). It was created by Google in Golang and has a sizable community as a result of that. Google eventually donated it
5 min read
Kubernetes - Namespaces
Kubernetes Namespace is a mechanism that enables you to organize resources. It is like a virtual cluster inside the cluster. A namespace isolates the resources from the resources of other namespaces. For example, You need to have different names for deployments/services in a namespace but you can ha
9 min read
Kubernetes - Node
Kubernetes Nodes are the Worker or master machines where the actual work happens. Each Kubernetes node has the services required to execute Pods and is controlled by the Control Plane. Each Kubernetes Node can have multiple pods and pods have containers running inside them. 3 processes in every Node
13 min read
Kubernetes - NodePort Service
NodePort service in Kubernetes is a service that is used to expose the application to the internet from where the end-users can access it. If you create a NodePort Service Kubernetes will assign the port within the range of (30000-32767). The application can be accessed by end-users using the node's
5 min read
Kubernetes - ClusterIP vs NodePort vs LoadBalancer
Three main service types are used in Kubernetes networking: ClusterIP, NodePort, and LoadBalancer. Each has a specific function in controlling external access and service-to-service communication. Comprehending their distinctions is essential for efficiently coordinating applications. This article e
8 min read
Kubernetes - Services
Software deployment, scaling, and management are all automated using Kubernetes, an open-source container orchestration system. K8s is another name for Kubernetes. Kubernetes was initially developed by Google and is now managed by the Cloud Native Computing Foundation. Despite the fact that it now s
3 min read
Kubernetes Pods: How to Create and Manage Them
Kubernetes is an open-source container orchestration system mainly used for automated software deployment, management, and scaling. Kubernetes is also known as K8s. Kubernetes was originally developed by Google, but it is now being maintained by the Cloud Native Computing Foundation. It was original
13 min read
How to Run Shell Commands in Kubernetes Pods or Containers
In Kubernetes, we create pods by adding an extra layer of information on containers. This Kubernetes in short is known as K8s, an open-source container orchestration tool developed by Google. It is used to orchestrate the containers for bringing Agility in software deployment through scaling, and ma
6 min read
Kubernetes - Creating Multiple Container in a Pod
Pre-requisite:- Kubernetes Kubernetes is a container management tool and it automates container deployment, load balancing, and container scaling. It is open-source and developed by Google in 2014 and written in Golang. All cloud providers adopt Kubernetes. It is scheduled runs and manages isolated
3 min read
Kubernetes - Replication Controller
With the help of the open-source container orchestration technology Kubernetes, software deployment, scalability, and management are mostly automated. Another name for Kubernetes is K8s. Google created Kubernetes, which is now overseen by the Cloud Native Computing Foundation. Even though it now wor
7 min read
Kuberneters - Difference Between Replicaset and Replication Controller
Pre-requisite: Kubernetes Kubernetes is also known as K8s is an open-source container orchestration tool developed by google which is used for automating software deployment, scaling, and management. Currently, it is being maintained by the cloud native computing foundation(CNCF). K8s has two versio
4 min read
What is Kubernetes Deployment?
Kubernetes is an open-source Container Management tool that automates container deployment, container scaling, descaling, and container load balancing (also called as container orchestration tool). It is written in Golang and has a huge community because it was first developed by Google and later do
10 min read
Configmaps
Kubernetes - ConfigMaps
An open-source container orchestration system called Kubernetes is primarily used for automated software deployment, scaling, and management. Another name for Kubernetes is K8s. Originally developed by Google, Kubernetes is now managed by the Cloud Native Computing Foundation. Although it now suppor
10 min read
Kubernetes - Create Config Map From Files
Pre-requisite: Kubernetes While creating a manifest file in Kubernetes, we can define environment variables. However, when you have a lot of manifest files, it will become difficult to manage the environment data stored in various manifest files. To overcome this issue, we can manage environment dat
2 min read
Kubernetes - Create ConfigMap From YAML File
A ConfigMap is a dictionary consisting of non-confidential data. Its primary role is to keep the configuration separate from the container image. ConfigMap can be created in different ways. This article will cover the declarative approach to creating ConfigMap from the YAML file. Example: apiVersion
1 min read
Kubernetes - Config Map From Directory
Pre-requisite:- Kubernetes Software deployment, scalability, and administration are mostly automated using Kubernetes, an open-source container orchestration framework. K8s is another name for Kubernetes. Kubernetes was initially developed by Google and is now managed by the Cloud Native Computing F
2 min read
Kubernetes - Injecting ConfigMap as Files
Pre-requisite:- Kubernetes The automated deployment, scaling, and administration of software using a system called Kubernetes, an open-source container orchestration tool. K8s is another name for Kubernetes. Kubernetes was initially developed by Google and is now managed by the Cloud Native Computin
3 min read
Kubernetes - Injecting ConfigMap in Pods
Pre-requisite: Kubernetes Leveraging the open-source container orchestration engine Kubernetes to automate the deployment, scalability, and management of applications. Another name for Kubernetes is K8s. Google originally created Kubernetes, which is currently overseen by the Cloud Native Computing
3 min read
Scaling and Updating Applications
Kubernetes - Service DNS
An open-source container orchestration system called Kubernetes is primarily employed for the automated deployment, scaling, and management of software. Another name for Kubernetes is K8s. Initially created by Google, Kubernetes is currently maintained by the Cloud Native Computing Foundation. Altho
11 min read
Additional Topics
What is Kubernetes API ?Complete Guide
Kubernetes API is an application that serves Kubernetes functionality through a RESTful interface and stores the state of the cluster via HTTP. Users can directly interact with the Kubernetes API or via tools like kubectl. It supports retrieving, creating, updating, and deleting primary resources vi
14 min read
Kubernetes - Taint and Toleration
A pod is a group of one or more containers and is the smallest deployable unit in Kubernetes. A node is a representation of a single machine in a cluster (we can simply view these machines as a set of CPU and RAM). A node can be a virtual machine, a physical machine in a data center hosted on a clou
6 min read
Kubernetes Resource Model (KRM) and How to Make Use of YAML?
Here we will explain how YAML can simplify system management and automation of most processes so that Kubernetes is a convenient working system. Basic Kubernetes Models: KRM and Everything-as-CodeAccording to Kubernetes co-founder Brian Grant, Kubernetes is very convenient thanks to the Kubernetes R
6 min read
Installing Private Git Server on K8s Cluster with Gitea and AKS
In this article, we are going to install a self-hosted Gitea server on top of Azure Kubernetes Service with Helm and set up a git repo. Having a private Git server might be beneficial these days. Gitea is a community-managed Git-compatible lightweight code hosting solution written in Go. It is publi
4 min read
Enable Remote Debugging For Java Application Deployed in Kubernetes Environment
During Development, developers have to debug their applications to resolve code problems. In order to debug a java application which is deployed on remote machine in a Kubernetes cluster, first developer has to do some steps to enable its application ready for debugging. Below are the manual steps t
2 min read
How to Enable JMX For Java Application Running in the Kubernetes Cluster?
Many times we want to monitor our application's CPU utilization, background thread behavior, and most importantly memory consumptions for tasks that deal with loads for data (500MB - 1GB) or much more data. Such monitoring helps to find which operation is causing heavy CPU or Memory utilization and
3 min read