liveBook · Manning

Configmap

As explained in Road to Kubernetes

A Kubernetes ConfigMap is a resource that stores non-sensitive configuration data as key-value pairs for use by workloads.

A ConfigMap is a Kubernetes resource for reusable, non-sensitive configuration stored as key-value pairs. A Pod or Deployment can use those values as environment variables or mount them as a read-only volume. This lets you change a container’s configuration without rebuilding its image or hard-coding the same values into multiple manifests. section 8.3.4 section 8.3.5

Replace the default NGINX page

Define a ConfigMap named nginx-configmap whose data includes an index.html key containing the replacement HTML. In the NGINX Deployment, declare a volume that names that ConfigMap, then mount it at /usr/share/nginx/html with readOnly: true. The ConfigMap supplies the page without changing the NGINX image; the container is not meant to edit the mounted configuration. section 8.3.4

Listing 8.8 K8s YAML manifest for an NGINX Deployment with a ConfigMap
apiVersion: apps/v1
kind: Deployment
metadata:
  name: custom-nginx-deploy
spec:
  replicas: 15
  selector:
    matchLabels:
      app: custom-nginx
  template:
    metadata:
      labels:
        app: custom-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        volumeMounts:
        - name: my-configmap-volume
          mountPath: /usr/share/nginx/html
          readOnly: true
      volumes:
      - name: my-configmap-volume
        configMap:
          name: nginx-configmap

The Deployment manifest shows both required pieces of the mount: a volumes entry pointing to nginx-configmap and a matching volumeMounts entry in the container. section 8.3.4

For environment variables, choose how much of a ConfigMap the container needs:

  • Use valueFrom.configMapKeyRef with a ConfigMap name and key to set one named environment variable from one value. section 8.3.5
  • Use envFrom.configMapRef to load every key in the ConfigMap’s data block as an environment variable. section 8.3.5

Configure two applications

A shared ConfigMap can hold PY_PORT, JS_PORT, and internal Service endpoints for Python and Node.js applications. A Deployment can map PY_PORT to its container’s PORT variable and also import the full ConfigMap. Create the ConfigMap before the Deployments that reference it; the example names its file 1-configmap.yaml for that reason. section 8.5.1

Do not treat a ConfigMap as a place for passwords or API keys. Kubernetes Secrets are intended for sensitive values, although their Base64-encoded values provide only limited protection. section 8.3.5 section 8.6

How Road to Kubernetes covers it

How deep it goes
Works through YAML data, volume declarations and mounts, and individual or whole-ConfigMap environment-variable references.
Examples
Demonstrates an NGINX index.html override and shared port and endpoint settings for Python and Node.js applications.
What it assumes
Builds on containers and environment variables while explaining Kubernetes resource attachment through familiar dotenv files and Docker Compose volumes.
What you can do after
Build it from scratch: define a ConfigMap manifest and reference it from a Deployment as environment variables or a read-only volume.
How it connects
Connects ConfigMaps to Pods, Deployments, environment variables, volumes, and Secrets.

Also explained in

Compare how 8 books explain configmap →

Related concepts

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