Simple Configuration with Kubernetes

The official Kubernetes documentation on using a ConfigMap for managing environment variables is thorough: it provides a way to do it using environment variables and writing some properties to a shared volume.

But it confused me a little when trying to work through the minimal case — I just want a single env var set and accessible to my pod, so here is the most minimal approach:

kubectl create configmap my-config --from-literal=my-env-var=val

That will tell your K8s cluster to create a ConfigMap called my-config with a single environment variable called my-env with a value of val.

To verify all is well run kubectl describe configmaps and you’ll see a Data section with your key name listed. The value is presumably not listed in case it is a secret of some sort, but you’ll note the length of the value is included.

Next you’ll need to reference this in your pod template:

- name: my-project
image: my-image:latest
- name: MY_ENV_VAR
name: my-config
key: my-env-var

This will map the my-env-var value to a environment variable called MY ENV VAR accessible to your container. Lovely!

Of course, you’ll need to reapply your configuration to the cluster for the changes to take effect.

Note that if any of your env vars are secrets, then there is a better way to deal with those.

Like what you read? Give Ciaran Archer a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.