Is there a way to share a configMap in kubernetes between namespaces?

Daniseijo picture Daniseijo · Apr 4, 2019 · Viewed 12.9k times · Source

We are using one namespace for the develop environment and one for the staging environment. Inside each one of this namespaces we have several configMaps and secrets but there are a lot of share variables between the two environments so we will like to have a common file for those.

Is there a way to have a base configMap into the default namespace and refer to it using something like:

- envFrom:
    - configMapRef:
        name: default.base-config-map

If this is not possible, is there no other way other than duplicate the variables through namespaces?

Answer

Crou picture Crou · Apr 4, 2019

Kubernetes version 1.13 and lower

They cannot be shared, because they cannot be accessed from a pods outside of its namespace. Names of resources need to be unique within a namespace, but not across namespaces.

Workaround it is to copy it over.

Copy secrets between namespaces

$ kubectl get secret <secret-name>  --namespace=<source-namespace> --export -o yaml | kubectl apply --namespace=<destination-namespace> -f -

Copy configmaps between namespaces

$ kubectl get configmap <configmap-name>  --namespace=<source-namespace> --export -o yaml | kubectl apply --namespace=<destination-namespace> -f -

Kubernetes version 1.14 and higher

Flag export have been deprecated in 1.14 Deprecate --export flag from get command #73787 Instead following command can be used:

kubectl get secret <secret-name> --namespace=<source-namespace>  -o yaml | sed 's/namespace: <from-namespace>/namespace: <to-namespace>/' | kubectl create -f -

If someone still see a need for the flag, export script was written by @zoidbergwill that is doing that nicely.