How to create ConfigMap in multiple namespaces?

QuestionsQuestions8 SkillsProKubernetes Create CommandOct, 28 2025
096

To create a ConfigMap in multiple namespaces, you can follow these steps:

  1. Create a ConfigMap YAML file: Define your ConfigMap in a YAML file, for example, configmap.yaml:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-config
    data:
      DATABASE_URL: postgres://user:password@host:port/dbname
  2. Use a script to apply the ConfigMap in multiple namespaces: You can use a shell script to loop through the desired namespaces and create the ConfigMap in each one. Here’s an example script:

    #!/bin/bash
    
    # List of namespaces
    namespaces=("namespace1" "namespace2" "namespace3")
    
    # Loop through each namespace and create the ConfigMap
    for ns in "${namespaces[@]}"; do
        kubectl apply -f configmap.yaml -n "$ns"
    done
  3. Run the script: Make the script executable and run it:

    chmod +x create-configmap.sh
    ./create-configmap.sh

This will create the same ConfigMap in each specified namespace. Make sure to replace namespace1, namespace2, and namespace3 with the actual names of the namespaces where you want to create the ConfigMap.

0 Comments

no data
Be the first to share your comment!