kubernetes Deployment. how to change container environment variables for rolling updates?

14,694

I think your best bet is to use configmaps in k8s and then change you pod template to get env variable values from the configmap see Consuming ConfigMap in pods

edit: I appologize I put the wrong link here. I have updated but for the TL;DR you can do the following.

apiVersion: v1
kind: ConfigMap
metadata:
 name: special-config
namespace: default
data:
 special.how: very
 special.type: charm

and then pod usage can look like this.

apiVersion: v1
kind: Pod
metadata:
 name: dapi-test-pod
spec:
  containers:
  - name: test-container
    image: gcr.io/google_containers/busybox
    command: [ "/bin/sh", "-c", "env" ]
    env:
      - name: SPECIAL_LEVEL_KEY
        valueFrom:
          configMapKeyRef:
            name: special-config
            key: special.how
      - name: SPECIAL_TYPE_KEY
        valueFrom:
          configMapKeyRef:
            name: special-config
            key: special.type
  restartPolicy: Never
Share:
14,694
yogs
Author by

yogs

programmer on earth...

Updated on June 05, 2022

Comments

  • yogs
    yogs almost 2 years

    Below is how I am using kunbernetes on google.

    I have one node application let's say Book-portal.

    node app is using environment variables for configurations.

    Step1: I created docker file and pushed

    gcr.io/<project-id>/book-portal:v1
    

    Step2: deployed with following commands

    kubectl run book-portal --image=gcr.io/<project-id>/book-portal:v1 --port=5555 --env ENV_VAR_KEY1=value1 --env ENV_VAR_KEY2=value2 --env ENV_VAR_KEY3=value3
    

    Step3:

    kubectl expose deployment book-portal --type="LoadBalancer"
    

    Step4: Get public ip with

    kubectl get services book-portal
    

    now assume I added new features and new configurations in next release.

    So to roll out new version v2

    Step1: I created docker file and pushed

    gcr.io/<project-id>/book-portal:v2
    

    Step2: Edit deployment

    kubectl edit deployment book-portal
    
    ---------------yaml---------------
    ...
        spec:
          replicas: 1
          selector:
            matchLabels:
              run: book-portal
          strategy:
            rollingUpdate:
              maxSurge: 1
              maxUnavailable: 1
            type: RollingUpdate
          template:
            metadata:
              creationTimestamp: null
              labels:
                run: book-portal
            spec:
              containers:
              - env:
                - name: ENV_VAR_KEY1
                  value: value1
                - name: ENV_VAR_KEY2
                  value: value2
                - name: ENV_VAR_KEY3
                  value: value3
                image: gcr.io/<project-id>/book-portal:v1
                imagePullPolicy: IfNotPresent
                name: book-portal
    ...
    ----------------------------------
    

    I am successfully able to change

    image:gcr.io/<project-id>/book-portal:v1 
    

    to

    image:gcr.io/<project-id>/book-portal:v2 
    

    But I can not add/change environment variables

              - env:
                - name: ENV_VAR_KEY1
                  value: value1
                - name: ENV_VAR_KEY2
                  value: value2
                - name: ENV_VAR_KEY3
                  value: value3
                - name: ENV_VAR_KEY4
                  value: value4
    
    1. Can anyone guide with what is best practices to pass configurations in node app on kubernetes?
    2. how should I handle environment variable changes during rolling updates?
  • yogs
    yogs almost 8 years
    Thanks for response. can you provide example in context of my question ?
  • ChrisMcKenzie
    ChrisMcKenzie almost 8 years
    I apologize i put the wrong documentation link in here I have updated and also copied there example to here for posterity