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: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: "abc-user"
password: "pwd-here"
Deployment yaml
apiVersion: v1
kind: Pod
metadata:
name: secret-env-pod
spec:
containers:
- name: mycontainer
image: redis
env:
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
restartPolicy: 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: v1
kind: Pod
metadata:
name: envar-demo
labels:
purpose: demonstrate-envars
spec:
containers:
- name: envar-demo-container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: DEMO_GREETING
value: "Hello from the environment"
- name: DEMO_FAREWELL
value: "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"