Friday, 10 October 2025

Kubernetes Pod with Environment Variables and Custom Commands – Real-World Example

Our course you can check :-   Udemy course 

Ques:-    

DevOps team is working on to setup some pre-requisites for an application that will send the greetings to different users. There is a sample deployment, that needs to be tested. Below is a scenario which needs to be configured on Kubernetes cluster. Please find below more details about it.

Create a pod named print-envars-greeting.

Configure spec as, the container name should be print-env-container and use bash image.

Create three environment variables:

a. GREETING and its value should be Welcome to

b. COMPANY and its value should be DevOps

c. GROUP and its value should be Industries

Use command ["/bin/sh", "-c", 'echo "$(GREETING) $(COMPANY) $(GROUP)"'] (please use this exact command), also set its restartPolicy policy to Never to avoid crash loop back.

You can check the output using kubectl logs -f print-envars-greeting command.


Ans:-


Here's the YAML manifest to create the print-envars-greeting pod as per your requirements:

raj@jumphost ~$ cat pod.yaml

apiVersion: v1

kind: Pod

metadata:

  name: print-envars-greeting

spec:

  containers:

  - name: print-env-container

    image: bash

    env:

    - name: GREETING

      value: "Welcome to"

    - name: COMPANY

      value: "DevOps"

    - name: GROUP

      value: "Industries"

    command: ["/bin/sh", "-c", 'echo "$(GREETING) $(COMPANY) $(GROUP)"']

  restartPolicy: Never


raj@jumphost ~$ kubectl apply -f pod.yaml  

pod/print-envars-greeting created


raj@jumphost ~$ kubectl get pods

NAME                    READY   STATUS      RESTARTS   AGE

print-envars-greeting   0/1     Completed   0          15s


raj@jumphost ~$ kubectl logs print-envars-greeting

Welcome to DevOps Industries

raj@jumphost ~$ 


You can convert this into a Job or CronJob for repeated execution!


No comments:

Post a Comment