Environment Variables
As most applications tend to follow the 12 factor config standard, setting environment variables becomes important to configure the applications. In K8s,there is 3 ways of setting them
Environment Variables in pod spec
As part of the pod definition, one can set environment variables that will be made available inside the container (similar to using the -e flag in docker):
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 80
env:
- name: APP_COLOR
value: blue
- name: APP_MODE
value: prodConfigMap
ConfigMap objects can be mounted as environment variables for containers. There are two options:
- Import all values from the
ConfigMap
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: myconfigmap- Import certain values
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 80
env:
- name: APP_COLOR
valueFrom:
configMapRef:
name: myconfigmap
key: colorSecrets
Secret objects can be mounted as environment variables for containers. There are two options:
- Import all values from the
Secret
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 80
envFrom:
- secretRef:
name: mysecret- Import certain values
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: simple-webapp-color
ports:
- containerPort: 80
env:
- name: APP_COLOR
valueFrom:
secretKeyRef:
name: my-secret
key: color