Skip to content

Helm Chart

Helm Chart 是一组 Kubernetes 资源模板的打包格式。

如果直接使用 kubectl apply -f,一个应用可能需要多个 YAML:

  • Deployment
  • Service
  • Ingress
  • ConfigMap
  • Secret
  • ServiceAccount
  • HPA

Helm 把这些资源组织成一个 Chart,并通过 values 参数化配置。

创建 Chart

shell
helm create hellok8s

目录结构:

text
hellok8s/
├── Chart.yaml
├── values.yaml
└── templates/
    ├── deployment.yaml
    ├── service.yaml
    ├── ingress.yaml
    ├── hpa.yaml
    └── _helpers.tpl

Chart.yaml

Chart.yaml 描述 Chart 元信息:

yaml
apiVersion: v2
name: hellok8s
description: A Helm chart for hellok8s
type: application
version: 0.1.0
appVersion: "1.0.0"
  • version:Chart 自身版本。
  • appVersion:应用版本。

values.yaml

values.yaml 保存默认配置:

yaml
replicaCount: 2

image:
  repository: guangzhengli/hellok8s
  tag: v1
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80
  targetPort: 3000

ingress:
  enabled: false
  className: nginx
  host: hellok8s.local

安装时可以覆盖:

shell
helm install hellok8s ./hellok8s \
  --set replicaCount=3 \
  --set image.tag=v2

也可以使用独立 values 文件:

shell
helm install hellok8s ./hellok8s -f values-prod.yaml

模板

Deployment 模板示例:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hellok8s.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "hellok8s.name" . }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "hellok8s.name" . }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "hellok8s.name" . }}
    spec:
      containers:
        - name: hellok8s
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.service.targetPort }}

模板语法来自 Go template。

渲染模板

不安装,只查看渲染结果:

shell
helm template hellok8s ./hellok8s

调试:

shell
helm install hellok8s ./hellok8s --dry-run --debug

这是排查模板问题最常用的命令。

安装、升级、回滚

安装:

shell
helm install hellok8s ./hellok8s

升级:

shell
helm upgrade hellok8s ./hellok8s --set image.tag=v2

安装或升级:

shell
helm upgrade --install hellok8s ./hellok8s

查看历史:

shell
helm history hellok8s

回滚:

shell
helm rollback hellok8s 1

卸载:

shell
helm uninstall hellok8s

常用函数

yaml
# 默认值
replicas: {{ .Values.replicaCount | default 1 }}

# 引号
image: {{ .Values.image.repository | quote }}

# 缩进 YAML
{{- toYaml .Values.resources | nindent 12 }}

# 必填值
{{ required "image.repository is required" .Values.image.repository }}

条件渲染

yaml
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "hellok8s.fullname" . }}
spec:
  ingressClassName: {{ .Values.ingress.className }}
{{- end }}

循环

yaml
env:
{{- range $key, $value := .Values.env }}
  - name: {{ $key }}
    value: {{ $value | quote }}
{{- end }}

values:

yaml
env:
  APP_ENV: prod
  LOG_LEVEL: info

lint

shell
helm lint ./hellok8s

helm lint 可以检查 Chart 结构和模板中的常见错误。

Chart 设计建议

  • values.yaml 只放用户需要配置的参数。
  • 模板中使用标准 Kubernetes labels。
  • 默认值要能在本地或测试环境直接跑起来。
  • 复杂逻辑尽量放在 _helpers.tpl,不要让资源模板难以阅读。
  • 先用 helm template 看渲染结果,再部署到集群。

参考