库类型Chart

    在Helm 3中引用了库chart,从形式上区别于Helm 2中chart维护的通用或辅助chart。 作为一个chart类型引入,可以提供:

    • 一种明确区分通用和应用chart的方法
    • 逻辑上阻止安装通用chart
    • 通用chart中的未渲染模板可以包含版本组件

    chart维护者可以定义一个通用的chart作为库并且现在可以确信Helm将以标准一致的方式处理chart。 也意味着通过改变chart类型来分享应用chart中的定义。

    像之前提到的,库chart是一种 Helm chart类型。意味着你可以从创建脚手架chart开始:

    本示例中创建自己的模板需要先删除目录中的所有文件。

    1. $ rm -rf mylibchart/templates/*

    不再需要values文件。

    1. $ rm -f mylibchart/values.yaml

    在创建通用代码之前,先快速回顾一下相关Helm概念。 (有时称为局部模板或子模板)是定义在一个文件中的简单模板,并分配了一个名称。在templates/目录中, 所有以下划线开始的文件(_)不会输出到Kubernetes清单文件中。因此依照惯例,辅助模板和局部模板被放置在_*.tpl_*.yaml文件中。

    这个示例中,我们要写一个通用的配置映射来创建一个空的配置映射源。在mylibchart/templates/_configmap.yaml文件中定义如下:

    1. {{- define "mylibchart.configmap.tpl" -}}
    2. apiVersion: v1
    3. kind: ConfigMap
    4. metadata:
    5. name: {{ .Release.Name | printf "%s-%s" .Chart.Name }}
    6. data: {}
    7. {{- end -}}
    8. {{- define "mylibchart.configmap" -}}
    9. {{- include "mylibchart.util.merge" (append . "mylibchart.configmap.tpl") -}}
    10. {{- end -}}

    这个配置映射结构被定义在名为mylibchart.configmap.tpl的模板文件中。data是一个空源的配置映射, 这个文件中另一个命名的模板是mylibchart.configmap。这个模板包含了另一个模板mylibchart.util.merge, 会使用两个命名的模板作为参数,称为mylibchart.configmapmylibchart.configmap.tpl

    复制方法mylibchart.util.mergemylibchart/templates/_util.yaml文件中的一个命名模板。 是 通用Helm辅助Chart的实用工具。因为它合并了两个模板并覆盖了两个模板的公共部分。

    1. {{- /*
    2. mylibchart.util.merge will merge two YAML templates and output the result.
    3. This takes an array of three values:
    4. - the top context
    5. - the template name of the overrides (destination)
    6. - the template name of the base (source)
    7. */}}
    8. {{- define "mylibchart.util.merge" -}}
    9. {{- $top := first . -}}
    10. {{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}}
    11. {{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}}
    12. {{- toYaml (merge $overrides $tpl) -}}
    13. {{- end -}}

    当chart希望使用通过配置自定义其通用代码时,这一点就非常重要。

    最后,将chart类型修改为library。需要按以下方式编辑mylibchart/Chart.yaml

    1. apiVersion: v2
    2. name: mylibchart
    3. description: A Helm chart for Kubernetes
    4. # A chart can be either an 'application' or a 'library' chart.
    5. #
    6. # Application charts are a collection of templates that can be packaged into versioned archives
    7. # to be deployed.
    8. #
    9. # Library charts provide useful utilities or functions for the chart developer. They're included as
    10. # a dependency of application charts to inject those utilities and functions into the rendering
    11. # pipeline. Library charts do not define any templates and therefore cannot be deployed.
    12. # type: application
    13. type: library
    14. # This is the chart version. This version number should be incremented each time you make changes
    15. # to the chart and its templates, including the app version.
    16. version: 0.1.0
    17. # This is the version number of the application being deployed. This version number should be
    18. # incremented each time you make changes to the application and it is recommended to use it with quotes.
    19. appVersion: "1.16.0"

    此时,有必要去检测一下chart是否变成了库chart:

    1. $ helm install mylibchart mylibchart/
    2. Error: library charts are not installable

    现在可以使用库chart了,这意味着要创建另一个脚手架chart:

    只需创建一个配置映射,需要再次清空模板文件:

    1. $ rm -rf mychart/templates/*

    我们需要在Helm模板中创建简单的配置映射,看起来类似下面这样:

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. data:

    我们将复用已经在mylibchart中创建的公共代码。在mychart/templates/configmap.yaml文件中构建配置映射如下:

    1. {{- include "mylibchart.configmap" (list . "mychart.configmap") -}}
    2. {{- define "mychart.configmap" -}}
    3. data:
    4. myvalue: "Hello World"
    5. {{- end -}}

    可以看到这简化了我们通过继承为添加了标准属性的公共配置映射定义必须要做的事情。在模板中添加了配置,在这个示例中的数据key myvalue和值。这个配置会覆盖公共配置映射中的空源。因为我们在上一节中提到的辅助方法mylibchart.util.merge,这是可行的。

    为了能使用通用代码,我们需要添加mylibchart作为依赖。将以下内容添加到mychart/Chart.yaml文件的末尾:

    1. # My common code in my library chart
    2. dependencies:
    3. - name: mylibchart
    4. version: 0.1.0
    5. repository: file://../mylibchart

    这包含了作为文件系统动态依赖的库chart,和我们的应用chart位于同一父路径下。由于将库chart作为动态依赖, 我们需要执行helm dependency update,它会拷贝库chart到你的charts/目录。

    1. $ helm dependency update mychart/
    2. Hang tight while we grab the latest from your chart repositories...
    3. ...Successfully got an update from the "stable" chart repository
    4. Update Complete. Happy Helming!⎈
    5. Saving 1 charts
    6. Deleting outdated charts

    现在我们准备好部署chart了。安装之前,需要先检测渲染过的模板。

    1. $ helm install mydemo mychart/ --debug --dry-run
    2. install.go:159: [debug] Original chart version: ""
    3. install.go:176: [debug] CHART PATH: /root/test/helm-charts/mychart
    4. NAME: mydemo
    5. LAST DEPLOYED: Tue Mar 3 17:48:47 2020
    6. NAMESPACE: default
    7. STATUS: pending-install
    8. REVISION: 1
    9. TEST SUITE: None
    10. USER-SUPPLIED VALUES:
    11. {}
    12. COMPUTED VALUES:
    13. affinity: {}
    14. fullnameOverride: ""
    15. image:
    16. pullPolicy: IfNotPresent
    17. repository: nginx
    18. imagePullSecrets: []
    19. ingress:
    20. annotations: {}
    21. enabled: false
    22. hosts:
    23. - host: chart-example.local
    24. paths: []
    25. tls: []
    26. mylibchart:
    27. global: {}
    28. nameOverride: ""
    29. nodeSelector: {}
    30. podSecurityContext: {}
    31. replicaCount: 1
    32. resources: {}
    33. securityContext: {}
    34. service:
    35. port: 80
    36. type: ClusterIP
    37. serviceAccount:
    38. annotations: {}
    39. create: true
    40. tolerations: []
    41. HOOKS:
    42. ---
    43. # Source: mychart/templates/configmap.yaml
    44. apiVersion: v1
    45. data:
    46. myvalue: Hello World
    47. kind: ConfigMap
    48. metadata:
    49. labels:
    50. app: mychart
    51. chart: mychart-0.1.0
    52. release: mydemo
    53. name: mychart-mydemo

    这个看起来像是我们需要的用myvalue: Hello World覆盖的配置映射。现在安装:

    我们可以检索这个版本并看到实际的版本已经加载。

    1. $ helm get manifest mydemo
    2. ---
    3. # Source: mychart/templates/configmap.yaml
    4. apiVersion: v1
    5. data:
    6. myvalue: Hello World
    7. kind: ConfigMap
    8. metadata:
    9. labels:
    10. app: mychart
    11. chart: mychart-0.1.0
    12. release: mydemo
    13. name: mychart-mydemo
    1. 注意:GitHub上的公共Helm辅助Chart不再被积极维护了,且该仓库已弃用并归档。

    这里有一种快速使用它的方法。更多细节请查看 。

    再创建一个脚手架:

    1. $ helm create demo
    2. Creating demo

    使用辅助chart中的公共代码。首先编辑负载文件demo/templates/deployment.yaml如下:

    1. {{- template "common.deployment" (list . "demo.deployment") -}}
    2. {{- define "demo.deployment" -}}
    3. ## Define overrides for your Deployment resource here, e.g.
    4. apiVersion: apps/v1
    5. spec:
    6. replicas: {{ .Values.replicaCount }}
    7. selector:
    8. matchLabels:
    9. {{- include "demo.selectorLabels" . | nindent 6 }}
    10. template:
    11. metadata:
    12. labels:
    13. {{- include "demo.selectorLabels" . | nindent 8 }}
    14. {{- end -}}

    现在这个service文件demo/templates/service.yaml变成了下面这样:

    1. {{- template "common.service" (list . "demo.service") -}}
    2. {{- define "demo.service" -}}
    3. ## Define overrides for your Service resource here, e.g.
    4. # metadata:
    5. # labels:
    6. # custom: label
    7. # spec:
    8. # ports:
    9. # - port: 8080
    10. {{- end -}}

    这些模板显示了如何从辅助模板中继承公共代码来将你的代码简化到资源的配置或自定义。

    为了能使用公共代码,我们需要添加一个common依赖。在demo/Chart.yaml文件最后啊添加以下内容:

    1. dependencies:
    2. - name: common
    3. repository: "https://charts.helm.sh/incubator/"

    注意:需要添加incubator仓库到Helm仓库列表中(helm repo add)。

    由于我们引用了一个chart作为动态依赖,需要执行helm dependency update。这样会将辅助chart拷贝到你的charts/目录。

    由于辅助chart使用了一些Helm2的结构,所以需要在demo/values.yaml中添加以下内容,确保在Helm 3脚手架中chart更新时可以加载nginx镜像:

    在部署之前可以使用 helm lint 和 命令测试chart模板是否正确。

    如果可以正常运行,使用 helm install 部署。

    NextHelm来源和完整性 →