【Kubernetes 019】包管理工具Helm安装和使用详解(最新Helm3版本)

为了安装应用方便,解决安装包的依赖关系,Centos里面有yum包管理工具。而为了自动安装部署,不用自己写大量的yaml文件,k8s中也出现了自己的包管理工具,就是这一节要学习的Helm。

我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。

几个概念

如果是手动安装一套k8s应用出来,我们需要分别创建应用中各个组件的Deployment以及Service的yaml文件。如果想在另一个k8s集群中去部署相同的一套应用,其实只需要将相同的一套yaml文件拷贝过去即可,顶多在更改几个参数。那么完全可以将应用所需要的yaml文件打包放在一个仓库里直接供人下载使用即可,这就是Helm的初衷。

不过当然Helm还集成了很多别的功能,例如一键更新和回滚,yaml文件设置动态参数等等,后面我们会慢慢学习到。

在真正开始使用Helm前有几个特有的概念要先熟悉下。

Chart

Chart就是按照一定目录结构保存的多个文件,用来描述所部署应用相关k8s资源的信息。Chart每被Helm使用一次,就部署一个应用到集群中,如果Chart被使用多次,就会把相同的应用部署多次。

通常是直接下载官方的Chart,微调下参数然后使用。也可以跟着官方文档中的步骤来创建自己的Chart,下面的实际操作中我们两种方式都会演示。

Release

可以将Helm的Chart类比Docker的Image,那么Release就是Docker的Container。一个Chart可以生成多个Release。

Repo

Repo就是存放Chart的仓库,和Docker Hub一样,Helm也有自己的Helm Hub。当然也是可以建立自己的私有仓库的。

安装Helm

官网获取最新的稳定版本二进制安装包,下载到本地

[root@k8s-master helm]# wget https://get.helm.sh/helm-v3.2.1-linux-amd64.tar.gz
--2020-05-15 17:39:15--  https://get.helm.sh/helm-v3.2.1-linux-amd64.tar.gz
Resolving get.helm.sh (get.helm.sh)... 152.195.19.97, 2606:2800:11f:1cb7:261b:1f9c:2074:3c
Connecting to get.helm.sh (get.helm.sh)|152.195.19.97|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12927632 (12M) [application/x-tar]
Saving to: ‘helm-v3.2.1-linux-amd64.tar.gz’

100%[=========================================================================================================================>] 12,927,632   322KB/s   in 14s    

2020-05-15 17:39:31 (879 KB/s) - ‘helm-v3.2.1-linux-amd64.tar.gz’ saved [12927632/12927632]

之后解压得到二进制文件

[root@k8s-master helm]# tar -zxvf helm-v3.2.1-linux-amd64.tar.gz
linux-amd64/
linux-amd64/README.md
linux-amd64/helm
linux-amd64/LICENSE

将二进制文件加入PATH即可

[root@k8s-master linux-amd64]# cp helm /usr/local/bin

接着运行helm help能看到帮助文档说明安装成功

注意,Helm3和Helm2最大的改变就是不需要再安装服务端的Tiller了,直接用config文件进行RBAC认证。

实际操作

下面就分别以使用官方Chart和建立自己本地的Chart为例来看看Helm的实际操作。

使用官方Chart

Helm的工作原理和yum还蛮像的,都是去本地配置的Repo中搜索,如果所有的Repo都搜索过没找到Chart就会报错。

Docker的工作原理是在Image的名称上指定位置,registry/author/image:version

所以首先添加官方的Repo

[root@k8s-master ~]# helm repo add stable https://kubernetes-charts.storage.googleapis.com/
"stable" has been added to your repositories

然后就可以去搜索这个repo中的chart了

[root@k8s-master ~]# helm search repo stable
NAME                                 	CHART VERSION	APP VERSION            	DESCRIPTION                                       
stable/acs-engine-autoscaler         	2.2.2        	2.1.1                  	DEPRECATED Scales worker nodes within agent pools
stable/aerospike                     	0.3.2        	v4.5.0.5               	A Helm chart for Aerospike in Kubernetes          
stable/airflow                       	6.10.4       	1.10.4                 	Airflow is a platform to programmatically autho...
...
...

当然如果想要进行模糊搜索的话可以用helm search hub xxx,例如

[root@k8s-master ~]# helm search hub airflow
URL                                        	CHART VERSION	APP VERSION	DESCRIPTION                                       
https://hub.helm.sh/charts/stable/airflow  	6.10.4       	1.10.4     	Airflow is a platform to programmatically autho...
https://hub.helm.sh/charts/bitnami/airflow 	6.2.2        	1.10.10    	Apache Airflow is a platform to programmaticall...
https://hub.helm.sh/charts/larribas/airflow	1.0.1        	1.10.7     	[Airflow](https://airflow.apache.org/) + Kubern...

如果发现是stable下的就可以去stable中安装了。

接下来就可以安装chart了,命令是helm install name chart,不过这里的chart可以是下面5中来源的一个

  1. By chart reference: helm install mymaria example/mariadb
  2. By path to a packaged chart: helm install mynginx ./nginx-1.2.3.tgz
  3. By path to an unpacked chart directory: helm install mynginx ./nginx
  4. By absolute URL: helm install mynginx https://example.com/charts/nginx-1.2.3.tgz
  5. By chart reference and repo url: helm install --repo https://example.com/charts/ mynginx nginx

节选自helm install --help

当然最简单的还是直接用官方的stable来安装了,例如

[root@k8s-master ~]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!⎈
[root@k8s-master ~]# helm install stable/mysql --generate-name
NAME: mysql-1589684111
LAST DEPLOYED: Sun May 17 10:55:14 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1589684111.default.svc.cluster.local
...
...

查看已部署的release以及release的状态

[root@k8s-master ~]# helm list
NAME            	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
mysql-1589684111	default  	1       	2020-05-17 10:55:14.045745038 +0800 +08	deployed	mysql-1.6.3	5.7.28     
[root@k8s-master ~]# helm status mysql-1589684111
NAME: mysql-1589684111
LAST DEPLOYED: Sun May 17 10:55:14 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
mysql-1589684111.default.svc.cluster.local
...
...

但是需要注意,通常直接部署都会有各种大大小小的问题,所以一般是先下载Chart到本地,编辑之后再从本地部署

[root@k8s-master ~]# cd helm
[root@k8s-master helm]# helm pull stable/mysql

这时候会下载一个tgz压缩包,解压以后就会看到chart的目录结构

[root@k8s-master helm]# cd mysql
[root@k8s-master mysql]# ll
total 36
-rwxr-xr-x. 1 root root   502 Jan  1  1970 Chart.yaml
-rwxr-xr-x. 1 root root 22483 Jan  1  1970 README.md
drwxr-xr-x. 3 root root   268 May 17 10:37 templates
-rwxr-xr-x. 1 root root  5774 May 17 10:40 values.yaml

下面在创建本地Chart中,我们会对这个目录结构进行详细说明。

我们先将刚才部署的release删除

[root@k8s-master mysql]# helm uninstall mysql-1589684111
release "mysql-1589684111" uninstalled

创建本地Chart

创建自己的chart,只需要按照规定的格式创建目录结构,写入文件即可

文件结构

目录名为chart名

例如创建一个叫xiaofu的文件夹

[root@k8s-master helm]# mkdir xiaofu
[root@k8s-master helm]# cd xiaofu

目录结构

完整的目录结构包括下面这些文件和目录

Chart.yaml          # A YAML file containing information about the chart
LICENSE             # OPTIONAL: A plain text file containing the license for the chart
README.md           # OPTIONAL: A human-readable README file
values.yaml         # The default configuration values for this chart
values.schema.json  # OPTIONAL: A JSON Schema for imposing a structure on the values.yaml file
charts/             # A directory containing any charts upon which this chart depends.
crds/               # Custom Resource Definitions
templates/          # A directory of templates that, when combined with values,
                    # will generate valid Kubernetes manifest files.
templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes

下面我们以一些必备文件为例创建一个chart,其余可选字段的说明可以查阅官方文档

Chart.yaml

该文件里面存放chart的一些基本信息,字段如下

apiVersion: The chart API version (required)
name: The name of the chart (required)
version: A SemVer 2 version (required)
kubeVersion: A SemVer range of compatible Kubernetes versions (optional)
description: A single-sentence description of this project (optional)
type: It is the type of chart (optional)
keywords:
  - A list of keywords about this project (optional)
home: The URL of this projects home page (optional)
sources:
  - A list of URLs to source code for this project (optional)
dependencies: # A list of the chart requirements (optional)
  - name: The name of the chart (nginx)
    version: The version of the chart ("1.2.3")
    repository: The repository URL ("https://example.com/charts") or alias ("@repo-name")
    condition: (optional) A yaml path that resolves to a boolean, used for enabling/disabling charts (e.g. subchart1.enabled )
    tags: # (optional)
      - Tags can be used to group charts for enabling/disabling together
    enabled: (optional) Enabled bool determines if chart should be loaded
    import-values: # (optional)
      - ImportValues holds the mapping of source values to parent key to be imported. Each item can be a string or pair of child/parent sublist items.
    alias: (optional) Alias usable alias to be used for the chart. Useful when you have to add the same chart multiple times
maintainers: # (optional)
  - name: The maintainers name (required for each maintainer)
    email: The maintainers email (optional for each maintainer)
    url: A URL for the maintainer (optional for each maintainer)
icon: A URL to an SVG or PNG image to be used as an icon (optional).
appVersion: The version of the app that this contains (optional). This needn't be SemVer.
deprecated: Whether this chart is deprecated (optional, boolean)
annotations:
  example: A list of annotations keyed by name (optional).

其中必须的只有前三个

  • apiVersion - 如果是必须Helm3及以上需要填v2,不然就填v1
  • name - 和目录名一致即可
  • version - 一个遵循SemVer2标准的版本,例如1.0.0

创建一个Chart.yaml文件如下

apiVersion: v1
name: xiaofu
version: 1.0.0

charts

这个目录里面存放本chart所依赖的别的chart,我们这里因为没有别的chart要依赖,所以放空。

templates和values.yaml

templates目录存放着k8s资源创建所用的yaml文件。为了达到动态生成的效果,这里使用了Go模板语言values.yaml中读取变量的值填入这些yaml文件中。

不懂Go模板也没太大关系,和python中的jinja差不多

我创建一个简单的Deployment以及对应的Service

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mynginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: mynginx
        version: v2
    spec:
      containers:
        - name: mynginx
          image: {{ .Values.image }}:{{ .Values.imageTag }}
          ports:
            - containerPort: 80
apiVersion: v1
kind: Service
metadata:
  name: mynginx-service
  namespace: default
spec:
  type: NodePort
  selector:
    app: mynginx
    version: v2
  ports:
    - name: http
      port: 8080
      targetPort: 80
      nodePort: 30000

注意观察Deployment中用点号去获取变量值的写法,然后创建values.yaml如下

image: mynginx
imageTag: v2

这样基本信息差不读多了,试着部署看看

[root@k8s-master helm]# helm install xiaofu --generate-name
NAME: xiaofu-1589688249
LAST DEPLOYED: Sun May 17 12:04:09 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

查看一下信息

[root@k8s-master helm]# kubectl get pod
NAME                                 READY   STATUS    RESTARTS   AGE
mynginx-deployment-b66f59f66-kncc6   1/1     Running   0          44s
mynginx-deployment-b66f59f66-wrtz6   1/1     Running   0          44s
mynginx-deployment-b66f59f66-xbmhz   1/1     Running   0          44s
[root@k8s-master helm]# kubectl get svc
NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP          18d
mynginx-service   NodePort    10.111.111.217   <none>        8080:30000/TCP   48s

之后访问k8s集群中任意节点IP的30000端口可以得到如下信息

this is mynginx v2

如果这时候要进行扩容以及更新镜像版本,就可以修改下模板文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mynginx-deployment
spec:
  replicas: {{ .Values.replica }}
  template:
    metadata:
      labels:
        app: mynginx
        version: v2
    spec:
      containers:
        - name: mynginx
          image: {{ .Values.image }}:{{ .Values.imageTag }}
          ports:
            - containerPort: 80

以及values.yaml

image: mynginx
imageTag: v1
replica: 5

之后更新下release

[root@k8s-master xiaofu]# helm upgrade xiaofu-1589688249 .
Release "xiaofu-1589688249" has been upgraded. Happy Helming!
NAME: xiaofu-1589688249
LAST DEPLOYED: Sun May 17 14:08:28 2020
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None

就会看到pod变多了

[root@k8s-master xiaofu]# kubectl get pod
NAME                                  READY   STATUS    RESTARTS   AGE
mynginx-deployment-7f66686d6c-9slrh   1/1     Running   0          74s
mynginx-deployment-7f66686d6c-bmdc7   1/1     Running   0          72s
mynginx-deployment-7f66686d6c-lmhlr   1/1     Running   0          69s
mynginx-deployment-7f66686d6c-qcqcv   1/1     Running   0          72s
mynginx-deployment-7f66686d6c-rjww5   1/1     Running   0          74s
[root@k8s-master xiaofu]# kubectl get svc
NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP          18d
mynginx-service   NodePort    10.111.111.217   <none>        8080:30000/TCP   125m

此时再访问node的30000端口就变成了v1镜像的内容。

查看历史变更信息

[root@k8s-master xiaofu]# helm history xiaofu-1589688249
REVISION	UPDATED                 	STATUS    	CHART       	APP VERSION	DESCRIPTION     
1       	Sun May 17 12:04:09 2020	superseded	xiaofu-1.0.0	           	Install complete
2       	Sun May 17 14:08:28 2020	deployed  	xiaofu-1.0.0	           	Upgrade complete

如果要回退

[root@k8s-master xiaofu]# helm rollback xiaofu-1589688249 1
Rollback was a success! Happy Helming!

就跟Deployment的回滚道理一样,因为旧的pod都没有被彻底删除,只是停用,所以回滚会很快。

这里只是简单的演示,更多的功能可以参考官方文档以及helm help帮助文档说明。

Helm常用命令汇总

把上面用到的一些常用命令汇总以下

命令说明
helm search hub xxx在Helm Hub上搜索Chart
helm search repo repo_name在本地配置的Repo中搜索Chart
helm install release_name chart_referencechart一共有5种reference
helm list查看已部署的release
helm status release_name查看release信息
helm upgrade release_name chart_reference修改chart信息后升级release
helm history release_name查看release的更新历史记录
helm rollback release_name revision回滚操作
helm uninstall release_name卸载release

总结

这一节我们学习了Helm的基本用法,下一节开始来看看用Helm管理的几个常用应用。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值