Home / How To / Configmaps in Kubernetes

Configmaps in Kubernetes



A ConfigMap is used to store non-confidential data. The information is in key value pairs. Pods can consume ConfigMaps as command line arguments, environment variables, or as configuration files in a volume.

Using a ConfigMap, we can disconnect environment-specific configuration from the container images. This can help us create our portable applications. ConfigMap does not provide encryption, so it is always recommended not to store confidential information in Configmap. Instead of using config, secrets can be used to store confidential information or security keys or passwords.

There are different ways to use ConfigMap data in a Pod:

  1. Command line argument to a container
  2. Environmental variables for a container
  3. Add a file in read-only volume.
  4. Write code to run inside the Pod that uses Kubernetes API to read a ConfigMap.

To learn more about Configmap, visit Kubernete̵

7;s official site here.

In this article we see two examples for accessing data from configmaps. In one example we use configuration maps as environment variables in the pod command and in the other we fill a volume with data stored in a ConfigMap.

Conditions

  1. Kubernetes Cluster with at least 1 worker node.
    Click here to learn how to create a Kubernetes cluster. This guide will help you create a Kubernetes cluster with 1 master and 2 nodes on AWS Ubuntu 18.04 EC2 instances.

What should we do?

  1. Create a Configmap and access its data

Create a Configmap and access its data.

Create a file with the following configuration definition.

vim my-configmap-demo1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap-demo1
data:
  variable_one: one
  variable_two: two

configmap definition

Configmap can be created with the following command.

kubectl create -f my-configmap-demo1.yaml

Get information about the configuration map we created.

kubectl get configmaps
kubectl describe configmaps my-configmap-demo1

create configuration folder

In the screenshot above you can see that we have our data “variable_one” and “variable_two” available in the configuration map we created.

This configuration map can now be accessed in Pod.

To access configuration data as environment variables in podcasts, let’s create a pod definition file with the following content.

vim my-pod-with-configmaps-as-environment-variables.yml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod-configmap-demo
spec:
  containers:
    - name: my-container-configmap-demo
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "echo $(variable_one_env) $(variable_two_env)" ]
      env:
        - name: variable_one_env
          valueFrom:
            configMapKeyRef:
              name: my-configmap-demo1
              key: variable_one
        - name: variable_two_env
          valueFrom:
            configMapKeyRef:
              name: my-configmap-demo1
              key: variable_two
  restartPolicy: Never

pod-definition-to-use-configmap-as-environment-variables

Now we are ready to create a pod that can access data from configmap as environment variables.

kubectl get podss
kubectl create -f my-pod-with-configmaps-as-environment-variables.yml
kubectl get pod

create-pod-to-use-configmap-as-environment-variables

Since the pod has completed its execution and existed because there is no process that can keep the pods alive, we can check its logs to see if configmap was available in the pod or not. To verify this, we have used the echo command in the pod that prints the values ​​of the variables.

kubectl get pods
kubectl logs my-pod-configmap-demo

check-pod-logs-to-see-configmap

In the screenshot above, it can be seen that the configmap data was available in the pod and that they have printed values ​​with the echo command as “one” and “two”.

Configmap data is also available as files in volumes.

Create a pod definition with the following content to access configuration data via volume.

vim my-pod-with-configmaps-added-to-volume.yml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod-configmap-demo2
spec:
  containers:
    - name: my-container-configmap-demo2
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/myconfig/" ]
      volumeMounts:
      - name: my-config-volume
        mountPath: /etc/myconfig
  volumes:
    - name: my-config-volume
      configMap:
        name: my-configmap-demo1
  restartPolicy: Never

pod-definition-to-mount-configmap-as-a-volume

Let’s now create a pod that can have configuration data available as files from the “my-config-volume” volume. This configuration data will be available under the “/ etc / myconfig” directory in the podcast.

kubectl get pods
kubectl create -f my-pod-with-configmaps-added-to-volume.yml
kubectl get pods

create-pod-to-use-configmap-from-volume-as-files

This time, the disc has finished successfully because there is no process to keep the disc alive.

But we have used the command “ls / etc / myconfig” which shows the contents of the directory “/ etc / myconfig”.

kubectl get pods

In the logs we can see that configmap data was available as files under “/ etc / myconfig”

kubectl logs my-pod-configmap-demo2

check-pod-logs-to-see-cofigmap-as-files

In the screenshot above, you can see that configmap data was available in pod as “variable_one” and “variable_two” files.

Conclusion

In this article, we learned how to create a configuration folder and access it in two ways. We saw the steps to access configuration data such as environment variables to the pod command and pod volume files.


Source link