FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Kubernetes for Beginners — Part 08 (ConfigMaps)

Nethmini Romina
FAUN — Developer Community 🐾
4 min readDec 27, 2020

--

Photo by cottonbro from Pexels

In the last article, we discuss how to use environmental variables in Kubernetes definition files. Now, we will see how to use a separate configuration file to manage environmental data. ConfigMaps are used to store these environmental configuration data in Kubernetes. It is a file in the form of key value pair. After configuring the configMap, you need to inject it into the pod definition in Kubernetes. So, the configuration data in the configMap file will be available as environmental variables in the application hosted inside the container in the pod.

Below is a code snippet copied from the previous article,

apiVersion: v1
kind: Pod
metadata:
name: sample-pod
spec:
containers:
- name: ubuntu-container
image: ubuntu
env:
- name: JAVA_HOME
value: /usr/java

Now will see how to define JAVA_HOME environmental variable in a configMap.

JAVA_HOME /usr/java

Using configMaps is no rocket science. It only consists of 2 steps as,

  1. Configure the configuration file
  2. Inject the config file into the pod definition

STEP 01: Configure the configuration file

There are 3 methods of creating a configmaps file.

Method 01

Using the command kubectl create configmap and passing all the variables in the command line.

Syntax:
kubectl create configmap <config-name> --from-literal=<key>=<value>
Example:
kubectl create configmap my-config --from-literal=JAVA_HOME=/usr/java

In the above example, we created a configmap named my-config with a key-value pair JAVA_HOME=/usr/java. If you want to add more variables, repeat --from-literal option with the key and value.

kubectl create configmap \
my-config --from-literal=JAVA_HOME=/usr/java
--from-literal=URL=http://localhost:8000

But if you have to configure many variables, this method will be a mess. Therefore you can use the method 02 of configuring the configuration file.

Method 02

Input configuration data using a file.

Syntax:
kubectl create configmap <config-name> --from-file=<path_to_file>
Example:
kubectl create configmap my-config --from-file=my_config.properties

Same as in the previous method, in this method using the --from-file option you can add your file with the configurations.

Method 03

The final method is by creating a definition file same as a pod definition. Below is a sample definition file.

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
JAVA_HOME: /usr/java
URL: http://localhost:8000

Now, we have defined a ConfigMap named my-config with two environmental variables, JAVA_HOME and URL. Save the file as config-maps.yaml and execute the command kubectl create -f config-maps.yaml. You can create different configuration files for different purposes as per your need.

Note:

- To view the existing config maps: kubectl get configmaps

- Get the config map description: kubectl describe configmaps

STEP 02: Inject the config file into the pod definition

Now we will see how to use the configured file in a Kubernetes pod. Below we have our sample pod definition. We will see how to inject the config maps file.

apiVersion: v1
kind: Pod
metadata:
name: sample-pod
spec:
containers:
- name: ubuntu-container
image: ubuntu

The property envFrom list is used to define the environmental variables in the pod definition. Each item in the list corresponds to a config map item following the name of the config map created (my-config).

apiVersion: v1
kind: Pod
metadata:
name: sample-pod
spec:
containers:
- name: ubuntu-container
image: ubuntu
envFrom:
- configMapRef:
name: my-config

If you want to import only one environmental variable, you can use the below pod definition pattern.

env: 
- name: JAVA_HOME
valueFrom:
configMapKeyRef:
name: my-config
key: JAVA_HOME

Config Maps also can be added as volumes in Kubernetes.

volumes: 
- name: my-config-volume
configMap:
name: my-config

We will discuss more on this topic with practical examples in coming up articles. In this article, we discussed how to create config maps and how to use them in Kubernetes pods. Keep in touch for more interesting topics on Kubernetes.

Previous article: Kubernetes for Beginners — Part 07 (Environmental variables)

Next article: Kubernetes for Beginners — Part 09 (Kubernetes Secrets)

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

No responses yet

Write a response