Environment Variables in Kubernetes

Ticker

6/recent/ticker-posts

Environment Variables in Kubernetes

Question : There are a number of parameters that are used by 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 it.

Create a namespace named as fieldref-namespace.

Create a pod named envars-fieldref under the namespace fieldref-namespace.

Configure spec as the 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 POD_NAMESPACE; printenv POD_IP POD_SERVICE_ACCOUNT; sleep 10; done;'

(Note: please take care of indentations)

Define five 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_NAMESPACE, set valueFrom fieldref and fieldPath should be metadata.namespace.

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

e.) The fifth 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 the pod and use printenv command.

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


Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1. At first  kubectl  utility configure and working from jump server, run below commands   

thor@jump_host /$ kubectl get namespace

NAME              STATUS   AGE

default           Active   2m15s

kube-node-lease   Active   2m19s

kube-public       Active   2m19s

kube-system       Active   2m20s

thor@jump_host /$

thor@jump_host /$ kubectl get pods

No resources found in default namespace.

thor@jump_host /$

2.  Create namespace as per the task       

thor@jump_host /$ kubectl create namespace fieldref-namespace

namespace/fieldref-namespace created

thor@jump_host /$

thor@jump_host /$ kubectl get namespace

NAME                 STATUS   AGE

default              Active   2m59s

fieldref-namespace   Active   5s

kube-node-lease      Active   3m3s

kube-public          Active   3m3s

kube-system          Active   3m4s

thor@jump_host /$


3.  Create yaml  file with all the parameters , you can copy form gitlab

    https://gitlab.com/nb-tech-support/devops.git

    Refer Below Video for more clarity ) 

thor@jump_host /$ vi /tmp/envars.yml

thor@jump_host /$ cat /tmp/envars.yml

apiVersion: v1

kind: Pod

metadata:

  name: envars-fieldref

  namespace: fieldref-namespace

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 POD_NAMESPACE;

          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_NAMESPACE

          valueFrom:

            fieldRef:

              fieldPath: metadata.namespace

        - name: POD_IP

          valueFrom:

            fieldRef:

              fieldPath: status.podIP

        - name: POD_SERVICE_ACCOUNT

          valueFrom:

            fieldRef:

              fieldPath: spec.serviceAccountName

thor@jump_host /$

 4.  Run below command to create pod   

thor@jump_host /$ kubectl create -f /tmp/envars.yml

pod/envars-fieldref created

thor@jump_host /$

5. wait for the pod running status

thor@jump_host /$ kubectl get pods -n fieldref-namespace

NAME              READY   STATUS    RESTARTS   AGE

envars-fieldref   1/1     Running   0          15s

thor@jump_host /$


6. Validate the task by login on pod and run printenv
      

thor@jump_host /$ kubectl exec -it envars-fieldref  -n fieldref-namespace  -- /bin/bash

root@envars-fieldref:/usr/local/apache2# printenv

KUBERNETES_SERVICE_PORT_HTTPS=443

KUBERNETES_SERVICE_PORT=443

HOSTNAME=envars-fieldref

POD_NAME=envars-fieldref

POD_NAMESPACE=fieldref-namespace

PWD=/usr/local/apache2

HTTPD_VERSION=2.4.48

HOME=/root

KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443

HTTPD_PATCHES=

TERM=xterm

NODE_NAME=node01

HTTPD_SHA256=1bc826e7b2e88108c7e4bf43c026636f77a41d849cfb667aa7b5c0b86dbf966c

SHLVL=1

KUBERNETES_PORT_443_TCP_PROTO=tcp

KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1

POD_IP=10.244.1.2

KUBERNETES_SERVICE_HOST=10.96.0.1

KUBERNETES_PORT=tcp://10.96.0.1:443

KUBERNETES_PORT_443_TCP_PORT=443

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

POD_SERVICE_ACCOUNT=default

HTTPD_PREFIX=/usr/local/apache2

_=/usr/bin/printenv

root@envars-fieldref:/usr/local/apache2#



7.  Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , 

please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user