liveBook · Manning

Configmap

As explained in Kubernetes for Developers

A ConfigMap is a Kubernetes object that stores non-confidential configuration data as key-value pairs or file contents.

Suppose an application needs a configuration file, but changing that file is the only customization you need. A ConfigMap lets you define the file in a Kubernetes manifest and present it to a container through a mounted volume, rather than building a custom container image. section 9.1.1 section 9.2.2

A ConfigMap holds configuration, not the application's persistent data. In a Redis StatefulSet, the container reads its configuration from /redis/conf, while a separate persistent volume holds data in /redis/data. An emptyDir has another job: it provides temporary space that containers in a Pod can share. section 9.2.1 section 9.1.1

One file for a Redis Pod

A ConfigMap defines redis.conf with settings such as the port and data directory. The StatefulSet mounts that ConfigMap at /redis/conf, then starts redis-server with /redis/conf/redis.conf as its argument. section 9.2.1

Listing 9.9 Chapter09/9.2.2_StatefulSet_Redis_Multi/redis-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-role-config
data:
  primary.conf: |                          #1
    bind 0.0.0.0
    port 6379
    protected-mode no
    appendonly yes
    dir /redis/data
  replica.conf: |                          #2
    replicaof redis-0.redis-service 6379   #3
    bind 0.0.0.0
    port 6379
    protected-mode no
    appendonly yes
    dir /redis/data
#1 The first file in the config map, to configure the primary role
#2 The second file in the config map, to configure the replica role
#3 Configure the Redis replica to reference the primary Pod by its name

The manifest defines two files in one ConfigMap: primary.conf and replica.conf. The replica file names the primary Pod, giving each Redis role the configuration it needs. section 9.2.2

        command:
       - bash
       - "-c"
       - |
         set -ex
         # Generate server-id from Pod ordinal index.
         [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
         ordinal=${BASH_REMATCH[1]}
         # Copy appropriate config files from config-map to emptyDir.
         mkdir -p /redis/conf/
         if [[ $ordinal -eq 0 ]]; then
           cp /mnt/redis-configmap/primary.conf /redis/conf/redis.conf
         else
           cp /mnt/redis-configmap/replica.conf /redis/conf/redis.conf
         fi

An init container uses the StatefulSet Pod's ordinal to copy the appropriate ConfigMap file into a shared emptyDir as redis.conf: primary configuration for ordinal 0, replica configuration for the others. section 9.2.2

A ConfigMap supplies settings; it does not make Redis replicas coordinate by itself. The role-specific configuration and init-container step turn separate StatefulSet Pods into a primary-and-replica setup. The demonstrated Redis configuration has no authentication, so it needs further consideration before production use. section 9.2.2 section 9.2.1

How Kubernetes for Developers covers it

How deep it goes
Works through direct file mounting and init-container selection of role-specific files, without detailing ConfigMap update behavior.
Examples
Uses Redis manifests with one configuration file, then primary and replica files selected by a Pod ordinal.
What it assumes
Introduces Pod volumes as directories, then draws on StatefulSet ordinals and init containers for the multirole example.
What you can do after
Operate/configure it by mounting Redis configuration in a StatefulSet and selecting role-specific files with an init container.
How it connects
Connects ConfigMaps to Pod volumes, StatefulSets, init containers, emptyDir volumes, and persistent volumes.

Also explained in

Compare how 8 books explain configmap →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.