How to Create and Use ConfigMaps in Kubernetes

By Alwyn Botha, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

This tutorial gives you practical experience in creating and using ConfigMaps in Kubernetes Pods. Note the name: ConfigMaps. Let’s look at what that means.

  • Config: keeps configuration information.
  • Maps: In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. Learn more on this Wiki page.

Simply put ConfigMaps store configuration maps/dictionaries of key=value data.

ConfigMaps store plain text values unencrypted — do not use if for secrets. Kubernetes has functionality that deals specifically with secrets, which is covered in the article How to Create and Use Secrets in Kubernetes.

ConfigMap values are frequently referenced using Linux environment variables.

Prerequisites

This tutorial is written using Windows 10, kubectl, and minikube. If you have a similar setup you are good to go.

Important Note for Windows Users

All files you create must have Unix (LF) line endings, not Windows ( CR LF ) endings. Read your editor documentation on how to change it to default to LF line endings. There should also be some EOL conversion menu option somewhere to convert to LF.

If you are using a server then you need a running Kubernetes cluster, and your kubectl command-line tool must be able to access your cluster. If you run kubectl get nodes and it shows a list of nodes — even just one — you are good to go.

You need about 2 weeks of basic beginner exposure to Kubernetes to follow these tutorials.

If you have zero Kubernetes experience I suggest you spend a few days reading at https://kubernetes.io/docs/concepts/ Unfortunately these are not step by step how-to tutorials, mostly in-depth theory with snippets assuming you know how to apply it. The https://kubernetes.io/docs/tasks/ are step by step tutorials, but you need to be quite familiar with the theory and concepts.

You need not be a theory expert to follow this tutorial, we will walk you thorough some practical experience of applying Kubernetes.

Basic Example: Create ConfigMaps from a file

You should speed-read rush through the first few basic examples. Its very easy to do and understand.

Create file that will be the input to our ConfigMap.

Create the ConfigMap using our file just created.

Find out what Kubernetes knows about our newly created ConfigMap via kubectl describe .

Our keys and values are shown at the bottom. ConfigMap successfully created.

This part of the tutorial focus on creating ConfigMaps — we can now delete it since ALL we wanted to learn was how to create a ConfigMap .

Create a ConfigMap with comments and blank lines in it.

Create the ConfigMap using our file just created.

Find out what Kubernetes knows about our newly created ConfigMap via kubectl describe .

Our 2 keys and their values are shown at the bottom.

IMPORTANT : Kubernetes does not have know-how of how to ignore the comments and blank lines if we use — from-file . In section 2 you will see how — from-env-file fixes that automatically.

Alternative way to get Kubernetes to show what it knows about our ConfigMap : kubectl get configmap Note the -o yaml at the end. If you leave that off, all you will get is a simple one-line list of our ConfigMap.

-o yaml specifies the output format to be YAML.

7 lines of metadata removed from kubectl get configmap output throughout this tutorial since it adds no value to the discussions.

Demo complete, delete the ConfigMap.

Basic Example: Create ConfigMaps using — from-env-file

This section shows how to create a ConfigMap using the Docker environment file format.

Create a ConfigMap using the same file from previous example: that file is in Docker environment file format.

Note the — from-env-file versus — from-file we used earlier.

Note that — from-env-file ignores the blank lines and comments perfectly — as it should.

The describe command shows the keys and values perfectly.

Best Practice: have your ConfigMaps in environment file format and use — from-env-file to create your ConfigMaps.

For more information about the Docker environment file format, visit this page. Only the syntax rules at the top are relevant. You just saw Kubernetes interprets those rules perfectly.

Alternative command to show facts about our ConfigMap: kubectl get configmap Note no ugly blank lines or # comments at the top.

Basic Example: Create ConfigMaps from Literal Values

Simple example of how to create ConfigMaps from literal values.

— from-literal= syntax specifies our keys and their literal value.

Find out what Kubernetes knows about our newly created ConfigMap via kubectl describe .

Our keys and their literal values are shown at the bottom. ConfigMap successfully created using literal values.

Demo done, delete it.

Second literal values example.

Use kubectl describe to find out if it got created successfully.

Basic Example: Create ConfigMaps from Directories

You will now see how to create a ConfigMap that combines the content of all files in a directory.

Run the following in your shell:

Note that — from-file=ConfigMap-Folder refers to our FOLDER.

Use kubectl describe to find out if it got created successfully.

It looks OK, but if we use kubectl get configmaps to show the ConfigMap we can see it is not OK.

I edited file-1 and file-2 using Windows line endings. Those r n in the output below should not be there.

Delete wrong ConfigMap.

Use your editor and convert line endings to be Unix format.

Recreate the ConfigMap.

Use kubectl get configmaps to show details. Perfect, n r all gone.

You are now familiar with all the ways of creating ConfigMaps. To access these values via environment variables is slightly more complex.

Access ConfigMap data via container environment variables

We did not delete ConfigMap literal-config-1. We use it here, this is its content:

Create a Pod that references literal-config-1 using configMapKeyRef.

ENV_CITY defines the name we want our environment variable to have.

Its value comes from : valueFrom … literal-config-1

We want the value of the city key to be placed inside ENV_CITY .

command: [ “/bin/sh”, “-c”, “env” ] runs a command env to display a list of our environment variables.

We use kubectl logs configmap-pod-1 to show the log of our pod. ENV_CITY shown near the end.

We successfully passed our city key value into the environment variable ENV_CITY.

You now know how to access ConfigMap data via container environment variables.

Get a list of our Pods. Note its status is completed. It only had to run the command env . The Pod then exited successfully.

Here is the complete output of describe . Too much detail, so I edited it below.

Edited output below:

Explains it all: Pod started and finished in a second. It is terminated, since it completed with exit code 0 = success.

Text below: Pod got initialized and scheduled = true.

It is NOT ready NOW since it terminated.

Demo complete, delete Pod.

Access 2 ConfigMap’s Data via Container Environment Variables

Let’s create another literal ConfigMap so we have 2 ConfigMaps to access in our Pod.

Check its successfully created. Looks good.

Create a Pod that refers to 2 different ConfigMaps.

The ENV_CITY environment variable gets its value from city key in literal-config-1 ConfigMap.

The ENV_SHIPPING environment variable gets its value from shipping key in literal-config-2 ConfigMap.

Check the logs: both our environment variables are shown. Success.

You now know how to access ConfigMap data from several ConfigMaps via container environment variables.

Access All Key-Value Pairs in a ConfigMap as Environment Variables

Show the content of both keys in literal-config-1

To access all the keys in a ConfigMap we just do not refer to any specific key in our spec.

Simple spec extract below : syntax to create environment variables from all keys in ConfigMap literal-config-1

Create the ConfigMap.

Show the Pod’s log. Both our keys were converted into environment variables.

Note that their names are still lower case: exactly as it is in the original literal-config-1 ConfigMap .

You have to make the names upper case in the original raw file if that is what you need.

Access All Key-Value Pairs in a 2 ConfigMaps as Environment Variables

To access all the keys in SEVERAL ConfigMaps we just do not refer to any specific key in our spec.

Create this ConfigMap.

This is the content of our 2 ConfigMaps :

The log of our Pod. Note all the keys from both the ConfigMaps are now live as environment variables.

Clean Up

Get a list of your ConfigMaps :

( Note the age, I created it yesterday when I wrote first part of this tutorial. )

Delete the ConfigMaps … you may have more to delete.

Run kubectl get cm again to check you deleted it all.

Run kubectl get po to get list of Pods.

Delete ones you do not need anymore :

For example :

Note: I use — force — grace-period=0 in these tutorials to immediately stop Pods. Otherwise it will take 30 seconds for Pod to gracefully shut down. Using — force in production is a sure way to cause data corruption.

Reference:https://www.alibabacloud.com/blog/how-to-create-and-use-configmaps-in-kubernetes_594724?spm=a2c41.12821011.0.0

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store