K8s 应用部署工具 Helm

发布于 2022-01-14  459 次阅读


目前项目大部分微服务化或者模块化,会分成很多个组件来部署,每个组件可能对应一个deployment,service,Ingress 的yaml还可能存在各种依赖关系,这样会导致一个项目会有很多的yaml,这样管理起来很麻烦Helm就可以解决这些。

一、Helm 简介

Helm 是 Kubernetes 的软件包管理工具,类似 Ubuntu 中使用的apt、Centos中使用的yum 或者Python中的 pip 一样。

Helm能够将一组K8S资源打包统一管理, 是查找、共享和使用为Kubernetes构建的软件的最佳方式。

1.1 Helm 可以做什么

目前Kubernetes 部署都通过 一个个 yaml,就想简介中说的,部署一个组件就是需要多个yaml文件。如果一个应用需要多个组件,那yaml文件就很多很多, 这些资源过于分散,不方便管理,直接提供kubectl来管理一个应用,让人头疼。

在部署一个应用,会遇到哪些问题:

  • 如何统一管理、配置和更新这些分散的k8s的应用资源文件
  • 如何分发和复用应用模板
  • 如何将应用的一系列资源当做一个软件包管理

对于使用者而言,使用Helm后不用需要编写复杂的应用部署文件,可以以简单的方式在Kubernetes上查找、安装、升级、回滚、卸载应用程序

1.2 Helm 基本概念

以下是Helm的一些关键特点和概念:

  1. Chart:Chart是Helm的核心概念,它是一个Kubernetes应用程序的打包格式。Chart包含了应用程序的资源定义(如Pod、Service、ConfigMap等)、依赖关系、配置模板和默认值等。
  2. Release:当用户使用Helm安装一个Chart时,会创建一个Release,表示一个特定版本的应用程序部署。每个Release都有一个唯一的名称和版本号。
  3. Repository:Repository是Chart的存储和分享位置。开发者可以将自己的Chart发布到Repository中,其他人可以从Repository中获取Chart并进行安装。
  4. Values文件:Values文件包含了Chart配置的值。通过Values文件,用户可以覆盖Chart中的默认配置,以适应不同的环境和需求。
  5. 模板引擎:Helm使用Go模板引擎来生成Kubernetes资源配置文件。这允许用户根据自己的需求自定义配置。

helm 把 kubernetes 资源打包到一个 chart 中,制作并完成各个 chart 和 chart 本身依赖关系并利用 chart 仓库实现对外分发,而 helm 还可通过 values.yaml 文件完成可配置的发布,如果chart 版本更新了,helm 自动支持滚更更新机制,还可以一键回滚,但是不是适合在生产环境使用,除非具有定义自制 chart 的能力。

二、安装 Helm

helm 版本对照表:https://helm.sh/zh/docs/topics/version_skew/

Helm 版本 支持的 Kubernetes 版本
3.12.x 1.27.x - 1.24.x
3.11.x 1.26.x - 1.23.x
3.10.x 1.25.x - 1.22.x
3.9.x 1.24.x - 1.21.x v3.9.4
3.8.x 1.23.x - 1.20.x v3.8.2
3.7.x 1.22.x - 1.19.x v3.7.2
3.6.x 1.21.x - 1.18.x
3.5.x 1.20.x - 1.17.x
3.4.x 1.19.x - 1.16.x
3.3.x 1.18.x - 1.15.x
3.2.x 1.18.x - 1.15.x
3.1.x 1.17.x - 1.14.x
3.0.x 1.16.x - 1.13.x
2.16.x 1.16.x - 1.15.x

2.1 helm部署

下载软件并部署

# 下载软件包
wget https://get.helm.sh/helm-v3.7.2-linux-amd64.tar.gz
# 解压
tar zxvf helm-v3.7.2-linux-amd64.tar.gz && mv linux-amd64/helm /usr/local/bin/

查看helm 是否可以用

[root@k8s-master01 test]# helm  version
version.BuildInfo{Version:"v3.7.2", GitCommit:"663a896f4a815053445eec4153677ddc24a0a361", GitTreeState:"clean", GoVersion:"go1.16.10"}

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

  • helm search hubArtifact Hub 中查找并列出 helm charts。 Artifact Hub中存放了大量不同的仓库。
  • helm search repo 从你添加(使用 helm repo add)到本地 helm 客户端中的仓库中进行查找。该命令基于本地数据进行搜索,无需连接互联网。
helm search hub wordpress
URL                                                 CHART VERSION APP VERSION DESCRIPTION
https://hub.helm.sh/charts/bitnami/wordpress        7.6.7         5.2.4       Web publishing platform for building blogs and ...

配置chart仓库

阿里云仓库

https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

官方仓库

https://hub.kubeapps.com/charts/incubator

微软仓库

http://mirror.azure.cn/kubernetes/charts/charts/

添加阿里云chart仓库,并更新

添加仓库
helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
# 更新本地仓库
helm repo update
## 查看 helm仓库
helm repo list
## 删除charts仓库

2.2 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:
      --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-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 "/root/.config/helm/registry/config.json")
      --repository-cache string     path to the file containing cached repository indexes (default "/root/.cache/helm/repository")
      --repository-config string    path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml")

helm 创建自定义的项目

###helm create mytest
[root@k8s-master01 mytest]tree
├── charts 用于存放所依赖的子 chart
├── Chart.yaml 描述这个 Chart 的相关信息、包括名字、描述信息、版本等
├── templates 模板目录,保留创建 k 8s 的资源清单文件
│  │
│  ├── deployment.yaml deployment 资源的 go 模板文件
│  │
│  ├── _helpers.tpl # 模板助手文件,定义的值可在模板中使用
│  │
│  ├── hpa.yaml 水平 pod 自动扩缩容 go 模板文件
│  │
│  ├── ingress.yaml 七层代理 go 模板文件
│  │
│  ├── NOTES.txt
│  │
│  ├── serviceaccount.yaml
│  │
│  ├── service.yaml service 的 go 模板文件
│  │
│  └── tests
│     │
│     └── test connection.yaml
└──values.yaml 模板的值文件,这些值会在安装时应用到 GO 模板生成部署文件

helm 试运行命令

helm install mytest ./mytest --debug --dry-run

helm 运行命令

helm install mytest ./mytest