Files

4.5 KiB

Assignment

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

Note: The kubectl utility on the jump-host has been configured to work with the Kubernetes cluster.

Solution

Kubernetes Pod with Env Vars — print-envars-greeting

A one-shot pod that echoes three environment variables using Kubernetes' $(VAR) substitution. Applied inline via a heredoc — no manifest file on disk.

Apply (heredoc → kubectl)

kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: print-envars-greeting
spec:
  restartPolicy: Never
  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"
EOF

Check the output

kubectl logs -f print-envars-greeting

Expected: Welcome to DevOps Industries

How it works

The heredoc apply pattern — quoting matters a lot here

  • kubectl apply -f - reads from stdin; nothing written to disk.
  • <<'EOF' (delimiter quoted) is critical for this task. The command contains $(GREETING), $(COMPANY), $(GROUP). If the heredoc delimiter were unquoted, the jump-host's shell would interpret $(...) as command substitution and try to run GREETING as a command — mangling the manifest before kubectl ever sees it. Quoting EOF passes the $(...) through literally so Kubernetes (not the shell) handles the expansion.

$(VAR) is Kubernetes substitution, not shell substitution

The exact command uses $(GREETING), not ${GREETING} or $GREETING. In a container's command/args, Kubernetes itself expands $(VAR) references using the container's declared env before the process starts. So Kubernetes rewrites the command to echo "Welcome to DevOps Industries", and /bin/sh then just echoes that literal string.

This is a subtle but important distinction:

  • $(VAR) — expanded by Kubernetes from the env list at container start.
  • ${VAR} / $VAR — expanded by the shell at runtime.

Both would produce the same output here (since sh also has the env vars), but the task pins the $(VAR) form, and it's Kubernetes doing the substitution. A gotcha to know: $(VAR) only resolves if a matching env entry exists; an undefined $(FOO) is left literal rather than erroring.

The env vars

Three env entries — GREETING="Welcome to", COMPANY="DevOps", GROUP="Industries" — supply the values Kubernetes substitutes into the command. Values are quoted because they contain spaces (Welcome to).

restartPolicy: Never

The command echoes once and exits 0 — a completed run, not a long-running process. With the default restartPolicy: Always, Kubernetes would see the container exit and keep restarting it, driving CrashLoopBackOff (even though it "succeeded"). Never tells Kubernetes not to restart it, so the pod ends in Completed state cleanly. This is why the task specifies it.

The bash image

image: bash pulls the official BusyBox-based bash image, which provides /bin/sh to run the command. It runs the echo and exits — exactly the one-shot behavior wanted.

Verify

# Pod ran to completion
kubectl get pod print-envars-greeting            # STATUS: Completed

# The greeting output
kubectl logs print-envars-greeting

Expected — print-envars-greeting in Completed status, and the logs printing Welcome to DevOps Industries.

Completed (not Running) is correct here — the pod's job was to print once and exit. If it shows CrashLoopBackOff, restartPolicy: Never didn't take.