Menu

Dipesh Majumdar

Blog and Paintings

Define environment variable in a pod manifest

In the below example i am defining an environment variable "envVar1" and then checking it inside the pod by 'printenv'

dipes@LAPTOP-FEA1CJ5Q MINGW64 ~/gitlabDOTcom_content/infrastructure (master)                                                                                                                                      
$ cat 01pod-002-oedpod.yaml                                                                                                                                                                                       
apiVersion: v1                                                                                                                                                                                                    
kind: Pod                                                                                                                                                                                                         
metadata:                                                                                                                                                                                                         
 name: oeudpod                                                                                                                                                                                                    
 namespace: k8s-cert                                                                                                                                                                                              
 labels:                                                                                                                                                                                                          
   app: oeupod-label                                                                                                                                                                                              
   name: oeupod                                                                                                                                                                                                   
spec:                                                                                                                                                                                                             
 containers:                                                                                                                                                                                                      
  - name: oeud                                                                                                                                                                                                    
    image: centos:7                                                                                                                                                                                               
    env:                                                                                                                                                                                                          
     - name: envVar1                                                                                                                                                                                              
       value: envVar1-value                                                                                                                                                                                       
    command:                                                                                                                                                                                                      
     - "bin/bash"                                                                                                                                                                                                 
     - "-c"                                                                                                                                                                                                       
     - "sleep 300"                                                                                                                                                                                                
                                                                                                                                                                                                                  
                                                                                                                                                                                                                  
dipes@LAPTOP-FEA1CJ5Q MINGW64 ~/gitlabDOTcom_content/infrastructure (master)                                                                                                                                      
$ kubectl create -f 01pod-002-oedpod.yaml                                                                                                                                                                         
pod "oeudpod" created                                                                                                                                                                                             
                                                                                                                                                                                                                  
dipes@LAPTOP-FEA1CJ5Q MINGW64 ~/gitlabDOTcom_content/infrastructure (master)                                                                                                                                      
$ kubectl -n k8s-cert get po -l app=oeupod-label                                                                                                                                                                  
NAME      READY     STATUS    RESTARTS   AGE                                                                                                                                                                      
oeudpod   1/1       Running   0          1m                                                                                                                                                                       
                                                                                                                                                                                                                  
dipes@LAPTOP-FEA1CJ5Q MINGW64 ~/gitlabDOTcom_content/infrastructure (master)                                                                                                                                      
$ kubectl -n k8s-cert exec oeudpod -- bash -c 'printenv'                                                                                                                                                          
HOSTNAME=oeudpod                                                                                                                                                                                                  
KUBERNETES_PORT=tcp://10.11.240.1:443                                                                                                                                                                             
KUBERNETES_PORT_443_TCP_PORT=443                                                                                                                                                                                  
KUBERNETES_SERVICE_PORT=443                                                                                                                                                                                       
KUBERNETES_SERVICE_HOST=10.11.240.1                                                                                                                                                                               
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin                                                                                                                                                 
PWD=/                                                                                                                                                                                                             
envVar1=envVar1-value                                                                                                                                                                                             
SHLVL=1                                                                                                                                                                                                           
HOME=/root                                                                                                                                                                                                        
KUBERNETES_PORT_443_TCP_PROTO=tcp                                                                                                                                                                                 
KUBERNETES_SERVICE_PORT_HTTPS=443                                                                                                                                                                                 
KUBERNETES_PORT_443_TCP_ADDR=10.11.240.1                                                                                                                                                                          
KUBERNETES_PORT_443_TCP=tcp://10.11.240.1:443                                                                                                                                                                     
_=/usr/bin/printenv                                                                                                                                                                                               
                                                                                                                                                                                                                  
dipes@LAPTOP-FEA1CJ5Q MINGW64 ~/gitlabDOTcom_content/infrastructure (master)                                                                                                                                      

But if you want to load env variable from a configmap this is the way to do it -

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx:1.7.7
    name: nginx
    env:
     - name: env-direct1
       value: "1234"
     - name: name
       valueFrom:
         configMapKeyRef:
             name: cm1
             key: name
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

 

Go Back

Comment