helm chart 介绍

一、chart介绍

Helm 使用一种名为 charts 的包格式,一个 chart 是描述一组相关的 Kubernetes 资源的文件集合,单个 chart 可能用于部署简单的应用,比如 nginx pod,或者复杂的应用,比如一个完成的lnmp应用程序。

Charts 是创建在特定目录下面的文件集合,然后可以将它们打包到一个版本化的存档中来部署。接下来我们就来看看使用 Helm 构建 charts 的一些基本方法。

二、chart概念

  • 创建chart
##创建一个名为testchart的chart
[root@node1 ~]# helm create   testchart
Creating testchart
[root@node1 ~]# 

####查看testchart的目录结构
[root@node1 ~]# tree testchart/
testchart/
├── charts         # 包含该 chart 依赖的所有 chart 的目录
├── Chart.yaml     # 包含当前 chart 信息的 YAML 文件
├── templates      ## 模板目录,与 values 结合使用时,将渲染生成 Kubernetes 资源清单文件
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml    # 当前 chart 的默认配置 values

3 directories, 10 files
[root@node1 ~]# 
  • chart.yaml文件
###默认的chart.yaml文件内容如下:
[root@node1 testchart]# cat Chart.yaml   | grep -Ev "(^#|^$)"
apiVersion: v2     ###chart API 版本 (必须)
name: testchart    ##chart 名 (必须)
description: A Helm chart for Kubernetes  ###一句话描述 (可选)
type: application  ###chart 类型 (可选)
version: 0.1.0   ##SemVer 2版本 (必须)
appVersion: "1.16.0"   ##包含的应用程序版本 (可选).
[root@node1 testchart]# 

###还有其他可选的配置项
kubeVersion: 兼容的 Kubernetes 版本 (可选)
keywords:
  - 当前项目关键字集合 (可选)
home: 当前项目的 URL (可选)
sources:
  - 当前项目源码 URL (可选)
dependencies: # chart 依赖列表 (可选)
  - name: chart 名称 (nginx)
    version: chart 版本 ("1.2.3")
    repository: 仓库地址 ("https://example.com/charts")
maintainers: # (可选)
  - name: 维护者名字 (对每个 maintainer 是必须的)
    email: 维护者的 email (可选)
    url: 维护者 URL (可选)
  • version
###在chart中有三种verion
apiVersion: v2
version: 0.1.0
appVersion: "1.16.0"


####version
每个 chart 都必须有一个版本号,版本必须遵循 SemVer2 标准,Kubernetes Helm 使用版本号作为 release 的标记,仓库中的软件包通过名称加上版本号来标识的。
如下将testchart打包
[root@node1 ~]#  helm  package testchart/
Successfully packaged chart and saved it to: /root/testchart-0.1.0.tgz
###可以看到包的名称与version 0.1.0对应


###apiVersion
对于 Helm 3 以上的版本 apiVersion 字段应该是 v2,之前版本的 Chart 应该设置为1,并且也可以有 Helm 3 进行安装。

###appVersion
appVersion字段与version字段无关,这是一种指定应用程序版本的方法,比如 mysql的Chart包可能有一个 appVersion: 5.7.0 的字段,表示Chart中包含的mysql版本是5.7.0,但是该字段对Chart版本的计算不会产生影响。
  • chart类型
type 字段定义 chart 的类型,可以定义两种类型:应用程序(application)和库(library)。应用程序是默认的类型,它是一个可以完整操作的标准 chart,库或者辅助类 chart 为 chart 提供了一些实用的功能,library 不同于应用程序 chart,因为它没有资源对象,所以无法安装。
  • dependencies(依赖)
在使用Helm部署应用时,一个 chart 包可能会依赖许多其他的 chart。这些依赖关系可以使用 Chart.yaml 中的依赖关系字段动态链接,也可以引入到 charts/ 目录手动进行管理,如下:
dependencies:
  - name: nginx
    version: 3.1.34
    repository: https://test1.com/charts
  - name: mysql
    version: 5.7.0
    repository: https://test2.com/charts
    
1:name 字段是所依赖的 chart 的名称
2:version 字段是依赖的 chart 版本
3:repository 字段是 chart 仓库的完整 URL,不过需要注意,必须使用 helm repo add 在本地添加该 repo

定义好了依赖项后,可以运行 helm dependency update 来更新依赖项,将根据你的依赖项配置将所有指定的 chart包下载到 charts/ 目录中:

[root@node1 ~]#  helm dependency update testchart

################################
还可以为依赖的包这是alias别名,如下:
dependencies:
  - name: nginx
    version: 3.1.34
    repository: https://test1.com/charts
    alias: nginx-1
  - name: nginx
    version: 3.1.34
    repository: https://test1.com/charts
    alias: nginx-2
  - name: nginx
    version: 3.1.34
    repository: https://test1.com/charts   
    
如果再运行helm dependency update命令,会在charts目录或得三个包
  • 模板
使用helm部署应用时,所使用的模板文件都放在templates/目录下,里面包含了应用启动所需的所有的参数
[root@node1 testchart]# tree templates/
templates/
├── deployment.yaml
├── _helpers.tpl
├── hpa.yaml
├── ingress.yaml
├── NOTES.txt
├── serviceaccount.yaml
├── service.yaml
└── tests
    └── test-connection.yaml

1 directory, 8 files
[root@node1 testchart]# 

###以deployment.yaml文件为例
[root@node1 testchart]# cat templates/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "testchart.fullname" . }}
  labels:
    {{- include "testchart.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "testchart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "testchart.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "testchart.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
[root@node1 testchart]# 

#####以上定义的values值都是在values文件中定义的,后面会讲到
  • values
上面提到,模板中使用的values值都是通过values文件定义的,当创建chart时,默认的values文件为values.yaml,如下:
[root@node1 testchart]# ls
charts  Chart.yaml  templates  values.yaml
[root@node1 testchart]# 
[root@node1 testchart]# cat values.yaml  | grep -Ev "(^$|#^)"
# Default values for testchart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
  repository: nginx
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: ""
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""
podAnnotations: {}
podSecurityContext: {}
  # fsGroup: 2000
securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000
service:
  type: ClusterIP
  port: 80
ingress:
  enabled: false
  className: ""
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local
resources: {}
  # 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: 128Mi
autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 100
  targetCPUUtilizationPercentage: 80
  # targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}
[root@node1 testchart]#

###当安装一个release时,如果使用默认的values.yaml文件,则可以使用
[root@node1 testchart]# helm install test .   ###用点来代替默认的values文件

如果要使用自定义的values文件,则可以使用如下命令
[root@node1 testchart]# helm install -f test-values.yaml test ###使用-f指定要使用的values文件即可
###########################
关于变量的定义后面会详细讲到,此处只做简单了解

三、chart仓库

chart 仓库实际上就是一个 HTTP 服务器,其中包含一个或多个打包的 chart 包,虽然可以使用 helm 来管理本地 chart 目录,但是在共享 charts 的时候,最好的还是使用 chart 仓库。

可以提供 YAML 文件和 tar文件并可以相应 GET 请求的任何 HTTP 服务器都可以作为 chart 仓库服务器。仓库的主要特征是存在一个名为 index.yaml 的特殊文件,该文件具有仓库中提供的所有软件包的列表以及允许检索和验证这些软件包的元数据。

在客户端,可以使用 helm repo 命令来管理仓库,但是 Helm 不提供用于将 chart 上传到远程 chart 仓库的工具。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值