Kubernetes ConfigMap Tutorial with Examples

Kubernetes ConfigMap Tutorial with Examples

A ConfigMap is a dictionary of key-value pairs that store configuration settings for your application. We can mount this configmaps in container as files or volumes or environment variables. Using configmaps we store configuration files in a ConfigMap and we can mount this configuration files into the container. Configmaps and secrets both are similar, both work in the similar way. Difference between secrets and configmaps is, we use secrets for some sensitive data and we use configmaps for non sensitive like configuration files and environment variables. In this article, I am going to demonstrate how to create kubernets configmaps.

How to create kubernets ConfigMaps

we can create configmaps in the same way as we created secrets but the difference is secretes are encrypted and configmaps are not encrypted.

Create ConfigMap From From a file

Create some sample configuration files

I have two configuration files in 'demo' directory. Those are game.properties and ui.properties

cat demo/game.properties

    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30

 

Cat demo/ui.properties

    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice

kubectl create configmap <configmap-name> --from-file=<path to file1> --from-file=<path to file2>

using above command we can create kuberntes configmap from files.

master $ kubectl create configmap myconfigmap --from-file=demo/game.properties --from-file=demo/ui.properties

configmap/myconfigmap created

we can add multiple --from-file argument to above command.

kubectl describe configmaps <configmap-name>

master $ kubectl describe configmaps myconfigmap

Name:         myconfigmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30

ui.properties:
----
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice

Events:  <none>

kubectl get configmap <configmap-name> -o yaml

master $ kubectl get configmap myconfigmap -o yaml

apiVersion: v1
data:
  game.properties: |2
        enemies=aliens
        lives=3
        enemies.cheat=true
        enemies.cheat.level=noGoodRotten
        secret.code.passphrase=UUDDLRLRBABAS
        secret.code.allowed=true
        secret.code.lives=30
  ui.properties: |2
        color.good=purple
        color.bad=yellow
        allow.textmode=true
        how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
  creationTimestamp: "2019-09-13T09:43:15Z"
  name: myconfigmap
  namespace: default
  resourceVersion: "1636"
  selfLink: /api/v1/namespaces/default/configmaps/myconfigmap
  uid: e8e96a2e-d60a-11e9-bab6-0242ac110012

Define the key to use when creating a ConfigMap from a file

we can assign a key to file name when you are creating ConfigMap using the --from-file argument

kubectl create configmap <configmap-name>  --from-file=<my-key-name>=<path-to-file>

master $ kubectl create configmap myconfigmap-2 --from-file=game-key=demo/game.properties

configmap/myconfigmap-2 created

Create ConfigMap From Directroy

kubectl create configmap <configmap-name> --from-file=<path to directory>

using above command we can create configmap from directory, that has configuration files.

master $ kubectl create configmap myconfigmap-3 --from-file=demo

configmap/myconfigmap-3 created

 

master $ kubectl describe configmaps myconfigmap-3

Name:         myconfigmap-3
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30

ui.properties:
----
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice

Events:  <none>

Create ConfigMaps from literal values

kubectl create configmap <configmap-name> --from-literal=<key>=<value> --from-literal=<key>=<value>

using above command we can create configmap from literal values

master $ kubectl create configmap myconfig4 --from-literal=dev.admin=marcony --from-literal=db.admin=antony

configmap/myconfig4 created

master $ kubectl describe configmap myconfig4

Name:         myconfig4
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
db.admin:
----
antony
dev.admin:
----
marcony
Events:  <none>

 

  • create kubernetes configmap from directory
  • create kubernetes configmap from file
  • create kubernetes configmap from literal values
  • how to create kubernetes configmaps
  • what is kubernetes configmaps

Leave a Reply

Your email address will not be published. Required fields are marked *