helmfile使用指南
helmfile 简介
Helmfile 是一个用于管理多个 Helm Chart 部署的工具,通过声明式配置文件(YAML)定义 Helm 的 releases、values、repositories 等。适用于复杂环境下的多应用部署,支持依赖管理、环境隔离和批量操作。
安装 Helmfile
- 通过脚本安装(Linux/macOS):
curl -Lo helmfile https://github.com/helmfile/helmfile/releases/download/v0.156.0/helmfile_0.156.0_linux_amd64 chmod +x helmfile mv helmfile /usr/local/bin/ - 通过包管理工具(如 Homebrew):
brew install helmfile
配置文件结构
Helmfile 的核心是 helmfile.yaml,典型结构如下:
repositories:- name: stableurl: https://charts.helm.sh/stablereleases:- name: nginxnamespace: defaultchart: stable/nginx-ingressversion: 1.41.3values:- replicaCount: 2
- repositories: 定义 Helm Chart 仓库。
- releases: 定义要部署的 Helm Release,包括 Chart 名称、版本、values 覆盖等。
常用命令
-
部署所有 Release
helmfile sync等价于
helm install或helm upgrade。 -
查看差异(Dry Run)
helmfile diff显示当前配置与集群状态的差异。
-
删除所有 Release
helmfile destroy -
指定环境文件
helmfile -e prod apply使用
environments配置多环境(如prod.yaml覆盖默认值)。
高级功能
-
环境隔离
environments:prod:values:- values/prod.yaml通过
-e prod切换环境。 -
依赖管理
helmfiles:- path: subdir/helmfile.yaml嵌套调用其他 Helmfile 配置。
-
条件渲染
使用templates动态生成配置:releases:- name: {{ .Environment.Name }}-app -
Hook 操作
在操作前后执行脚本:releases:- name: mysqlhooks:pre-sync:- command: "echo 'Pre-install hook'"
示例:多环境部署
-
创建
helmfile.yaml和environments/prod.yaml:# helmfile.yaml environments:prod:values: [environments/prod.yaml]releases:- name: myappchart: ./charts/myappvalues: [{{ .Environment.Name }}/values.yaml] -
运行部署:
helmfile -e prod sync
调试与日志
- 启用详细日志:
helmfile --log-level debug sync - 查看生成后的模板:
helmfile template
通过 Helmfile 可以显著简化多 Helm Chart 的管理,尤其适合 CI/CD 流水线和多集群场景。
