Node.js › Docker & Deployment

How do Kubernetes ConfigMaps and Secrets differ, and how are they typically consumed by a Node.js pod?

Intermediate

Answer

A ConfigMap stores non-sensitive configuration data (feature flags, non-secret URLs) as key-value pairs that can be injected into a pod as environment variables or mounted files. A Secret stores similar key-value data but intended for sensitive values (passwords, API keys), base64-encoded (not encrypted by default, though can be encrypted at rest with additional configuration) and with tighter default access controls -- both are typically consumed by a Node.js application identically, simply as environment variables, regardless of which Kubernetes object supplied them.
# Deployment spec referencing both
env:
  - name: FEATURE_FLAG
    valueFrom: { configMapKeyRef: { name: app-config, key: featureFlag } }
  - name: DATABASE_PASSWORD
    valueFrom: { secretKeyRef: { name: app-secrets, key: dbPassword } }
Real-world example A Node.js deployment loads its non-sensitive log level and feature flags from a ConfigMap while loading its database password and API keys from a Secret, both surfaced identically to the application as regular environment variables, keeping the Node.js code itself agnostic to which mechanism supplied each value.

Common follow-ups: Why is Kubernetes Secret's default base64 encoding not the same thing as actual encryption?;How would you configure encryption at rest for Kubernetes Secrets in a production cluster?