As a user, when you are running applications in Kubernetes, you may want to pass dynamic values to your applications at runtime to control the way how they behave.
This is known as Application configuration.
Two types of application Configuration are:
With ConfigMaps, one can store Configuration data in Kubernetes . ConfigMaps store data in the form of a key-value map. ConfigMap data can be passed to user’s container applications.
Simple example of a ConfigMap:
Secrets are similar to ConfigMaps but are designed to store sensitive data, such as passwords or API keys, more securely. They are created & used similarly to ConfigMaps.
Simple example of Secrets:
To pass the Application configuration(ConfigMaps & Secrets) to the containers there are two ways:
1) via Environment Variables: User can pass ConfigMap & Secret data to their containers as Environment Variables. These variables will be visible to the Container process at runtime.
2) via Configuration Volumes: Configuration data from ConfigMaps & Secrets can also be passed to containers in the form of mounted volumes. This will cause the config data to appear in files available to the container file system.
Each top-level key in the configuration data will appear as a file containing all keys below that top-level key.
Let us try a hands-on on the above two:
via Environment Variables-
- Create a ConfigMap with the following content:
2. Create the config map & verify the content:
3. Similarly create a Secret
4. Now, create a pod & apply:
Now, if you check the logs for this pod, you will observe the values which were defined in the ConfigMap & Secret:
This proves the case for the ‘Environment Variables’ way of passing Application Configuration data to the containers(Application)
via Configuration Volumes-
1. Create a pod with the following content:
2. Add Volume mount to set the mountPath:
3. Create the Pod & check the config data:
So, this was the second way of passing ConfigMap & Secret to the user Applications.
In the next post, we will understand on how to manage Container resources.