Sobes.tech
Junior

What is a ConfigMap in Kubernetes?

sobes.tech AI

Answer from AI

ConfigMap is an API object used to store non-confidential data in key-value pairs in Kubernetes. It allows separating application configuration from its container image, making applications more portable and simplifying configuration management.

Data in ConfigMap can be used in the following ways:

  • As environment variables in a container.
  • As command-line arguments for the container.
  • As files in a volume mounted into a Pod.

Example of creating a ConfigMap from a file:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  my-config-file.properties: |
    database.url=jdbc:mysql://localhost:3306/mydatabase
    database.username=mydbuser
    database.password=secret

Using ConfigMap in a Pod via environment variables:

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
spec:
  containers:
  - name: my-app
    image: my-app-image
    env:
    - name: DATABASE_URL
      valueFrom:
        configMapKeyRef:
          name: my-config
          key: database.url
    - name: DATABASE_USERNAME
      valueFrom:
        configMapKeyRef:
          name: my-config
          key: database.username

Using ConfigMap in a Pod via mounting as a file:

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
spec:
  containers:
  - name: my-app
    image: my-app-image
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: my-config

Key features:

  • Used for non-confidential data. For confidential data, Secrets should be used.
  • Allows dynamic updating of configuration without rebuilding the image.
  • Data in ConfigMap is limited in size (default 1MB).
  • Some changes in ConfigMap may require restarting the Pod to apply them, depending on the usage method (environment variables require restart, mounting as a file can be dynamic).