Environment Variables in Kubernetes
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.
Create a pod named
envars.Container name should be
fieldref-container, use imageredispreferable latest tag, use commandsh -cand args should bewhile 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)
Define
Fourenvironment variables as mentioned below:a.) The first
envshould be named asNODE_NAME, set valueFrom fieldref and fieldPath should bespec.nodeName.b.) The second env should be named as
POD_NAME, set valueFrom fieldref and fieldPath should bemetadata.name.c.) The third
envshould be named asPOD_IP, set valueFrom fieldref and fieldPath should bestatus.podIP.d.) The fourth
envshould be named asPOD_SERVICE_ACCOUNT, set valueFrom fieldref and fieldPath should bespec.serviceAccountName.Set restart policy to
Never.To check the output, exec into the pod and use
printenvcommand.
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

