• 【云原生】kubernetes应用程序包管理工具Helm


     

     

    Helm

    • 什么是 Helm

    • 安装 Helm

    • 重要概念

    • 使用 Helm

    1 简介

    官网地址: Helm

    Helm是一个Kubernetes应用程序包管理工具,它允许你轻松管理和部署Kubernetes应用程序。Helm通过使用称为Charts的预定义模板来简化Kubernetes应用程序的部署和管理。Chart包含了一组Kubernetes对象定义,可以描述一个应用程序的完整部署和资源需求,包括Deployment、Service、ConfigMap、Secret等。使用Helm,你可以轻松地安装、升级、卸载和回滚Kubernetes应用程序。

    同时,Helm还提供了一些便捷的功能,如依赖管理、全局变量、条件渲染等,可以帮助你更好地管理应用程序的部署。Helm有两个主要的组件:Helm客户端(helm)和Helm服务器(Tiller)。Helm客户端可以在本地运行,而Tiller则运行在Kubernetes集群中,并负责将Charts转换为Kubernetes对象。

    2 安装

    下载地址: Releases · helm/helm · GitHub

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

    1. 下载 需要的版本

    2. 解压(tar -zxvf helm-v3.0.0-linux-amd64.tar.gz)

    3. 在解压目录中找到helm程序,移动到需要的目录中(mv linux-amd64/helm /usr/local/bin/helm)

    4. 添加仓库: helm repo add bitnami https://charts.bitnami.com/bitnami

    5. 验证安装: helm help.

    3 重要概念

    • Chart 代表着 Helm 包。它包含在 Kubernetes 集群内部运行应用程序,工具或服务所需的所有资源定义。你可以把它看作是 Homebrew formula,Apt dpkg,或 Yum RPM 在Kubernetes 中的等价物。

    • Repository(仓库) 是用来存放和共享 charts 的地方。它就像 Perl 的 CPAN 档案库网络 或是 Fedora 的 软件包仓库,只不过它是供 Kubernetes 包所使用的。

    • Release 是运行在 Kubernetes 集群中的 chart 的实例。一个 chart 通常可以在同一个集群中安装多次。每一次安装都会创建一个新的 release。以 MySQL chart为例,如果你想在你的集群中运行两个数据库,你可以安装该chart两次。每一个数据库都会拥有它自己的 releaserelease name

    4 使用

    4.1 搜索 charts

    Helm 自带一个强大的搜索命令,可以用来从两种来源中进行搜索:

    • helm search hubArtifact Hub 中查找并列出 helm charts。 Artifact Hub中存放了大量不同的仓库。

    • helm search repo 从你添加(使用 helm repo add)到本地 helm 客户端中的仓库中进行查找。该命令基于本地数据进行搜索,无需连接互联网。

    4.2 安装

    使用 helm install 命令来安装一个新的 helm 包。最简单的使用方法只需要传入两个参数:你命名的release名字和你想安装的chart的名称。

    1. [root@k8s-node1 ~]# helm install nginx bitnami/nginx
    2. NAME: nginx
    3. LAST DEPLOYED: Wed Apr  5 07:18:04 2023
    4. NAMESPACE: kube-system
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None
    8. NOTES:
    9. CHART NAME: nginx
    10. CHART VERSION: 13.2.34
    11. APP VERSION: 1.23.4
    12. ** Please be patient while the chart is being deployed **
    13. NGINX can be accessed through the following DNS name from within your cluster:
    14.   nginx.kube-system.svc.cluster.local (port 80)
    15. To access NGINX from outside the cluster, follow the steps below:
    16. 1. Get the NGINX URL by running these commands:
    17. NOTE: It may take a few minutes for the LoadBalancer IP to be available.
    18.       Watch the status with: 'kubectl get svc --namespace kube-system -w nginx'
    19.    export SERVICE_PORT=$(kubectl get --namespace kube-system -o jsonpath="{.spec.ports[0].port}" services nginx)
    20.    export SERVICE_IP=$(kubectl get svc --namespace kube-system nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    21.    echo "http://${SERVICE_IP}:${SERVICE_PORT}"

    注意: 安装chart时创建了一个新的 release 对象。上述发布被命名为 nginx。 (如果想让Helm生成一个名称,删除发布名称并使用--generate-name。)

    helm 安装顺序: Helm | 使用Helm


    4.3 查看列表

    你可以通过 helm list 命令看到当前部署的所有 release:

    1. [root@k8s-node1 ~]# helm list
    2. NAME   NAMESPACE       REVISION       UPDATED                                 STATUS         CHART           APP VERSION
    3. nginx   kube-system     1               2023-04-05 07:34:48.421708425 -0400 EDT deployed       nginx-13.2.34   1.23.4
    4.3 查看状态

    你可以使用 helm status 来追踪 release 的状态,或是重新读取配置信息:

    1. [root@k8s-node1 ~]# helm status nginx
    2. NAME: nginx
    3. LAST DEPLOYED: Wed Apr  5 07:18:04 2023
    4. NAMESPACE: kube-system
    5. STATUS: deployed
    6. REVISION: 1
    7. TEST SUITE: None
    8. NOTES:
    9. CHART NAME: nginx
    10. CHART VERSION: 13.2.34
    11. APP VERSION: 1.23.4
    12. ** Please be patient while the chart is being deployed **
    13. NGINX can be accessed through the following DNS name from within your cluster:
    14.   nginx.kube-system.svc.cluster.local (port 80)
    15. To access NGINX from outside the cluster, follow the steps below:
    16. 1. Get the NGINX URL by running these commands:
    17. NOTE: It may take a few minutes for the LoadBalancer IP to be available.
    18.       Watch the status with: 'kubectl get svc --namespace kube-system -w nginx'
    19.    export SERVICE_PORT=$(kubectl get --namespace kube-system -o jsonpath="{.spec.ports[0].port}" services nginx)
    20.    export SERVICE_IP=$(kubectl get svc --namespace kube-system nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    21.    echo "http://${SERVICE_IP}:${SERVICE_PORT}"
    4.4 卸载
    1. [root@k8s-node1 ~]# helm uninstall nginx
    2. release "nginx" uninstalled

    5 安装自定义 Chart

    上述安装方式只会使用 chart 的默认配置选项。很多时候,我们需要自定义 chart 来指定我们想要的配置。

    5.1 拉取自定义的 chart
    [root@k8s-node1 ~]# helm create app

    注意: 当前目录中会出现一个 nginx-xxx.tgz 文件 这个文件就是 charts 的包。

    5.2 查看 chart 目录结构
    1. app/
    2. Chart.yaml
    3. values.yaml
    4. charts/
    5. templates/
    6. ...
    • templates/ 目录包括了模板文件。当Helm评估chart时,会通过模板渲染引擎将所有文件发送到templates/目录中。 然后收集模板的结果并发送给Kubernetes。

    • values.yaml 文件也导入到了模板。这个文件包含了chart的 默认值 。这些值会在用户执行helm installhelm upgrade时被覆盖。

    • Chart.yaml 文件包含了该chart的描述。你可以从模板中访问它。charts/目录 可以 包含其他的chart(称之为 子chart)。

    5.3 自定义模板
    $ rm -rf mychart/templates/*
    • 编写 namespace.yaml

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: {{ .Chart.Name }}
    5. namespace: {{ .Values.namespace }}
    • 编写 deployment.yml

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: {{ .Chart.Name}}
    5. namespace: {{.Values.namespace}}
    6. labels:
    7.   app: {{ .Chart.Name}}
    8. spec:
    9. replicas: {{ .Values.replicas }}
    10. template:
    11.   metadata:
    12.     name: {{ .Chart.Name}}
    13.     labels:
    14.       app: {{ .Chart.Name}}
    15.   spec:
    16.     containers:
    17.       - name: {{ .Chart.Name}}
    18.         image: {{ .Values.image}}
    19.         imagePullPolicy: {{.Values.imagePullPolicy}}
    20.         ports:
    21.           - containerPort: {{.Values.containerPort}}
    22.     restartPolicy: {{ .Values.restartPolicy }}
    23. selector:
    24.   matchLabels:
    25.     app: {{ .Chart.Name}}
    • 编写 service.yml

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: {{.Chart.Name}}
    5. namespace: {{.Values.namespace}}
    6. spec:
    7. selector:
    8.   app: {{.Chart.Name}}
    9. ports:
    10.   - port: {{.Values.service.port}}
    11.     targetPort: {{.Values.containerPort}}
    12. type: {{ .Values.service.type }}
    • 编写 Chart.yml

    1. apiVersion: v2
    2. name: app
    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. # This is the chart version. This version number should be incremented each time you make changes
    14. # to the chart and its templates, including the app version.
    15. # Versions are expected to follow Semantic Versioning (https://semver.org/)
    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. Versions are not expected to
    19. # follow Semantic Versioning. They should reflect the version the application is using.
    20. # It is recommended to use it with quotes.
    21. appVersion: "1.16.0"
    • 编写 values.yaml

    1. replicas: 1
    2. namespace: app
    3. image: nginx:1.19
    4. imagePullPolicy: IfNotPresent
    5. restartPolicy: Always
    6. containerPort: 80
    7. service:
    8. port: 80
    9. type: ClusterIP
    5.4 验证是否存在错误
    $ helm lint app
    5.5 打包自定义 chart
    $ helm package app
    5.6 安装 chart
    $ helm install app myapp-1.tgz

  • 相关阅读:
    88. 合并两个有序数组 (Swift版本)
    HarmonyOS【ArkUI组件--TextInput】
    解决windeployqt打包exe的“VCINSTALLDIR is not set“问题
    webpack
    flowable-ui部署
    小程序上新(2022.10.13~11.14)
    【Linux kernel/cpufreq】framework ----cpufreq core(1)
    WindowAssigner设计
    掌握Perl并发:线程与进程编程全攻略
    手机穿戴设备能力共享,提升丰富交互体验
  • 原文地址:https://blog.csdn.net/weixin_53678904/article/details/132375509