Kubernetes—commonly known as K8s—is an open-source container orchestration platform originally developed by Google and now maintained by the Cloud Native Computing Foundation (CNCF). It is designed to automate the deployment, scaling, and management of containerized applications, helping organizations achieve agility and consistency across environments.
Running shell commands inside Kubernetes Pods or containers is a fundamental skill for developers and DevOps engineers managing applications in Kubernetes clusters. This guide will walk you through how to do it step-by-step, using Minikube for demonstration.
Setting Up a Kubernetes Cluster
Before executing shell commands, ensure that your Kubernetes cluster is properly set up. You can do this in one of two ways:
Full Cluster Setup – A production-grade environment with multiple nodes.
Single-Node Setup with Minikube – A lightweight local Kubernetes environment ideal for testing and development.
The main difference is that Minikube runs on a single node, making it simpler to configure and manage. You can install Minikube based on your system’s OS and architecture. (For more details, refer to: Setting Up Minikube in Your Local System.)
Understanding Kubernetes Pods and Containers
In Kubernetes, Pods are the smallest deployable units. A pod encapsulates one or more tightly coupled containers that share the same network namespace and storage volumes.
Think of a Pod as an abstraction layer on top of containers that adds metadata and orchestration capabilities. It contains the application’s code, libraries, runtime, and dependencies—ensuring consistent behavior across environments.
Now, let’s learn how to execute shell commands inside a Kubernetes Pod.
Executing Commands Inside a Kubernetes Pod
In this example, we’ll use Minikube to demonstrate the process.
Step 1: Start Minikube
Run the following command to start your local Kubernetes cluster:
minikube start
This initializes and configures a single-node Kubernetes cluster.
Step 2: Check Minikube Status
Verify that Minikube is running:
minikube status
If everything is working correctly, you should see the cluster components listed as Running.
Step 3: Create a Pod Using an NGINX Image
Run the following command to create a Pod:
kubectl run nginx-pod --image=nginx
This command pulls the NGINX image from Docker Hub and creates a pod named nginx-pod.
Step 4: List All Pods
Confirm that your pod is created and running:
kubectl get pods
You should see nginx-pod listed with a Running status.
Step 5: Access the Minikube Environment
Connect to your Minikube instance via SSH:
minikube ssh
This allows you to interact directly with the underlying virtual machine hosting your Kubernetes cluster.
Step 6: List Running Containers
Inside the Minikube environment, list all containers:
docker ps -a
You’ll see containers corresponding to your Kubernetes pods, including the NGINX container.
Step 7: Access the Container Shell
To enter the NGINX container, use:
docker exec -it  /bin/bash
Once inside, you can run Linux shell commands such as:
ls
pwd
cd /usr/share/nginx/html
You are now working directly within the containerized environment of your Kubernetes pod.
Alternative: Using kubectl exec (Recommended)
Instead of SSHing into Minikube and using Docker, you can directly execute commands in your pod from your local terminal:
kubectl exec -it nginx-pod -- /bin/bash
This command opens an interactive shell inside the nginx-pod container without requiring Docker commands.
Best Practices for Running Shell Commands in Kubernetes
Use kubectl exec Carefully Always specify the exact pod and container when executing commands to avoid unintentional changes.
Use the -c Flag for Multi-Container Pods When a pod has multiple containers, access a specific one by adding the -c flag:kubectl exec -it -c -- /bin/bash
Execute Sequential Commands with && For running multiple commands in sequence:kubectl exec -it nginx-pod -- sh -c "cd /usr/share/nginx/html && ls && cat index.html"
Debug with kubectl debug Launch a debugging session in a running pod:kubectl debug -it nginx-pod --image=busybox This is useful for troubleshooting live workloads.
Use Non-Interactive Execution for Automation For scripts or CI/CD pipelines, run non-interactive commands:kubectl exec nginx-pod --stdin=false --tty=false -- ls /var/log/nginx
Conclusion
Understanding how to run shell commands inside Kubernetes Pods is essential for managing, debugging, and maintaining containerized applications.
Using tools like Minikube simplifies learning and experimentation, while kubectl exec provides a direct and efficient way to interact with your containers.
Mastering these techniques will help developers and DevOps engineers manage Kubernetes environments with greater confidence, agility, and control—boosting both development speed and operational reliability.
    
Top comments (0)