Thursday 16 July 2020

Kubernetes: Specify environment variables in Pod definition file

You can specify environment variables using ‘env’ property.

 

envVariables.yml
apiVersion: v1
kind: Pod
metadata:
  name: read-env-variables-pod
  labels:
    app: hello-world
    author: krishna
    serviceType: terminal-app
spec:
  containers:
    - name: execute-command-container
      image: busybox
      command: ["/bin/sh"]
      args: ["-c", "while true; do echo \"Hi, $AUTHOR , about you : $ABOUT_ME\"; sleep 5; done"]
      env:
      - name: AUTHOR
        value: krishna
      - name: ABOUT_ME
        value: I am a blogger

Create Pod using the above definition file.

$kubectl create -f envtVariables.yml 
pod/read-env-variables-pod created

Make sure that the Pod is running.

$kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
read-env-variables-pod   1/1     Running   0          32s

Now, you can see what is happening in the container using ‘kubectl logs' command.

$kubectl logs read-env-variables-pod
Hi, krishna , about you : I am a blogger
Hi, krishna , about you : I am a blogger
Hi, krishna , about you : I am a blogger

For every 5 seconds, environment variables get printed to console.

 

You can delete the pod by executing the below command.

kubectl delete pod read-env-variables-pod

$kubectl delete pod read-env-variables-pod
pod "read-env-variables-pod" deleted

Previous                                                    Next                                                    Home

No comments:

Post a Comment