Building Cloud Expertise with centron - Our Tutorials

Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.

Commands in a Kubernetes Pod: A Comprehensive Guide

Hello, readers! Using Commands and Arguments in a Kubernetes Pod

This article talks about Using Commands and Arguments in a Kubernetes Pod with different scenarios for a better understanding.

So, let us begin!! 🙂

Use of Commands and Arguments – Process execution

When we say that an application runs within a Kubernetes Pod, we actually mean that the container is wrapped and presented as a Pod.

A container wraps up all the necessary dependencies and commands to execute processes together and sits within a Pod. During the creation of a Pod, we can define commands and arguments that will run within the container altogether.

Usually, the command and arguments that we define in a custom form override the default command and arguments of the base container image.

In the context of this topic, we will be dealing with ways to create and define commands and arguments for a container running as an application Pod.

Define Commands and Arguments for a Kubernetes Pod

In order to define an argument within a container, we can make use of the command field. The moment we define commands, we would be needing arguments to be passed to it. We can pass these arguments to the command using the args field.

In the below example, we have passed the command printenv to the container for it to print the values for the environment variable KUBECONFIG as an argument to it.

Example: pod.YAML

apiVersion: v1
kind: Pod
  name: demo-cmd
  - name: cmd-arg-demo
    image: debian
    command: ["printenv"]
    args: ["KUBECONFIG"]
  restartPolicy: OnFailure

Let us now apply the above file and create a Pod.

Once we create a Pod, we can get the logs of the pod and the specific container to look for the result of the command execution.


The output returns the value for the command execution. That is, it displays the path of the KUBECONFIG file as the value.

1. Using env variables to define arguments

As a variant, we can make use of environment variables to pass the value of the arguments to the commands. Let us have a look at the below section of code-

Example: Sample Code

- name: data
  value: "002234-welcome-message"
command: ["/bin/data"]
args: ["$(data)"]

Using the above block of code, we can pass the value of the arguments using an environment variable. Here, we pass the value of the argument to the command in the form of a variable named data whose value is specified as an environment variable.

Apart from environment variables, we can also parse the value in the form of a ConfigMap and a Secret in a similar manner.

2. Running commands within a shell

At times, when we wish to execute multiple commands altogether, we would need a shell to be running within the container for the execution.

This can be achieved through running a virtual shell at the run-time.

For the same, we define a command to run all the specified commands within the Pod in the shell as shown below-

command: ["/bin/sh"]
args: ["-c", "while true; do echo Welcome to JournalDev; sleep 100;done"]

Here, in this example, we have instructed the Pod to use a shell to run a BASH script executing multiple commands altogether such as the while loop execution – A Comprehensive Guide

Start Your Free Trial Today!

Take your Kubernetes projects to the next level with our advanced cloud platform. Experience the ease and efficiency of managing Commands and Arguments in a Kubernetes Pod with our robust and scalable cloud solution.

Try for free!