configMap referenced by Multi deployment and POD - unix1998/technical_notes GitHub Wiki

A single ConfigMap YAML file can be mounted by multiple deployments and pods in Kubernetes. Here's how it works:

Sharing Configuration:

  • A ConfigMap stores key-value pairs of data. This data can be used to configure applications running within your pods.
  • Multiple deployments and pods can reference the same ConfigMap YAML file.
  • Each deployment or pod can access and utilize the entire set of key-value pairs within the ConfigMap, or they can reference specific keys relevant to their needs.

Scenarios:

  • Shared Configuration: If multiple deployments or pods require the same configuration values, mounting the same ConfigMap offers a centralized and efficient way to manage that configuration. Any changes made to the ConfigMap are automatically reflected in all deployments and pods referencing it.

  • Partial Use: Deployments or pods can access specific keys within the ConfigMap that are relevant to their functionality. They don't have to utilize the entire set of key-value pairs. This allows for more granular configuration based on individual deployment or pod needs.

Benefits:

  • Centralized Management: Changes to configuration are made in one place (the ConfigMap) and propagate to all deployments and pods referencing it.
  • Reduced Duplication: Eliminates the need to embed configuration data directly within container images or deployment manifests.
  • Flexibility: Deployments and pods can access different parts of the same ConfigMap based on their requirements.

Example:

# config.yaml (ConfigMap definition)
apiVersion: v1
kind: ConfigMap
metadata:
  name: shared-config
data:
  database_url: "dbuser:dbpassword@localhost:3306/mydb"
  api_key: "your_api_key"

# deployment-1.yaml (references entire ConfigMap)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-1
spec:
  template:
    spec:
      containers:
      - name: my-app
        volumeMounts:
        - name: config-volume
          mountPath: /app/config
      volumes:
      - name: config-volume
        configMap:
          name: shared-config

# deployment-2.yaml (references specific key)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-2
spec:
  template:
    spec:
      containers:
      - name: my-other-app
        env:
        - name: API_KEY
          valueFrom:
            configMapKeyRef:
              name: shared-config
              key: api_key
      volumes: []  # No volume mount needed for single key access

In this example:

  • Both deployment-1 and deployment-2 reference the shared-config ConfigMap.
  • deployment-1 mounts the entire ConfigMap volume, allowing access to all key-value pairs within the pod.
  • deployment-2 only uses an environment variable to reference the specific api_key key within the ConfigMap.

In essence, a single ConfigMap YAML file can be effectively used by multiple deployments and pods, offering a centralized and flexible approach to managing configuration data in your Kubernetes environment.