Kubernetes: Env file to Config Maps and Secrets

In Kubernetes (and OpenShift), environment variables can be managed in the config maps. If the values are sensitive like username, passwords, and private keys, then we can use secrets to store them and pass to the application.

But most the time, at the initial phase and dev environments, what we initially do is keep the configurations in .properties or .env files. But if those files contain a large number of config values, then adding one by one into a .yaml file is painful. But there is a single command that we can use (which is hidden in the documentation).

Create a config map from .env or properties file

For Kubernetes

kubectl create configmap app-configs --from-env-file=application.properties

For OpenShift

oc create configmap app-configs --from-env-file=application.properties

Above 2 commands will create config map named app-configs based on the content in the properties file (.env file) application.properties

You can pass the -n argumet to specify the namespace, otherwise this config map will be created in the default namespace.

Example .env file

PORT=8080
MYSQL_HOST=192.168.1.100
MYSQL_DATABASE=my_db

Generated ConfigMap

Can view the config map using following commands

  • kubectl get configmaps game-config -o yaml
  • oc get configmaps game-config -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: 2020-42-21T20:36:28Z
name: app-configs
namespace: default
resourceVersion: "xxxx"
uid: xxxxxxxxxxxxxxxxxxxxx
data:
PORT: '8080'
MYSQL_HOST: 192.168.1.100
MYSQL_DATABASE: my_db

Official Documentation