Loading Posts...

How To Configure Kubernetes PODs To Use ConfigMap Data

ConfigMaps can be used to store application configuration artifacts, with the container image and, customize the containerized applications. I thought to write up an article to show, how we can configure Kubernetes PODs to use ConfigMaps data with the containers. Basically, two methods will be discussed in this article.

How To Create A ConfigMaps

First of all, ConfigMap should be created with the parameters. I have created tcKey1= tc-Value-1 and, tcKey2= tc-Value-2, parameters in the ConfigMap, here is the yaml file to create the ConfigMap object .

apiVersion: v1
kind: ConfigMap
metadata:
   name: tc-config-map
data:
   tcKey1: tc-Value-1
   tcKey2: tc-Value-2

Create the ConfigMap object with kubectl command pointing the yaml file, ConfigMap object will be created as below

Configure Kubernetes PODs To Use ConfigMap create configmap

Read More: Kubernetes Error: Unable To Connect To The Server Tcp I/O Timeout

Use ConfigMaps To Store Values As Environment Variables

I have deployed a Kubernetes POD with the name of “tc-configmap-pod“, referencing the created ConfigMap. My “tcKey1” value is passed to the “TC_VAR” environmental variable.

apiVersion: v1
kind: Pod
metadata:
  name: tc-configmap-pod
spec:
  containers:
  - name: tc-container
    image: busybox
    command: ['sh', '-c', "echo $(TC_VAR) && sleep 3600"]
    env:
    - name: TC_VAR
      valueFrom:
        configMapKeyRef:
          name: tc-config-map
          key: tcKey1
Configure Kubernetes PODs To Use ConfigMap env

If you logged in the container shell and echo the “TC_VAR” environmental variable, you can see the value which I passed through the ConfigMap.

Here is the command to access the Container shell

kubectl exec POD_NAME -it --sh
Configure Kubernetes PODs To Use ConfigMap view env

How To Pass ConfigMap Data With A File, And A Mounted Volueme

We can pass the ConfigMap data to the container as a form of a file and a mounted volume. I have created the below yaml file for my POD to mount the ConfigMap, to the “/etc/config” mount location. Volume name would be “tc-config-volume“.

apiVersion: v1
kind: Pod
metadata:
  name: tc-configmap-volume-pod
spec:
  containers:
  - name: tc-app-container
    image: busybox
    command: ['sh', '-c', "echo $(cat /etc/config/tcKey2) && sleep 3600"]
    volumeMounts:
      - name: tc-config-volume
        mountPath: /etc/config
  volumes:
    - name: tc-config-volume
      configMap:
        name: tc-config-map

Configure Kubernetes PODs To Use ConfigMap mount

If you access the container shell and check the mount location, two files should be created as below. I have specified two keys in my ConfigMap and two files were created accordingly. Files contained the specified values in the ConfigMap.

Configure Kubernetes PODs To Use ConfigMap view mount volumes

You can directly view the value you specified in the key, with below command

kubectl exec POD_NAME -it -- cat /etc/config/tcKey1

I hope this article will help someone to understand ConfigMaps objects, which can be used in Kubernetes easily.

For More Information:

Click to rate this post!
[Total: 3 Average: 5]

Aruna Fernando

"Sharing knowledge doesn't put your job at risk - iron sharpen iron" I heard this and it's true.

Get Updates Directly To Your Inbox!

   

Leave a Comment

Loading Posts...