Tutorials  /  Container & Cloud

Commands in a Kubernetes Pod: A Comprehensive Guide

Ccentron Redaktion · January 2024 ·4 min read ·Container & Cloud, Tutorial

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.

K8

Matching infrastructure at centron

From container to cluster: managed Kubernetes with cStack starts at €29.99 per month – control plane, autoscaler and traffic included. Explore managed Kubernetes →

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

YAML
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.

Console
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.

Console
kubectl logs -f demo-cmd

Output:

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

Code
/home/.kube

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

YAML
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.

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-

Code
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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Container & Cloud
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren