Kubernetes environment file

For all those who used docker-compose and similar tools, sometimes having environment files is crucial to keep docker-compose.yaml file organized and to make it easier to use and understand. When you move from docker-compose to Kubernetes you will probably search for this kind of feature. Prior to Kubernetes version 1.6, it was not possible to add environment file as a ConfigMap and just use it for container specs when needed. It is really useful when you have a lot of properties for your app and especially when you want to use it for multiple apps. So, instead of specifying each environment variable individually we can reference the whole ConfigMap.

Defining environment file

As an example I will use Hadoop properties from a project I worked on recently. I had a lot containers and they all shared the same properties. Here is ConfigMap:

Then we need to create it like any other Kubernetes resource from yaml file:

Using environment file

When environment file is defined we could just reference it for any container in the same namespace. Here as an example I will use namenode deployment:

Also, if you have some special environment variables you want to use just for this particular container, you could add them together with ConfigMap:

I hope you enjoyed this short blog post. Please leave a comment if something is not clear enough or you need some help. And don’t forget to share 🙂

Follow me

Alen Komljen

Building and automating infrastructure with Docker, Kubernetes, kops, Helm, Rancher, Terraform, Ansible, SaltStack, Jenkins, AWS, GKE and many others.
Follow me

Latest posts by Alen Komljen (see all)

Alen KomljenKubernetes environment file