Configmaps can be used to store a large or complex amount of environment variables or configuration information for a pod.
2 Steps: Create configMap and then Inject into Pod.
From the command line, create a configMap called app-config with a key called APP_COLOR and set it's value to blue
#kubectl create configmap <configmap-name> \
#--from-literal=<key1>=<value1> \
#--from-literal=<key2>=<value2>
kubectl create configmap app-config \
--from-literal=APP_COLOR=blue \
--from-literal=APP_MOD=prod
#kubectl create configmap <configmap-name> --from-file=<path to file>
kubectl create configmap app-config --from-file=app_config.properties
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_COLOR: blue
APP_MODE: prod
kubectl create -f <app_config>.yaml
kubectl get configmaps
kubectl describe configmaps
kubectl get cm mycm -o jsonpath="{.data}"
{"seal_id":"104387","tier":"dev"}%
You can also retrieve the value for a specific key:
➜ ~ k get cm mycm -o jsonpath="{.data.seal_id}"
104387%
➜ ~ k get cm mycm -o jsonpath="{.data.tier}"
dev%
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_COLOR: blue
APP_MODE: prod
To create a pod utilizing all the environment variables from a config map, utilize the envFrom in the container spec:
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: mmumshad/simple-webapp-color
ports:
- name: http
containerPort: 8080
protocol: TCP
envFrom:
- configMapRef:
name: app-config #name of the configMap
- configMapRef:
name: second-config # name of 2nd configMap
To create a pod utilizing the environment variables from a config map, utilize the envFrom in the container spec:
apiVersion: v1
kind: Pod
metadata:
name: simple-webapp-color
labels:
name: simple-webapp-color
spec:
containers:
- name: simple-webapp-color
image: mmumshad/simple-webapp-color
ports:
- name: http
containerPort: 8080
protocol: TCP
env:
- name: APP_COLOR
valueFrom:
configMapKeyRef:
name: app-config #name of the configMap
key: APP_COLOR
- name: APP_MODE
valueFrom:
configMapKeyRef:
name: app-config
key: APP_MODE
apiVersion: v1
kind: Pod
metadata:
name: simple-web-app
labels:
name: simple-web-app
spec:
containers:
- name: simple-web-app
image: mmumshad/simple-web-app
ports:
- name: http
containerPort: 8080
protocol: TCP
volumeMounts:
- name: config-vol
mountPath: "/etc/config"
readOnly: true
volumes:
- name: config-vol
configMap:
name: app-config
defaultMode: 0440 # Read only for group and id.