Configure ConfigMaps in Applications
- Take me to Video Tutorial
In this section, we will take a look at configuring configmaps in applications
ConfigMaps
- There are 2 phases involved in configuring ConfigMaps.
- First, create the configMaps
- Second, Inject then into the pod.
- There are 2 ways of creating a configmap.
-
The Imperative way
$ kubectl create configmap app-config --from-literal=APP_COLOR=blue --from-literal=APP_MODE=prod $ kubectl create configmap app-config --from-file=app_config.properties (Another way) -
The Declarative way
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_COLOR: blue APP_MODE: prodCreate a config map definition file and run the 'kubectl create` command to deploy it. $ kubectl create -f config-map.yaml
-
View ConfigMaps
-
To view configMaps
$ kubectl get configmaps (or) $ kubectl get cm -
To describe configmaps
$ kubectl describe configmaps
ConfigMap in Pods
-
Inject configmap in pod
apiVersion: v1 kind: Pod metadata: name: simple-webapp-color spec: containers: - name: simple-webapp-color image: simple-webapp-color ports: - containerPort: 8080 envFrom: - configMapRef: name: app-configapiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_COLOR: blue APP_MODE: prod$ kubectl create -f pod-definition.yaml
There are other ways to inject configuration variables into pod
-
You can inject it as a
Single Environment Variable -
You can inject it as a file in a
Volume
K8s Reference Docs
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-files