Sunday, May 20, 2018

Kubernetes - Config Map

While working with application running inside containers, there will be always be a need for the config file. Application that need to connect to backends and other config values are always sent as a configuration files. When we have our application in a container , the dependencies and configurations are also need to be added as a part of image.

Kubernetes provides us a different way of passing configuration by using configMaps. Configmap is one of two ways to provide configurations to your application. ConfigMaps is a simple key/value store, which can store simple values to files.

ConfigMaps is a way to decouple the application-specific artifacts from the container image, thereby enabling better portability and externalization. lets create a configMap with the configuration data,
[root@manja17-I13330 kubenetes-config]# cat configMap.yml
apiVersion: v1
kind: ConfigMap
metadata:
 name: testing-service-staging
 labels:
   name: testing-service-staging
data:
 config: |-
   ---
   :verbose: true
   :environment: test
   :logfile: log/sidekiq.log
   :concurrency: 20
   :queues:
     - [default, 1]
   :dynamic: true

   :timeout: 300

I created a config Map which includes my configuration for the application.
[root@manja17-I13330 kubenetes-config]# kubectl create -f configMap.yml
configmap "testing-service-staging" created

[root@manja17-I13330 kubenetes-config]# kubectl get configmaps
NAME                                   DATA AGE
testing-service-staging    1 4s

Now create a pod with the config map as,
[root@manja17-I13330 kubenetes-config]# cat configmap-file-mount.yml
apiVersion: v1
kind: Pod
metadata:
 name: testing-service
spec:
 containers:
 - name: testing
   image: docker.io/jagadesh1982/testing-service
   volumeMounts:
     - mountPath: /etc/sidekiq/config
       name: testing-service-staging
 volumes:
   - name: testing-service-staging
     configMap:
        name: testing-service-staging
        items:
         - key: config
           path: testing-stage.yml

We can see that the config map is even mounted as a volume. In the above case we created a config Map with the one that we created earlier as"testing-service-staging" and then mounted it on /etc/sidekiq/config. the file that we created is testing-stage.yml with the config data available in the config map that we created earlier.

Now access the configuration file that we mounted using as,
[root@manja17-I13330 kubenetes-config]# kubectl exec testing-service -c testing -it bash
root@testing-service:/usr/src/app# cd /etc/sidekiq/
root@testing-service:/etc/sidekiq# ls
config
root@testing-service:/etc/sidekiq# cat config/
cat: config/: Is a directory
root@testing-service:/etc/sidekiq# cd config/
root@testing-service:/etc/sidekiq/config# ls
testing-stage.yml
root@testing-service:/etc/sidekiq/config# cat testing-stage.yml
---
:verbose: true
:environment: test
:logfile: log/sidekiq.log
:concurrency: 20
:queues:
 - [default, 1]
:dynamic: true
:timeout: 300root@testing-service:/etc/sidekiq/config# exit
exit

We can see that when we access the config file testing-stage.yml file we have the same config data from the configmap that we created. More to Come , Happy Learning :-)


No comments :

Post a Comment