Skip to content Skip to sidebar Skip to footer

How To Provide Env Variable From K8s To A Python App During The Docker Run

I have a docker file in which I am hardcoding the env variables for now as it gets injected in the app during the build process. Now, I want to inject those during the runtime when

Solution 1:

First Create k8s configmap or k8s secret(better for sensitive data) in your cluster. Then read these values in k8s deployment yaml as env variables for pod.

official Docs: https://kubernetes.io/docs/concepts/configuration/secret/

Eg. Create secret yaml

apiVersion:v1kind:Secretmetadata:name:mysecrettype:Opaquedata:username:"abc-user"password:"pwd-here"

Deployment yaml

apiVersion:v1kind:Podmetadata:name:secret-env-podspec:containers:-name:mycontainerimage:redisenv:-name:DB_USERNAMEvalueFrom:secretKeyRef:name:mysecretkey:username-name:DB_PASSWORDvalueFrom:secretKeyRef:name:mysecretkey:passwordrestartPolicy:Never

Solution 2:

You can pass env variables to k8s pod with pod spec field: env.

Look a the following example from k8s docs:

apiVersion:v1kind:Podmetadata:name:envar-demolabels:purpose:demonstrate-envarsspec:containers:-name:envar-demo-containerimage:gcr.io/google-samples/node-hello:1.0env:-name:DEMO_GREETINGvalue:"Hello from the environment"-name:DEMO_FAREWELLvalue:"Such a sweet sorrow"

Also take a look at k8s documentaion for more information:

Post a Comment for "How To Provide Env Variable From K8s To A Python App During The Docker Run"