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







