# Configure a Pod to Use a ConfigMap

Configure a Pod to Use a ConfigMapMany applications rely on configuration which is used during either application initialization or runtime. Most times, there is a requirement to adjust values assigned to configuration parameters. ConfigMaps are a Kubernetes mechanism that let you inject configuration data into application pods.The ConfigMap concept allow you to decouple configuration artifacts from image content to keep containerized applications portable. For example, you can download and run the same container image to spin up containers for the purposes of local development, system test, or running a live end-user workload.This page provides a series of usage examples demonstrating how to create ConfigMaps and configure Pods using data stored in ConfigMaps.Before you beginYou need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:iximiuz LabsKillercodaKodeKloudPlay with KubernetesYou need to have the wget tool installed. If you have a different tool such as curl, and you do not have wget, you will need to adapt the step that downloads example data.Create a ConfigMapYou can use either kubectl create configmap or a ConfigMap generator in kustomization.yaml to create a ConfigMap.Create a ConfigMap using kubectl create configmapUse the kubectl create configmap command to create ConfigMaps from directories, files, or literal values:kubectl create configmap <map-name> <data-source> where <map-name> is the name you want to assign to the ConfigMap and <data-source> is the directory, file, or literal value to draw the data from. The name of a ConfigMap object must be a valid DNS subdomain name.When you are creating a ConfigMap based on a file, the key in the <data-source> defaults to the basename of the file, and the value defaults to the file content.You can use kubectl describe or kubectl get to retrieve information about a ConfigMap.Create a ConfigMap from a directoryYou can use kubectl create configmap to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose filename is a valid key in the directory and packages each of those files into the new ConfigMap. Any directory entries except regular files are ignored (for example: subdirectories, symlinks, devices, pipes, and more).Note:Each filename being used for ConfigMap creation must consist of only acceptable characters, which are: letters (A to Z and a to z), digits (0 to 9), '-', '_', or '.'. If you use kubectl create configmap with a directory where any of the file names contains an unacceptable character, the kubectl command may fail.The kubectl command does not print an error when it encounters an invalid filename.Create the local directory:mkdir -p configure-pod-container/configmap/ Now, download the sample configuration and create the ConfigMap:# Download the sample files into `configure-pod-container/configmap/` directory wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties # Create the ConfigMap kubectl create configmap game-config --from-file=configure-pod-container/configmap/ The above command packages each file, in this case, game.properties and ui.properties in the configure-pod-container/configmap/ directory into the game-config ConfigMap. You can display details of the ConfigMap using the following command:kubectl describe configmaps game-config The output is similar to this:Name: game-config 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 The game.properties and ui.properties files in the configure-pod-container/configmap/ directory are represented in the data section of the ConfigMap.kubectl get configmaps game-config -o yaml The output is similar to this:apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: 2022-02-18T18:52:05Z name: game-config namespace: default resourceVersion: "516" uid: b4952dc3-d670-11e5-8cd0-68f728db1985 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 Create ConfigMaps from filesYou can use kubectl create configmap to create a ConfigMap from an individual file, or from multiple files.For example,kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties would produce the following ConfigMap:kubectl describe configmaps game-config-2 where the output is similar to this:Name: game-config-2 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 You can pass in the --from-file argument multiple times to create a ConfigMap from multiple data sources.kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties You can display details of the game-config-2 ConfigMap using the following command:kubectl describe configmaps game-config-2 The output is similar to this:Name: game-config-2 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 Use the option --from-env-file to create a ConfigMap from an env-file, for example:# Env-files contain a list of environment variables. # These syntax rules apply: # Each line in an env file has to be in VAR=VAL format. # Lines beginning with # (i.e. comments) are ignored. # Blank lines are ignored. # There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)). # Download the sample files into `configure-pod-container/configmap/` directory wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties # The env-file `game-env-file.properties` looks like below cat configure-pod-container/configmap/game-env-file.properties enemies=aliens lives=3 allowed="true" # This comment and the empty line above it are ignored kubectl create configmap game-config-env-file \ --from-env-file=configure-pod-container/configmap/game-env-file.properties would produce a ConfigMap. View the ConfigMap:kubectl get configmap game-config-env-file -o yaml the output is similar to:apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: 2019-12-27T18:36:28Z name: game-config-env-file namespace: default resourceVersion: "809965" uid: d9d1ca5b-eb34-11e7-887b-42010a8002b8 data: allowed: '"true"' enemies: aliens lives: "3" Starting with Kubernetes v1.23, kubectl supports the --from-env-file argument to be specified multiple times to create a ConfigMap from multiple data sources.kubectl create configmap config-multi-env-files \ --from-env-file=configure-pod-container/configmap/game-env-file.properties \ --from-env-file=configure-pod-container/configmap/ui-env-file.properties would produce the following ConfigMap:kubectl get configmap config-multi-env-files -o yaml where the output is similar to this:apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: 2019-12-27T18:38:34Z name: config-multi-env-files namespace: default resourceVersion: "810136" uid: 252c4572-eb35-11e7-887b-42010a8002b8 data: allowed: '"true"' color: purple enemies: aliens how: fairlyNice lives: "3" textmode: "true" Define the key to use when creating a ConfigMap from a fileYou can define a key other than the file name to use in the data section of your ConfigMap when using the --from-file argument:kubectl create configmap game-config-3 --from-file=<my-key-name>=<path-to-file> where <my-key-name> is the key you want to use in the ConfigMap and <path-to-file> is the location of the data source file you want the key to represent.For example:kubectl create configmap game-config-3 --from-file=game-special-key=configure-pod-container/configmap/game.properties would produce the following ConfigMap:kubectl get configmaps game-config-3 -o yaml where the output is similar to this:apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: 2022-02-18T18:54:22Z name: game-config-3 namespace: default resourceVersion: "530" uid: 05f8da22-d671-11e5-8cd0-68f728db1985 data: game-special-key: | enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten secret.code.passphrase=UUDDLRLRBABAS secret.code.allowed=true secret.code.lives=30 Create ConfigMaps from literal valuesYou can use kubectl create configmap with the --from-literal argument to define a literal value from the command line:kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm You can pass in multiple key-value pairs. Each pair provided on the command line is represented as a separate entry in the data section of the ConfigMap.kubectl get configmaps special-config -o yaml The output is similar to this:apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: 2022-02-18T19:14:38Z name: special-config namespace: default resourceVersion: "651" uid: dadce046-d673-11e5-8cd0-68f728db1985 data: special.how: very special.type: charm Create a ConfigMap from generatorYou can also create a ConfigMap from generators and then apply it to create the object in the cluster's API server. You should specify the generators in a kustomization.yaml file within a directory.Generate ConfigMaps from filesFor example, to generate a ConfigMap from files configure-pod-container/configmap/game.properties# Create a kustomization.yaml file with ConfigMapGenerator cat <<EOF >./kustomization.yaml configMapGenerator: - name: game-config-4 options: labels: game-config: config-4 files: - configure-pod-container/configmap/game.properties EOF Apply the kustomization directory to create the ConfigMap object:kubectl apply -k . configmap/game-config-4-m9dm2f92bt created You can check that the ConfigMap was created like this:kubectl get configmap NAME DATA AGE game-config-4-m9dm2f92bt 1 37s and also:kubectl describe configmaps/game-config-4-m9dm2f92bt Name: game-config-4-m9dm2f92bt Namespace: default Labels: game-config=config-4 Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.p... 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 Events: <none> Notice that the generated ConfigMap name has a suffix appended by hashing the contents. This ensures that a new ConfigMap is generated each time the content is modified.Define the key to use when generating a ConfigMap from a fileYou can define a key other than the file name to use in the ConfigMap generator. For example, to generate a ConfigMap from files configure-pod-container/configmap/game.properties with the key game-special-key# Create a kustomization.yaml file with ConfigMapGenerator cat <<EOF >./kustomization.yaml configMapGenerator: - name: game-config-5 options: labels: game-config: config-5 files: - game-special-key=configure-pod-container/configmap/game.properties EOF Apply the kustomization directory to create the ConfigMap object.kubectl apply -k . configmap/game-config-5-m67dt67794 created Generate ConfigMaps from literalsThis example shows you how to create a ConfigMap from two literal key/value pairs: special.type=charm and special.how=very, using Kustomize and kubectl. To achieve this, you can specify the ConfigMap generator. Create (or replace) kustomization.yaml so that it has the following contents:--- # kustomization.yaml contents for creating a ConfigMap from literals configMapGenerator: - name: special-config-2 literals: - special.how=very - special.type=charm Apply the kustomization directory to create the ConfigMap object:kubectl apply -k . configmap/special-config-2-c92b5mmcf2 created Interim cleanupBefore proceeding, clean up some of the ConfigMaps you made:kubectl delete configmap special-config kubectl delete configmap env-config kubectl delete configmap -l 'game-config in (config-4,config-5)' Now that you have learned to define ConfigMaps, you can move on to the