Helm入门(一篇就够了)

简介: Helm入门(一篇就够了)

01 引言

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

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

  • 简化部署 :Helm允许使用单个命令轻松部署和管理应用程序,从而简化了整个部署过程;
  • 高度可配置:Helm Charts提供了高度可配置的选项,可以轻松自定义和修改应用程序的部署配置;
  • 版本控制 :Helm允许管理应用程序的多个版本,从而轻松实现版本控制和回滚;
  • 模板化:Helm Charts使用YAML模板来定义Kubernetes对象的配置,从而简化了配置过程,并提高了可重复性和可扩展性;
  • 应用程序库:Helm具有应用程序库的概念,可以轻松地共享和重用Helm Charts,从而简化了多个应用程序的部署和管理;
  • 插件系统:Helm拥有一个强大的插件系统,允许您扩展和定制Helm的功能,以满足特定的需求和要求。

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

02 Helm工作流程

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

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

  1. 开发者首先创建并编辑chart的配置;
  2. 接着打包并发布至Helm的仓库(Repository);
  3. 当管理员使用helm命令安装时,相关的依赖会从仓库下载;
  4. 接着helm会根据下载的配置部署资源至k8s;

03 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中用于隔离资源的逻辑分区

04 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命令创建一个名为wordpressChart

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

4.3 创建/编辑Chart配置

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

4.3.1 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-helm
apiVersion: v1
version: 1.0.0

4.3.2 values.yaml

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

image:
  repository: nginx
  tag: '1.19.8'

4.3.3 templates

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

apiVersion: apps/v1
kind: Deployment
metadata:
  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文件:

helm package wordpress/

这将生成一个名为wordpress-0.1.0.tgztarball文件。

4.5 发布Chart

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

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

4.6 安装Release

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

helm install mywordpress myrepo/wordpress

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

4.7 管理Release

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

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

这将升级 mywordpressWordPress应用程序镜像版本为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。

05 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 的状态,或是重新读取配置信息:

# [root@k8s-master nginx-helm-v2]# helm status mynginx
NAME: mynginx
LAST DEPLOYED: Fri Oct 29 14:27:32 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

06 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.

07 文末

参考资料:

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

相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
存储 Kubernetes Ubuntu
第一章 Helm 详细介绍
第一章 Helm 详细介绍
286 0
|
存储 Kubernetes Linux
helm 简介及基本使用
helm 简介及基本使用
3386 0
helm 简介及基本使用
|
Kubernetes NoSQL Redis
第五章 使用Helm部署一个应用
第五章 使用Helm部署一个应用
268 2
|
Kubernetes 应用服务中间件 Linux
helm安装和使用-通过helm部署k8s应用
helm安装和使用-通过helm部署k8s应用
|
Kubernetes 搜索推荐 前端开发
containerd 镜像构建工具 -- nerdctl 和 buildkit
containerd 镜像构建工具 -- nerdctl 和 buildkit
7600 0
W9
|
10月前
|
运维 关系型数据库 MySQL
轻松管理Linux服务器的5个优秀管理面板
Websoft9 应用管理平台,github 2k star 开源软件,既有200+的优秀开源软件商店,一键安装。又有可视化的Linux管理面板,文件、数据库、ssl证书方便快捷管理。
W9
820 2
|
Kubernetes 容器 Perl
在k8S中,如何向Pod中指定容器传递环境变量?有哪些方式?
在k8S中,如何向Pod中指定容器传递环境变量?有哪些方式?
|
Kubernetes 安全 关系型数据库
Helm入门(一篇就够了)
Helm快速入门
21748 0
|
Kubernetes Cloud Native Java
云原生|kubernetes|ingress-nginx插件部署(kubernetes-1.23和最新版controller-1.6.4)
云原生|kubernetes|ingress-nginx插件部署(kubernetes-1.23和最新版controller-1.6.4)
2238 0
|
存储 Kubernetes API
k8s教程(Volume篇)-k8s存储机制概述
k8s教程(Volume篇)-k8s存储机制概述
913 0

热门文章

最新文章