Kubernetes: How to run bash commands in a pod/container (with examples)

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Kubernetes, also known as K8s, is an open-source platform designed to automate the deployment, scaling, and operations of application containers. It manages clusters of dynamic environments and is widely used for containers orchestration. Understanding how to interact with pods and containers is fundamental for Kubernetes users. One of the most common tasks is executing bash commands within a pod. This tutorial will explore several methods to run bash commands inside Kubernetes pods, advancing from basic techniques to more complex scenarios.

Prerequisites

Before getting started, make sure you meet the followintg requirements:

  • A Kubernetes cluster
  • kubectl, the command-line tool for Kubernetes
  • Basic understanding of Kubernetes concepts (pods, containers, etc.)
  • Bash knowledge

Basic Commands Execution

Let’s start with the most straightforward method to execute commands within a pod: using kubectl exec.

kubectl exec my-pod -- ls /

This command will list the root directory of ‘my-pod’. The -- ensures that any following commands are passed to the pod, not to kubectl.

Output:

bin
dev
etc
home
...

Entering a Running Container

If you need to interact with a container shell directly, you can do this:

kubectl exec -it my-pod -- /bin/bash

The -i flag stands for interactive, and -t for TTY (a terminal interface). This command gives you a bash shell inside ‘my-pod’ container.

Running Commands in Specific Containers

In pods with multiple containers, specify which container you want to execute the command in.

kubectl exec -it my-pod -c my-container -- /bin/bash

Here, -c followed by the container name tells Kubernetes which container to target.

Executing Scripts Inside a Pod

You might have bash scripts that you’d prefer to run inside the pod. Here’s how you could do it:

kubectl cp my-script.sh my-pod:/tmp/
kubectl exec my-pod -- /bin/bash /tmp/my-script.sh

First, use kubectl cp to copy the script into the pod, then run it with kubectl exec.

Pass Environment Variables

This example demonstrates how to set environment variables for the execution:

kubectl exec my-pod -- env VAR1=value1 VAR2=value2 /bin/bash -c 'echo $VAR1 $VAR2'

You will see output:

value1 value2

Running Commands Without a Shell

You can run commands without opening a shell. For a single command this is fine, but it can be tricky for complex commands that require a shell, like pipelines or loops.

kubectl exec my-pod -- ls -l /

If you need to run multiple commands you should ideally enter the shell or consider writing a script that encapsulates the functionality.

Advanced Usage

For more complex scenarios, for instance when debugging issues, you might want to inspect the state of the container’s process, open-files, network traffic or perform system tracing.

kubectl exec my-pod -c my-container -- ps aux
kubectl exec my-pod -c my-container -- lsof -i
kubectl exec my-pod -c my-container -- netstat -tulpn
kubectl exec my-pod -c my-container -- strace -p <pid>

Each of these commands provide vital information that might be necessary when diagnosing issues.

Troubleshooting

Sometimes, you might face difficulties while executing commands. Here are common issues:

  • Error response from daemon: Container is not running. Make sure the pod and container are in a running state.
  • Session closed: Network problems or policy could cause premature disconnection from the session.
  • Command not found: The container might not have the command installed, or you might need to provide the full path.
  • Permission denied: The user inside the container may not have the necessary permissions.

Conclusion

This tutorial covered various methods to run bash commands inside a Kubernetes pod or container. Mastering these techniques is essential for Kubernetes administration and troubleshooting. By following the examples provided, Kubernetes users can effectively interact with their pods and gain valuable insights into their containerized applications.