Table of contents
Config-MAP
Before talking about the use of the config-maps in Kubernetes, let's discuss why we required configuration files in any applications. so every application has its own configuration data, normally when you start the development of a new application, you usually start by having the app configured through command-line arguments or we use famous environment variables to inject configuration data in the application. The Kubernetes resource for storing configuration data is called a ConfigMap. You can use config-maps to store configuration data or not you can configure your apps by:
Passing command-line arguments to containers
setting custom environment variables for each container
First let's see how the command-line arguments being used:
Defining the command and arguments in docker
[ENTRYPOINT] : Defines the executable when the container is started
[CMD] : Specifies the arguments that get passes to the ENTRYPOINT.
First create one shell-scripting file
Now you will modify the Dockerfile so it uses the exec version of the ENTRYPOINT instruction and set the default interval to 10 seconds using the CMD instruction
you can now build and push the image to DockerHUB.
# docker build -t dockerimage .
# docker push docker.io/ondemandlabsdoitlive/fortune:args ( now you can test the image by running it locally with docker)
#docker run -idt docker.io/ondemandlabsdoitlive/fortune:args
Configured to generate new fortune every 10 seconds
Now that you're sure your image honors the argument passed to it, let's see how to use it in a pod
vi fortune-pod.yaml ( create one yaml file to create a pod and to pass arguments)
you added the arguments array to the container defination. Try creating this pod now. The values of the array will be passed to the container as coomand-line arguments when it is run. The array notation used in this listing is used when we have one argument or a few. If you have several, you can use following notations for example ( args:
- foo
- bar
- "15'
Setting environment variables for a container
As i said containerized applications often use environment variables as a source of configuration options. kubernetes allows you to specify a custom list of environment variables for each container of a pod
Specifying environment variables in a container definition
Adding a single variable to the environment variable list
As i mentioned you can set environment variable inside the container but not at the pod level.
NOTE: Kuberntes also automatically exposes environment variables for each service in the same name-space. These environment variables are basically auto-injected configuration.
Drawback of hard-coding environment variables:
Having values effectively hardcoded in the pod means you need to have separate pod definations for your pruduction and your development pods. To re-use the same pod defination in multiple environments like DEV, QA, PROD, you should decouple the configuration from the pod descriptor. That is very difficult but luckily you can do by using CONFIG-MAP
Inroduction of Config-MAP
Kubernetes allows separating configuration options into a separate object called COnfig-Map. The application can also read the contents of a CONFIG-MAP directly through the kubernetes REST API endpoint if needed
Regardless of how an app consumes a config-map, having the config in a separate object allows you to keep multiple manifest files for config-maps with the same name, each manifest file for a different environments like (QA, PROUCTION, DEVELOPMENT). Because pods reference the Config-map by name, so that you can use different config in each environment
creating a configmap
Let's inspect the yaml descriptor of the config-map you created by using kubectl get command, as shown in the following screenshot
when you run below command
# kubectl create configmap my-config --from-file=config-file.conf
kubectl looks for the file config-file.conf in the directory you run kubectl in. then it will store the content of the file under the key config-file.conf in the configmap ( the file name is used as the map key)
kubectl create configmap my-config --from-file=customkey=config-file.conf
Above command will store the file's contents under the key customkey and also you can add multiple adds
Creating a configmap from files in a Directory
Instead of importing each file individually, you can even import all files from a file directory
#kubectl create configmap my-config --from-file=/path/to/dir
In this case, kubectl will create an individual map entry for each file in the specified directory
Passing a configMap entry to a container as an environment variable
you defined an environment variable called "INTERVAL" and set its value to whatever is stored in the fortune-config (configMap under the sleep-interval.
Referencing Non-existing ConfigMaps in a pod
you might think what happens if the referenced ConfigMap doesn't exists when you create the pod. Kubernetes schedules the pod normally and tries to run its container, the container starts even if the ConfigMap doesn't exist.
Passing all entries of a CONFIGMAP as environment variables at once.