Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

k8s
kubernetes
security
configuration

What is a ConfigMap in Kubernetes?

Anjana Shankar

Today, in the world of application development, it's recommended to separate the configuration from the code. Kubernetes' ConfigMaps helps us do this.

Create a ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
# property-like keys; each key maps to a simple value
"license": "XXXX"
"licensedto": "dummy"
# file-like keys
app.properties: |
key1=value1
key2=value2
Create a configmap

Uses of ConfigMap

We can use the ConfigMap for configuration inside a pod in the following ways:

  • Using commands and arguments inside a container.

  • Using environment variables for a container

  • Adding the ConfigMaps as a file in read-only volume, from where the application can read.

  • Using the Kubernetes API in the code running inside the container that can read the configmap.

Example

Here, we load the ConfigMaps values during pod initialization as environment variables.

apiVersion: v1
kind: Pod
metadata:
name: configmap-example-pod
spec:
containers:
- name: example-container
image: alpine
command: ["sleep", "3600"]
env:
# Define the environment variable
- name: LICENSE # Environment Variable name, can be different from configmap key
valueFrom:
configMapKeyRef:
name: example-config # The ConfigMap this value comes from.
key: license # The key to fetch.
Load the configmap values

Explanation

  • Line 10: We declare that the following lines in spec will set the environment of the container.

  • Line 12: We declare the name of the environment variable.

  • Line 14: We tell Kubernetes that the value of the environment variable is coming from the ConfigMap key.

  • Line 15: We provide the name of the ConfigMap declared in the previous example.

  • Line 16: We declare the key whose value is to be assigned to the environment variable.

Mount ConfigMap as a file in read-only volume

Following is an example of how to use values from a ConfigMap at the time of container initialization:

apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
command: ["sleep", "3600"]
volumeMounts:
- name: config
mountPath: "/config"
readOnly: true
volumes:
- name: config
configMap:
name: example-config
Mount the configmap

Explanation

Volumes are set at the pod level, then mounted into containers inside that pod.

  • Line 15: We provide the name of the config volume.

  • Line 17: We assign the name of the ConfigMap that has to be mounted.

The mounted configuration maps are updated automatically when the value in the `ConfigMap` changes.

Conclusion

ConfigMaps are a great mechanism to set up configurations separate from code. The configurations are updated automatically and redeployment is not required.

Note: The above does not hold true if the ConfigMap is marked as immutable.

RELATED TAGS

k8s
kubernetes
security
configuration
RELATED COURSES

View all Courses

Keep Exploring
Related Courses