Kubernetes

How to Create Kubernetes ConfigMaps

Damian Igbe, Phd
Feb. 26, 2022, 6:59 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

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:

  • hard-coded in the app itself
  • passed as an environmental variable

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.

Creating Kubernetes ConfigMaps

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.

To create from the CLI

kubectl create configmap weather --from-literal=Weather=summer

From Manifest file

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

Referencing Kubernetes ConfigMaps  in Containers

There are several ways to reference configmaps  in a pod

  1. Using ConfigMapKeyRef
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.

An Example ConfigMaps

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

ConfigMaps used in Volumes

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 
 

Conclusion

In this tutorial, you learned how to create configMaps. Take note of the following:

  • ConfigMap must be created before being referenced in a Pod specification (unless you mark the ConfigMap as “optional”).  Otherwise:
    •  The Pod won’t start if a referenced ConfigMap  doesn’t exist.
    •  The Pod won’t start if the referenced key doesn’t exist in the ConfigMap
  • ConfigMaps reside in a specific namespace. A ConfigMap can only be referenced by pods residing in the same namespace.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role