helm介绍
helm是一个k8s的包管理工具,就像linux系统下的包管理器,如yum、apt等,可以很方便的将之前打包好的yaml文件部署到k8s上
相比之前的部署一个简单的服务流程:
(1)编写yaml文件
(2)创建pod
(3)创建svc
(4)做代理Ingress
这种部署单一的应用,相对于简单的应用比较合适,如果是微服务项目,需要几十个服务,每一个服务都有一套yaml文件,维护与更改就不是很方便
但是使用helm可以:
(1)把yaml文件整体管理起来
(2)实现yaml的高效复用
(3)使用helm应用级别的版本管理(升级降级)
其中有3个重要的概念
(1)helm:一个命令行客户端工具,主要是用于k8s应用chart的创建、打包、发布和管理
(2)chart:应用描述,一系列用于描述k8s资源相关文件的集合,把yaml打包,是yaml的集合,支持推送到docker仓库中
(3)release:基于chart的部署实体,应用级别的版本管理。一个chart被helm运行后将会生成对应的一个release,将在k8s中创建出真实运行的资源对象。
helm v3版本安装
(1)下载helm tar文件包,上传到linux服务器中
(2)把helm文件移到/usr/bin目录下
(3)完成
[root@k8s-master dwz]# tar -zxvf helm-v3.3.4-linux-amd64.tar.gz linux-amd64/ linux-amd64/README.md linux-amd64/LICENSE linux-amd64/helm [root@k8s-master dwz]# ls gisserver helm-v3.3.4-linux-amd64.tar.gz iserver linux-amd64 [root@k8s-master dwz]# cd linux-amd64/ [root@k8s-master linux-amd64]# ls helm LICENSE README.md [root@k8s-master linux-amd64]# mv helm /usr/bin/ [root@k8s-master 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_DRIVER | set the backend storage driver. Values are: configmap, secret, memory, postgres | | $HELM_DRIVER_SQL_CONNECTION_STRING | set the connection string the SQL storage driver should use. | | $HELM_NO_PLUGINS | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins. | | $KUBECONFIG | set an alternative Kubernetes configuration file (default "~/.kube/config") |
配置helm仓库
(1)添加仓库
helm repo add 仓库名称 仓库地址
[root@k8s-master linux-amd64]# helm repo add aliyun https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts-incubator/ "aliyun" has been added to your repositories
(2)查看仓库
[root@k8s-master linux-amd64]# helm repo list NAME URL aliyun https://aliacs-app-catalog.oss-cn-hangzhou.aliyuncs.com/charts-incubator/
(3)更新仓库地址
[root@k8s-master ~]# helm repo update Hang tight while we grab the latest from your chart repositories... ...Successfully got an update from the "aliyun" chart repository Update Complete. ⎈Happy Helming!⎈
(4)移除仓库
[root@k8s-master ~]# helm repo remove aliyun "aliyun" has been removed from your repositories [root@k8s-master ~]# helm repo list Error: no repositories to show
使用helm快速部署应用实例
添加微软仓库
[root@k8s-master ~]# helm repo add weiruan http://mirror.azure.cn/kubernetes/charts/ "weiruan" has been added to your repositories
(1)使用命令搜索应用(weave)
[root@k8s-master ~]# helm search repo weave NAME CHART VERSION APP VERSION DESCRIPTION weiruan/weave-cloud 0.3.9 1.4.0 DEPRECATED - Weave Cloud is a add-on to Kuberne... weiruan/weave-scope 1.1.12 1.12.0 DEPRECATED - A Helm chart for the Weave Scope c...
(2)根据搜索内容选择安装应用
[root@k8s-master ~]# helm install ui weiruan/weave-scope
(3)查看安装列表
[root@k8s-master ~]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION ui default 1 2020-10-28 15:19:15.421361319 +0800 CST deployed weave-scope-1.1.8 1.12.0
(4)查看安装具体信息
[root@k8s-master ~]# helm status ui NAME: ui LAST DEPLOYED: Wed Oct 28 15:19:15 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: You should now be able to access the Scope frontend in your web browser, by using kubectl port-forward: kubectl -n default port-forward $(kubectl -n default get endpoints \ ui-weave-scope -o jsonpath='{.subsets[0].addresses[0].targetRef.name}') 8080:4040 then browsing to http://localhost:8080/. For more details on using Weave Scope, see the Weave Scope documentation: https://www.weave.works/docs/scope/latest/introducing/
(5)查看pod运行状态
[root@k8s-master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE weave-scope-agent-ui-655pp 1/1 Running 3 15d weave-scope-agent-ui-bzttt 1/1 Running 2 15d weave-scope-agent-ui-hkxfz 1/1 Running 1 15d weave-scope-cluster-agent-ui-5d4d6c97bf-5xpqf 1/1 Terminating 0 8d weave-scope-cluster-agent-ui-5d4d6c97bf-f528k 1/1 Running 1 7d1h weave-scope-frontend-ui-757c6668c5-vqpdj 1/1 Running 1 7d1h weave-scope-frontend-ui-757c6668c5-zhlzp 1/1 Terminating 0 8d
(6)查看svc情况
[root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 50d ui-weave-scope ClusterIP 10.102.105.28 <none> 80:32448/TCP 15d
现在的weave的svc类型是ClusterIP,需要给改成NodePort类型,外部就可以访问了
[root@k8s-master ~]# kubectl edit svc ui-weave-scope
保存退出
再查看svc
[root@k8s-master ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 50d ui-weave-scope NodePort 10.102.105.28 <none> 80:32448/TCP 15d
使用浏览器访问http://ip:32448
完成一键安装部署
创建自定义Chart
1.使用命令创建Chart
[root@k8s-master helm]# helm create mychart Creating mychart
创建成功之后,会生成四个文件
charts
Chart.yaml 当前chart属性配置信息
templates 编写yaml文件存放的目录
values.yaml yaml文件可以使用的全局变量
2.在templates中创建yaml文件
[root@k8s-master templates]# kubectl create deployment web --image=nginx --dry-run -o yaml > deployment-nginx.yaml
[root@k8s-master templates]# kubectl get pod NAME READY STATUS RESTARTS AGE weave-scope-agent-ui-655pp 1/1 Running 3 15d weave-scope-agent-ui-bzttt 1/1 Running 2 15d weave-scope-agent-ui-hkxfz 1/1 Running 1 15d weave-scope-cluster-agent-ui-5d4d6c97bf-5xpqf 1/1 Terminating 0 8d weave-scope-cluster-agent-ui-5d4d6c97bf-f528k 1/1 Running 1 7d4h weave-scope-frontend-ui-757c6668c5-vqpdj 1/1 Running 1 7d4h weave-scope-frontend-ui-757c6668c5-zhlzp 1/1 Terminating 0 8d web-d86c95cc9-kvssx 1/1 Running 0 30m
删除web这个pod,因为我们要用helm方式创建
[root@k8s-master templates]# kubectl delete deployment web deployment.apps "web" deleted kubectl expose deployment web --port=80 --target-port=80 --type=NodePort --dry-run -o yaml > service .yaml
创建完deployment之后,pod成功启动才能创建svc,要不然会报错(这两步是为了生成yaml文件,自己写有些麻烦)
[root@k8s-master templates]# ls deployment-nginx.yaml service.yaml
3.安装mychart
[root@k8s-master helm]# helm install nginx mychart/ NAME: nginx LAST DEPLOYED: Thu Nov 12 20:18:36 2020 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None
查看pod、svc
[root@k8s-master helm]# kubectl get pod NAME READY STATUS RESTARTS AGE weave-scope-agent-ui-655pp 1/1 Running 3 15d weave-scope-agent-ui-bzttt 1/1 Running 2 15d weave-scope-agent-ui-hkxfz 1/1 Running 1 15d weave-scope-cluster-agent-ui-5d4d6c97bf-5xpqf 1/1 Terminating 0 8d weave-scope-cluster-agent-ui-5d4d6c97bf-f528k 1/1 Running 1 7d4h weave-scope-frontend-ui-757c6668c5-vqpdj 1/1 Running 1 7d4h weave-scope-frontend-ui-757c6668c5-zhlzp 1/1 Terminating 0 8d web-d86c95cc9-vzmnd 1/1 Running 0 6m23s [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 50d ui-weave-scope NodePort 10.102.105.28 <none> 80:32448/TCP 15d web NodePort 10.98.105.180 <none> 80:31375/TCP 6m28s
浏览器访问:http://ip:31375
重载应用
[root@k8s-master helm]# helm upgrade nginx mychart Release "nginx" has been upgraded. Happy Helming! NAME: nginx LAST DEPLOYED: Thu Nov 12 21:49:50 2020 NAMESPACE: default STATUS: deployed REVISION: 3 TEST SUITE: None