0
0

Kubernetes Helm自定义Chart、升级回滚与仓库

2026-11-01
Kubernetes Helm自定义Chart、升级回滚与仓库

一、自定义 Chart(不基于 values.yaml)

1. 创建并清空模板

helm create 01-demo-no-value

> 01-demo-no-value/values.yaml          # 清空 values.yaml
rm -rf 01-demo-no-value/templates/*     # 清空默认模板

Chart 目录结构:

01-demo-no-value/
├── charts/
├── Chart.yaml
├── .helmignore
├── templates/
└── values.yaml

2. 编写资源清单

templates/service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: xiuxian-svc-nodeport
spec:
  type: NodePort
  selector:
    apps: xiuxian
  ports:
  - port: 80
    targetPort: 80
    nodePort: 3080

templates/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-xiuxian
spec:
  replicas: 3
  selector:
    matchLabels:
      apps: xiuxian
  template:
    metadata:
      labels:
        apps: xiuxian
    spec:
      containers:
      - name: c1
        image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v3

3. 部署 / 查看 / 卸载

helm install xiuxian 01-demo-no-value -n demo-helm
helm list -n demo-helm
kubectl get pods,deploy,svc -o wide -n demo-helm

helm -n demo-helm uninstall xiuxian

二、Chart.yaml 信息

apiVersion: v2
name: demo-xiuxian
description: https://www.example.com
type: application   # application 可部署;library 是库,只提供函数
version: 0.2.1      # 主版本.次版本.修订号(SemVer)
appVersion: "0.2.2"

要点:

  • type: application 表示可部署的应用,library 表示库(无模板文件,供 application 调用函数);
  • version 是 Chart 版本,appVersion 是应用版本;
  • 版本号遵循语义化版本(SemVer):主版本号(不兼容 API 修改)、次版本号(向下兼容的新功能)、修订号(向下兼容的问题修正)。

三、NOTES.txt:部署后的帮助信息

templates/NOTES.txt 会在 helm install / upgrade 后打印出来:

欢迎使用自定义 Chart 程序,当前 Chart 版本:{{.Chart.Name}}:{{.Chart.Version}}
正在使用的应用版本:{{.Chart.AppVersion}}

release 名称:{{.Release.Name}},名称空间:{{.Release.Namespace}},
修订版本:{{.Release.Revision}},是否升级:{{.Release.IsUpgrade}}

官网:https://www.example.com

常用内置对象:.Chart(Chart 信息)、.Release(release 信息)、.Values(values.yaml 值)、.Template、.Capabilities(集群能力)。

四、基于 values.yaml 的 Chart

把可变字段抽到 values.yaml,模板用 {{.Values...}} 引用。

1. values.yaml

common:
  xiuxian:
    name: xiuxian
    labels:
      apps: xiuxian

deploy:
  xiuxian:
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps
    tag: v1
    replicas: 3

service:
  xiuxian:
    type: NodePort
    ports:
      port: 80
      targetPort: 80
      nodePort: 3080

2. deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{.Values.common.xiuxian.name}}
spec:
  replicas: {{.Values.deploy.xiuxian.replicas}}
  selector:
    matchLabels:
      apps: xiuxian
  template:
    metadata:
      labels:
        apps: xiuxian
    spec:
      containers:
      - name: c1
        image: "{{.Values.deploy.xiuxian.image}}:{{.Values.deploy.xiuxian.tag}}"

3. service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{.Values.common.xiuxian.name}}
spec:
  type: {{.Values.service.xiuxian.type}}
  selector:
    apps: xiuxian
  ports:
  - port: {{.Values.service.xiuxian.ports.port}}
    targetPort: {{.Values.service.xiuxian.ports.targetPort}}
    nodePort: {{.Values.service.xiuxian.ports.nodePort}}

部署:

helm -n demo-helm install xiuxian 02-demo-use-value/

五、Helm 常用函数

函数作用
default设置默认值
quote / squote加双引号 / 单引号
indent / nindent缩进 n 空格 / 换行后缩进
toYaml把对象渲染成 YAML
upper / lower / title大小写转换
printf格式化输出
replace替换字符串
trunc截断到指定长度
trimSuffix / trimPrefix去掉后缀 / 前缀

完整函数列表参考官方文档:https://helm.sh/zh/docs/chart_template_guide/function_list/。

六、Helm 流程控制

1. if / else 多分支

{{ if eq .Values.env "prod" }}
  ...
{{ else if eq .Values.env "test" }}
  ...
{{ else }}
  ...
{{ end }}

2. with 作用域

with 改变当前作用域(.),在块内用 . 访问子对象:

{{ with .Values.deploy }}
replicas: {{ .replicas }}
{{ end }}

3. 变量和注释

{{- $name := .Values.name -}}   # 定义变量
{{ $name }}                     # 引用变量
{{/* 这是注释,不会渲染到输出 */}}

4. range 循环

{{ range .Values.list }}
- {{ . }}
{{ end }}

七、自定义模板:define / template / include

关键字说明
define定义模板,一般放在 templates/_helpers.tpl
template引用模板,不支持传参、不支持加载其他模板
include引用模板,支持传参和加载其他模板,传参时别忘了末尾的 .

_helpers.tpl 示例:

{{- define "demo.fullname" -}}
{{ .Release.Name }}-{{ .Chart.Name }}
{{- end -}}

引用:

name: {{ include "demo.fullname" . }}

验证模板渲染(不真正部署):

helm -n demo-helm install games 12-demo-template --dry-run

## 八、Helm 升级与回滚

### 1. 声明式升级(-f 文件)

```bash
sed -i '/tag:/s/v0.2.1/v0.2.2/' 13-demo-update-rollback/values.yaml

helm -n demo-helm upgrade \
  -f 13-demo-update-rollback/values.yaml \
  games 13-demo-update-rollback
# REVISION: 2

2. 响应式升级(--set)

helm -n demo-helm upgrade \
  --set deploy.games.tag=v0.2.3,deploy.games.replicas=5 \
  games 13-demo-update-rollback
# REVISION: 3

3. 查看历史版本

helm -n demo-helm history games
# 1 Install complete
# 2 Upgrade complete
# 3 Upgrade complete

4. 回滚

helm -n demo-helm rollback games        # 回滚到上一个版本
helm -n demo-helm rollback games 1      # 回滚到指定版本

九、第三方 Chart 仓库

1. 添加仓库

helm repo list

helm repo add azure http://mirror.azure.cn/kubernetes/charts/
helm repo add demo-aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo update

2. 搜索 Chart

helm search repo mysql
helm search repo redis

3. 拉取并部署

helm pull demo-aliyun/elasticsearch-exporter            # 默认最新版
helm pull demo-aliyun/elasticsearch-exporter --version 0.1.1

tar xf elasticsearch-exporter-0.1.2.tgz
helm install demo-es-exporter elasticsearch-exporter

第三方 Chart 直接部署可能报错(如 Deployment 的 apiVersion 还是 apps/v1beta2),需要按提示改成 apps/v1;访问不成功多半是镜像拉取失败。

十、打包并推送到 Harbor

1. 打包

helm package 13-demo-update-rollback
# 生成 demo-games-v0.2.1.tgz

2. 推送 / 拉取 OCI Chart

# 推送
helm push elasticsearch-exporter-0.1.1.tgz \
  oci://harbor.example.com/demo-elasticstack \
  --ca-file /etc/docker/certs.d/harbor.example.com/ca.crt \
  --cert-file /etc/docker/certs.d/harbor.example.com/harbor.example.com.cert

# 拉取
helm pull oci://harbor.example.com/demo-elasticstack/elasticsearch-exporter \
  --version 0.1.1 \
  --ca-file /etc/docker/certs.d/harbor.example.com/ca.crt \
  --cert-file /etc/docker/certs.d/harbor.example.com/harbor.example.com.cert

旧版 Harbor 用 --with-chartmuseum 内嵌 Chart 管理(2.8 后被移除),新版 Harbor 用 OCI 接口管理 Chart。

十一、总结

  1. Chart 由 Chart.yaml(元信息)、values.yaml(默认值)、templates/(模板)组成;
  2. 不基于 values 的 Chart 没用到 Helm 的参数化特点,实际多用 values.yaml 抽离可变字段;
  3. 常用函数:default、quote、indent/nindent、toYaml、upper/lower/title、printf、replace、trunc、trimSuffix/trimPrefix;
  4. 流程控制:if/else、with、变量、range;
  5. 模板复用:define 定义、template/include 引用,include 支持传参;
  6. 升级:声明式 upgrade -f、响应式 upgrade --set;helm history 看历史,helm rollback 回滚;
  7. 第三方仓库:helm repo add → helm search repo → helm pull;
  8. Chart 打包 helm package,推送到 Harbor 用 OCI 协议(helm push oci://)。

十二、作业

  1. 完成课堂所有练习并整理思维导图。

  2. 扩展:用 Helm 部署任意 3 个游戏镜像到 K8s,并把 Chart 打包推送到 Harbor 仓库。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或者给予支持!