57/100 Print Environment Variables
The Nautilus 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.
- Create a
podnamedprint-envars-greeting. - Configure spec as, the container name should be
print-env-containerand usebashimage. - 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 itsrestartPolicypolicy toNeverto avoid crash loop back. - You can check the output using
kubectl logs -f print-envars-greetingcommand.
First, create the yaml file for our pod which will be named print-envars-greeting.yaml
apiVersion: v1
kind: Pod
metadata:
name: print-envars-greeting
spec:
containers:
- name: print-env-container
image: bash
command: ["/bin/sh", "-c", 'echo "$(GREETING) $(COMPANY) $(GROUP)"']
env:
- name: GREETING
value: "Welcome to"
- name: COMPANY
value: "DevOps"
- name: GROUP
value: "Industries"
restartPolicy: Never
Apply the yaml file and then check the status of the pod:
thor@jumphost ~$ kubectl apply -f print-envars-greeting.yaml
pod/print-envars-greeting created
thor@jumphost ~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
print-envars-greeting 0/1 Completed 0 76
Finally, view the log output:
thor@jumphost ~$ kubectl logs -f print-envars-greeting
Welcome to DevOps IndustriesExplanation
restartPolicy: Never - This ensures the Pod won’t restart once it completes the echo command.
The bash image runs the command and prints the concatenated environment variables.
The echo "$(GREETING) $(COMPANY) $(GROUP)" syntax uses shell substitution to combine them.