ConfigMap

ConfigMaps are Kubernetes objects that allows you to separate configuration data/files from image content to keep containerized applications portable.

ConfigMaps bind configuration files, command-line arguments, environment variables, port numbers, and other configuration artifacts to your Pods containers and system components at run-time.

ConfigMaps are very useful for storing and sharing non-sensitive, unencrypted configuration information.

Like Secrets, you can create configmaps from files and with yaml declaration. We can use configmaps by referring with its name and as a volume.

Create a ConfigMap

You can create configmaps from directories, files, or literal values using kubectl create configmap.

$ cd configs
configs$ cat app.properties 
environment=production
logging=INFO
logs_path=$APP_HOME/logs/
parllel_jobs=3
wait_time=30sec
$ kubectl create configmap app-config \ 
  --from-file configs/app.properties
configmap "app-config" created

which is same as

$ kubectl create configmap app-config \ 
  --from-file configs/
$ kubectl create configmap app-config \ 
  --from-literal environment=production \
  --from-literal logging=INFO
  .......
$ kubectl get configmap/app-config
NAME         DATA      AGE
app-config   1         1m
$ kubectl describe configmap/app-config
Name:         app-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
app.properties:
----
environment=production
logging=INFO
logs_path=$APP_HOME/logs/
parllel_jobs=3
wait_time=30sec

Events:  <none>

Using YAML declaration:

The configmap YAML file will looks like below

kind: ConfigMap
apiVersion: v1
metadata:
  name: app-config
  namespace: default
data:
  app.properties: |
   environment=production
   logging=INFO
   logs_path=$APP_HOME/logs/
   parllel_jobs=3
   wait_time=30sec

Here is the basic nginx configmap for nginx.conf

kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-config
data:
  nginx.conf: |
    events {
    }
    http {
      server {
        listen 80 default_server;
        listen [::]:80 default_server;
        
        index    index.html index.htm index.php;
        
        root /var/www/html;
        server_name _;
        location / {
          try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
          include fastcgi_params;
          fastcgi_param REQUEST_METHOD $request_method;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_pass 127.0.0.1:9000;
        }
      }
    }

 

Using ConfigMap

ConfigMaps can be used to populate individual environment variables as shown in below :

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
    - name: my-app
      image: my-app:latest
      env:
        - name: ENVIRONMENT
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: environment
   - name: LOG_MODE
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: logging
   - name: LOG_PATH
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: logs_path
   - name: THREDS_CLOUNT
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: parllel_jobs

ConfigMaps can also be consumed in volumes.

The most basic way is to populate the volume with files where the key is the filename and the content of the file is the value of the key:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-web
spec:
   volumes:
     - name: nginx-config
       configMap:
         name: nginx-config
   containers:
     - image: nginx:1.7.9
       name: nginx
       ports:
        - containerPort: 443
          name: nginx-https
        - containerPort: 80
          name: nginx-http
       volumeMounts:
         - name: nginx-config
           mountPath: /etc/nginx/nginx.conf
           subPath: nginx.conf
Advertisements