Kubernetes configmap guide

A ConfigMap is a Kubernetes resource that allows you to store configuration data as key-value pairs. You can use ConfigMaps to separate configuration data from your application code and enable configuration updates without the need for a full application restart. When used, Pods can use it as an environment variable, as a command-line argument, or as a configuration file in a storage volume. The main purpose of ConfigMap is to decouple images from configuration files so as to achieve portability and reusability of images.

ConfigMap does not provide privacy or encryption. If the data you want to store is confidential, please use Secret, or use other third-party tools to keep your data private instead of ConfigMap.

4 ways to create a configmap

before we creating a configmap, introduce some commands to check and get a configmap

// get configmaps
kubectl get cm
// check a configmap
kubectl describe cm cm-name
  • create a configmap by specifing its key/value in command line(–from-literal=key=value)
kubectl create configmap cm1 --from-literal=host=127.0.0.1 --from-literal=port=3306
  • create a configmap from file(–from-file=file)
echo -n 127.0.0.1 > host
echo -n 3306 > port

kubectl create configmap cm2 --from-file=./host --from-file=./port
  • create configmap from key/value paires in file(–from-env-file=file)
# cat env.txt
host=127.0.0.1
port=3306

kubectl create configmap cm3 --from-env-file=env.txt
  • create configmap from yaml file
# vi cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm4
data:
  host: 127.0.0.1
  port: "3306"

kubectl apply -f cm4.yml

3 ways to use a configmap

  • Pass configmap values to a pod by environment
# vi pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm1
spec:
  containers:
  - name: mysql-pod
    image: mysql:5.7
    args: [ "/bin/sh", "-c", "sleep 10000" ]
    envFrom:							
    - configMapRef:
        name: cm1	

kubectl apply -f pod-cm1.yml
kubectl exec pod-cm1 --env
  • Pass configmap values to a pod by volume mount
#vi pod-cm2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm2
spec:
  containers:
  - name: mysql-pod
    image: mysql:5.7
    args: [ "/bin/sh", "-c", "sleep 10000" ]
    volumeMounts:                               
    - name: vol-cm                              
      mountPath: "/etc/mysql"                   
      readOnly: true                           
  volumes:
  - name: vol-cm                                
    configMap:
      name: cm2      

kubectl apply -f pod-cm2.yml
kubectl exec pod-cm2 – cat /etc/mysql/host
kubectl exec pod-cm2 – cat /etc/mysql/port
  • overwrite a config file by using subpath mount
# create a configmap with key index.html, value: index.html
kubectl create configmap nginx-index --from-file=index.html
# vi subpath.yaml
apiVersion: v1
kind: Pod
metadata:
  name: subpath-cm
spec:
  containers:
  - name: c1
    image: nginx:1.17.1-alpine
    volumeMounts:
    - name: nginx-config
      mountPath: /usr/share/nginx/html/index.html
      subPath: index.html		#subpath is the key in configmap

  volumes:
  - name: nginx-config
    configMap:
      name: nginx-index		

Configmap Hot update

Passing configmap by environment will not receive hot update in pod, you need to manually restart your pod to take effect when config changes in configmap.

Passing configmap by volume mount will receive host update in pod, it will take 30 seconds to take effect.

Passing configmap by subpath mount will not receive host udpate in pod, you need to manually restart your pod to take effect.

Leave a Reply

Your email address will not be published. Required fields are marked *