helm chart 快速入门
個人blog傳送門
概述
helm chart 是一種描述如何部署應用到 kubernetes 中的文檔格式。
helm 項目提供了命令行工具 helm 來進行部署包的管理,并且支持接入 chart 倉庫,如果你用過 linux 各大發行版的源,或者 docker 的鏡像倉庫,相信可以迅速 Get 到這種方案理念。目前官方的中央倉庫為 artifacthub。
下載一個 helm 3 執行程序到本地,然后我們可以通過執行helm repo add <repo-name> <repo-url>來添加開發環境的 chart 倉庫。helm search repo <repo-name>可以瀏覽當前倉庫中有的 chart。
使用helm create <charts-name>可以創建一個初始 chart,文件結構如下:
# tree mychart mychart ├── charts/ # 子 chart ├── Chart.yaml # chart 的概要信息 ├── templates # 模板,對標 k8s 中的種種資源類型 │ ├── deployment.yaml │ ├── _helpers.tpl │ ├── hpa.yaml │ ├── ingress.yaml │ ├── NOTES.txt │ ├── serviceaccount.yaml │ ├── service.yaml │ └── tests │ └── test-connection.yaml └── values.yaml # 一些需要傳遞給模板的值被放置在這里除了 Chart.yaml、values.yaml 是必須要有的,其他都是可選的。當然在實際使用時,我們無疑是需要 templates/ 里的模板的,比如 deployment.yaml、service.yaml,模板從 values.yaml 中獲取值來渲染得到最終的部署相關資源描述文件。
Chart.yaml
Chart.yaml 中主要是放一些概要信息,比如 chart 的名稱、版本、維護者、依賴(即子 chart)。
name: mychart description: A Helm chart for Kubernetes type: application # application or library version: 0.1.0 # chart release version appVersion: "1.16.0" # version of some application inside the chart, such as nginx. Just a message, not important# optional maintainers: - email: <>name: <># optional dependencies: - name: <>version: <>repository: <> # optional注意在 Helm 3 中,不再需要 requirements.yaml 來描述 dependencies 。
在聲明了 dependencies 的 chart 中執行helm dependency update或helm dependency build將會自動生成一個 Chart.lock 文件,且如果設置了依賴項的 repository,會到倉庫中查找并打包為 .tgz 文件下載到 charts/ 路徑下。
當然,你也可以直接使用helm pull <chart-name>直接從倉庫中抓取對應的 chart 到 charts/ 下。
values.yaml
values.yaml 中放置一些需要傳遞給模板的值,在模板文件中你將可以通過 {{.Values.xxx.xxx}} 來進行引用。
# Default values for mychart. # This is a YAML-formatted file. # Declare variables to be passed into your templates. replicaCount: 1image:repository: nginxpullPolicy: IfNotPresent# Overrides the image tag whose default is the chart appVersion.tag: ""imagePullSecrets: [] nameOverride: "" fullnameOverride: ""serviceAccount:# Specifies whether a service account should be createdcreate: true# Annotations to add to the service accountannotations: {}# The name of the service account to use.# If not set and create is true, a name is generated using the fullname templatename: "" podAnnotations: {} podSecurityContext: {}# fsGroup: 2000 securityContext: {}# capabilities:# drop:# - ALL# readOnlyRootFilesystem: true# runAsNonRoot: true# runAsUser: 1000service:type: ClusterIPport: 80ingress:enabled: falseclassName: ""annotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths:- path: /pathType: ImplementationSpecifictls: []# - secretName: chart-example-tls# hosts:# - chart-example.localresources: {}# We usually recommend not to specify default resources and to leave this as a conscious# choice for the user. This also increases chances charts run on environments with little# resources, such as Minikube. If you do want to specify resources, uncomment the following# lines, adjust them as necessary, and remove the curly braces after 'resources:'.# limits:# cpu: 100m# memory: 128Mi# requests:# cpu: 100m# memory: 128Miautoscaling:enabled: falseminReplicas: 1maxReplicas: 100targetCPUUtilizationPercentage: 80# targetMemoryUtilizationPercentage: 80nodeSelector: {}tolerations: []affinity: {} route:host: xxx..cn如果想要約束 values.yaml 中的配置結構,可以創建一個 values.schema.json,如下截取了對 values.yaml 中 route 設置結構的約束。
{"$schema": "http://json-schema.org/schema","properties": {..."route": {"properties": {"host": {"type": "string","description": "ingress full hostname for icc"}}}} }覆蓋子 chart 配置
一個可用服務通常涉及到多個應用,比如業務應用會依賴 redis 之類中間件,這就是 Chart.yaml 中設置 dependencies 的用意。當我們依賴于別人提供的 chart,但在部署時又需要對其中的一些配置進行調整,這時候就可以通過在父 Chart.yaml 中設置能對應到子 chart 中 values.yaml 中的配置的配置項,來達到覆蓋子 chart 中配置值的目的。
假設現在 mychart 有一個 mysubchart 的依賴。
# tree . ├── charts │ └── mysubchart │ ├── charts │ ├── Chart.yaml │ ├── templates │ └── values.yaml ├── Chart.yaml ├── templates └── values.yaml在 mychart/charts/mysubchart/values.yaml 中存在一個配置項 dessert。
dessert: cake可以通過在 mychart/values.yaml 中設置另一個值來覆蓋。指明子 chart 名稱,然后是 values.yaml 中的路徑即可。
mysubchart:dessert: ice-cream最終,我們得到的 chart 的結構可能如下
# tree . ├── Chart.lock ├── charts │ ├── mysubchart │ │ ├── charts │ │ ├── Chart.yaml │ │ ├── templates │ │ │ ├── deployment.yaml │ │ │ ├── _helpers.tpl │ │ │ ├── hpa.yaml │ │ │ ├── ingress.yaml │ │ │ ├── NOTES.txt │ │ │ ├── serviceaccount.yaml │ │ │ ├── service.yaml │ │ │ └── tests │ │ │ └── test-connection.yaml │ │ └── values.yaml │ └── redis.tgz ├── Chart.yaml ├── templates │ ├── deployment.yaml │ ├── _helpers.tpl │ ├── hpa.yaml │ ├── ingress.yaml │ ├── NOTES.txt │ ├── serviceaccount.yaml │ ├── service.yaml │ └── tests │ └── test-connection.yaml ├── values.schema.json └── values.yamlreference
https://helm.sh/zh/docs/chart_template_guide/getting_started/
總結
以上是生活随笔為你收集整理的helm chart 快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vscode无法实现函数跳转,跳转到函数
- 下一篇: vue函数定义的多种写法