This is a tutorial on how to create Kubernetes ConfigMaps, but first, let’s understand what ConfigMaps are.
There are several ways to pass variables to an application:
A major disadvantage of hard-coding is that each time you need to change the values, you have to edit the code. This is not a desirable feature. When stored as environmental variables, you just have to change the values of the environmental variables. This is far better than hard-coded variables.
With containers, environmental variables serve the same purpose of passing variables to the containers. The choice here is either to hard-code them in each container or to have them in a central place accessible to all containers. Kubernetes ConfigMaps and Secret help with storing them in a central place accessible to all containers. Kubernetes ConfigMaps are used for non-sensitive data, while secrets are used for sensitive data.
ConfigMaps can either be created directly from the kubecetl CLI or by first defining a manifest file with the details and then used to create the the ConfigMaps.
kubectl create configmap weather --from-literal=Weather=summer
apiVersion: v1 kind: ConfigMap metadata: name: weathervariable namespace: default data: weather: summer today: hot tomorrow: mild With the manifest file we can do $ kubectl create -f weather.yaml
There are several ways to reference configmaps in a pod
env: - name: seasons valueFrom: configMapKeyRef: name: weathervariable key: weather
2. Using envFrom to define ALL the ConfigMap’s data as Pod environment variables. The key from the ConfigMap becomes the environment variable name in the Pod.
envFrom: ConfigMapRef name: weathervariable
Once the environment variables have been defined, you can use ConfigMap-defined environment variables in the command section of the Pod specification using the $(VAR_NAME) Kubernetes substitution syntax. See Kubernetes documentation for more details.
Let’s use a Kubernetes deployment to illustrate how a ConfigMaps is used.
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: deploy1 spec: replicas: 3 template: metadata: labels: name: Cmdeploy spec: containers: - name: containercm1 image: nginx ports: - containerPort: 80 env: - name: weatherseasons valueFrom: configMapKeyRef: name: weathervariable key: weather - name: weathertoday valueFrom: configMapKeyRef: name: weathervariable key: today $kubctl get pods $kubectl exec deploy1-110334546-6ls17 env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=deploy1-110334546-6ls17 weathertoday=hot weatherseasons=summer KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1 NGINX_SERVICE_HOST=10.0.0.237 NGINX_SERVICE_PORT=80 NGINX_PORT=tcp://10.0.0.237:80 FRONTEND_SERVICE_PORT=80 FRONTEND_PORT=tcp://10.0.0.139:80 FRONTEND_PORT_80_TCP=tcp://10.0.0.139:80 NGINX_PORT_80_TCP_ADDR=10.0.0.237 NGINX_SERVICE_PORT_WEB_FRONTEND=80
First, define the configMaps. We will re-use the configmaps defined above
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: deploy2 spec: replicas: 3 template: metadata: labels: name: Cmdeploy spec: containers: - name: containercm1 image: nginx volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: weathervariable $kubectl exec deploy2-3969493798-93jwx ls /etc/config/ weather today tomorrow
In this tutorial, you learned how to create configMaps. Take note of the following: