Tuesday, 14 October 2025

Kubernetes FieldRef Environment Variables: Dynamic Pod Metadata in Action

Our course you can check :-   Udemy course  


Ques:-   

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 image httpd 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)

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 shoulbe be spec.serviceAccountName.

Set restart policy to Never.

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


Ans:-

Here’s the complete Kubernetes YAML manifest for your scenario involving environment variables using fieldRef:

raj@jumphost ~$ cat pod.yaml


apiVersion: v1

kind: Pod

metadata:

  name: envars

spec:

  restartPolicy: Never

  containers:

  - name: fieldref-container

    image: httpd:latest

    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


raj@jumphost ~$ kubectl apply -f pod.yaml

pod/envars created


raj@jumphost ~$ kubectl get all

NAME         READY   STATUS    RESTARTS   AGE

pod/envars   1/1     Running   0          10s


NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE

service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   28m


raj@jumphost ~$ kubectl exec -it envars -- sh

# printenv

POD_IP=10.244.0.5

HTTPD_VERSION=2.4.65

KUBERNETES_SERVICE_PORT=443

KUBERNETES_PORT=tcp://10.96.0.1:443

HOSTNAME=envars

HOME=/root

HTTPD_PATCHES=

NODE_NAME=control-plane

TERM=xterm

POD_NAME=envars

KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1

PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

POD_SERVICE_ACCOUNT=default

KUBERNETES_PORT_443_TCP_PORT=443

HTTPD_SHA256=58b8be97d9940ec17f7656c0c6b9f41b618aac468b894b534148e3296c53b8b3

KUBERNETES_PORT_443_TCP_PROTO=tcp

HTTPD_PREFIX=/usr/local/apache2

KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443

KUBERNETES_SERVICE_PORT_HTTPS=443

KUBERNETES_SERVICE_HOST=10.96.0.1

PWD=/usr/local/apache2


You should see the values for:

  • NODE_NAME
  • POD_NAME
  • POD_IP
  • POD_SERVICE_ACCOUNT


Conclusion:-

In this practical tutorial, you'll learn how to dynamically inject Kubernetes pod and node metadata into your application using fieldRef environment variables. This technique is essential for configuring applications that need runtime awareness of their environment—without hardcoding values.

We’ll walk through:

  • Creating a pod with environment variables sourced from Kubernetes fields
  • Using spec.nodeName, metadata.name, status.podIP, and spec.serviceAccountName
  • Building a container that prints these values continuously
  • Verifying the setup using kubectl exec and printenv

This course is ideal for DevOps engineers, SREs, and Kubernetes learners who want to master dynamic configuration techniques using native Kubernetes features.

No comments:

Post a Comment