详细见(37条消息) kubernetes之helm简介、安装、配置、使用指南_菲宇的博客-CSDN博客_helm

Helm从入门到实践

Helm是Kubernetes的软件包的管理工具.

Helm 是什么

Helm 是 Kubernetes 的包管理器。包管理器类似于我们在 Ubuntu 中使用的apt、Centos中使用的yum 或者Python中的 pip 一样,能快速查找、下载和安装软件包。Helm 由客户端组件 helm 和服务端组件 Tiller 组成, 能够将一组K8S资源打包统一管理, 是查找、共享和使用为Kubernetes构建的软件的最佳方式.

Helm 解决了什么痛点?

在 Kubernetes中部署一个可以使用的应用,需要涉及到很多的 Kubernetes 资源的共同协作。比如你安装一个 WordPress 博客,用到了一些 Kubernetes (下面全部简称k8s)的一些资源对象,包括 Deployment 用于部署应用、Service 提供服务发现、Secret 配置 WordPress 的用户名和密码,可能还需要 pv 和 pvc 来提供持久化服务。并且 WordPress 数据是存储在mariadb里面的,所以需要 mariadb 启动就绪后才能启动 WordPress。这些 k8s 资源过于分散,不方便进行管理,直接通过 kubectl 来管理一个应用,你会发现这十分蛋疼。
所以总结以上,我们在 k8s 中部署一个应用,通常面临以下几个问题:

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

Helm 相关组件及概念

Helm包含两个组件,分别是helm客户端和Tiller服务器

  • helm 是一个命令行工具,用于本地开发及管理chart,chart仓库管理等
  • Tiller 是 Helm 的服务端。Tiller 负责接收 Helm 的请求,与 k8s 的 apiserver 交互,根据chart 来生成一个 release 并管理 release
  • chart Helm的打包格式叫做chart,所谓chart就是一系列文件, 它描述了一组相关的 k8s 集群资源
  • release 使用 helm install 命令在 Kubernetes 集群中部署的 Chart 称为 Release
  • Repoistory Helm chart 的仓库,Helm 客户端通过 HTTP 协议来访问存储库中 chart 的索引文件和压缩包

Helm 原理

下面两张图描述了 Helm 的几个关键组件 Helm(客户端)、Tiller(服务器)、Repository(Chart 软件仓库)、Chart(软件包)之间的关系以及它们之间如何通信

image-20230224145527721

image-20230224145540489

创建release

  • helm 客户端从指定的目录或本地tar文件或远程repo仓库解析出chart的结构信息
  • helm 客户端指定的 chart 结构和 values 信息通过 gRPC 传递给 Tiller
  • Tiller 服务端根据 chart 和 values 生成一个 release
  • Tiller 将install release请求直接传递给 kube-apiserver

删除release

  • helm 客户端从指定的目录或本地tar文件或远程repo仓库解析出chart的结构信息
  • helm 客户端指定的 chart 结构和 values 信息通过 gRPC 传递给 Tiller
  • Tiller 服务端根据 chart 和 values 生成一个 release
  • Tiller 将delete release请求直接传递给 kube-apiserver

更新release

  • helm 客户端将需要更新的 chart 的 release 名称 chart 结构和 value 信息传给 Tiller
  • Tiller 将收到的信息生成新的 release,并同时更新这个 release 的 history
  • Tiller 将新的 release 传递给 kube-apiserver 进行更新

chart 的基本结构

Helm的打包格式叫做chart,所谓chart就是一系列文件, 它描述了一组相关的 k8s 集群资源。Chart中的文件安装特定的目录结构组织, 最简单的chart 目录如下所示:

image-20230224150030064

  • charts 目录存放依赖的chart
  • Chart.yaml 包含Chart的基本信息,包括chart版本,名称等
  • templates 目录下存放应用一系列 k8s 资源的 yaml 模板
  • _helpers.tpl 此文件中定义一些可重用的模板片断,此文件中的定义在任何资源定义模板中可用
  • NOTES.txt 介绍chart 部署后的帮助信息,如何使用chart等
  • values.yaml 包含了必要的值定义(默认值), 用于存储 templates 目录中模板文件中用到变量的值

安装Helm

可以通过预编译的二进制文件来安装helm的客户端命令,具体的版本可以到helm的github上去下载:

https://github.com/helm/helm/releases

本文档使用的版本:

https://get.helm.sh/helm-v3.7.2-linux-amd64.tar.gz

过程

Helm客户端安装

(1)先上传helm的压缩包到服务器

(2)解压并且复制helm命令

1
2
3
tar -zxf helm-v3.7.2-linux-amd64.tar.gz \
&& cd linux-amd64 \
cp helm /usr/local/bin/

(3)验证helm命令

输入helm命令,如果输出如下,则表示helm安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
[root@nccztsjb-node-11 linux-amd64]# helm
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 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
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.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")

Use "helm [command] --help" for more information about a command.

Helm 服务端安装Tiller

注意:现在K8S集群上每个节点安装socat软件(yum install -y socat),不然会报错:

1
2
E0522 22:22:15.492436   24409 portforward.go:331] an error occurred forwarding 38398 -> 44134: error forwarding port 44134 to pod dc6da4ab99ad9c497c0cef1776b9dd18e0a612d507e2746ed63d36ef40f30174, uid : unable to do port forwarding: socat not found.
Error: cannot connect to Tiller

Tiller 是以 Deployment 方式部署在 Kubernetes 集群中的,只需使用以下指令便可简单的完成安装。

1
helm init

由于 Helm 默认会去 storage.googleapis.com 拉取镜像,如果你当前执行的机器不能访问该域名的话可以使用以下命令来安装:

1
2
3
helm init --client-only --stable-repo-url https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts/
helm repo add incubator https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts-incubator/
helm repo update
1
2
3
4
5
# 创建服务端
helm init --service-account tiller --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.9.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

# 创建TLS认证服务端,参考地址:https://github.com/gjmzj/kubeasz/blob/master/docs/guide/helm.md
helm init --service-account tiller --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.9.1 --tiller-tls-cert /etc/kubernetes/ssl/tiller001.pem --tiller-tls-key /etc/kubernetes/ssl/tiller001-key.pem --tls-ca-cert /etc/kubernetes/ssl/ca.pem --tiller-namespace kube-system --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

在 Kubernetes 中安装 Tiller 服务,因为官方的镜像因为某些原因无法拉取,使用-i指定自己的镜像,可选镜像:registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.9.1(阿里云),该镜像的版本与helm客户端的版本相同,使用helm version可查看helm客户端版本。

给Tiller授权

因为 Helm 的服务端 Tiller 是一个部署在 Kubernetes 中 Kube-System Namespace 下 的 Deployment,它会去连接 Kube-Api 在 Kubernetes 里创建和删除应用。

而从 Kubernetes 1.6 版本开始,API Server 启用了 RBAC 授权。目前的 Tiller 部署时默认没有定义授权的 ServiceAccount,这会导致访问 API Server 时被拒绝。所以我们需要明确为 Tiller 部署添加授权。

创建 Kubernetes 的服务帐号和绑定角色

1
2
$ kubectl create serviceaccount --namespace kube-system tiller
$ kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller

为 Tiller 设置帐号

1
2
3
# 使用 kubectl patch 更新 API 对象
$ kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
deployment.extensions "tiller-deploy" patched

查看是否授权成功

1
2
3
$ kubectl get deploy --namespace kube-system   tiller-deploy  --output yaml|grep  serviceAccount
serviceAccount: tiller
serviceAccountName: tiller

验证 Tiller 是否安装成功

1
2
3
4
5
6
$ kubectl -n kube-system get pods|grep tiller
tiller-deploy-6d68f5c78f-nql2z 1/1 Running 0 5m

$ helm version
Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}

Helm 常用命令

查看版本

#helm version

查看当前安装的charts

#helm list

查询 charts

#helm search redis

安装charts

#helm install –name redis –namespaces prod bitnami/redis

查看charts状态

#helm status redis

删除charts

#helm delete –purge redis

增加repo

#helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

#helm repo add –username admin –password password myharbor https://harbor.qing.cn/chartrepo/charts

更新repo仓库资源

#helm repo update

创建charts

#helm create helm_charts

测试charts语法

1
#helm lint 

打包charts

#cd helm_charts && helm package ./

查看生成的yaml文件

#helm template helm_charts-0.1.1.tgz

更新image

#helm upgrade –set image.tag=’v2019-05-09-18-48-40’ study-api-en-oral myharbor/study-api-en-oral

回滚relase

#helm hist study-api-en-oral

#helm rollback study-api-en-oral 4

发布到私有harbor仓库脚本

request_url=’https://harbor.qing.cn/api/chartrepo/charts/charts

user_name=’admin’

password=’password’

chart_file=’helm_charts-0.1.3.tgz’

curl -i -u “$user_name:$password” -k -X POST “${request_url}” \

-H “accept: application/json” \

-H “Content-Type: multipart/form-data” \

-F “chart=@${chart_file};type=application/x-compressed”

echo $result