You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在values.yaml的嵌套对象中引用Kubernetes Secret

如何在Helm的values.yaml中引用Kubernetes Secret里的PASSWORD值?

现有配置

values.yaml

test-app:
  properties:
    data:
      password: // how to reference the secret PASSWORD

已创建的Secret(secret.yaml)

apiVersion: v1
kind: Secret
data:
  PASSWORD: xxx

需要明确:values.yaml本身是静态配置文件,无法直接引用Kubernetes Secret的内容,必须结合Helm模板或Kubernetes资源配置实现,以下是两种安全且常用的方案:

方式一:通过环境变量注入Secret值

如果应用支持从环境变量读取密码,可在Deployment模板中配置环境变量引用Secret,values.yaml仅做说明标记:

修改后的values.yaml

test-app:
  properties:
    data:
      password: "从环境变量SECRET_PASSWORD读取" # 仅作为配置说明,实际由应用读取环境变量

在Helm的Deployment模板中添加环境变量配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-app
spec:
  template:
    spec:
      containers:
      - name: test-app
        image: your-app-image:tag
        env:
        - name: SECRET_PASSWORD
          valueFrom:
            secretKeyRef:
              name: your-secret-name # 替换为你的Secret实际名称
              key: PASSWORD
        # 应用启动时读取环境变量SECRET_PASSWORD作为password参数值

方式二:将Secret挂载为文件供应用读取

如果应用支持从文件读取密码,可将Secret挂载为容器内的只读文件,values.yaml中配置文件路径:

修改后的values.yaml

test-app:
  properties:
    data:
      password: "/run/secrets/db-password" # 容器内挂载Secret的文件路径

在Helm的Deployment模板中配置Secret挂载

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-app
spec:
  template:
    spec:
      volumes:
      - name: secret-volume
        secret:
          secretName: your-secret-name # 替换为你的Secret实际名称
      containers:
      - name: test-app
        image: your-app-image:tag
        volumeMounts:
        - name: secret-volume
          mountPath: "/run/secrets"
          readOnly: true
        # 应用读取/run/secrets/PASSWORD文件内容作为password参数值

不推荐的方式:直接传递Secret值到values

如果一定要在values.yaml中填充Secret值,可通过helm命令行传递,但这种方式会在命令历史中留下敏感信息,风险较高:

helm install test-app ./your-chart --set test-app.properties.data.password=$(kubectl get secret your-secret-name -o jsonpath='{.data.PASSWORD}' | base64 -d)

此时values.yaml可保留空占位符:

test-app:
  properties:
    data:
      password: ""

内容的提问来源于stack exchange,提问作者user3908406

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:45:17