关于 Helm 子 chart 的使用总结

137 阅读3分钟

1. 写在最前面

工作越久就会越愿意简单操作,比如在 K8s 上部署服务的时候,若平台提供了一个极简快速上线方案:拷贝一个其他人写好的 chart ,然后简单的替换掉 serviceName,同时增加 servcie 需要的配置,似乎也能够完成需求。

但是就是这么的麻木完成,似乎好像会觉得违背自己的初心。受教育的意义在于培养出每个人独特的性格,让我们知道除了要做什么之外,也要能判断出这么做到底好不好,努力试图从中找出一种更优雅的解法,也许会成为平凡工作日的一种快乐。

1.1 背景

问题:平台侧会为业务提供一个同一个 base chart ,其中作用是管理如何在 K8s 平台部署 woker 服务。

一般解法:参考了一下其他业务的使用方法,基本无一例外的都是将 base chart 拷贝出来一下,替换一下 values.yaml 文件下的 serviceName ,同时增加一下业务自己 configmap 配置。

注:这解法或许在让业务快速上线的大要求下,找不出什么可以指责的地方。但是总觉得前期不肯花费时间在这个 chart 优化的上面,后面大概率会因为调整 chart 的配置文件出锅。

2. 解法

当前的解法也并非最优的解法,是指在「一般解法」的基础上做了解耦。

  • 子 chart :使用平台侧的 base chart 作为子 chart,因为 chart 的管理当前在 git 的代码仓库下,所以为了方便更新子 chart 的引用使用了 git submodule

  • 父 chart:

    • 显示依赖子 chart
    • 在父 chart 的 value.yaml 文件下会指定可以覆盖子 chart 的 serviceName 等信息
    • 在父 chart 的 templates 目录下,会定义业务自己需要的 configmap 配置

2.1 示例

以下仅说明按照如上思路,创建的解法示例,并发真实使用的代码。

注:不能将公司的代码推送到 github,不能将公司的代码截图展示,谨记!

  • 目录结构

    $> tree
    .
    ├── child-chart
    │   ├── Chart.yaml
    │   ├── charts
    │   ├── templates
    │   │   └── configmap.yaml
    │   └── values.yaml
    └── parent-chart
        ├── Chart.lock
        ├── Chart.yaml
        ├── charts
        │   └── child-chart-0.1.0.tgz
        ├── templates
        │   └── configmap.yaml
        └── values.yaml
    
  • 在 parent-chart 的 Chart.yaml 中指定依赖

    apiVersion: v2
    name: parent-chart
    description: A Helm chart for Kubernetes
    
    # A chart can be either an 'application' or a 'library' chart.
    #
    # Application charts are a collection of templates that can be packaged into versioned archives
    # to be deployed.
    #
    # Library charts provide useful utilities or functions for the chart developer. They're included as
    # a dependency of application charts to inject those utilities and functions into the rendering
    # pipeline. Library charts do not define any templates and therefore cannot be deployed.
    type: application
    
    # This is the chart version. This version number should be incremented each time you make changes
    # to the chart and its templates, including the app version.
    # Versions are expected to follow Semantic Versioning (https://semver.org/)
    version: 0.1.0
    
    # This is the version number of the application being deployed. This version number should be
    # incremented each time you make changes to the application. Versions are not expected to
    # follow Semantic Versioning. They should reflect the version the application is using.
    # It is recommended to use it with quotes.
    appVersion: "1.16.0"
    
    dependencies:
      - name: child-chart
        version: 0.1.0
        repository: "file://../child-chart"
    

    注:dependencies 关键字说明了 parent-chart 依赖了 child-chart

  • 更新依赖

    $> helm dependency build ./parent-chart
    Saving 1 charts
    Deleting outdated charts
    
  • 配置 parent-chart 下的 value.yaml 值

企业微信截图_ea015b34-94bf-4441-a933-d80324b6a99d.png

  • 显示 template 渲染后的效果

    $> helm template ./parent-chart --debug
    install.go:178: [debug] Original chart version: ""
    install.go:195: [debug] CHART PATH: /Users/ly/go/src/example/chart-demo/parent-chart
    
    ---
    # Source: parent-chart/charts/child-chart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: child-chart-configmap
    data:
      myvalue: child
      myvalue2: global
    ---
    # Source: parent-chart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: parent-chart-configmap
    data:
      myvalue: parent
    

    注:子 chart 可以直接用 Values.global.xxx 引用父 chart 中定义的变量

3. 碎碎念

是找到了更优解法的一周,所以从周一到周五日子过的简直飞速,不是解法需要这么长时间,是 helm 的学习曲线比较陡峭。

  • 有生之年,活到这么大,已经很不容易了。
  • 我算是那种给自己找盼头才能活下去的人。像什么几天后去见喜欢的人,几号又有谁的展览,几个月以后的演唱会。没有盼头活不下去。没有喜欢事物也活不下去。活着的全部动力我全部都压在这一点点「喜欢」上。不是我喜欢他们,是他们在救我。
  • 人的精神寄托可以是音乐,可以是书籍,可以是工作,可以是山川湖海,唯独不可以是人。人是一个非常复杂而凉薄的物种,唯利是图是它的基本属性,如果你对人性有过高的期待,那一定会伤痕累累,心灰如死。 ~ 《百年孤独》