【问题标题】:Is there a way to share a configMap in kubernetes between namespaces?有没有办法在命名空间之间共享 kubernetes 中的 configMap?
【发布时间】:2019-08-26 04:32:17
【问题描述】:

我们将一个命名空间用于开发环境,一个用于暂存环境。在每个命名空间中,我们都有几个 configMaps 和 secrets,但是两个环境之间有很多共享变量,所以我们希望有一个公共文件。

有没有办法让一个基本的 configMap 进入默认命名空间并使用类似的东西来引用它:

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

如果这不可能,除了通过命名空间复制变量之外,没有其他方法吗?

【问题讨论】:

  • 我觉得如果k8s支持就好了。但是到目前为止还没有。所以唯一的方法是在不同的 NS 上创建 dup。

标签: kubernetes namespaces configmap


【解决方案1】:

Kubernetes 1.13 及更早版本

它们不能共享,因为它们不能从其命名空间之外的 pod 访问。资源名称在命名空间内必须是唯一的,但跨命名空间则不能。

解决方法是复制它。

在命名空间之间复制秘密
kubectl get secret <secret-name> --namespace=<source-namespace> --export -o yaml \
  | kubectl apply --namespace=<destination-namespace> -f -
在命名空间之间复制配置映射
kubectl get configmap <configmap-name>  --namespace=<source-namespace> --export -o yaml \
  | kubectl apply --namespace=<destination-namespace> -f -

Kubernetes 1.14+

The --export flag was deprecated in 1.14 而是可以使用以下命令:

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

如果有人仍然认为需要该标志,则export script@zoidbergwill 编写。

【讨论】:

  • 对于那些使用 powershell 的人,这个命令将是: kubectl get secret --namespace=  -o yaml | % {$_.replace("命名空间:","命名空间:")} | kubectl create -f -
【解决方案2】:

请使用以下命令从一个命名空间复制到另一个命名空间

kubectl get configmap &lt;configmap-name&gt; -n &lt;source-namespace&gt; -o yaml | sed 's/namespace: &lt;source-namespace&gt;/namespace: &lt;dest-namespace&gt;/' | kubectl create -f -

kubectl get secret &lt;secret-name&gt; -n &lt;source-namespace&gt; -o yaml | sed 's/namespace: &lt;source-namespace&gt;/namespace: &lt;dest-namespace&gt;/' | kubectl create -f -

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多