依赖管理

Chart依赖关系

Helm中,一个chart可能依赖于任何数量的其他chart。这些依赖关系可以通过requirements.yaml文件动态链接或引入charts/目录并手动管理。虽然有一些团队需要手动管理依赖关系的优势,但声明依赖关系的首选方法是使用chart内部的requirements.yaml文件。

requirements.yaml来管理依赖关系

requirements.yaml文件是列出chart的依赖关系的简单文件。

dependencies:
  - name: apache
    version: 1.2.3
    repository: http://example.com/charts
  - name: mysql
    version: 3.2.1
    repository: http://another.example.com/charts
  • name字段是chart的名称。
  • version字段是chart的版本。
  • repository字段是chart repo的完整URL。请注意,还必须使用helm repo add添加该repo到本地才能使用。

有了依赖关系文件,你可以通过运行helm dependency update,它会使用你的依赖关系文件将所有指定的chart下载到你的charts/目录中。

$ helm dep up foochart
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "local" chart repository
...Successfully got an update from the "stable" chart repository
...Successfully got an update from the "example" chart repository
...Successfully got an update from the "another" chart repository
Update Complete. Happy Helming!
Saving 2 charts
Downloading apache from repo http://example.com/charts
Downloading mysql from repo http://another.example.com/charts

helm dependency update检索chart时,它会将它们作为chart存档存储在charts/目录中。因此,对于上面的示例,可以在chart目录中看到以下文件:

charts/
  apache-1.2.3.tgz
  mysql-3.2.1.tgz

通过requirements.yaml管理chart是一种轻松更新chart的好方法,还可以在整个团队中共享requirements信息。

requirements.yaml中的alias字段

除上述其他字段外,每个requirement条目可能包含可选字段alias。为依赖的chart添加别名会将chart放入依赖关系中,并使用别名作为新依赖关系的名称。如果需要使用其他名称访问chart,可以使用alias

# parentchart/requirements.yaml
dependencies:
  - name: subchart
    repository: http://localhost:10191
    version: 0.1.0
    alias: new-subchart-1
  - name: subchart
    repository: http://localhost:10191
    version: 0.1.0
    alias: new-subchart-2
  - name: subchart
    repository: http://localhost:10191
    version: 0.1.0

在上面的例子中,我们将得到parentchart3个依赖关系:

subchart
new-subchart-1
new-subchart-2

requirements.yaml中的tagscondition字段

除上述其他字段外,每个需求条目可能包含可选字段tagscondition。所有charts都会默认加载。如果存在tagscondition字段,将对它们进行评估并用于控制应用的chart的加载。

  • Condition: condition字段包含一个或多个YAML路径(用逗号分隔。如果此路径存在于顶级父级的值中并且解析为布尔值,则将根据该布尔值启用或禁用chart。只有在列表中找到的第一个有效路径才被评估,如果没有路径存在,那么该条件不起作用。

  • Tags:标签字段是与此chart关联的YAML标签列表。在顶级父级的值中,可以通过指定标签和布尔值来启用或禁用所有带有标签的chart

# parentchart/requirements.yaml
dependencies:
  - name: subchart1
    repository: http://localhost:10191
    version: 0.1.0
    condition: subchart1.enabled, global.subchart1.enabled
    tags:
      - front-end
      - subchart1

  - name: subchart2
    repository: http://localhost:10191
    version: 0.1.0
    condition: subchart2.enabled,global.subchart2.enabled
    tags:
      - back-end
      - subchart2
# parentchart/values.yaml

subchart1:
  enabled: true
tags:
  front-end: false
  back-end: true

在上面的示例中,所有带有标签的front-endcharts都将被禁用,但由于subchart1.enabled的值在父项值中为 “真”,因此条件将覆盖该front-end标签,subchart1会启用。由于subchart2被标记back-end和标签的计算结果为truesubchart2将被启用。还要注意的是,虽然subchart2有一个在requirements.yaml中指定的条件,但父项的值中没有对应的路径和值,因此条件无效。

--set 参数可使用来更改tagconditions值。

helm install --set tags.front-end=true --set subchart2.enabled=false
  • Conditions (设置values)会覆盖tags配置。第一个存在的condition路径生效,后续该chartcondition路径将被忽略。
  • 如果chart的某tag的任一tag的值为true,那么该tag的值为true,并启用这个chart
  • Tagsconditions值必须在顶级父级的值中进行设置。
  • tags: 值中的关键字必须是顶级关键字。目前不支持全局和嵌套 tags: 表格。

依赖管理

所有使用helm部署的应用中如果没有特别指定chart的名字都会生成一个随机的Release name,例如romping-frogsexy-newton等,跟启动docker容器时候容器名字的命名规则相同,而真正的资源对象的名字是在YAML文件中定义的名字,我们成为App name,两者连接起来才是资源对象的实际名字:Release name-App name。

而使用helm chart部署的包含依赖关系的应用,都会使用同一套Release name,在配置YAML文件的时候一定要注意在做服务发现时需要配置的服务地址,如果使用环境变量的话,需要像下面这样配置。

env:
  - name: SERVICE_NAME
    value: "{{ .Release.Name }}-{{ .Values.image.env.SERVICE_NAME }}"

这是使用了Go template的语法。至于 {{ .Values.image.env.SERVICE_NAME }} 的值是从values.yaml文件中获取的,所以需要在values.yaml中增加如下配置:

image:
  env:
    SERVICE_NAME: k8s-app-monitor-test

本地依赖

在本地当前chart配置的目录下启动helm server,我们不指定任何参数,直接使用默认端口启动。

helm serve

将该repo加入到repo list中。

helm repo add local http://localhost:8879

在浏览器中访问 http://localhost:8879 可以看到所有本地的chart

然后下载依赖到本地。

helm dependency update

这样所有的chart都会下载到本地的 charts 目录下。

共享

我们可以修改Chart.yaml中的helm chart配置信息,然后使用下列命令将chart打包成一个压缩文件。

$ helm package .

打包出mychart-0.1.0.tgz文件,我们可以在requirements.yaml中定义应用所依赖的chart,例如定义对mariadb的依赖:

dependencies:
  - name: mariadb
    version: 0.6.0
    repository: https://kubernetes-charts.storage.googleapis.com

使用helm lint .命令可以检查依赖和模板配置是否正确。

上一页