Helm Chart三分钟轻松掌握


我们的日常工作中需创建、修改和部署Helm Chart,以管理应用程序的部署。Helm是Kubernetes的应用程序包管理器,它负责协调应用程序的下载、安装和部署。 chart就是一个描述Kubernetes相关资源的文件集合。

​ 那么为什么会有人使用 Helm 呢? Helm 通过模板化方法在 Kubernetes 中更轻松地管理应用程序的部署。所有 Helm 图表都遵循相同的结构。不必为每个应用程序手动编写单独的YAML文件,只需创建一个Helm chart,让Helm为你将应用程序部署到集群。

安装Helm

Helm 的每个 版本都为各种操作系统提供二进制版本。这些二进制版本可以手动下载和安装。

  1. 下载 版本
  2. 打开包装 ( tar -zxvf helm-v3.0.0-linux-amd64.tar.gz)
  3. 在解压后的目录中找到helm二进制文件,并将其移动到所需的目的地 ( mv linux-amd64/helm /usr/local/bin/helm)

创建Helm Chart

首先确认我们刚刚的helm已经安装成功:

[root@master ~]# which helm
/usr/local/bin/helm
[root@master ~]# helm --help
The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

启动一个新的Helm图标只需要一个简单的命令:

helm create mychartname

为了贴合线上使用,将创建chart auto-excels

[root@master ~]# helm create auto-excels^C
[root@master ~]# cd auto-excels/
[root@master auto-excels]# ll
total 16
drwxr-xr-x 2 root root 4096 Sep 21 20:46 charts
-rw-r--r-- 1 root root 1146 Sep 21 20:46 Chart.yaml
drwxr-xr-x 3 root root 4096 Sep 22 10:09 templates
-rw-r--r-- 1 root root 1941 Sep 22 10:23 values.yaml

检查图表结构

刚刚我们创建了图表,看看它的结构,看看里面有什么。 Chart.yamlvalues.yaml 这两个文件定义了图表的名称以及部署时将包含那些值。

Chart.yaml
apiVersion: v2         ##API 版本
name: auto-excel	## 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		##chart类型

# 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"

chart部分包括了正在使用的API版本(必需),图表的名称和描述,图表的类型(默认为application),图表的版本以及应用程序的版本(应用版本会在更改时增加)。

Template

图表中最重要的部分时模板目录。 它包含将部署到集群中的应用程序的所有配置。这个应用程序有一个基本的部署、入口、服务帐户和服务。该目录还包括一个测试目录,其中包括一个连接到应用程序的测试。这些应用程序功能中的每一个在**templates/**下都有自己的模板文件:

[root@master auto-excels]# ll templates/
total 32
-rw-r--r-- 1 root root 1853 Sep 22 10:09 deployment.yaml
-rw-r--r-- 1 root root 1812 Sep 21 20:46 _helpers.tpl
-rw-r--r-- 1 root root  925 Sep 21 20:46 hpa.yaml
-rw-r--r-- 1 root root 2085 Sep 21 20:46 ingress.yaml
-rw-r--r-- 1 root root 1759 Sep 21 20:46 NOTES.txt
-rw-r--r-- 1 root root  326 Sep 21 20:46 serviceaccount.yaml
-rw-r--r-- 1 root root  370 Sep 21 20:46 service.yaml
drwxr-xr-x 2 root root 4096 Sep 21 20:46 tests

还有另外一个charts目录,它里面时空的。 它允许您添加部署应用程序所需的相关图表。这个高级的玩法我们先略过,让charts/ 文件夹先为空。

value

当Helm渲染chart时,它会通过模板引擎遍历目录中的每个文件。

模板的Value通过两种方式提供:

  • Chart开发者可以在chart中提供一个命名为 values.yaml 的文件。这个文件包含了默认值。
  • Chart用户可以提供一个包含了value的YAML文件。可以在命令行使用 helm install命令时提供。

当用户提供自定义value时,这些value会覆盖chart的values.yaml文件中value。

# Default values for auto-excels.
# 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: {}

从顶部开始,您可以看到replicaCount自动设置为 1,这意味着只会出现一个 pod。对于这个示例,您只需要一个 pod,但您可以看到告诉 Kubernetes 运行多个 pod 以实现冗余是多么容易。

image部分有两个地方需要查看:repositorypullPolicypillPolicy设置为IfNotPresent;这意味着如果集群中不存在该镜像,才回去拉取该镜像版本。

replicaCount: 1

image:
  repository: hub.17usoft.com/gstrain/test
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: "master-183b8d7"

这个还有另外两个选项:Always, 这意味着它将在每次部署或重新启动时拉取镜像(建议), 以及Latest,它将始终拉取最新版本的可用的图像 。这些都是容器的基本知识,相信大家一定都很熟悉。

Naming and secrets

接下来,看一下图表中的 nameOverride 。第一个 nameOverride 是imagePullSecrets,这是一个用于提取私密的设置。接下来是nameOverridefullnameOverride。从您运行helm create的那一刻起,它的名称 (autoexcel) 就被添加到了许多配置文件中——从上面的 YAML 文件到templates/_helper.tpl文件。如果您在创建图表后需要重命名图表,则在此处修改。

imagePullSecrets: [{ name: first-registry}]
nameOverride: "autoexcel"
fullnameOverride: "autoexcel"

Accounts

服务帐户提供用户身份以在集群内的 pod 中运行。 如果留空,将使用helpers.tpl文件根据全名生成名称。

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: "autoexcel"

Security

您可以配置 pod 安全性来设置要使用的文件系统组类型或可以使用和不可以使用的用户的限制。了解这些选项对于保护 Kubernetes pod 很重要,当我们深入学习k8s的时候再去了解,现在我们暂时为空。

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

Service

图表中有两种不同类型的网络选项。

ClusterIP: 将服务暴露在集群内部 IP 上。选择此值会使与您的应用程序关联的服务只能从集群内访问。

NodePort: 在ClusterIP基础上暴露出一个随机端口至外部,供外部访问

service:
  type: NodePort
  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

Resoueces

默认情况下,Pod 运行没有 CPU 和内存的限额。 这意味着系统中的任何 Pod 将能够像执行该 Pod 所在的节点一样,消耗足够多的 CPU 和内存。

一般会针对某些应用的 pod 资源进行资源限制,这个资源限制是通过resources 的 requests 和 limits 来实现

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

Tolerations, nodeselectors, and affinities

这一部分主要将的是资源调度,有兴趣的同学可以了解一下之前这边文章。

Deploy

现在我们已经对创建的Helm图表进行了必要的修改,我们可以使用install命令来部署它

[root@master project]# helm install auto-excels auto-excels/ --values auto-excels/values.yaml -n default
NAME: auto-excels
LAST DEPLOYED: Thu Sep 22 22:19:46 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=autoexcel,app.kubernetes.io/instance=auto-excels" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT

该命令的输出将为您提供连接到应用程序的后续步骤,包括设置端口转发,以便您可以从本地主机访问应用程序来验证应用是否正常。

[root@master project]# kubectl get po -n default
NAME                          READY   STATUS    RESTARTS        AGE
autoexcel-5f846d8c6c-ccjvg    1/1     Running   0   			8m52s
[root@master project]# kubectl get svc -n default
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
autoexcel    NodePort    10.253.110.124   <none>        8889:45511/TCP   8m52s

恭喜!您已经学会了使用Helm图表的部署,刚开始我们可以选择nginx来进行学习和试验。

到这里只是第一步,我们还有很多东西要学习。如果想更深入的学习Helm,可到官网学习

参考文献:

https://helm.sh/docs/

https://www.jianshu.com/p/026f17489f2b

https://zhuanlan.zhihu.com/p/511413922

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力做一名技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值