Skip to main content

Command Palette

Search for a command to run...

Environment Variables in Kubernetes

Published
2 min readView as Markdown
R

I’m a hands-on software engineer with over 18 years of experience solving real-world problems with code. I’ve spent most of that time building web applications, backend systems, and automation tools — often using Python, Django, REST, and a healthy mix of SQL and shell scripts on Linux.

Along the way, I’ve also picked up frontend work with Angular and React, and built infrastructure using Docker, Kubernetes, AWS, and Terraform. I wouldn’t call myself a DevOps engineer, but I do believe in owning the full stack — from writing the API to making sure it runs smoothly in production.

I’ve worked in all kinds of teams — large, small, remote, distributed, fast-paced, slow-paced. For the last few years, I’ve been freelancing, which has been both freeing and demanding in the best possible way. It’s pushed me to keep learning, stay sharp, and step outside my comfort zone. I love the mix of flexibility and challenge it brings.

This is part six of a series of lab tasks from KodeKloud for Kubernetes. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.

The Task

There are a number of parameters that are used by the applications. We need to define these as environment variables, so that we can use them as needed within different configs. Below is a scenario which needs to be configured on Kubernetes cluster. Please find below more details about the same.

  1. Create a pod named envars.

  2. Container name should be fieldref-container, use image redis preferable latest tag, use command sh -c and args should be

     while true;
        do
        echo -en '/n';
        printenv NODE_NAME POD_NAME;
        printenv POD_IP POD_SERVICE_ACCOUNT;
        sleep 10;
        done;
    

    (Note: please take care of indentations)

  3. Define Four environment variables as mentioned below:

    a.) The first env should be named as NODE_NAME, set valueFrom fieldref and fieldPath should be spec.nodeName.

    b.) The second env should be named as POD_NAME, set valueFrom fieldref and fieldPath should be metadata.name.

    c.) The third env should be named as POD_IP, set valueFrom fieldref and fieldPath should be status.podIP.

    d.) The fourth env should be named as POD_SERVICE_ACCOUNT, set valueFrom fieldref and fieldPath should be spec.serviceAccountName.

  4. Set restart policy to Never.

  5. To check the output, exec into the pod and use printenv command.

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

Solution

This is a simple one. Just dry-run a pod with needed image and output YAML specification. Edit YAML as required by the task.

$ kubectl run envars --image=redis:latest --dry-run=client -o yaml > envars.yaml
$

Final YAML:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: envars
  name: envars
spec:
  containers:
  - image: redis:latest
    name: fieldref-container
    command: ["sh", "-c"]
    args:
      - while true; do
        echo -en '/n';
        printenv NODE_NAME POD_NAME;
        printenv POD_IP POD_SERVICE_ACCOUNT;
        sleep 10;
        done;
    env:
      - name: NODE_NAME
        valueFrom:
          fieldRef:
            fieldPath: spec.nodeName
      - name: POD_NAME
        valueFrom:
          fieldRef:
            fieldPath: metadata.name
      - name: POD_IP
        valueFrom:
          fieldRef:
            fieldPath: status.podIP
      - name: POD_SERVICE_ACCOUNT
        valueFrom:
          fieldRef:
            fieldPath: spec.serviceAccountName
  restartPolicy: Never

Finally use kubectl to apply the specification:

$ kubectl apply -f env-vars/envars.yml 
pod/envars created
$ kubectl get pods 
NAME     READY   STATUS              RESTARTS   AGE
envars   0/1     ContainerCreating   0          4s
$ kubectl get pods --watch
NAME     READY   STATUS    RESTARTS   AGE
envars   1/1     Running   0          11s
^C
$ kubectl exec envars -- printenv NODE_NAME
minikube
$ kubectl exec envars -- printenv POD_NAME
envars
$ kubectl exec envars -- printenv POD_IP
10.244.0.14

Cleanup

$ kubectl delete pod envars
pod "envars" deleted

More from this blog

Rajesh

25 posts