Skip to main content

Managing ConfigMaps

1. Creating ConfigMap from Literal

Field Description Example
apiVersion API version to use v1
kind Resource type ConfigMap
metadata.name Name of the ConfigMap app-config
data Key-value pairs of configuration data {"database.url": "jdbc:postgresql://db:5432/myapp"}

Example: Creating ConfigMap from Literal Values

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  database.url: jdbc:postgresql://db:5432/myapp
  log.level: INFO

2. Creating ConfigMap from File

Field Description Example
metadata.name Name of the ConfigMap app-config
data Key-value pairs of configuration data
database.url: "jdbc:postgresql://db:5432/myapp"
log.level: "INFO"

Example: Creating ConfigMap from File

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  database.url: "jdbc:postgresql://db:5432/myapp"
  log.level: "INFO"
Note: The data field can contain any key-value pairs, and values are stored as strings.

3. Creating ConfigMap from Env File

Field Description Example
apiVersion API version to use v1
kind Resource type ConfigMap
metadata.name Name of the ConfigMap app-config
data Key-value pairs from env file LOG_LEVEL=info

Example: Creating ConfigMap from env file

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: info
  DATABASE_URL: "postgresql://localhost:5432/mydb"
Note: The data field in a ConfigMap stores key-value pairs as strings. Values can be referenced in Pod specs using $(VAR_NAME) syntax or via environment variables.

4. Creating ConfigMap from YAML

Field Description Example
apiVersion API version of the ConfigMap resource v1
kind Type of the Kubernetes resource ConfigMap
metadata.name Name of the ConfigMap app-config
data Key-value pairs of configuration data
database.url: "jdbc:postgresql://db:5432/myapp"
log.level: "INFO"

Example: Creating a ConfigMap from YAML

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  database.url: "jdbc:postgresql://db:5432/myapp"
  log.level: "INFO"

5. Listing ConfigMaps

Command Description Notes
kubectl get configmaps List all ConfigMaps in the current namespace Default output format is table
kubectl get configmaps -A List all ConfigMaps across all namespaces Useful for cluster-wide overview
kubectl get configmaps -o yaml Get ConfigMaps in YAML format Shows full resource definition including data
kubectl get configmaps -o json Get ConfigMaps in JSON format Machine-readable output for scripting
kubectl get configmaps --show-labels List ConfigMaps with their labels Helpful for filtering by label

Example: Listing ConfigMaps in Current Namespace

kubectl get configmaps
NAME                DATA   AGE
app-config          2      5d
database-config     1      2w

Example: Listing All ConfigMaps Across Namespaces

kubectl get configmaps -A
NAMESPACE     NAME                DATA   AGE
default       app-config          2      5d
kube-system   kube-proxy          1      3w

6. Describing ConfigMap Details

Field Description Example Value
apiVersion API version of the ConfigMap resource v1
kind Type of the Kubernetes resource ConfigMap
metadata.name Name of the ConfigMap app-config
metadata.namespace Namespace where the ConfigMap resides default
data Key-value pairs stored in the ConfigMap {"config.json": "{\n \"port\": 8080\n}"}

Example: Describe ConfigMap using kubectl

kubectl describe configmap app-config

Example: Get ConfigMap details in YAML format

kubectl get configmap app-config -o yaml

7. Using ConfigMap as Environment Variables

Property Value Description
envFrom configMapRef Reference to a ConfigMap to load all key-value pairs as environment variables
env configMapKeyRef Reference to a specific key in a ConfigMap to set as an environment variable

Example: Using ConfigMap as Environment Variables

apiVersion: v1
kind: Pod
metadata:
  name: configmap-env-pod
spec:
  containers:
    - name: app-container
      image: nginx
      envFrom:
        - configMapRef:
            name: app-config
  restartPolicy: Never

Example: Using Specific ConfigMap Key as Environment Variable

apiVersion: v1
kind: Pod
metadata:
  name: configmap-key-env-pod
spec:
  containers:
    - name: app-container
      image: nginx
      env:
        - name: DATABASE_URL
          valueFrom:
            configMapKeyRef:
              name: app-config
              key: database-url
  restartPolicy: Never
Note: Environment variables derived from ConfigMaps are injected at pod creation time and are not updated if the ConfigMap changes. For updates, consider using volume mounts or envFrom with configMapRef and a sidecar or init container to refresh the environment.

8. Mounting ConfigMap as Volume

Field Description Example
configMap.name Name of the ConfigMap to mount my-config
configMap.items.key Key from the ConfigMap to mount app.properties
configMap.items.path Path in the volume where the file will be mounted config/app.properties
configMap.items.mode Permissions for the mounted file (octal) 0644

Example: Mounting ConfigMap as Volume

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: app-container
    image: nginx
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: my-config
Note: Changes to the ConfigMap will not automatically update mounted volumes. A pod restart is required for updates.

9. Using Specific ConfigMap Keys

Key Type Description Example
data object Contains key-value pairs of configuration data
{
  "database.url": "jdbc:postgresql://db:5432/myapp",
  "log.level": "INFO"
}
binaryData object Stores binary data as base64-encoded strings
{
  "ca.crt": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0t...",
  "client.p12": "TVRJZ..."
}

Example: Mounting Specific Keys

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: app-config
      items:
      - key: database.url
        path: db-url
      - key: log.level
        path: log-level

Example: Using Keys in Environment Variables

apiVersion: v1
kind: Pod
metadata:
  name: env-pod
spec:
  containers:
  - name: app
    image: myapp
    env:
    - name: DB_URL
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: database.url
    - name: LOG_LEVEL
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: log.level

10. Updating ConfigMaps

Method Description Notes
kubectl patch Updates a ConfigMap by applying a JSON patch Useful for targeted updates without replacing the entire ConfigMap
kubectl edit Opens the ConfigMap in an editor for manual changes Changes are applied immediately after saving
kubectl apply -f Applies a ConfigMap from a YAML file Replaces the existing ConfigMap with the new one

Example: Patching a ConfigMap

kubectl patch configmap my-config -p '{"data":{"key2":"new_value"}}'

Example: Editing a ConfigMap

kubectl edit configmap my-config
Note: Changes to ConfigMaps are not automatically propagated to running pods. Pods must be restarted or recreated to pick up the new values.

11. Using Immutable ConfigMaps

Property Value Description
immutable true When set to true, prevents updates to the ConfigMap after creation. This ensures data consistency and is useful for security-sensitive configurations.

Example: Creating an Immutable ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  annotations:
    kubernetes.io/immutable: "true"
data:
  config.json: |
    {
      "database": {
        "host": "localhost",
        "port": 5432
      }
    }
Note: Once a ConfigMap is marked as immutable, any attempt to update it will result in an error. This behavior helps enforce configuration integrity in production environments.

12. Deleting ConfigMaps

Command Description Notes
kubectl delete configmap <configmap-name> Delete a ConfigMap by name Deletes the specified ConfigMap from the current namespace
kubectl delete configmap <configmap-name> -n <namespace> Delete a ConfigMap from a specific namespace Deletes the ConfigMap from the specified namespace
kubectl delete configmap --all Delete all ConfigMaps in the current namespace Deletes all ConfigMaps in the current namespace
kubectl delete configmap -l <label-selector> Delete ConfigMaps matching a label selector Deletes all ConfigMaps that match the provided label selector

Example: Delete a ConfigMap

kubectl delete configmap my-config

Example: Delete a ConfigMap in a specific namespace

kubectl delete configmap my-config -n production
Note: Deleting a ConfigMap does not affect pods that are currently using it. The pods will continue to use the ConfigMap until they are restarted or re-deployed.

Was this article helpful?