Kubernetes 101 Series – Kubernetes ConfigMaps

0

In this post, let's look at how to create Kubernetes ConfigMaps. And once they are created, we will also look at how to inject them into the pod. The previous post discussed commands and arguments, click here if you are interested to read.

A Kubernetes ConfigMap is an object that can be used to store non-sensitive data in key-value pairs. And then the Pods can consume the Kubernetes ConfigMaps either as environment variables or as configuration files in a volume.

How to create Kubernetes ConfigMaps?

Imperative Method:

There are multiple ways that you can create ConfigMaps using the imperative way if you want a quick and easy way.

The first method is just passing your key-value pair as shown below using the --from-literal parameter.

kubectl create configmap my-configmap --from-literal=nginx_port=80 --from-literal=config_file=/etc/nginx/customconfig

The second way is to create the same config using the --from-file parameter. Create a file called myconfigmapfile with the below content.

nginx_port=80
config_file=/etc/nginx/customconfig

And now we can use the below command to create the ConfigMap.

kubectl create configmap my-configmap --from-file myconfigmapfile

Declarative Method:

This is the preferred method of creating any object in Kubernetes. The YAML file for the same ConfigMap will look like below.

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  config_file: "/etc/nginx/customconfig"
  nginx_port: "80"

As usual, we will create the ConfigMap using the kubectl utility.

kubectl create -f my-configmap.yaml

To list the ConfigMaps, type below. You can also use the describe command to look at the actual key-value pairs.

kubectl get configmaps

Kubernetes ConfigMaps

How to use the ConfigMaps inside the Pods?

There are various ways that you can add the ConfigMaps into your pods. We will see the three different ways below.

All key-value pairs added all at once as environment variables

The yaml file for this scenario will look like below.

apiVersion: v1
kind: Pod
metadata:
  name: my-configmap-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
    envFrom:
    - configMapRef:
        name: my-configmap

Within the container spec, we can call the field envFrom and then in turn call our ConfigMap that we created earlier.

We can deploy the pod using kubectl and also look at the environment variables using the kubectl exec command as seen below.

kubectl create -f my-configmap-pod.yaml
kubectl get pods
kubectl describe pods my-configmap-pod

Kubernetes ConfigMaps

One or more key-value pairs added as environment variables

The yaml for this example is shown below.

apiVersion: v1
kind: Pod
metadata:
  name: my-configmap-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
    env:
    - name: nginx_port
      valueFrom:
        configMapKeyRef:
          name: my-configmap
          key: nginx_port

Here, instead of envFrom, we use env and then use valueFrom and call the keys that we have in the configmap.

kubectl create -f my-configmap-pod.yaml
kubectl get pods
kubectl describe pods my-configmap-pod

The same commands can be used to create the pod and look at the environment variables inside the pod.

Kubernetes ConfigMaps

Add the ConfigMap as a volume inside the pod

The yaml for this example will look like below.

apiVersion: v1
kind: Pod
metadata:
  name: my-configmap-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
    volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: my-configmap

We haven't discussed a whole lot about volumes, which we will later. For now, we can mount a volume using the container using the field volumeMounts and providing the name and mount path.

And we are using the ConfigMap data to populate the contents of that volume as seen above.

This will create a file for each key with its name and the content of that file will the value of that key. We can see how it looks in action using the commands.

kubectl create -f my-configmap-pod.yaml
kubectl get pods
kubectl exec my-configmap-pod -- ls /etc/config/
kubectl exec my-configmap-pod -- cat /etc/config/config_file
kubectl exec my-configmap-pod -- cat /etc/config/nginx_port

Kubernetes ConfigMaps

Well, that's it for this post. In the next post, we will discuss Kubernetes Secrets and why should we use them.

I hope this has been informative and thank you for reading!

Share.

About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

Leave A Reply