Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Projects and Workloads

ConfigMaps and Secrets

Keep configuration out of images with ConfigMaps and Secrets: create them with oc, inject as environment variables or files, understand base64, and handle updates safely.

One image should run in dev, test and production. What differs between those places, such as database host, feature flags and passwords, is configuration. OpenShift stores it in two object types: ConfigMaps for ordinary settings and Secrets for sensitive values. Your Pods read them at start, so you never rebuild an image to change a setting.

Creating a ConfigMap

Terminal
oc create configmap web-config \
  --from-literal=LOG_LEVEL=info \
  --from-literal=FEATURE_BANNER=true

oc create configmap web-files --from-file=nginx.conf=./nginx.conf
oc get configmap web-config -o yaml

Or as YAML:

YAML
apiVersion: v1
kind: ConfigMap
metadata:
  name: web-config
data:
  LOG_LEVEL: info
  FEATURE_BANNER: "true"
  app.properties: |
    greeting=Hello
    max.items=20

A ConfigMap value is always a string, so quote "true" and "20". Keys are file-name-like (letters, digits, -, _, .). ConfigMaps are limited to about 1 MiB.

Creating a Secret

Terminal
oc create secret generic db-credentials \
  --from-literal=username=app \
  --from-literal=password='s3cr3t-value'

oc create secret docker-registry regcred \
  --docker-server=registry.example.com \
  --docker-username=me --docker-password='...' \
  --docker-email[email protected]
oc create secret tls web-tls --cert=tls.crt --key=tls.key

A secret's data values are base64-encoded. Base64 is an encoding, not encryption: anyone who can read the Secret can decode it. The protection comes from RBAC (who may get secrets) and from etcd encryption, which OpenShift can enable for the cluster. There is also stringData, which lets you write plain text in YAML and have the API server store it encoded.

YAML
apiVersion: v1
kind: Secret
metadata:
  name: db-credentials
type: Opaque
stringData:
  username: app
  password: change-me

Never commit real Secret manifests to Git. Use sealed or externally managed secrets (for example the External Secrets Operator, Vault or sealed secrets) when you keep manifests in version control. The values above are placeholders.

Secret types include Opaque (default), kubernetes.io/dockerconfigjson (registry credentials), kubernetes.io/tls, kubernetes.io/basic-auth and kubernetes.io/service-account-token.

Using them as environment variables

YAML
containers:
  - name: web
    image: quay.io/example/web:1.0
    env:
      - name: LOG_LEVEL
        valueFrom:
          configMapKeyRef:
            name: web-config
            key: LOG_LEVEL
      - name: DB_PASSWORD
        valueFrom:
          secretKeyRef:
            name: db-credentials
            key: password
    envFrom:
      - configMapRef:
          name: web-config

envFrom imports every key as a variable. The shortcut oc set env does the same without editing YAML:

Terminal
oc set env deployment/web --from=configmap/web-config
oc set env deployment/web --from=secret/db-credentials --prefix=DB_
oc set env deployment/web --list

Using them as files

Mounting gives your app real files, which is right for config files and certificates.

YAML
containers:
  - name: web
    volumeMounts:
      - name: cfg
        mountPath: /etc/web
        readOnly: true
      - name: creds
        mountPath: /var/run/secrets/db
        readOnly: true
volumes:
  - name: cfg
    configMap:
      name: web-files
  - name: creds
    secret:
      secretName: db-credentials
      defaultMode: 0440
Terminal
oc set volume deployment/web --add --name=cfg --type=configmap \
  --configmap-name=web-files --mount-path=/etc/web

What happens when they change?

  • Environment variables are read at container start. Changing the ConfigMap does not change a running container. Restart: oc rollout restart deployment/web.
  • Mounted files are updated by the kubelet after a delay (usually within a minute or two), but your app must re-read the file. Files mounted with subPath never update.
  • Immutable ConfigMaps and Secrets (immutable: true) cannot be edited, which protects against accidents and lowers API server load. To change, create a new one with a new name and update the Deployment.

Good practices

  1. One ConfigMap per concern and per environment; do not bake env names into images.
  2. Put only secret values in Secrets; keep ordinary settings in ConfigMaps.
  3. Give least privilege: few people and service accounts may read Secrets.
  4. Mount secrets as files when you can; environment variables can leak through crash dumps and describe output of child processes.
  5. Rotate credentials, and design apps to reload or restart cleanly.

Stand-in: base64 is not encryption

Secrets use base64 for transport safety with binary data. This stand-in shows how trivial it is to reverse. Use it to explain to colleagues why RBAC matters.

Python Stand-in: base64 encoding and decoding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: resolving env vars from sources

Python Stand-in: build a container environment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A missing key makes the Pod stay in CreateContainerConfigError until you fix it. Check with oc describe pod.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Is base64 in a Secret a form of encryption?

    Base64 is reversible without a key. Protect Secrets with RBAC and etcd encryption.
  2. 2. You change a ConfigMap used through env vars. What must you do for running Pods to see it?

    Environment variables are read at start, so restart the Pods, for example with oc rollout restart.
  3. 3. Which approach is safest for a TLS private key?

    Secrets with restricted RBAC, mounted as files, avoid leaking the key through images or manifests.
  4. 4. What does immutable: true do on a ConfigMap?

    Immutable objects cannot be modified, which prevents accidental edits and reduces watch load.

Summary

  • ConfigMaps hold ordinary settings; Secrets hold sensitive values (base64-encoded, not encrypted).
  • Inject them as env vars (env, envFrom, oc set env) or mount them as files (oc set volume).
  • Env var changes need a restart; mounted files refresh but apps must re-read them.
  • Protect Secrets with RBAC, etcd encryption and external secret tooling; never commit them to Git.

Next: Requests, Limits and Quotas