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

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

Last updated: January 31, 2024

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.

Next Article: Working with Kubernetes Dashboard: A Practical Guide (with examples)

Previous Article: Kubernetes Ingress Error 403: Content-Length too long

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide