How to run a command inside a Kubernetes Container/Pod
Kubernetes is an open-source container orchestration platform that automates container deployment, scaling, and management. In Kubernetes, a pod is the smallest and simplest unit in the Kubernetes object model, representing a single instance of a running process in a cluster. Sometimes, you may need to run a command inside a Kubernetes container/pod. In this article, we’ll explore how to do that using kubectl exec
command.

Prerequisites
Before we begin, make sure you have the following:
- A Kubernetes cluster up and running
kubectl
CLI installed and configured to connect to the Kubernetes cluster- Basic knowledge of Kubernetes objects and their lifecycle.
Running a Command Inside a Kubernetes Container/Pod
To run a command inside a Kubernetes container/pod, we use the kubectl exec
command. This command allows us to execute a command inside a running container/pod. Here’s the syntax for the kubectl exec
command:
kubectl exec <pod-name> -- <command>
The <pod-name>
parameter is the name of the pod in which you want to execute the command, and <command>
is the command you want to execute.
Real Time example:
— If you wanted to create a Network policy to make sure there is only incoming connection from a particular pod
— Consider the below example: This will only allow connection from the pod with the label “role: frontend” to the pod with the label “role: db”
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: test-network-policy namespace: default spec: podSelector: matchLabels: role: db policyTypes: - Ingress ingress: - from: - podSelector: matchLabels: role: frontend
— Once you have configured the network policy, We can use the “kubectl exec” command to make sure it is working.
kubectl exec -it pod -- /bin/sh sh #nc -z -v -w <pod ip address>.default.pod.cluster.local.
Running a Command with Arguments
If you need to pass arguments to the command you want to run inside the container, you can do so by adding them after the command. Here’s an example:
kubectl exec my-pod -- echo arg1 arg2
This will execute the echo
command inside the container and pass arg1
and arg2
as arguments.
Running a Command in a Specific Container
If the pod has multiple containers, you can specify which container to run the command in by adding the -c
or --container
flag to the kubectl exec
command. Here’s an example:
perlCopy codekubectl exec my-pod -c my-container -- ls
This will execute the ls
command inside the container named my-container
.
Create a temporary Pod to access other pods
— We can also, Create a new pod and try accessing different pod
kubectl run -it --rm test --image=busybox --restart=Never -- sh sh #nc -z -v -w <pod ip address>.default.pod.cluster.local.
— Above commands, help you to check the connectivity between pods
By the above ways, We can run commands inside a container, and I have explained one single example to make you understand better
Check out here for other Kubernetes learning post
Conclusion
In this article, we’ve learned how to run a command inside a Kubernetes container/pod using the
kubectl exec
command. We’ve also seen how to run a command with arguments and how to specify which container to run the command in. Thekubectl exec
command is a powerful tool that can be used to debug issues in Kubernetes clusters and to perform various tasks on running containers.
Good Luck with your Learning !!
Related Topics:
How to list all running pod names in Kubernetes
How to get the YAML file from Kubernetes objects (Pod, Deployment, Services, and combined)?