Skip to content

ConfigMap volume mounting

ConfigMap volume mounting is a mechanism in Kubernetes used to decouple configuration artifacts from image content to maintain container portability.^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]

This approach allows a single image to be deployed across different environments (such as test and production) by injecting environment-specific configuration at runtime, rather than baking configuration files into the container image.^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]

Implementation

To mount a ConfigMap as a volume, a volume block must be defined in the Pod specification referencing the ConfigMap name, and a volumeMounts block must be added to the container specification to define the mountPath.^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]

YAML Configuration

The following example demonstrates a Deployment configuration that mounts a ConfigMap named dubbo-monitor-cm into the container at the directory /dubbo-monitor-simple/conf.^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]

volumes:
  - name: configmap-volume
    [ConfigMap](<./configmap.md>):
      name: dubbo-monitor-cm
containers:
  - name: dubbo-monitor
    # ... container ports and image details ...
    volumeMounts:
      - name: configmap-volume
        mountPath: /dubbo-monitor-simple/conf

Use Cases

  • Decoupling Configuration: Facilitates the management of configuration files (like dubbo.properties) independently of the application code.^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]
  • Environment Separation: Enables the use of a single application image for multiple environments by swapping the mounted ConfigMap or its data, which helps in scenarios requiring separate test and production configurations.^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]
  • Kubernetes
  • [[Apollo]]
  • [[Configuration management]]

Sources

^[400-devops-06-kubernetes-k8s-paas-06k8sapollo.md]