DEV Community

Cover image for 4.Print Environment Variables
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

4.Print Environment Variables

Lab Information

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:
Enter fullscreen mode Exit fullscreen mode

a. GREETING and its value should be Welcome to

b. COMPANY and its value should be DevOps

c. GROUP and its value should be Datacenter

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.
Enter fullscreen mode Exit fullscreen mode

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

Lab Solutions

Step 1: Create the Pod YAML configuration

Create a file named print-envars-greeting.yaml:

cat > print-envars-greeting.yaml << 'EOF'
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: "Datacenter"
  restartPolicy: Never
EOF
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Pod

Apply the configuration to create the pod:

kubectl apply -f print-envars-greeting.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Check the Pod Status

Monitor the pod status to see when it completes:

kubectl get pod print-envars-greeting
Enter fullscreen mode Exit fullscreen mode

You'll see the status change from Pending → Running → Completed (or Succeeded).

Step 4: View the Output

Check the logs to see the output of the command:

kubectl logs print-envars-greeting
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)