这是 Helm 的快速参考备忘单。Helm 是 Kubernetes 的包管理器,用于查找、安装、升级、回滚和发布 Chart。示例以 Helm 4 为主,并标注常见的 Helm 3 差异。
快速入门
安装与检查
BASH 复制
1 2 3 4 5 $ brew install helm $ winget install Helm.Helm
查看版本、帮助和客户端环境
TEXT 复制
1 2 3 $ helm version $ helm help $ helm env
Helm 使用当前 kubectl 上下文访问集群。执行部署前可先运行:
BASH 复制
1 2 $ kubectl config current-context $ kubectl cluster-info
核心概念
名称
说明
Chart
一组 Kubernetes 资源模板组成的应用包
Repository
存放和分发 Chart 的仓库
Release
Chart 安装到集群后形成的实例
Revision
Release 每次安装、升级或回滚产生的版本
Values
用于覆盖 Chart 默认配置的值
最短工作流
添加仓库并刷新索引
BASH 复制
1 2 $ helm repo add bitnami https://charts.bitnami.com/bitnami $ helm repo update
搜索、查看配置并安装
BASH 复制
1 2 3 $ helm search repo bitnami/nginx $ helm show values bitnami/nginx > values.yaml $ helm install my-nginx bitnami/nginx -f values.yaml
查看、升级和卸载
BASH 复制
1 2 3 $ helm status my-nginx $ helm upgrade my-nginx bitnami/nginx -f values.yaml $ helm uninstall my-nginx
仓库与 Chart
仓库管理
BASH 复制
1 2 3 4 5 6 7 $ helm repo add stable https://example.com/charts $ helm repo add private https://example.com/charts \ --username <username> --password <password> $ helm repo list $ helm repo update $ helm repo update stable $ helm repo remove stable
--force-update 可覆盖同名仓库配置:
BASH 复制
1 $ helm repo add stable https://new.example.com/charts --force-update
搜索 Chart
搜索已添加的本地仓库索引
BASH 复制
1 2 3 4 $ helm search repo nginx $ helm search repo nginx --versions $ helm search repo nginx --version '^15.0.0' $ helm search repo nginx --devel
搜索 Artifact Hub
查看 Chart
BASH 复制
1 2 3 4 5 $ helm show chart bitnami/nginx $ helm show values bitnami/nginx $ helm show readme bitnami/nginx $ helm show crds bitnami/nginx $ helm show all bitnami/nginx
指定 Chart 版本:
BASH 复制
1 $ helm show values bitnami/nginx --version 15.0.0
下载 Chart
BASH 复制
1 2 3 4 5 $ helm pull bitnami/nginx $ helm pull bitnami/nginx --version 15.0.0 $ helm pull bitnami/nginx --untar $ helm pull bitnami/nginx --untar --untardir ./charts $ helm pull bitnami/nginx --verify
安装与升级
安装 Release
BASH 复制
1 2 3 4 5 $ helm install <release> <chart> $ helm install my-app ./mychart $ helm install my-app ./mychart-0.1.0.tgz $ helm install my-app bitnami/nginx --version 15.0.0 $ helm install my-app bitnami/nginx -n production --create-namespace
让 Helm 自动生成 Release 名称:
BASH 复制
1 2 3 4 $ helm install bitnami/nginx --generate-name $ helm install bitnami/nginx \ --generate-name \ --name-template 'web-{{ now | unixEpoch }}'
常用安装参数
BASH 复制
1 2 3 4 5 6 7 8 $ helm install my-app ./chart \ --namespace production \ --create-namespace \ --values values.yaml \ --set image.tag=1.2.3 \ --wait \ --timeout 10m \ --rollback-on-failure
参数
作用
-n, --namespace
指定命名空间
--create-namespace
命名空间不存在时创建
-f, --values
读取自定义 Values 文件,可多次指定
--set
从命令行设置值,可多次指定
--wait
等待资源达到就绪状态
--timeout
设置单次 Kubernetes 操作超时
--rollback-on-failure
失败时回滚或删除安装,并自动启用等待
--dry-run=client
仅在客户端模拟,不连接集群
--dry-run=server
在服务端模拟和校验,需要连接集群
Helm 3 使用 --atomic 表示失败时自动回滚;Helm 4 已将它替换为含义更清晰的 --rollback-on-failure。
升级 Release
BASH 复制
1 2 3 4 5 $ helm upgrade my-app ./chart $ helm upgrade my-app bitnami/nginx --version 15.0.0 $ helm upgrade my-app ./chart -f values.yaml --set image.tag=1.2.4 $ helm upgrade my-app ./chart --reuse-values $ helm upgrade my-app ./chart --reset-values
Release 不存在时安装,存在时升级:
BASH 复制
1 2 3 $ helm upgrade --install my-app ./chart \ -n production --create-namespace \ -f values.yaml --wait --rollback-on-failure
预览变更
BASH 复制
1 2 3 4 5 6 7 8 9 $ helm template my-app ./chart -f values.yaml $ helm install my-app ./chart --dry-run=client --debug $ helm upgrade my-app ./chart --dry-run=client --debug $ helm install my-app ./chart --dry-run=server --debug
模拟命令可能输出 Secret 内容,不要把结果直接写入公开日志;Helm 4 可添加 --hide-secret 隐藏 Secret。
Values 配置
设置 Values
BASH 复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ helm install my-app ./chart --set replicaCount=3 $ helm install my-app ./chart --set image.repository=nginx,image.tag=1.27 $ helm install my-app ./chart --set-string service.annotations.version=01 $ helm install my-app ./chart --set-file config=./app.conf $ helm install my-app ./chart --set-json 'tolerations=[{"key":"dedicated"}]' $ helm install my-app ./chart --set 'ports[0]=80,ports[1]=443' $ helm install my-app ./chart --set 'nodeSelector.kubernetes\.io/role=worker'
Values 优先级
优先级从低到高:Chart 默认 values.yaml、父 Chart Values、-f 文件、--set 参数。后出现的同类参数覆盖前面的值。
BASH 复制
1 2 3 4 $ helm upgrade --install my-app ./chart \ -f values-common.yaml \ -f values-production.yaml \ --set image.tag=1.2.3
获取生效的 Values
BASH 复制
1 2 3 4 5 6 $ helm get values my-app $ helm get values my-app -n production -o yaml $ helm get values my-app -n production --all
Release 管理
列出 Release
BASH 复制
1 2 3 4 5 6 7 8 9 $ helm list $ helm list -n production $ helm list --all-namespaces $ helm list --all $ helm list --pending $ helm list --failed $ helm list --deployed $ helm list --filter '^my-' $ helm list -o json
查看状态与部署内容
BASH 复制
1 2 3 4 5 6 $ helm status my-app -n production $ helm get all my-app -n production $ helm get manifest my-app -n production $ helm get notes my-app -n production $ helm get hooks my-app -n production $ helm get metadata my-app -n production
历史与回滚
BASH 复制
1 2 3 4 5 6 7 8 9 $ helm history my-app -n production $ helm history my-app -n production --max 20 $ helm rollback my-app 2 -n production $ helm rollback my-app 2 -n production --wait --timeout 10m $ helm rollback my-app 0 -n production
执行回滚前先用 helm history 确认目标 Revision。
测试与卸载
BASH 复制
1 2 3 4 5 $ helm test my-app -n production $ helm test my-app -n production --logs $ helm uninstall my-app -n production $ helm uninstall my-app -n production --keep-history $ helm uninstall my-app -n production --dry-run
Chart 开发
创建 Chart
BASH 复制
1 2 3 4 5 $ helm create mychart $ helm lint ./mychart $ helm lint ./mychart -f values-production.yaml --strict $ helm template my-app ./mychart $ helm install my-app ./mychart --dry-run --debug
常见目录结构:
TEXT 复制
1 2 3 4 5 6 7 mychart/ ├── Chart.yaml # Chart 元数据和依赖 ├── values.yaml # 默认配置 ├── charts/ # 依赖 Chart ├── crds/ # CustomResourceDefinition ├── templates/ # Kubernetes 模板 └── templates/NOTES.txt # 安装后的提示
依赖管理
BASH 复制
1 2 3 $ helm dependency list ./mychart $ helm dependency update ./mychart $ helm dependency build ./mychart
update 根据 Chart.yaml 解析依赖,更新 Chart.lock 和 charts/。
build 根据现有 Chart.lock 重建 charts/,适合可复现构建。
模板调试
BASH 复制
1 2 3 4 5 6 7 8 9 10 $ helm template my-app ./mychart --debug $ helm template my-app ./mychart \ --show-only templates/deployment.yaml $ helm template my-app ./mychart --kube-version 1.34.0 $ helm template my-app ./mychart --api-versions example.com/v1
模板中常用对象:
YAML 复制
1 2 3 4 5 6 7 8 {{ .Release.Name }} {{ .Release.Namespace }} {{ .Chart.Name }} {{ .Chart.AppVersion }} {{ .Values.image.repository }} {{ include "mychart.fullname" . }} {{ required "image.tag is required" .Values.image.tag }} {{ .Values.config | toYaml | nindent 2 }}
打包与验证
BASH 复制
1 2 3 4 5 6 7 8 $ helm package ./mychart $ helm package ./mychart --destination ./dist $ helm package ./mychart --version 1.2.3 --app-version 2.0.0 $ helm package ./mychart --dependency-update $ helm verify ./mychart-1.2.3.tgz $ helm install my-app ./mychart-1.2.3.tgz --verify
OCI Registry
登录与退出
BASH 复制
1 2 3 4 $ helm registry login registry.example.com $ helm registry login registry.example.com \ --username <username> --password-stdin $ helm registry logout registry.example.com
登录地址只填写主机名和可选端口,不添加 https:// 或仓库路径。
推送 Chart
BASH 复制
1 2 $ helm package ./mychart $ helm push mychart-1.2.3.tgz oci://registry.example.com/helm
推送目标不包含 Chart 名称和版本;Helm 会从包内元数据生成它们。
拉取、查看和安装 OCI Chart
BASH 复制
1 2 3 4 5 $ helm pull oci://registry.example.com/helm/mychart --version 1.2.3 $ helm show values oci://registry.example.com/helm/mychart --version 1.2.3 $ helm install my-app oci://registry.example.com/helm/mychart --version 1.2.3 $ helm upgrade --install my-app \ oci://registry.example.com/helm/mychart --version 1.2.3
实用命令
全局参数
BASH 复制
1 2 3 4 5 $ helm <command > --namespace production $ helm <command > --kube-context <context> $ helm <command > --kubeconfig ./kubeconfig $ helm <command > --debug $ helm <command > --qps 20 --burst-limit 50
自动补全
BASH 复制
1 2 3 4 5 6 7 8 9 10 11 $ source <(helm completion bash) $ source <(helm completion zsh) $ helm completion fish | source PS> helm completion powershell | Out-String | Invoke-Expression
插件管理
BASH 复制
1 2 3 4 $ helm plugin list $ helm plugin install <plugin-url> $ helm plugin update <plugin-name> $ helm plugin uninstall <plugin-name>
插件由第三方代码组成,安装前应检查来源、权限和兼容版本。
常见排错
检查当前集群和 Helm 环境
BASH 复制
1 2 $ kubectl config current-context $ helm env
查看失败状态、历史和渲染后的资源
TEXT 复制
1 2 3 $ helm status my-app -n production $ helm history my-app -n production $ helm get manifest my-app -n production
检查集群事件和工作负载
TEXT 复制
1 2 3 $ kubectl get events -n production --sort-by=.lastTimestamp $ kubectl get pods -n production $ kubectl describe pod <pod-name> -n production
安全部署范式
BASH 复制
1 2 3 4 5 6 7 8 $ helm upgrade --install my-app ./chart \ --namespace production \ --create-namespace \ --values values-production.yaml \ --wait \ --timeout 10m \ --rollback-on-failure \ --history-max 10
在生产环境固定 --version 和镜像标签,避免隐式升级。
先执行 helm lint 和 helm template,再部署到集群。
使用 --rollback-on-failure 前确认失败后自动回滚或删除符合预期;Helm 3 对应参数为 --atomic。
不要把密码直接放入 --set、Shell 历史或 CI 日志。
卸载不会保证删除 PVC、CRD 和外部资源,操作后应单独检查。
参考资料