Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

ConfigMaps | Kubernetes

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

A ConfigMap allows you to decouple environment-specific configuration from your container images so that your applications are easily portable.

Create Configmap and Inject it into the pods.

Configmap creation

Imperative way -

#Imperative way using (--from-literal)
k create configmap --from-literal==value \
--from-literal==value
k create configmap mysql-config --from-literal=MYSQL_ROOT_HOST=root --from-literal=MYSQL_ROOT_PASSWORD=password

Create a text or JSON file. write down all the environment variables to that file. Suppose, a text file mysql-env.txt with the following data-

MYSQL_ROOT_HOST=root
MYSQL_ROOT_PASSWORD=password

Now, create a configmap named as mysql-config by using mysql-env.txt file.

#Imperative way using (--from-env-file) 
k create configmap --from-env-file=
k create configmap mysql-config --from-env-file=/root/mysql-env.txt

Declarative way -

apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
MYSQL_ROOT_HOST: root
MYSQL_ROOT_PASSWORD: password

Now, Inject the environment variable from configmap.

#Inject entire configmap to the pod
apiVersion: v1
kind: Pod
metadata:
labels:
app: mysql-db
name: mysql-db
spec:
containers:
- image: mysql
name: mysql-db
envFrom:
- configMapRef:
name: mysql-config

Inject only the necessary environment variable from one or more configmaps.

#Inject only the necessary environment varible from a configmap to the pod
apiVersion: v1
kind: Pod
metadata:
labels:
app: mysql-db
name: mysql-db
spec:
containers:
- image: mysql
name: mysql-db
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-config
key: MYSQL_ROOT_PASSWORD

Next, Inject environment variables from multiple configmaps. For that, create another text file app-env.txt with the following data-

APP_STATE=stable
APP_MODE=prod

And then, create another configmap named as app-config by using app-env.txt file.

k create configmap app-config --from-env-file=app-env.txt

#Inject environment varible from multiple configmaps to the pod
apiVersion: v1
kind: Pod
metadata:
labels:
app: mysql-db
name: mysql-db
spec:
containers:
- image: mysql
name: mysql-db
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-config
key: MYSQL_ROOT_PASSWORD
      - name: APP_STATE
valueFrom:
configMapKeyRef:
name: app-config
key: APP_STATE

Importing files into the pods by populating configmaps and then mounting the volume to the pod

Create a new directory config-files with multiple files. e.g: user-info.txt and admin-info.txt

root/config-files/
|- user-data.txt
|- admin-info.txt

Create user-data.txt with the following data-

city: Nobeoka
state: Miyazaki
country: Japan

Create admin-info.txt with the following data-

username: superuser
password: admin123

Subsequently, Create a new configmap user-configusing --from-file option with/root/config-files directory

k create configmap user-config \
--from-file= or
#Configmap with directory
k create configmap user-config --from-file=/root/config-files
#Configmap with a single-file
k create configmap test-config --from-file=user-data.txt

After creating user-config configmap, The definition file will be looks like this-

k get configmap user-config -o yaml

apiVersion: v1
data:
admin-info.txt: |
username: superuser
password: admin123
user-data.txt: |
city: Nobeoka
state: Miyazaki
country: Japan

kind: ConfigMap
metadata:
creationTimestamp: "2022-08-07T09:38:22Z"
name: user-config
namespace: default
resourceVersion: "2007"
uid: 915e805a-cb55-4309-977a-566b7a8ed6ac

As of now, a new configmap is created with a directory which contains several files. From now on,user-config configmap can be imported into the pods using volume mounts.

#Pod-definition with configmap mounted as a volume into the pod
apiVersion: v1
kind: Pod
metadata:
name: web-server
spec:
containers:
- name: wordpress
image: wordpress
volumeMounts:
- name: config-volume
mountPath: /etc/config
#Directory where files will be located
volumes:
- name: config-volume
configMap:
name: user-config

Use k exec web-server -it bash command to enter into the pod and check if user-data.txt and admin-info.txtfile located at/etc/config location.

Alternatively, k exec web-server -- ls /etc/config

If there are multiple files populated into the configmap. There is a way of importing only the necessary ones into the pods.

#Pod-definition with configmap. Importing only the necessary files into the pods. e.g: user-data.txt

apiVersion: v1
kind: Pod
metadata:
name: web-server
spec:
containers:
- name: wordpress
image: wordpress
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: user-config
items:
- key: user-data.txt # filename on configmaps
path: user-info.txt # filename on pods

References:

  • Configure a Pod to Use a ConfigMap
  • Certified Kubernetes Administrator (CKA) Training| KodeKloud

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

🚀Developers: Learn and grow by keeping up with what matters, JOIN FAUN.


ConfigMaps | Kubernetes was originally published in FAUN Publication on Medium, where people are continuing the conversation by highlighting and responding to this story.

Share the post

ConfigMaps | Kubernetes

×

Subscribe to Top Digital Transformation Strategies For Business Development: How To Effectively Grow Your Business In The Digital Age

Get updates delivered right to your inbox!

Thank you for your subscription

×