While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Hello, readers! This article talks about Using Commands and Arguments in a Kubernetes Pod with different scenarios for a better understanding.
So, let us begin!! :)
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.
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.
apiVersion: v1 kind: Pod metadata: name: demo-cmd spec: containers: - name: cmd-arg-demo image: debian command: ["printenv"] args: ["KUBECONFIG"] restartPolicy: OnFailure
Let us now apply the above file and create a Pod.
kubectl apply -f pod.yaml
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.
kubectl logs -f demo-cmd
The output returns the value for the command execution. That is, it displays the path of the KUBECONFIG file as the value.
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
env: - 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.
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.
By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions.
For more questions related to Docker and Kubernetes, Stay tuned with us.
Till then, Happy Learning! :)
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up
Click below to sign up and get $100 of credit to try our products over 60 days!