Helm
- 1、引入
- 2、概述
- 2.1 重点
- 2.2 V3版本的Helm
- 2.2.1 与之前版本的不同之处
- 2.2.2 V3版本的运行流程
- 3、安装和配置仓库、一些附带操作
- 3.1 安装
- 3.2 配置仓库
- 3.3 常用命令
- 3.4 添加helm的自动补齐
- 4、快速部署应用(weave应用)
- 5、 自行创建Chart
- 5.1 Chart目录内容解析
- 5.2 简单安装部署
- 5.3 应用升级回退
- 6、Chart模板实现高效复用
- 6.1 在values.yaml文件中定义全局变量
- 6.2 在templates中的yaml文件中定义变量
1、引入
- K8S 上的应用对象,都是由特定的资源描述组成,包括 deployment、service 等。都保存 各自文件中或者集中写到一个配置文件。然后 kubectl apply –f 部署。如果应用只由一 个或几个这样的服务组成,上面部署方式足够了。而对于一个复杂的应用,会有很多类似 上面的资源描述文件,例如微服务架构应用,组成应用的服务可能多达十个,几十个。如 果有更新或回滚应用的需求,可能要修改和维护所涉及的大量资源文件,而这种组织和管 理应用的方式就显得力不从心了。且由于缺少对发布过的应用版本管理和控制,使 Kubernetes 上的应用维护和更新等面临诸多的挑战,主要面临以下问题:(1)如何将这 些服务作为一个整体管理 (2)这些资源文件如何高效复用 (3)不支持应用级别的版本管理
2、概述
2.1 重点
- Helm 是一个 Kubernetes 的包管理工具,就像 Linux 下的包管理器,如 yum/apt 等,可以 很方便的将之前打包好的 yaml 文件部署到 kubernetes 上。
- Helm 有 3 个重要概念: (1)helm:一个命令行客户端工具,主要用于 Kubernetes 应用 chart 的创建、打包、发 布和管理。 (2)Chart:简单理解就是把yaml文件进行打包,是yaml文件的集合。官方描述为应用描述,一系列用于描述 k8s 资源相关文件的集合。 (3)Release:基于 Chart 的部署实体。一个 chart 被 Helm 运行后将会生成对应的一个 release;将在 k8s 中创建出真实运行的资源对象。通俗来说,每次部署后都会对应一个版本,也是所说的release。主要是为了进行版本管理。
2.2 V3版本的Helm
2.2.1 与之前版本的不同之处
- 删除了Tiller
- release可以在不同命名空间中进行重用
- 可以将Chart直接推送到docker镜像仓库中
2.2.2 V3版本的运行流程
3、安装和配置仓库、一些附带操作
3.1 安装
官网下载V3版本Helm
[root@master helm]# tar zxvf helm-v3.8.2-linux-amd64.tar.gz
linux-amd64/
linux-amd64/helm
linux-amd64/LICENSE
linux-amd64/README.md
[root@master helm]# ls
helm-v3.0.0-linux-amd64.tar.gz helm-v3.8.2-linux-amd64.tar.gz linux-amd64
[root@master helm]# cd linux-amd64/
[root@master linux-amd64]# ls
helm LICENSE README.md
[root@master linux-amd64]# mv helm /usr/bin/ #移动
3.2 配置仓库
##添加仓库: helm repo add 自己起仓库名字 仓库地址
[root@master ~]# helm repo add stable http://mirror.azure.cn/kubernetes/charts ##添加微软仓库
"stable" has been added to your repositories
[root@master ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 添加阿里云仓库
"aliyun" has been added to your repositories
[root@master ~]# helm repo update ##更新仓库
[root@master ~]# helm repo list ##列出仓库信息
NAME URL
stable http://mirror.azure.cn/kubernetes/charts
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
[root@master ~]# helm repo remove stable ##删除仓库
"stable" has been removed from your repositories
[root@master ~]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
3.3 常用命令
命令 | 描述 |
---|---|
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 客户端版本 |
3.4 添加helm的自动补齐
[root@master bash_completion.d]# echo "source <(helm completion bash)" >> ~/.bash_profile
[root@master bash_completion.d]# source ~/.bash_profile
[root@master bash_completion.d]# helm completion bash > /usr/share/bash-completion/completions/helm
4、快速部署应用(weave应用)
[root@master ~]# helm install ui aliyun/weave-scope #出现下面错误是因为仓库的问题,阿里云可能没事实时匹配到k8s,换成微软源可以解决
Error: INSTALLATION FAILED: unable to build kubernetes objects from release manifest: [unable to recognize "": no matches for kind "DaemonSet" in version "extensions/v1beta1", unable to recognize "": no matches for kind "Deployment" in version "apps/v1beta1"]
[root@master ~]# helm repo add stable http://mirror.azure.cn/kubernetes/charts/ ##添加微软仓库
[root@master ~]# helm repo update
[root@master ~]# helm repo list
NAME URL
aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
stable http://mirror.azure.cn/kubernetes/charts/
##部署
[root@master ~]# helm search repo weave ##部署的是一个ui界面应用,所以需要对外暴露端口,需要查看SVC的状态是否为NodePort
NAME CHART VERSION APP VERSION DESCRIPTION
aliyun/weave-cloud 0.1.2 Weave Cloud is a add-on to Kubernetes which pro...
aliyun/weave-scope 0.9.2 1.6.5 A Helm chart for the Weave Scope cluster visual...
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...
##helm install 自己起的名称 仓库里包的名称
[root@master ~]# helm install ui stable/weave-scope
WARNING: This chart is deprecated
W0514 11:23:29.545784 20101 warnings.go:70] rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
W0514 11:23:29.546481 20101 warnings.go:70] rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
W0514 11:23:29.567192 20101 warnings.go:70] rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
W0514 11:23:29.569900 20101 warnings.go:70] rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
NAME: ui
LAST DEPLOYED: Sat May 14 11:23:29 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@master ~]# helm list ##查看部署的应用
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
ui default 1 2022-05-14 11:23:29.478377222 +0800 CST deployed weave-scope-1.1.12 1.12.0
[root@master ~]# helm status ui #查看部署应用的状态
NAME: ui
LAST DEPLOYED: Sat May 14 11:23:29 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@master ~]# kubectl get pods ##查看部署的pod
NAME READY STATUS RESTARTS AGE
weave-scope-agent-ui-d7fv8 1/1 Running 0 9m21s
weave-scope-agent-ui-mrnb5 1/1 Running 0 9m21s
weave-scope-agent-ui-nrtrn 1/1 Running 0 9m21s
weave-scope-cluster-agent-ui-5cbc84db49-fzf7f 1/1 Running 0 9m21s
weave-scope-frontend-ui-6698fd5545-pn8tc 1/1 Running 0 9m21s
web-96d5df5c8-zcshb 1/1 Running 1 15h
[root@master ~]# kubectl get svc ##ui-weave-scope的svc状态没有暴露端口
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 65d
ui-weave-scope ClusterIP 10.101.125.54 <none> 80/TCP 9m25s
web NodePort 10.96.63.223 <none> 80:31281/TCP 15h
[root@master ~]# kubectl edit svc ui-weave-scope ## 编辑内部yaml文件,将状态换为NodePort
service/ui-weave-scope edited
[root@master ~]# kubectl describe svc ui-weave-scope
Name: ui-weave-scope
Namespace: default
Labels: app=weave-scope
app.kubernetes.io/managed-by=Helm
chart=weave-scope-frontend-1.1.10
component=frontend
heritage=Helm
release=ui
Annotations: cloud.weave.works/launcher-info:
{
"server-version": "master-4fe8efe",
"original-request": {
"url": "/k8s/v1.7/scope.yaml"
},
"email-address": "support@weave.works",
"source-app": "weave-scope",
"weave-cloud-component": "scope"
}
meta.helm.sh/release-name: ui
meta.helm.sh/release-namespace: default
Selector: app=weave-scope,component=frontend,release=ui
Type: NodePort ##状态已经修改
IP Families: <none>
IP: 10.101.125.54
IPs: 10.101.125.54
Port: http 80/TCP
TargetPort: http/TCP
NodePort: http 31055/TCP
Endpoints: 10.244.2.36:4040
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 65d
ui-weave-scope NodePort 10.101.125.54 <none> 80:31055/TCP 15m
web NodePort 10.96.63.223 <none> 80:31281/TCP 15h
- 最终布置效果
5、 自行创建Chart
5.1 Chart目录内容解析
- chart.yaml 指当前chart属性配置信息
- templates 编写的yaml文件放到当前目录
- values.yaml yaml文件可以使用全局变量
5.2 简单安装部署
[root@master ~]# helm create mychart ##创建Chart
[root@master ~]# cd mychart/
[root@master mychart]# ls
charts Chart.yaml templates values.yaml
[root@master mychart]# cd templates/
[root@master templates]# ls
deployment.yaml hpa.yaml NOTES.txt service.yaml
_helpers.tpl ingress.yaml serviceaccount.yaml tests
[root@master mychart]# kubectl create deployment web1 --image=nginx --dry-run -o yaml >
deployment.yaml
[root@master mychart]# kubectl create deployment web1 --image=nginx ##后续创建svc需要web1存在
[root@master templates]# kubectl expose deployment web1 --port=80 --target-port=80 --type=NodePort --dry-run -o yaml > service.yaml ##创建service.yaml
[root@master ~]# kubectl delete deployments.apps web1 ##删除,一会通过helm进行安装
[root@master templates]# ls
deployment.yaml service.yaml
# helm安装
[root@master ~]# helm install web1 mychart/ ##install+ 自己起名称+ Chart目录
NAME: web1
LAST DEPLOYED: Sat May 14 20:15:51 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 66d
ui-weave-scope NodePort 10.101.125.54 <none> 80:31055/TCP 8h
web NodePort 10.96.63.223 <none> 80:31281/TCP 24h
web1 NodePort 10.108.249.102 <none> 80:30502/TCP 7s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
web1-6fbb48567f-pdgxv 1/1 Running 0 13s
5.3 应用升级回退
[root@master ~]# helm history web1
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Sat May 14 20:15:51 2022 superseded mychart-0.1.0 1.16.0 Install complete
2 Sat May 14 20:18:18 2022 superseded mychart-0.1.0 1.16.0 Upgrade complete
3 Sat May 14 20:18:31 2022 deployed mychart-0.1.0 1.16.0 Rollback to 1
[root@master ~]# helm upgrade web1 mychart/ ##升级
Release "web1" has been upgraded. Happy Helming!
NAME: web1
LAST DEPLOYED: Sat May 14 20:18:18 2022
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
[root@master ~]# helm rollback web1 ##回退到上一版本
Rollback was a success! Happy Helming!
[root@master ~]# helm rollback web1 2 ##回退到指定版本
6、Chart模板实现高效复用
-
通过传递参数,动态渲染模板,yaml文件内容动态传入参数生成。主要是通过value.yaml文件
-
实现主要分为两步:1、通过values.yaml文件定义变量和值;2、在具体的模板下的yaml文件中进行获取变量
-
一般情况下,yaml文件中不同的有name、label、replicas、tag、port
6.1 在values.yaml文件中定义全局变量
[root@master ~]# cd mychart/
[root@master mychart]# ls
charts Chart.yaml templates values.yaml
[root@master mychart]# vim values.yaml
[root@master mychart]# cat values.yaml ##主要修改的变量信息
image: nginx
replicas: 1
tag: 1.16
label: nginx
port: 80
6.2 在templates中的yaml文件中定义变量
- 通过表达式形式定义变量 {{ .Values.变量名称}} 还有一种常用的定义名字的{{ .Release.Name}}
[root@master templates]# pwd
/root/mychart/templates
[root@master templates]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label}}
name: {{ .Release.Name}}-deploy
spec:
replicas: {{ .Values.replicas}}
selector:
matchLabels:
app: {{ .Values.label}}
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label}}
spec:
containers:
- image: {{ .Values.image}}
name: {{ .Values.label}}
resources: {}
status: {}
[root@master templates]# cat 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: {{ .Release.Name}}-svc
type: NodePort
status:
loadBalancer: {}
[root@master ~]# helm install web2 mychart/ --dry-run ##生成说明成功了
NAME: web2
LAST DEPLOYED: Sat May 14 21:33:23 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: web2-svc
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
name: nginx
resources: {}
status: {}
[root@master ~]# helm install web2 mychart/
NAME: web2
LAST DEPLOYED: Sat May 14 21:35:06 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web2-deploy-6799fc88d8-8khvj 1/1 Running 0 25s
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web2-svc NodePort 10.101.45.10 <none> 80:30704/TCP 30s