Config map
A ConfigMap is a dictionary of configuration settings. This dictionary consists of key-value pairs of strings. Kubernetes provides these values to your containers. Like with other dictionaries (maps, hashes, ...) the key lets you get and set the configuration value.
Use a ConfigMap to keep your application code separate from your configuration
It is an important part of creating a Twelve-Factor Application.
This lets you change easily configuration depending on the environment (development, production, testing) and to dynamically change the configuration at runtime
A ConfigMap stores configuration settings for your code. Store connection strings, public credentials, hostnames, and URLs in your ConfigMap.
create a configmap in YAML and mount a configmap as a Volume
Create that ConfigMap like any other Kubernetes resources using `kubectl apply -f $file.yaml`. After that, you mount the ConfigMap as a Volume in your Pod's YAML specification.
Lets create config map yaml file config-map-yaml
# cat config-map.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: example-configmap
data:
# Configuration values can be set as key-value properties
database: mongodb
database_uri: mongodb://localhost:27017
# Or set as complete file contents (even JSON!)
keys: |
image.public.key=771
rsa.public.key=42
save and exit the yaml file
Create the ConfigMap using the command # kubectl apply -f config-map.yaml
# kubectl apply -f config-map.yaml
configmap/example-configmap created
Mount the configmap through a volume
# cat pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: pod-using-configmap
spec:
# Add the ConfigMap as a volume to the Pod
volumes:
# `name` here must match the name
# specified in the volume mount
- name: example-configmap-volume
# Populate the volume with config map data
configMap:
# `name` here must match the name
# specified in the ConfigMap's YAML
name: example-configmap
containers:
- name: container-configmap
image: nginx:1.7.9
# Mount the volume that contains the configuration data
# into your container filesystem
volumeMounts:
# `name` here must match the name
# from the volumes section of this pod
- name: example-configmap-volume
mountPath: /etc/config
save and exit the file.
Attach to the created Pod using `kubectl exec -it pod-using-configmap sh`. Then run `ls /etc/config` and you can see each key from the ConfigMap added as a file in the directory. Use `cat` to look at the contents of each file and you’ll see the values from the Configmap
Using a configmap with environment variables and envform
we can consume a ConfigMap via environment variables in a running container using the `envFrom` property.
Set the `envFrom` key in each container to an object containing the list of ConfigMaps you want to include.
# cat pod-env-var.yaml
kind: Pod
apiVersion: v1
metadata:
name: pod-env-var
spec:
containers:
- name: env-var-configmap
image: nginx:1.7.9
envFrom:
- configMapRef:
name: example-configmap
save and exit the file
Attach to the created Pod using `kubectl exec -it pod-env-var sh`. Then run `env` and see that each key from the ConfigMap is now available as an environment variable
what are other ways to create and use configmap
There are three other ways to create ConfigMaps using the `kubectl create configmap` command
$ use the contents of an entire directory with kubectl create configmap my-config --from-file=./my/dir/path/
$use the contents of a file or specific set of files with kubectl create configmap my-config --from-file=./my/file.txt
$use literal key-value pairs defined on the command line with kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2
we can get more information about this command using kubectl to create configmap --help
Relevant Blogs:
Stateless application deployment in kubernetes cluster
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post