26,k8s 之helm

1,Helm 介绍
Helm是一个Kubernetes的包管理工具,就像Linux下的包管理器,如yum/apt等,可以很方便的将之前打包好的yaml文件部署到kubernetes上。

Helm有两个重要概念:

	helm:一个命令行客户端工具,主要用于Kubernetes应用chart的创建、打包、发布和管理。
	Chart:应用描述,一系列用于描述 k8s 资源相关文件的集合。
	Release:基于Chart的部署实体,一个 chart 被 Helm 运行后将会生成对应的一个 release;将在k8s中创建出真实运行的资源对象。

2,部署Helm客户端
	Helm客户端下载地址:https://github.com/helm/helm/releases
	解压移动到/usr/bin/目录即可。

	wget https://get.helm.sh/helm-v3.0.0-linux-amd64.tar.gz
	tar zxvf helm-v3.0.0-linux-amd64.tar.gz 
	mv linux-amd64/helm /usr/bin/

3,Helm常用命令
| **命令**   | **描述**                                                     |
| ---------- | ------------------------------------------------------------ |
| create     | 创建一个chart并指定名字                                      |
| dependency | 管理chart依赖                                                |
| get        | 下载一个release。可用子命令:all、hooks、manifest、notes、values |
| history    | 获取release历史                                              |
| install    | 安装一个chart                                                |
| list       | 列出release                                                  |
| package    | 将chart目录打包到chart存档文件中                             |
| pull       | 从远程仓库中下载chart并解压到本地  # helm pull stable/mysql --untar |
| repo       | 添加,列出,移除,更新和索引chart仓库。可用子命令:add、index、list、remove、update |
| rollback   | 从之前版本回滚                                               |
| search     | 根据关键字搜索chart。可用子命令:hub、repo                   |
| show       | 查看chart详细信息。可用子命令:all、chart、readme、values    |
| status     | 显示已命名版本的状态                                         |
| template   | 本地呈现模板                                                 |
| uninstall  | 卸载一个release                                              |
| upgrade    | 更新一个release                                              |
| version    | 查看helm客户端版本                                           |
| ---------- | ------------------------------------------------------------ |

4,配置国内Chart仓库
	微软仓库(http://mirror.azure.cn/kubernetes/charts/)这个仓库强烈推荐,基本上官网有的chart这里都有。
	阿里云仓库(https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts  )
	官方仓库(https://hub.kubeapps.com/charts/incubator)官方chart仓库,国内有点不好使。

添加存储库:
	helm repo add stable http://mirror.azure.cn/kubernetes/charts
	helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 
	helm repo update
	
查看配置的存储库:
	helm repo list
	helm search repo stable

删除存储库:
	helm repo remove aliyun
	
	helm show values stable/mysql

安装:
	helm install db stable/mysql
	
查看发布状态:
	helm status db 
	
5,安装前自定义chart配置选项

上面部署的mysql并没有成功,这是因为并不是所有的chart都能按照默认配置运行成功,可能会需要一些环境依赖,例如PV。
所以我们需要自定义chart配置选项,安装过程中有两种方法可以传递配置数据:

	--values(或-f):指定带有覆盖的YAML文件。这可以多次指定,最右边的文件优先
	--set:在命令行上指定替代。如果两者都用,--set优先级高

	--values使用,先将修改的变量写到一个文件中


[root@centos7 helm]# cat pv.yml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 8Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /data/nfs/wwwroot/
    server: 192.168.0.14
[root@centos7 helm]# 
[root@centos7 ~]# kubectl apply -f pv.yaml 
persistentvolume/my-pv created
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
db-mysql-864bfb89bb-fcrhm   1/1     Running   0          15m
[root@centos7 ~]#
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get secret --namespace default db-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
77fIePCp27
[root@centos7 ~]#
[root@centos7 ~]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
db-mysql-864bfb89bb-fcrhm   1/1     Running   0          19m
[root@centos7 ~]# 
[root@centos7 ~]# kubectl exec -it db-mysql-864bfb89bb-fcrhm bash
root@db-mysql-864bfb89bb-fcrhm:/# 
root@db-mysql-864bfb89bb-fcrhm:/# mysql -uroot -p77fIePCp27
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 81
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

	
	
[root@centos7 ~]# cat -n values.yml 
     1	mysqlRootPassword: testing
     2	mysqlUser: k8s
     3	mysqlPassword: k8s123
     4	mysqlDatabase: k8s
     5	persistence:
     6	  enabled: true
     7	  storageClass: "managed-nfs-storage"
     8	  accessMode: ReadWriteOnce
     9	  size: 8Gi
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# helm install db2 -f values.yml stable/mysql
[root@centos7 ~]# kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
db-mysql-864bfb89bb-fcrhm    1/1     Running   0          41m
db2-mysql-7c54f54bc7-wsg9j   1/1     Running   0          3m3s
[root@centos7 ~]#

	
	
	
6、安装前官方自定义chart配置选项
6-1,values使用,先将修改的变量写到一个文件中
# helm show values stable/mysql
# cat config.yaml //自定义的一些变量。就是在创建了一些value
persistence:
  enabled: true
  storageClass: "managed-nfs-storage"
  accessMode: ReadWriteOnce
  size: 8Gi
mysqlUser: "k8s"
mysqlPassword: "123456"
mysqlDatabase: "k8s"
# helm install db -f config.yaml stable/mysql
# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
db-mysql-57485b68dc-4xjhv                 1/1     Running   0          8m51s

以上将创建具有名称的默认MySQL用户k8s,并授予此用户访问新创建的k8s数据库的权限,但将接受该图表的所有其余默认值。

6-2,命令行替代变量:
# helm install db --set persistence.storageClass="managed-nfs-storage" stable/mysql

可以把chart包下载下来查看详情(直接拉下来就解压了):
# helm pull stable/mysql --untar

	
	
	
	
	
	
	
	
	
	
1,创建chat模板,构建一个Helm Chart
[root@centos7 ~]# helm create mychart
Creating mychart
[root@centos7 ~]# ll mychart/
total 8
drwxr-xr-x. 2 root root    6 Jul 12 10:20 charts
-rw-r--r--. 1 root root  905 Jul 12 10:20 Chart.yaml
drwxr-xr-x. 3 root root  146 Jul 12 10:20 templates
-rw-r--r--. 1 root root 1490 Jul 12 10:20 values.yaml
[root@centos7 ~]# 
2,创建Chart后,接下来就是将其部署
[root@centos7 ~]# helm install test mychart
NAME: test
LAST DEPLOYED: Sun Jul 12 10:21:12 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=test" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:80
[root@centos7 ~]# 
[root@centos7 ~]# kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
test-mychart-b5cd6d7c8-qgqct   1/1     Running   0          51s
[root@centos7 ~]# 

[root@centos7 ~]# tree mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 9 files
[root@centos7 ~]# 

- Chart.yaml:用于描述这个 Chart的基本信息,包括名字、描述信息以及版本等。
- values.yaml :用于存储 templates 目录中模板文件中用到变量的值。
- Templates: 目录里面存放所有yaml模板文件。
- charts:目录里存放这个chart依赖的所有子chart。
- NOTES.txt :用于介绍Chart帮助信息, helm install 部署后展示给用户。例如:如何使用这个 Chart、列出缺省的设置等。
- _helpers.tpl:放置模板助手的地方,可以在整个 chart 中被其他yaml重复使用

3,也可以打包推送的charts仓库共享别人使用。
[root@centos7 ~]# helm package mychart/
Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz
[root@centos7 ~]#


4,创建一个简单的chart
[root@centos7 ~]# cd mychart
[root@centos7 mychart]# 
[root@centos7 mychart]# ll
total 8
drwxr-xr-x. 2 root root   6 Jul 12 10:20 charts
-rw-r--r--. 1 root root 905 Jul 12 10:20 Chart.yaml
drwxr-xr-x. 2 root root  29 Jul 12 11:08 templates
-rw-r--r--. 1 root root  52 Jul 12 10:59 values.yaml
[root@centos7 mychart]# 
[root@centos7 mychart]# ll charts/
total 0
[root@centos7 mychart]# cat values.yaml 
name: hello
replicas: 3
image: nginx
imageTag: 1.17
[root@centos7 mychart]# 
[root@centos7 mychart]# cat templates/deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mychart
  name: {{ .Values.name }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: mychart
  template:
    metadata:
      labels:
        app: mychart
    spec:
      containers:
      - image: {{ .Values.image  }}:{{ .Values.imageTag  }}
        name: nginx
[root@centos7 mychart]# vim Chart.yaml 
[root@centos7 mychart]# 
[root@centos7 mychart]# cd
[root@centos7 ~]# 
[root@centos7 ~]# helm install hello mychart
NAME: hello
LAST DEPLOYED: Sun Jul 12 11:13:46 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@centos7 ~]# 
[root@centos7 ~]# helm list
NAME 	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART        	APP VERSION
db   	default  	1       	2020-07-11 21:19:00.155191143 +0800 CST	deployed	mysql-1.6.6  	5.7.30     
hello	default  	1       	2020-07-12 11:13:46.563225827 +0800 CST	deployed	mychart-0.1.0	1.16.0     
test 	default  	1       	2020-07-12 10:21:12.561456454 +0800 CST	deployed	mychart-0.1.0	1.16.0     
[root@centos7 ~]#
使用如下命令可以看到实际的模板被渲染过后的资源文件:
[root@centos7 ~]# helm get manifest hello
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mychart
  name: hello
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mychart
  template:
    metadata:
      labels:
        app: mychart
    spec:
      containers:
      - image: nginx:1.17
        name: nginx

[root@centos7 ~]#

升级:
[root@centos7 ~]# vim mychart/values.yaml
[root@centos7 ~]# helm upgrade hello /root/mychart
回滚:
[root@centos7 ~]# helm history hello
REVISION	UPDATED                 	STATUS    	CHART        	APP VERSION	DESCRIPTION     
1       	Sun Jul 12 11:13:46 2020	superseded	mychart-0.1.0	1.16.0     	Install complete
2       	Sun Jul 12 11:29:09 2020	superseded	mychart-0.1.0	1.16.0     	Upgrade complete
3       	Sun Jul 12 11:29:19 2020	deployed  	mychart-0.1.0	1.16.0     	Upgrade complete
[root@centos7 ~]# helm rollback hello 1
Rollback was a success! Happy Helming!
[root@centos7 ~]# 
卸载:
[root@centos7 ~]# helm list
NAME 	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART        	APP VERSION
db   	default  	1       	2020-07-11 21:19:00.155191143 +0800 CST	deployed	mysql-1.6.6  	5.7.30     
hello	default  	4       	2020-07-12 11:33:17.362740753 +0800 CST	deployed	mychart-0.1.0	1.16.0     
test 	default  	1       	2020-07-12 10:21:12.561456454 +0800 CST	deployed	mychart-0.1.0	1.16.0     
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# 
[root@centos7 ~]# helm uninstall db
release "db" uninstalled
[root@centos7 ~]# helm uninstall hello
release "hello" uninstalled
[root@centos7 ~]# helm uninstall test
release "test" uninstalled
[root@centos7 ~]#

查看渲染结果:
[root@centos7 ~]# helm install --dry-run web mychart
NAME: web
LAST DEPLOYED: Sun Jul 12 11:54:01 2020
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mychart
  name: hello
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mychart
  template:
    metadata:
      labels:
        app: mychart
    spec:
      containers:
      - image: nginx:1.16
        name: nginx

[root@centos7 ~]# 

 

posted @ 2020-07-10 23:26  pwcc  阅读(278)  评论(0)    收藏  举报