>_ IzzyLab

Hands-on task

This task is step 6 of the course “Kubernetes: manifests from Namespace to RBAC”

k8s: env from ConfigMap and Secret

medium~13 min

The Kubernetes cluster in this sandbox (kubectl is already configured) already has a ConfigMap app-config and a Secret app-secret. /root/pod.yaml holds a Pod app (nginx:1.27-alpine) with no environment variables. Open the file in the Editor tab and pull in the whole configuration from the ConfigMap app-config and the Secret app-secret via envFrom. Then create the pod in the cluster from this file and make sure the variables from both objects are visible inside the container.

What you'll practise:
  • kubernetes
  • configmap
  • secret
  • envfrom
  • environment variables
  • kubectl apply
  • kubectl exec

Topic breakdown

In Kubernetes, configuration is kept out of the image in separate objects: a ConfigMap for regular values and a Secret for sensitive ones. You can pass them into a container two ways: env sets variables one by one by name, while envFrom pulls in ALL of an object's keys AT ONCE as environment variables. envFrom is a list of sources, where each element references a ConfigMap (configMapRef) or a Secret (secretRef) by name. This field lives in a specific container's definition. Environment variables are read when the container starts: if you change the ConfigMap later, an already running pod won't see it, and the envFrom field of an existing pod can't be changed at all — the pod is recreated.

What's checked

  1. manifest is valid (kubeconform)
  2. envFrom from ConfigMap app-config
  3. envFrom from Secret app-secret
  4. the app pod sees a variable from the ConfigMap
  5. the app pod sees a variable from the Secret

What it looks like