ConfigMaps and Secrets

Provide configuration to a Node.js container with ConfigMaps and Secrets, and verify when configuration updates take effect.

🎯 Introduction

The Node.js order service is already deployed to Kubernetes. Now it needs configuration: the development environment uses APP_VERSION=v1, which may later change to v2, and the application also needs a private DEMO_TOKEN. Here, APP_VERSION is simply a marker for observing configuration changes, not the actual version of the Node.js application code.

Hard-coding these values means every configuration change requires editing code and rebuilding the image. Can we change deployment configuration without changing the order service image?

Kubernetes provides two resource types: ConfigMaps store ordinary configuration, and Secrets store sensitive configuration. Using the same order service, we will create configuration, pass it to the container, read it in Node.js, and verify when updates take effect.


🧱 Why use ConfigMaps and Secrets?

Start with the distinction:

ResourceStoresExample here
ConfigMapOrdinary configuration, such as version markers and service addressesAPP_VERSION=v1
SecretSensitive configuration, such as passwords and tokensDEMO_TOKEN=local-only (local practice only)

Neither is a Node.js source file or starts containers itself. We will configure the Deployment to provide both values as environment variables when the order container starts.

How does the order service get its configuration?

Same Node.js order service image

The code and image stay the same

ConfigMap

Ordinary configuration

APP_VERSION=v1
Secret

Sensitive configuration

DEMO_TOKEN=••••

The Deployment references the configuration

Inject values into container environment variables when creating a Pod

Node.js reads values through process.env

Read APP_VERSION and check whether DEMO_TOKEN exists

This diagram shows configuration injection, not a network request path.

Keep this relationship in mind: ConfigMap / Secret stores values → Deployment references them → Node.js in a new Pod reads them through process.env.


🛠 Creating and inspecting configuration

Reuse the course Namespace and orders Deployment. Create two configuration resources:

kubectl create configmap orders-config -n course --from-literal=APP_VERSION=v1
kubectl create secret generic orders-secret -n course --from-literal=DEMO_TOKEN=local-only

Simulated output:

configmap/orders-config created
secret/orders-secret created

The first command creates orders-config with key APP_VERSION and value v1. The second creates orders-secret containing the demonstration DEMO_TOKEN.

Confirm that the resources exist without displaying the Secret's contents:

kubectl get configmap orders-config -n course
kubectl get secret orders-secret -n course

Simulated output:

NAME            DATA   AGE
orders-config   1      10s

NAME            TYPE     DATA   AGE
orders-secret   Opaque   1      10s

DATA=1 means each resource contains one item. Creating them only stores the values in Kubernetes; it does not provide them to the order service yet. Next, configure the Deployment to use them.

local-only is a demonstration value. Do not put real passwords or tokens directly in commands: shell history and process arguments may expose them.

🧩 Reading configuration in the order container

1. Reference the configuration in the Deployment

Open the order service's Deployment YAML, find the order container under containers, and add env. This is a partial example from the existing file, not a complete Deployment manifest you can apply directly. Add only env; retain the image, ports, replica count, and other existing settings:

spec:
  template:
    spec:
      containers:
        - name: orders
          # Keep existing fields such as image and ports
          env:
            - name: APP_VERSION
              valueFrom:
                configMapKeyRef:
                  name: orders-config
                  key: APP_VERSION
            - name: DEMO_TOKEN
              valueFrom:
                secretKeyRef:
                  name: orders-secret
                  key: DEMO_TOKEN

If your container has a name other than orders, keep it rather than renaming it to match the example. env belongs at the same level as that container's image and ports, not outside template.

You do not need to memorize every field. Focus on these two mappings:

  • configMapKeyRef: Reads APP_VERSION from orders-config into an environment variable with the same name.
  • secretKeyRef: Reads DEMO_TOKEN from orders-secret into an environment variable with the same name.

Save the existing Deployment YAML, then run the following, adjusting the file path to match your earlier work:

kubectl apply -f deployment.yaml -n course
kubectl rollout status deployment/orders -n course

Simulated output:

deployment.apps/orders configured
deployment "orders" successfully rolled out

configured means the Deployment configuration changed. Once the rollout completes, new Pods use the declared environment variables.

2. Verify directly with Node.js

Leave the Express endpoints unchanged. kubectl exec runs a command inside a running order container from your local terminal. Use node -e to execute a short JavaScript snippet that reads the configuration. Boolean() checks only whether the token has a value, without printing it:

kubectl exec -n course deployment/orders -- \
  node -e 'console.log({
    version: process.env.APP_VERSION,
    hasToken: Boolean(process.env.DEMO_TOKEN)
  })'

Simulated output:

{ version: 'v1', hasToken: true }

This is the key verification result:

  • version: 'v1' means Node.js read the ConfigMap value.
  • hasToken: true means Node.js can read the variable supplied by the Secret. We check only its presence and do not print the token.

Injecting environment variables does not automatically add an APP_VERSION field to Express's /api/info response. The endpoint must explicitly read and return it for a browser to see it. We verify through process.env to avoid changing the existing business endpoint.


🔄 Why does an updated ConfigMap still return the old value?

Suppose you want to change the version marker from v1 to v2. Keep the image and update only the ConfigMap:

kubectl patch configmap orders-config -n course \
  --type merge -p '{"data":{"APP_VERSION":"v2"}}'

Simulated output:

configmap/orders-config patched

After updating the ConfigMap, check the container's current version marker without restarting the order service:

kubectl exec -n course deployment/orders -- \
  node -p 'process.env.APP_VERSION'

Simulated output:

v1

node -p prints an expression's result. Although the ConfigMap now contains v2, the running container still reads v1.

Why did it not immediately become v2? We used environment variable injection: the configuration is read when the Pod starts its container. Updating a ConfigMap does not rewrite a running Node.js process's environment.

Have the Deployment create Pods using the new configuration:

kubectl rollout restart deployment/orders -n course
kubectl rollout status deployment/orders -n course

Simulated output:

deployment.apps/orders restarted
deployment "orders" successfully rolled out

Once the new Pods are ready, run the same check again:

kubectl exec -n course deployment/orders -- \
  node -p 'process.env.APP_VERSION'

Simulated output:

v2

It now returns v2 because the new Pod read the updated ConfigMap at startup.

Remember: updating a ConfigMap changes the stored value. Existing containers using it through environment variables do not update automatically; create new Pods to read the new value.


⚠️ Does a Secret guarantee password security?

No. A Secret separates sensitive configuration from ordinary configuration, but it does not imply default encryption or prevent all users from reading it.

In real projects, never commit real credentials to Git or print them in endpoint responses or logs. Protect Secrets through access controls, encryption at rest, and other measures. Those security settings do not change the core operations here and can be studied later as needed.


❓ Questions and answers

Q: Do ConfigMaps and Secrets modify the Node.js image?

No. We changed Kubernetes configuration and the Deployment; the application image does not need rebuilding.

Q: Why can Node.js not read the ConfigMap I created?

Creating a resource does not inject it into a container. Reference it in the Deployment's env and let new Pods use the updated configuration.

Q: Do Secret updates also require a restart?

When injected as environment variables, as here, running containers do not automatically receive new values either. You normally need to recreate the Pods.


🧪 Exercise

Independently change APP_VERSION in orders-config from v2 to v3. Use the earlier node -p command to compare the value before and after a rolling restart.

Success criteria: Observe the old value before restart and v3 afterward; explain what ConfigMaps and Secrets store and why real tokens must not appear in logs or endpoint responses.


🎉 You can now provide configuration through Kubernetes without rebuilding the Node.js image, and understand when configuration updates take effect.