DevOps Blog

Introduction to Kubernetes ConfigMap

Toye Idowu
by Toye Idowu
3 minute read

In this post, i am going to go over ConfigMaps. I assume you have a basic understanding of Kubernetes and its use cases. It is also important to have a basic understanding of what a pod is. To follow along with the demo example, you will need kubectl and minikube installed.

What is a ConfigMap?

When working with 12 factor apps one of the factor is configs so that mean when dealing with microservices, you need to figure out how the configurations will be applied. In situations where you want to deploy to multiple environments like stage, dev and prod, it is not ideal to bake the configs into the application because of the difference in environments. Ideally you want separate configurations to match the environment you are deploying to. This is where ConfigMaps comes into play. It allows you to decouple configuration artifacts from image content. This allows containerized application to become portable without worrying about configurations. ConfigMap is similar to Secrets, but provides a way of working with strings that don’t contain sensitive information. Users and system components can store configuration data in ConfigMap.

How to create ConfigMap?

Creating ConfigMaps is pretty simple and straightforward. You can create it with directories, file or literal values. Let us see each in action. The rest of the blog post will use a simple example to demonstrate how to work with a configmap.

From a directory

To create configmap from a directory, we must create or have an existing directory with our configs in there.

$ mkdir configmap-demo 

wget the configs into our configmap-demo directory.

$ wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/game.properties -O configmap-demo
$ wget https://k8s.io/docs/tasks/configure-pod-container/configmap/kubectl/ui.properties -O configmap-demo

What we just did was download test config files into our directory. We can then create a configmap from our directory that will have all the files we downloaded.

$ kubectl create configmap demo-configmap --from-file=configmap-demo
configmap "demo-configmap" created

If we describe our configmap, we will see both files as data entries with their contents.

$ kubectl describe configmap demo-configmap
Name:         demo-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

Events:  <none>

From a file

Creating from a file is very similar to creating from directory. All we need to do is pass in the name of the file to –from-file argument. When creating configmap this way, you can pass in as many files as you want to the –from-file argument and it will add it to the configmap.

From literal value

Creating configmap this way mean you can specify your configuration directly from command line without creating any file or directory. For example “kubectl create configmap special-config –from-literal=special.how=very –from-literal=special.type=charm”. You can have multiple key-value pairs if needed.

Using configmap in a pod

Lets create a test configmap

$ kubectl create configmap special-config --from-literal=special.how=very
configmap "special-config" created

Then create a pod to use the configmap as an env variable.

demo-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config

              # Specify the key associated with the value
              key: special.how
  restartPolicy: Never
$ Kubectl create -f demo-pod.yaml
pod "configmap-demo-pod" created

We can now see that there is an environment variable set in the pod with our defined configmap value.

$ kubectl logs configmap-demo-pod | grep SPECIAL_LEVEL_KEY
SPECIAL_LEVEL_KEY=very

E-Book: Avoid Sticker Shock—How to Determine the True Cost of Clouds

Cost reduction is one of the main reasons for moving to the cloud. Cost reduction is not a guarantee – but is achievable with the right plan. Get insight into the right steps to take for migrating workloads to the cloud and reducing costs as a result.
Read the E-Book ›

These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.

See an error or have a suggestion? Please let us know by emailing blogs@bmc.com.

About the author

Toye Idowu

Toye Idowu

Olatoye is a Certified Kubernetes Administrator and experienced DevOps/Platform engineering Consultant with a demonstrated history of working in the Information Technology and Services industry.