1. 首页
  2. 技术知识

helmfile声明式部署Helm Chart使用详解

目录

    说明

      场景说明

    安装

      helmfile.yaml 介绍helmfile 调试

    安装 chart

      helmfile 更新或者删除某个 chart查看变更资料参考

说明

使用 helmfile 时,我们首先得了解 helm 的使用,以及如何开发一个 helm chart。helm 是 kubernetes 的包管理工具。在实际的使用场景中我们涉及同时部署多个 chart、区分不同的部署环境、版本控制等需求。基于此需求,可以使用 helmfile 工具。helmfile 通过 helmfile 文件帮助用户管理和维护多个 helm chart,可以来区分环境、实现版本控制。github 链接:helmfile[1]

场景说明

我们在公有云场景或者私有化场景中,同一个产品可能涉及多套环境的配置,例如:每套环境部署依赖的环境差异、使用的数据库、消息队列中间件等实例的地址、账号密码等都不同。因此针对不同环境我们需要维护开发环境、测试环境、预生产环境、生产环境甚至多套环境的部署文件以及秘钥文件,每个小小的改动将涉及多套环境配置的修改,这给运维人员增加了极大的负担,以及多套环境的配置如何保持统一,也极大的考验运维人员的细致程度,极大的增加了运维的复杂度。同时涉及的数据库中间件实例的账户密码的存放,也给运维流程增加了巨大的安全隐患。基于上面的述求,这里可以将业务部署的各服务文件改造成 helm chart,同时区分多套环境以及版本控制,我们使用 helmfile 来统一部署管理。涉及实例涉及的账户密码,我们可以使用 helm secrets 来实现加密解密,以及来保证运维的安全性,从而极大的减少运维的复杂度。关于 helm secrets 的使用,我们在其他文章进行的详细的介绍。

安装

helmfile 提供了多种安装方式,具体可以参考:helmfile release[2]helmfile 还支持运行在容器中,可以很方便的集成到 CICD 的流程中:

  1. # helm 2
  2. $ docker run –rm –net=host -v “${HOME}/.kube:/root/.kube” -v “${HOME}/.helm:/root/.helm” -v “${PWD}:/wd” –workdir /wd quay.io/roboll/helmfile:v0.135.0 helmfile sync
  3. # helm 3
  4. $ docker run –rm –net=host -v “${HOME}/.kube:/root/.kube” -v “${HOME}/.config/helm:/root/.config/helm” -v “${PWD}:/wd” –workdir /wd quay.io/roboll/helmfile:helm3-v0.135.0 helmfile sync

复制代码
helmfile.yaml 介绍

helmfile.yaml 是 helmfile 的核心文件,其用来声明所有的配置。下面会简要介绍一下,具体说明可以参考官方文档:helmfile-configuration[3]

  1. # 声明 repo 配置
  2. repositories:
  3. – name: <repo-name>
  4.   # url: repo url
  5.   # 可以设置基础配置 或 tls 认证
  6.   # certFile: certificate 文件
  7.   # keyFile: key 文件
  8.   # username: 用户名
  9.   # password: 密码
  10. # helm 二进制文件的路径
  11. helmBinary: path/to/helm3
  12. # helm 的一些默认设置,这些配置与 `helm SUBCOMMAND` 相同,可以通过这个配置声明一些,默认的配置
  13. helmDefaults:
  14.   tillerNamespace: tiller-namespace  #dedicated default key for tiller-namespace
  15.   tillerless: false                  #dedicated default key for tillerless
  16.   kubeContext: kube-context          #dedicated default key for kube-context (–kube-context)
  17.   cleanupOnFail: false               #dedicated default key for helm flag –cleanup-on-fail
  18.   # additional and global args passed to helm (default “”)
  19.   args:
  20.     – “–set k=v”
  21.   # verify the chart before upgrading (only works with packaged charts not directories) (default false)
  22.   verify: true
  23.   # wait for k8s resources via –wait. (default false)
  24.   wait: true
  25.   # time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks, and waits on pod/pvc/svc/deployment readiness) (default 300)
  26.   timeout: 600
  27.   # performs pods restart for the resource if APPlicable (default false)
  28.   recreatePods: true
  29.   # forces resource update through delete/recreate if needed (default false)
  30.   force: false
  31.   # when using helm 3.2+, automatically create release namespaces if they do not exist (default true)
  32.   createNamespace: true
  33.   …
  34. # 为 helmfile 中所有的 release 设置相同的 label,可用于为所有 release 标记相同的版本
  35. commonLabels:
  36.   hello: world
  37. # 设置 release 配置(支持多 release)
  38. releases:
  39.   # 远程 chart 示例(chart 已经上传到 remote 仓库)
  40.   – name: vault                            # name of this release
  41.     namespace: vault                       # target namespace
  42.     createNamespace: true                  # helm 3.2+ automatically create release namespace (default true)
  43.     labels:                                # Arbitrary key value pairs for filtering releases
  44.       foo: bar
  45.     chart: roboll/vault-secret-manager     # the chart being installed to create this release, referenced by `repository/chart` syntax
  46.     version: ~1.24.1                       # the semver of the chart. range constraint is supported
  47.     condition: vault.enabled               # The values lookup key for filtering releases. Corresponds to the boolean value of `vault.enabled`, where `vault` is an arbitrary value
  48.     missingFileHandler: Warn # set to either “Error” or “Warn”. “Error” instructs helmfile to fail when unable to find a values or secrets file. When “Warn”, it prints the file and continues.
  49.     # Values files used for rendering the chart
  50.     values:
  51.       # Value files passed via –values
  52.       – vault.yaml
  53.       # Inline values, passed via a temporary values file and –values, so that it doesn’t suffer from type issues like –set
  54.       – address: https://vault.example.com
  55.       # Go template available in inline values and values files.
  56.       – image:
  57.           # The end result is more or less YAML. So do `quote` to prevent number-like strings from accidentally parsed into numbers!
  58.           # See https://github.com/roboll/helmfile/issues/608
  59.           tag: {{ requiredEnv “IMAGE_TAG” | quote }}
  60.           # Otherwise:
  61.           #   tag: “{{ requiredEnv “IMAGE_TAG” }}”
  62.           #   tag: !!string {{ requiredEnv “IMAGE_TAG” }}
  63.         db:
  64.           username: {{ requiredEnv “DB_USERNAME” }}
  65.           # value taken from environment variable. Quotes are necessary. Will throw an error if the environment variable is not set. $DB_PASSWORD needs to be set in the calling environment ex: export DB_PASSWORD=’password1′
  66.           password: {{ requiredEnv “DB_PASSWORD” }}
  67.         proxy:
  68.           # Interpolate environment variable with a fixed string
  69.           domain: {{ requiredEnv “PLATFORM_ID” }}.my-domain.com
  70.           scheme: {{ env “SCHEME” | default “https” }}
  71.     # Use `values` whenever possible!
  72.     # `set` translates to helm’s `–set key=val`, that is known to suffer from type issues like https://github.com/roboll/helmfile/issues/608
  73.     set:
  74.     # single value loaded from a local file, translates to –set-file foo.config=path/to/file
  75.     – name: foo.config
  76.       file: path/to/file
  77.     # set a single array value in an array, translates to –set bar[0]={1,2}
  78.     – name: bar[0]
  79.       values:
  80.       – 1
  81.       – 2
  82.     # set a templated value
  83.     – name: namespace
  84.       value: {{ .Namespace }}
  85.     # will attempt to decrypt it using helm-secrets plugin
  86.   # 本地 chart 示例(chart 保存在本地)
  87.   – name: grafana                            # name of this release
  88.     namespace: another                       # target namespace
  89.     chart: ../my-charts/grafana              # the chart being installed to create this release, referenced by relative path to local helmfile
  90.     values:
  91.     – “../../my-values/grafana/values.yaml”             # Values file (relative path to manifest)
  92.     – ./values/{{ requiredEnv “PLATFORM_ENV” }}/config.yaml # Values file taken from path with environment variable. $PLATFORM_ENV must be set in the calling environment.
  93.     wait: true
  94. # 可以嵌套其他的 helmfiles,支持从本地和远程拉取 helmfile
  95. helmfiles:
  96. – path: path/to/subhelmfile.yaml
  97.   # label 选择器可以过滤需要覆盖的 release
  98.   selectors:
  99.   – name=prometheus
  100.   # 覆盖 value
  101.   values:
  102.   # 使用文件覆盖
  103.   – additional.values.yaml
  104.   # 覆盖单独的 key
  105.   – key1: val1
  106. – # 远程拉取配置
  107.   path: git::https://github.com/cloudposse/helmfiles.git@releases/kiam.yaml?ref=0.40.0
  108. # 如果指向不存在路径,则打印告警错误
  109. missingFileHandler: Error
  110. # 多环境管理
  111. environments:
  112.   # 当没有设置 `–environment NAME` 时,使用 default
  113.   default:
  114.     values:
  115.     # 内容可以是文件路径或者 key:value
  116.     – environments/default/values.yaml
  117.     – myChartVer: 1.0.0-dev
  118.   # “production” 环境,当设置了 `helmfile –environment production sync` 时
  119.   production:
  120.     values:
  121.     – environment/production/values.yaml
  122.     – myChartVer: 1.0.0
  123.     # disable vault release processing
  124.     – vault:
  125.         enabled: false
  126.     ## `secrets.yaml` is decrypted by `helm-secrets` and available via `{{ .Environment.Values.KEY }}`
  127.     secrets:
  128.     – environment/production/secrets.yaml
  129.     # 当占不到 `environments.NAME.values` 时,可以设置为 “Error”, “Warn”, “Info”, “Debug”,默认是 “Error”
  130.     missingFileHandler: Error
  131. # 分层管理,可以将所有文件合并,顺序为:environments.yaml < – defaults.yaml < – templates.yaml < – helmfile.yaml
  132. bases:
  133. – environments.yaml
  134. – defaults.yaml
  135. – templates.yaml
  136. # API 功能
  137. apiVersions:
  138. – example/v1

复制代码
helmfile 调试

这里,编排好相关的 helmfile 后,我们可以使用下面的命令进行调试

  1. # 查看目录结构
  2. $ ls
  3. README.org    environments  helm          helmfile      helmfile.yaml releases
  4. # 查看helmfile.yaml
  5. $ cat helmfile.yaml
  6. environments:
  7.   # 不指定环境时,默认使用默认测试环境
  8.   default:
  9.     values:
  10.        – environments/test/config.yaml
  11.        – environments/test/versions.yaml
  12.        – environments/test//namespaces.yaml
  13.     secrets:
  14.        – environments/test/secrets.yaml
  15.   test:
  16.     values:
  17.       – environments/test/config.yaml
  18.       – environments/test/versions.yaml
  19.       – environments/test/namespaces.yaml
  20.     secrets:
  21.        – environments/test/secrets.yaml
  22. helmDefaults:
  23.   createNamespace: true
  24. releases:
  25.   – name: password-secrets
  26.     kubeContext: {{ .Values.kubeContext.service }}
  27.     namespace: {{ .Values.namespaces.service }}
  28.     chart: helm/charts/secrets
  29.     values:
  30.       – releases/secrets.yaml.gotmpl
  31.     labels:
  32.       app: secrets
  33.   – name: web
  34.     kubeContext: {{ .Values.kubeContext.business }}
  35.     namespace: {{ .Values.namespaces.business }}
  36.     chart: helm/charts/web
  37.     values:
  38.       – releases/web.yaml.gotmpl
  39.     labels:
  40.       app: web
  41. # helmfile调试
  42. $ helmfile -e test template

复制代码
安装 chart

  1. helmfile -e test sync

复制代码
helmfile 更新或者删除某个 chart

这里可以通过–selector指定 label 来进行更新或者删除:

  1. # 更新web服务
  2. helmfile -e test –selector app=web sync
  3. # 删除web服务
  4. helmfile -e test –selector app=web delete

复制代码
查看变更

  1. # 查看文件的变更信息
  2. helmfile -e test –selector app=web diff
  3. # 只查看文件的变更部分信息
  4. helmfile -e test –selector app=web diff –context 4

复制代码
资料参考

[1]helmfile: https://github.com/roboll/helmfile

[2]helmfile release: https://github.com/roboll/helmfile/releases

[3]helmfile-configuration: https://github.com/roboll/helmfile#configuration


声明:本文分享的软件服务以及语言均源于网络,只做针对这些软件服务或者语言的使用实践进行分享和整理。本公众号不对任何人进行推荐,在使用这些软件或编程代码时有可能会引发一些问题,甚至导致数据丢失,请您自行承担相应的后果!本公众号概不负责! 若您觉得公众号发布的内容若侵犯到您的权益,请联系及时管理员沟通!

以上就是helmfile声明式部署Helm Chart使用详解的详细内容,更多关于helmfile部署Helm Chart的资料请关注软件技术网其它相关文章!

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/105947.html

联系我们