# Environment Variables in Kubernetes

This is part six of a series of lab tasks from KodeKloud for Kubernetes. Master blog listing all parts can be seen [**here**](https://eklavvya.hashnode.dev/kodekloud-kubernetes-lab-solutions). 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
    
    ```bash
    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.

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

Final YAML:

```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:

```bash
$ 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

```bash
$ kubectl delete pod envars
pod "envars" deleted
```
