ITPub博客

首页 > 云计算 > Docker/K8 > Helm入门(一篇就够了)

Helm入门(一篇就够了)

Docker/K8 作者:Linksla 时间:2023-06-01 09:34:29 0 删除 编辑

一、前言  

在  k8s 中,我们很多时候需要部署很多个应用,特别是微服务的项目,如果每个服务部署都需要使用 kubectl apply依次执行,这将是一件很痛苦的事。

这个时候,如果一键部署所有应用,使用 Helm ( https://helm.sh )是一个很不错的选择,它具备如下的能力:

  • 简化部署 :Helm允许使用单个命令轻松部署和管理应用程序,从而简化了整个部署过程;

  • 高度可配置:Helm Charts提供了高度可配置的选项,可以轻松自定义和修改应用程序的部署配置;

  • 版本控制 :Helm允许管理应用程序的多个版本,从而轻松实现版本控制和回滚;

  • 模板化:Helm Charts使用YAML模板来定义Kubernetes对象的配置,从而简化了配置过程,并提高了可重复性和可扩展性;

  • 应用程序库:Helm具有应用程序库的概念,可以轻松地共享和重用Helm Charts,从而简化了多个应用程序的部署和管理;

  • 插件系统:Helm拥有一个强大的插件系统,允许您扩展和定制Helm的功能,以满足特定的需求和要求。

Helm本质就是一个k8s的包管理器。


二、Helm工作流程 

以下是Helm的工作流程( 注意 : 这里使用的是Helm的v3版本,该版本没有了 tiller而是用更加简单和灵活的架构, 直接通过 kubeconfig连接 apiserver ,简化安全模块,降低了用户的使用壁垒 ):

图片

如上图所示,Helm的工作流程总结如下:

  1. 开发者首先创建并编辑chart的配置;

  2. 接着打包并发布至Helm的仓库(Repository);

  3. 当管理员使用helm命令安装时,相关的依赖会从仓库下载;

  4. 接着helm会根据下载的配置部署资源至k8s。


三、Helm概念 

在使用Helm的过程中,需要理解如下的几个核心的概念:

概念
描述
Chart 一个Helm包,其中包含了运行一个应用所需要的镜像、依赖和资源定义等,还可能包含Kubernetes集群中的服务定义,类似Homebrew中的formula、APT的dpkg或者Yum的rpm文件
Repository 存储Helm Charts的地方
Release Chart在k8s上运行的Chart的一个实例,例如,如果一个MySQL Chart想在服务器上运行两个数据库,可以将这个Chart安装两次,并在每次安装中生成自己的Release以及Release名称
Value Helm Chart的参数,用于配置Kubernetes对象
Template 使用Go模板语言生成Kubernetes对象的定义文件
Namespace Kubernetes中用于隔离资源的逻辑分区

四、Helm的使用 

下面简单讲下Helm的使用。



4.1 安装Helm

首先需要在本地机器或Kubernetes集群上安装Helm 。可以从Helm官方网站下载适合自己平台的二进制文件,或使用包管理器安装Helm,安装教程参考  https://helm.sh 。



4.2 创建Chart

使用helm create命令创建一个新的Chart,Chart目录包含描述应用程序的文件和目录,包括 Chart.yaml、values.yaml、templates目录等;

例如:在本地机器上使用 helm create命令创建一个名为 wordpress的 Chart: 图片

在当前文件夹,可以看到创建了一个wordpress的目录,且里面的内容如下: 图片



4.3 配置Chart

使用编辑器编辑Chart配置文件,包括Chart.yaml和values.yaml 。


Chart.yaml:

Chart.yaml 包含 Chart 的元数据和依赖项

Chart.yaml的模板及注释如下:

    
    
    apiVersion: chart API 版本 (必需)  #必须有
    
    
    name: chart名称 (必需)     # 必须有 
    
    
    version: 语义化2 版本(必需) # 必须有
    
    
    
    kubeVersion: 兼容Kubernetes版本的语义化版本(可选) description: 一句话对这个项目的描述(可选) type: chart类型 (可选) keywords:   - 关于项目的一组关键字(可选) home: 项目home页面的URL (可选) sources:   - 项目源码的URL列表(可选) dependencies: # chart 必要条件列表 (可选)   - name: chart名称 (nginx)     version: chart版本 ("1.2.3")     repository: (可选)仓库URL ("https://example.com/charts") 或别名 ("@repo-name")     condition: (可选) 解析为布尔值的yaml路径,用于启用/禁用chart (e.g. subchart1.enabled )     tags: # (可选)       - 用于一次启用/禁用 一组chart的tag     import-values: # (可选)       - ImportValue 保存源值到导入父键的映射。每项可以是字符串或者一对子/父列表项     alias: (可选) chart中使用的别名。当你要多次添加相同的chart时会很有用
    maintainers: # (可选) # 可能用到   - name: 维护者名字 (每个维护者都需要)     email: 维护者邮箱 (每个维护者可选)     url: 维护者URL (每个维护者可选)
    icon: 用做icon的SVG或PNG图片URL (可选) appVersion: 包含的应用版本(可选)。不需要是语义化,建议使用引号 deprecated: 不被推荐的chart (可选,布尔值) annotations:   example: 按名称输入的批注列表 (可选).

    举例:

      name: nginx-helmapiVersion: v1version: 1.0.0


      Values.yaml:

      values.yaml包含应用程序的默认配置值,举例:

        image:  repository: nginx  tag: '1.19.8'


        templates:

        在模板中引入values.yaml里的配置,在模板文件中可以通过  .VAlues 对象访问到,例如:

          apiVersion: apps/v1kind: Deploymentmetadata:  name: nginx-helm-{{ .Values.image.repository }}spec:  replicas: 1  selector:    matchLabels:      app: nginx-helm  template:    metadata:      labels:        app: nginx-helm    spec:      containers:      - name: nginx-helm        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}        ports:        - containerPort: 80          protocol: TCP




          4.4 打包Chart

          使用helm package命令将Chart打包为一个tarball文件,例如在wordpress目录中使用helm package命令将Chart打包为一个tarball文件, 这将生成一个名为 wordpress-0.1.0.tgz 的 tarball 文件:

            helm package wordpress/



            4.5 发布Chart

            将打包好的Chart发布到一个Helm Repository中。可以使用  helm repo add  命令添加一个Repository,然后使用helm push命令将Chart推送到Repository中,例如:

              helm repo add myrepo https://example.com/chartshelm push wordpress-0.1.0.tgz myrepo



              4.6 安装Release

              使用helm install命令安装Chart的Release,可以通过命令行选项或指定values.yaml文件来配置Release,例如:

                helm install mywordpress myrepo/wordpress

                这将在 Kubernetes 集群中创建一个名为 mywordpress 的 Release ,包含 WordPress 应用程序和 MySQL 数据库。



                4.7 管理Release

                使用 helm ls 命令查看当前运行的Release列表,例如:

                  helm upgrade mywordpress myrepo/wordpress --set image.tag=5.7.3-php8.0-fpm-alpine

                  这将升级  mywordpress  的 WordPress 应用程序镜像版本为 5.7.3-php8.0-fpm-alpine 。


                  可以使用 helm rollback 命令回滚到先前版本,例如:

                    helm rollback mywordpress 1

                    这将回滚 mywordpress 的版本到1。



                    4.8 更新Chart

                    在应用程序更新时,可以更新Chart配置文件和模板,并使用helm package命令重新打包Chart。然后可以使用helm upgrade命令升级已安装的Release,可以按照以下步骤更新Chart:

                    1. 在本地编辑Chart配置或添加新的依赖项;

                    2. 使用helm package命令打包新的Chart版本;

                    3. 使用helm push命令将新的Chart版本推送到Repository中;

                    4. 使用helm repo update命令更新本地或远程的Helm Repository;

                    5. 使用helm upgrade命令升级现有Release到新的Chart版本。

                    例如,可以使用以下命令更新WordPress的Chart版本:

                      helm upgrade mywordpress myrepo/wordpress --version 0.2.0

                      这将升级mywordpress的Chart版本到0.2.0,其中包括新的配置和依赖项。


                      如果需要删除一个Release,可以使用helm uninstall命令。例如:

                        helm uninstall mywordpress

                        这将删除名为mywordpress的Release,同时删除WordPress应用程序和MySQL数据库。


                        如果需要删除与Release相关的PersistentVolumeClaim,可以使用helm uninstall命令的--delete-data选项,例如:

                          helm uninstall mywordpress --delete-data

                          这将删除名为mywordpress的Release,并删除与之相关的所有PersistentVolumeClaim。


                          五、Helm的执行安装顺序

                          Helm按照以下顺序安装资源:

                          • Namespace

                          • NetworkPolicy

                          • ResourceQuota

                          • LimitRange

                          • PodSecurityPolicy

                          • PodDisruptionBudget

                          • ServiceAccount

                          • Secret

                          • SecretList

                          • ConfigMap

                          • StorageClass

                          • PersistentVolume

                          • PersistentVolumeClaim

                          • CustomResourceDefinition

                          • ClusterRole

                          • ClusterRoleList

                          • ClusterRoleBinding

                          • ClusterRoleBindingList

                          • Role

                          • RoleList

                          • RoleBinding

                          • RoleBindingList

                          • Service

                          • DaemonSet

                          • Pod

                          • ReplicationController

                          • ReplicaSet

                          • Deployment

                          • HorizontalPodAutoscaler

                          • StatefulSet

                          • Job

                          • CronJob

                          • Ingress

                          • APIService

                          Helm 客户端不会等到所有资源都运行才退出,可以使用 helm status 来追踪 release 的状态,或是重新读取配置信息:

                            # $ helm status mynginxNAME: mynginxLAST DEPLOYED: Fri Oct 29 14:27:32 2021NAMESPACE: defaultSTATUS: deployedREVISION: 1TEST SUITE: None

                            六、Helm命令汇总

                            在最后,附上所有helm的命令,直接控制台使用  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
                              Environment variables:
                              | Name                               | Description                                                                                       | | ------------------------------------|---------------------------------------------------------------------------------------------------| | $HELM_CACHE_HOME                   | set an alternative location for storing cached files.                                             | | $HELM_CONFIG_HOME                  | set an alternative location for storing Helm configuration.                                       | | $HELM_DATA_HOME                    | set an alternative location for storing Helm data.                                                | | $HELM_DEBUG                        | indicate whether or not Helm is running in Debug mode                                             | | $HELM_DRIVER                       | set the backend storage driver. Values are: configmap, secret, memory, sql.                       | | $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use.                                      | | $HELM_MAX_HISTORY                  | set the maximum number of helm release history.                                                   | | $HELM_NAMESPACE                    | set the namespace used for the helm operations.                                                   | | $HELM_NO_PLUGINS                   | disable plugins. Set HELM_NO_PLUGINS= 1 to disable plugins.                                        | | $HELM_PLUGINS                      | set the path to the plugins directory                                                             | | $HELM_REGISTRY_CONFIG              | set the path to the registry config file.                                                         | | $HELM_REPOSITORY_CACHE             | set the path to the repository cache directory                                                    | | $HELM_REPOSITORY_CONFIG            | set the path to the repositories file.                                                            | | $KUBECONFIG                        | set an alternative Kubernetes configuration file ( default "~/.kube/config")                       | | $HELM_KUBEAPISERVER                | set the Kubernetes API Server Endpoint for authentication                                         | | $HELM_KUBECAFILE                   | set the Kubernetes certificate authority file.                                                    | | $HELM_KUBEASGROUPS                 | set the Groups to use for impersonation using a comma-separated list.                             | | $HELM_KUBEASUSER                   | set the Username to impersonate for the operation.                                                | | $HELM_KUBECONTEXT                  | set the name of the kubeconfig context.                                                           | | $HELM_KUBETOKEN                    | set the Bearer KubeToken used for authentication.                                                 | | $HELM_KUBEINSECURE_SKIP_TLS_VERIFY | indicate if the Kubernetes API server 's certificate validation should be skipped (insecure)       | | $HELM_KUBETLS_SERVER_NAME          | set the server name used to validate the Kubernetes API server certificate                        | | $HELM_BURST_LIMIT                  | set the default burst limit in the case the server contains many CRDs (default 100, -1 to disable)|
                              Helm stores cache, configuration, and data based on the following configuration order:
                              - If a HELM_*_HOME environment variable is set, it will be used - Otherwise, on systems supporting the XDG base directory specification, the XDG variables will be used - When no other location is set a default location will be used based on the operating system
                              By default, the default directories depend on the Operating System. The defaults are listed below:
                              | Operating System | Cache Path                | Configuration Path             | Data Path               | |------------------|---------------------------|--------------------------------|-------------------------| | Linux            | $HOME/.cache/helm         | $HOME/.config/helm             | $HOME/.local/share/helm | | macOS            | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm      | | Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |
                              Usage:  helm [command]
                              Available Commands:  completion  generate autocompletion scripts for the specified shell  create      create a new chart with the given name  dependency  manage a chart's dependencies  env         helm client environment information   get         download extended information of a named release   help         Help about any command  history     fetch release history   install     install a chart  lint        examine a chart for possible issues   list         list releases   package     package a chart directory into a chart archive   plugin       install, list, or uninstall Helm plugins  pull        download a chart from a repository and ( optionally) unpack it in local directory  push        push a chart to remote  registry    login to or logout from a registry  repo         add, list, remove, update, and index chart repositories   rollback    roll back a release to a previous revision   search       search for a keyword in charts   show         show information of a chart   status      display the status of the named release   template    locally render templates   test        run tests for a release   uninstall   uninstall a release   upgrade     upgrade a release   verify       verify that a chart at the given path has been signed and is valid   version     print the client version information
                              Flags:       --burst-limit int                 client-side default throttling limit (default 100)       --debug                           enable verbose output  -h, --help                            help for helm       --kube-apiserver string           the address and the port for the Kubernetes API server       --kube-as-group stringArray       group to impersonate for the operation, this flag can be repeated to specify multiple groups.       --kube-as-user string             username to impersonate for the operation       --kube-ca-file string             the certificate authority file for the Kubernetes API server connection       --kube-context string             name of the kubeconfig context to use       --kube-insecure-skip-tls-verify   if true, the Kubernetes API server's certificate will not be checked for validity. This will make your HTTPS connections insecure       --kube-tls-server-name string     server name to use for Kubernetes API server certificate validation. If it is not provided, the hostname used to contact the server is used       --kube-token string               bearer token used for authentication       --kubeconfig string               path to the kubeconfig file  -n, --namespace string                namespace scope for this request       --registry-config string          path to the registry config file (default "/Users/yanglinwei/Library/Preferences/helm/registry/config.json")       --repository-cache string         path to the file containing cached repository indexes (default "/Users/yanglinwei/Library/Caches/helm/repository")       --repository-config string        path to the file containing repository names and URLs (default "/Users/yanglinwei/Library/Preferences/helm/repositories.yaml")
                              Use "helm [command] --help" for more information about a command.


                              七、文末

                              本文主要讲解了Helm的一些概念及用法,希望能帮助到大家,谢谢大家的阅读,本文完!


                              附参考资料:

                              • https://zhuanlan.zhihu.com/p/434249526

                              • https://helm.sh/docs/

                              链接: https://blog.csdn.net/qq_20042935/article/details/130558912

                              (版权归原作者所有,侵删)


                              来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70013542/viewspace-2955924/ ,如需转载,请注明出处,否则将追究法律责任。

                              请登录后发表评论 登录
                              全部评论

                              注册时间:2022-02-09

                              • 博文量
                                416
                              • 访问量
                                280342