Helm值管理Skill helm-values

Helm值管理技能用于在Kubernetes部署中自定义应用程序配置,通过管理Helm值文件和配置覆盖实现。包括值文件结构、覆盖策略、环境特定配置、全局值、模式验证、秘密管理和复杂值结构处理。关键词:Helm, Kubernetes, 配置管理, 值文件, 部署优化, DevOps, 云原生。

Docker/K8s 0 次安装 0 次浏览 更新于 3/25/2026

name: helm-values user-invocable: false description: 用于管理Helm值文件和配置覆盖,以自定义Kubernetes部署。 allowed-tools: []

Helm 值

在Helm中管理值文件和配置覆盖。

值层次结构

Helm从多个源合并值(优先级从低到高):

  1. 内置默认值
  2. 图表的 values.yaml
  3. 父图表的值
  4. 使用 -f 指定的值文件(可以多个)
  5. 使用 --set 的单个参数

values.yaml 结构

按资源组织

# 全局设置
global:
  environment: production
  domain: example.com

# 应用程序设置
app:
  name: myapp
  version: "1.0.0"

# 镜像设置
image:
  repository: myregistry/myapp
  pullPolicy: IfNotPresent
  tag: ""  # 覆盖 appVersion

# 服务设置
service:
  type: ClusterIP
  port: 80
  targetPort: 8080

# Ingress 设置
ingress:
  enabled: false
  className: nginx
  annotations: {}
  hosts:
    - host: myapp.example.com
      paths:
        - path: /
          pathType: Prefix
  tls: []

# 资源
resources:
  limits:
    cpu: 500m
    memory: 512Mi
  requests:
    cpu: 250m
    memory: 256Mi

# 持久性
persistence:
  enabled: false
  storageClass: ""
  accessMode: ReadWriteOnce
  size: 8Gi

覆盖策略

使用文件覆盖

# 单个文件
helm install myrelease ./mychart -f custom-values.yaml

# 多个文件(后文件覆盖前文件)
helm install myrelease ./mychart \
  -f values-base.yaml \
  -f values-production.yaml

使用 --set 覆盖

# 单个值
helm install myrelease ./mychart --set image.tag=2.0.0

# 多个值
helm install myrelease ./mychart \
  --set image.tag=2.0.0 \
  --set replicaCount=5

# 嵌套值
helm install myrelease ./mychart \
  --set ingress.enabled=true \
  --set ingress.hosts[0].host=myapp.com

使用 --set-string 覆盖

# 强制字符串类型(适用于数字样式的字符串)
helm install myrelease ./mychart \
  --set-string version="1.0"

使用 --set-file 覆盖

# 从文件读取值
helm install myrelease ./mychart \
  --set-file config=./config.json

环境特定值

values-development.yaml

replicaCount: 1

image:
  tag: "dev-latest"
  pullPolicy: Always

resources:
  limits:
    cpu: 200m
    memory: 256Mi

ingress:
  enabled: true
  hosts:
    - host: dev.myapp.com

postgresql:
  enabled: true

values-production.yaml

replicaCount: 5

image:
  tag: "1.0.0"
  pullPolicy: IfNotPresent

resources:
  limits:
    cpu: 1000m
    memory: 1Gi

ingress:
  enabled: true
  hosts:
    - host: myapp.com
  tls:
    - secretName: myapp-tls
      hosts:
        - myapp.com

postgresql:
  enabled: false
  external:
    host: prod-db.example.com

全局值

父图表 values.yaml

global:
  environment: production
  storageClass: fast-ssd

  postgresql:
    auth:
      existingSecret: db-credentials

子图表访问

# 在子图表模板中
environment: {{ .Values.global.environment }}

模式验证

values.schema.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["image"],
  "properties": {
    "replicaCount": {
      "type": "integer",
      "minimum": 1,
      "maximum": 10
    },
    "image": {
      "type": "object",
      "required": ["repository"],
      "properties": {
        "repository": {
          "type": "string"
        },
        "tag": {
          "type": "string"
        },
        "pullPolicy": {
          "type": "string",
          "enum": ["Always", "IfNotPresent", "Never"]
        }
      }
    },
    "service": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": ["ClusterIP", "NodePort", "LoadBalancer"]
        },
        "port": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        }
      }
    }
  }
}

值中的秘密

不要提交秘密

# values.yaml - 公共默认值
database:
  host: ""
  username: ""
  password: ""

# values-secrets.yaml - 不在git中
database:
  host: "prod-db.example.com"
  username: "dbuser"
  password: "super-secret"

使用外部秘密

# values.yaml
database:
  useExistingSecret: true
  existingSecretName: db-credentials
# 在模板中
{{- if .Values.database.useExistingSecret }}
secretKeyRef:
  name: {{ .Values.database.existingSecretName }}
  key: password
{{- else }}
value: {{ .Values.database.password | quote }}
{{- end }}

复杂值结构

列表

# values.yaml
extraEnvVars:
  - name: LOG_LEVEL
    value: info
  - name: API_KEY
    valueFrom:
      secretKeyRef:
        name: api-secret
        key: key
# 模板
{{- range .Values.extraEnvVars }}
- name: {{ .name }}
  {{- if .value }}
  value: {{ .value | quote }}
  {{- else if .valueFrom }}
  valueFrom:
    {{- toYaml .valueFrom | nindent 4 }}
  {{- end }}
{{- end }}

映射

# values.yaml
annotations:
  prometheus.io/scrape: "true"
  prometheus.io/port: "9090"

labels:
  team: platform
  environment: production
# 模板
annotations:
  {{- range $key, $value := .Values.annotations }}
  {{ $key }}: {{ $value | quote }}
  {{- end }}

最佳实践

记录值

# 带注释的 values.yaml
## 副本数量
## @param replicaCount - Pod 副本数量
replicaCount: 3

## 镜像配置
## @param image.repository - Docker 镜像仓库
## @param image.tag - Docker 镜像标签(默认为图表 appVersion)
image:
  repository: myapp
  tag: ""

合理的默认值

# 提供生产就绪的默认值
replicaCount: 3  # 不是 1

resources:
  limits:
    cpu: 500m
    memory: 512Mi
  requests:
    cpu: 500m  # 与限制相同以保证服务质量
    memory: 512Mi

功能标志

# 允许启用/禁用功能
features:
  metrics:
    enabled: true
    port: 9090

  tracing:
    enabled: false
    endpoint: ""

查看计算后的值

# 查看最终合并的值
helm get values myrelease

# 查看所有值(包括默认值)
helm get values myrelease --all

# 使用值模板化
helm template myrelease ./mychart -f custom-values.yaml