Environment Variables in Kubernetes

Task :

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.

Solution :

thor@jump_host /$ cd /home/thor
thor@jump_host ~$ kubectl create ns fieldref-namespace
namespace/fieldref-namespace created
thor@jump_host ~$ vi 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 ~$ kubectl apply -f envars.yml 
pod/envars-fieldref created
thor@jump_host ~$ kubectl get pod -n fieldref-namespace
NAME READY STATUS RESTARTS AGE
envars-fieldref 1/1 Running 0 30s
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.46
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
HTTPD_PATCHES=
TERM=xterm
NODE_NAME=node01
HTTPD_SHA256=740eddf6e1c641992b22359cabc66e6325868c3c5e2e3f98faf349b61ecf41ea
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
POD_IP=10.244.1.3
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# exit
exit
thor@jump_host ~$ kubectl logs envars-fieldref -n fieldref-namespace
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
-en

node01
envars-fieldref
fieldref-namespace
10.244.1.3
default
thor@jump_host ~$

Leave a Reply

Your email address will not be published. Required fields are marked *