在 Kubernetes configMaps 中使用敏感环境变量

Using sensitive environment variables in Kubernetes configMaps

我知道您可以在 pod 规范中使用 ConfigMap 属性作为环境变量,但是您可以在 configmap 中使用在 pods 规范中声明的环境变量吗?

例如:

我有一个秘密密码,我想在我的 configmap application.properties 中访问它。秘密看起来像这样:

apiVersion: v1
data:
  pw: THV3OE9vcXVpYTll==
kind: Secret
metadata:
  name: foo
  namespace: foo-bar
type: Opaque

所以在 pod 规范中,我将秘密引用为环境变量。 configMap 将作为规范中的卷安装:

    env:
      - name: PASSWORD
        valueFrom:
          secretKeyRef:
            name: foo
            key: pw
...

然后在我的 configMap 中,我可以像这样引用秘密值:

apiVersion: v1
kind: ConfigMap
metadata:
  name: application.properties
  namespace: foo-bar
data:
  application.properties: /
    secret.password=$(PASSWORD)

我在网上找到的任何内容都只是将 configMap 值作为环境变量使用,而没有提及在 configMap 值中使用环境变量。

目前这不是 Kubernetes 功能。

有一个已关闭的问题请求此功能,这是一个有争议的话题,因为讨论在关闭后的几个月仍在进行: Reference Secrets from ConfigMap #79224

引用结束评论:

Best practice is to not use secret values in envvars, only as mounted files. if you want to keep all config values in a single object, you can place all the values in a secret object and reference them that way. Referencing secrets via configmaps is a non-goal... it confuses whether things mounting or injecting the config map are mounting confidential values.

我建议您阅读整个线程以了解他的原因,也许可以为您的环境找到另一种方法来获取此变量。


"OK, but this is Real Life, I need to make this work"

那我推荐你这个解决方法:

Import Data to Config Map from Kubernetes Secret

它在容器的入口点用 shell 进行替换。