采用 Helm 可以把零散的 Kubernetes 应用配置文件作为一个 Chart 管理,Chart 源码可以和源代码一起放到 Git 库中管理。通过把 Chart 参数化,可以在测试环境和生产环境采用不同的 Chart 参数配置。
下图是采用了 Helm 的一个 CI/CD 流程
Helm与CI/CD流水线 - 图1

使用问题

  • Helm 如何管理多环境下 (Test、Staging、Production) 的业务配置?

Chart 是支持参数替换的,可以把业务配置相关的参数设置为模板变量。使用 helm install 命令部署的时候指定一个参数值文件,这样就可以把业务参数从 Chart 中剥离了。
例如: helm install —values=values-production.yaml wordpress。

  • Helm 如何解决服务依赖?

在 Chart 里可以通过 requirements.yaml (helm V2)声明对其它 Chart 的依赖关系。如下面声明表明 Chart 依赖 Apache 和 MySQL 这两个第三方 Chart。

  1. dependencies:
  2. - name: mariadb
  3. version: 2.1.1
  4. repository: https://kubernetes-charts.storage.googleapis.com/
  5. condition: mariadb.enabled
  6. tags:
  7. - wordpress-database
  8. - name: apache
  9. version: 1.4.0
  10. repository: https://kubernetes-charts.storage.googleapis.com/
  • 如何让 Helm 连接到指定 Kubernetes 集群?

Helm 默认使用和 kubectl 命令相同的配置访问 Kubernetes 集群,其配置默认在 ~/.kube/config 中。

  • 如何在部署时指定命名空间?

helm install 默认情况下是部署在 default 这个命名空间的。如果想部署到指定的命令空间,可以加上 —namespace 参数,比如:
$ helm install local/mychart --name mike-test --namespace mynamespace

重写Helm chart values

Option 1: Command line parameters

While installing your Helm Chart, you can specify the tag name dynamically with —set.
For example:$ helm install --set image.tag=12345 <your-chart-name>

Option 2: Separate values.yaml files

You can store separate values.yaml in your repository, like:

  • values.dev.yaml
  • values.prod.yaml

Then, update the correct values in your pipeline.

I just ran into this issue with GitHub Actions. As the other answer already noted, set the image.tag in values.yaml to something as a default. I use latest as the default.
The problem is that the helm upgrade command only upgrades if the image tag is different. So “latest” isn’t unique enough for Helm to do the rolling update. I use a SHA in GitHub Actions as my unique version tag. You could tag the image like this:
def dockerImage = docker.build("repo/myapp:${env.NAME}-${env.BUILD_NUMBER}")
Then in your helm command just add a —set:
helm upgrade <helm-app> <helm-chart> --set image.tag=${env.NAME}-${env.BUILD_NUMBER}
This command will override whatever value is in values.yaml. Keep in mind that the —set value must follow the structure of your values.yaml so in this case image is a top level object, with a property named tag:

  1. values.yaml
  2. image
  3. name
  4. tag
  5. pullPolicy
  6. port
  7. replicaCount

参考:

Charts版本控制

使用helm动态更新k8s里的docker镜像