1. 概述
1. 引入helm
之前我们部署一个应用的基本过程: 部署deployment、service、ingress。如果少数应用还好,如果部署微服务项目,每个服务有一套yaml,需要维护大量的yaml, 可能管理不太方便。
helm可以把这些yaml 作为一个整体管理,实现yaml 高效复用,可以实现应用级别的管理。
2. 简介
helm 是k8s 的一个包管理工具,类似于linux 下面的包管理器,比如yum\apt 等,可以很方便的将之前打包好的yaml 文件部署到k8s 集群上。
三个重要概念:
(1) heml: 一个命令行客户端工具
(2) chart: 把yaml 打包,是yaml 集合
(3) Release: 基于chart 部署实体,应用级别的版本管理。
其架构如下:
2. 安装以及查看
1. 下载安装
到 https://github.com/helm/helm/tags 下载, 然后上传到linux
解压且将解压之后helm 目录复制到 /usr/bin 目录
[root@k8smaster1 helm]# tar -zxvf ./helm-v3.0.0-linux-amd64.tar.gz linux-amd64/ linux-amd64/helm linux-amd64/README.md linux-amd64/LICENSE [root@k8smaster1 helm]# cp ./linux-amd64/helm /usr/bin/
查看版本
[root@k8smaster1 helm]# helm version version.BuildInfo{Version:"v3.0.0", GitCommit:"e29ce2a54e96cd02ccfce88bee4f58bb6e2a28b6", GitTreeState:"clean", GoVersion:"go1.13.4"}
2. 配置仓库:
[root@k8smaster1 helm]# helm repo add stable http://mirror.azure.cn/kubernetes/charts "stable" has been added to your repositories [root@k8smaster1 helm]# helm repo list NAME URL stable http://mirror.azure.cn/kubernetes/charts [root@k8smaster1 helm]# helm repo remove stable "stable" has been removed from your repositories [root@k8smaster1 helm]# helm repo list Error: no repositories to show [root@k8smaster1 helm]# helm repo add stable http://mirror.azure.cn/kubernetes/charts "stable" has been added to your repositories Usage: helm repo add [NAME] [URL] [flags] [root@k8smaster1 helm]# 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!⎈
3. 简单使用
语法:
搜索: helm search repo 名称 安装: heml install 安装名称 搜索之后应用名称 查看安装状态: heml list \ helm status 安装名称
1. 测试:
[root@k8smaster1 helm]# helm search repo weave NAME CHART VERSION APP VERSION DESCRIPTION stable/weave-cloud 0.3.9 1.4.0 DEPRECATED - Weave Cloud is a add-on to Kuberne... stable/weave-scope 1.1.12 1.12.0 DEPRECATED - A Helm chart for the Weave Scope c... [root@k8smaster1 helm]# helm install ui stable/weave-scope NAME: ui LAST DEPLOYED: Wed Jan 19 23:44:18 2022 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/ [root@k8smaster1 helm]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION ui default 1 2022-01-19 23:44:18.40844005 -0500 EST deployed weave-scope-1.1.12 1.12.0 [root@k8smaster1 helm]# helm status ui NAME: ui LAST DEPLOYED: Wed Jan 19 23:44:18 2022 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/
删除chart 用如下命令:
[root@k8smaster1 volumestest]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION ui default 1 2022-01-19 23:44:18.40844005 -0500 EST deployed weave-scope-1.1.12 1.12.0 web2 default 1 2022-01-21 01:30:45.768452911 -0500 EST deployed mychart-0.1.0 1.16.0 [root@k8smaster1 volumestest]# helm delete web2 release "web2" uninstalled [root@k8smaster1 volumestest]# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION ui default 1 2022-01-19 23:44:18.40844005 -0500 EST deployed weave-scope-1.1.12 1.12.0
2. 修改service type 类型
(1) 查看类型,发现其 type 是 ClusterIP
[root@k8smaster1 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d nginx NodePort 10.109.175.64 <none> 80:32503/TCP 3d1h tomcat NodePort 10.100.178.25 <none> 8080:30158/TCP 3d ui-weave-scope ClusterIP 10.107.48.241 <none> 80/TCP 6h1m
(2) 修改为NodePort
执行如下命令:
[root@k8smaster1 ~]# kubectl edit svc ui-weave-scope service/ui-weave-scope edited [root@k8smaster1 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d nginx NodePort 10.109.175.64 <none> 80:32503/TCP 3d2h tomcat NodePort 10.100.178.25 <none> 8080:30158/TCP 3d1h ui-weave-scope NodePort 10.107.48.241 <none> 80:30197/TCP 6h29m
改为NodePort 即可从主机通过k8s的节点访问30197 端口查看相关界面
3. 查看:
weave scope 是一个k8s可视化界面,可以查看k8s 集群相关
weave scope 可以查看好多信息,比如pods 信息,每个pod 的日志以及secribe信息,测试:
(1) 查看所有pods
(2) 查看pod 信息
(3) 查看日志
(4) 查看describe 信息
(5) 查看service信息
(6) 查看pod 对应的容器信息以及进入容器
1》 点击containers 容器
2》查看容器信息以及进入容器
3. heml 创建chart
1. 创建mychart, 实际会 创建一个文件夹
[root@k8smaster1 chart]# helm create mychart Creating mychart [root@k8smaster1 chart]# ls mychart [root@k8smaster1 chart]# cd mychart/
查看相关目录信息:
[root@k8smaster1 mychart]# ll -R .: total 8 drwxr-xr-x 2 root root 6 Jan 20 23:12 charts -rw-r--r-- 1 root root 905 Jan 20 23:12 Chart.yaml drwxr-xr-x 3 root root 146 Jan 20 23:12 templates -rw-r--r-- 1 root root 1490 Jan 20 23:12 values.yaml ./charts: total 0 ./templates: total 24 -rw-r--r-- 1 root root 1626 Jan 20 23:12 deployment.yaml -rw-r--r-- 1 root root 1847 Jan 20 23:12 _helpers.tpl -rw-r--r-- 1 root root 1030 Jan 20 23:12 ingress.yaml -rw-r--r-- 1 root root 1581 Jan 20 23:12 NOTES.txt -rw-r--r-- 1 root root 207 Jan 20 23:12 serviceaccount.yaml -rw-r--r-- 1 root root 361 Jan 20 23:12 service.yaml drwxr-xr-x 2 root root 34 Jan 20 23:12 tests ./templates/tests: total 4 -rw-r--r-- 1 root root 383 Jan 20 23:12 test-connection.yaml
Chart.yaml 当前chart的版本信息
charts: 一个空文件夹
templates: 一些默认的yaml 文件
values.yaml: 类似于全局变量提取的文件,抽取之后templates 文件夹下面的yaml 可以复用该变量
2. 创建deployment 和 service 的yaml
[root@k8smaster1 templates]# kubectl create deployment web1 --image=nginx --dry-run -o yaml > deployment.yaml W0120 23:20:39.498957 118143 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client. [root@k8smaster1 templates]# ls deployment.yaml [root@k8smaster1 templates]# kubectl apply -f deployment.yaml deployment.apps/web1 created [root@k8smaster1 templates]# kubectl expose deployment web1 --port=80 --target-port=80 --type=NodePort --dry-run -o yaml > service.yaml W0120 23:23:08.805846 118735 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client. [root@k8smaster1 templates]# kubectl delete -f deployment.yaml deployment.apps "web1" deleted [root@k8smaster1 templates]# ls deployment.yaml service.yaml
先创建deployment.yaml, 然后创建资源; 然后生成service.yaml, 这个要求暴露的deployment 必须存在。然后删掉之前创建的的deployment。 这么操作是为了得到两个yaml。
3. 执行创建
[root@k8smaster1 chart]# helm install web1 mychart/ NAME: web1 LAST DEPLOYED: Thu Jan 20 23:26:09 2022 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None [root@k8smaster1 chart]# kubectl get pods,svc -o wide | grep web1 pod/web1-7f87dfbd56-9c6rb 1/1 Running 0 19s 10.244.2.105 k8snode2 <none> <none> service/web1 NodePort 10.111.151.4 <none> 80:30539/TCP 19s app=web1
4. 执行升级
1. 编辑yaml 文件, 将nginx 的image 版本改为1.14
2. 执行如下命令升级
[root@k8smaster1 chart]# helm upgrade web1 mychart Release "web1" has been upgraded. Happy Helming! NAME: web1 LAST DEPLOYED: Thu Jan 20 23:36:36 2022 NAMESPACE: default STATUS: deployed REVISION: 2 TEST SUITE: None
3. weave-scope 查看nginx 版本
或者自己进入容器查看:
[root@k8smaster1 chart]# kubectl exec -it web1-6cffb575cb-wbfrf -- bash root@web1-6cffb575cb-wbfrf:/# nginx -v nginx version: nginx/1.14.2
5. 使用values.yaml 实现变量提取
1. values.yaml 增加如下配置
replicaCount: 1 image: nginx:1.15 label: nginx port: 80
2. templates的yaml 文件使用values.yaml 定义的变量
(1) 通过表达式形式获取
{{ .Values.varname}}
(2) 获取全局信息
{{ .Release.Name}} 获取发布名称
3. 修改templates下面的两个yaml
(1) deployment.yaml 文件如下
apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: {{ .Values.label}} name: {{ .Release.Name}}-deploy spec: replicas: {{ .Values.replicaCount}} selector: matchLabels: app: {{ .Values.label}} strategy: {} template: metadata: creationTimestamp: null labels: app: {{ .Values.label}} spec: containers: - image: {{ .Values.image}} name: nginx resources: {} status: {}
(2) service.yaml 内容如下:
apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: {{ .Values.label}} name: {{ .Release.Name}}-svc spec: ports: - port: {{ .Values.port}} protocol: TCP targetPort: {{ .Values.port}} selector: app: {{ .Values.label}} type: NodePort status: loadBalancer: {}
4. 测试执行查看输出的yaml
[root@k8smaster1 chart]# helm install --dry-run web2 mychart/ NAME: web2 LAST DEPLOYED: Fri Jan 21 01:28:30 2022 NAMESPACE: default STATUS: pending-install REVISION: 1 TEST SUITE: None HOOKS: MANIFEST: --- # Source: mychart/templates/service.yaml apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: nginx name: web2-svc spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: NodePort status: loadBalancer: {} --- # Source: mychart/templates/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: web2-deploy spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.15 name: nginx resources: {} status: {}
5. 执行创建查看资源
[root@k8smaster1 chart]# helm install web2 mychart/ NAME: web2 LAST DEPLOYED: Fri Jan 21 01:30:45 2022 NAMESPACE: default STATUS: deployed REVISION: 1 TEST SUITE: None [root@k8smaster1 chart]# kubectl get pods,svc -o wide | grep web2 pod/web2-deploy-7d79cb6c68-7sjkb 1/1 Running 0 52s 10.244.2.106 k8snode2 <none> <none> service/web2-svc NodePort 10.96.166.138 <none> 80:32181/TCP 52s app=nginx
weave-scope 查看镜像信息: