通过传递参数,动态渲染模板,让yaml文件内容,动态传入参数
[root@k8s-master01 mychart]# ls
charts Chart.yaml templates values.yaml
# 在chart有values.yaml文件,定义yaml文件全局变量
一. helm变量使用
#我们大部分部署的服务结构基本一样,不同之处大致分为几种
1. image
2. tag
3. label名称
4. port
5. replicas
1. 定义全局变量
[root@k8s-master01 ~]# cd mychart/
[root@k8s-master01 mychart]# ls
charts Chart.yaml templates values.yaml
[root@k8s-master01 mychart]# pwd
/root/mychart
vim valume.yaml
replicaCount: 1 #副本数量
image: nginx #镜像
tag: 1.16 #版本
label: nginx #标签
port: 80 #端口
2. 编辑之前部署的yaml文件
#常见变量使用
{{ .Values.变量名称 }} #在yaml文件中调用变量的方法
{{ .Release.Name }} #因为每次的名称都不一样,获取当前实体的名称来使用
切换目录
[root@k8s-master01 mychart]# cd templates/
[root@k8s-master01 templates]# ls
deployment.yaml service.yaml
修改yaml
cat > deployment.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: {{ .Release.Name }}-deploy
name: {{ .Release.Name }}-deploy #helm install 时起的名称加-deply
spec:
replicas: 1
selector:
matchLabels:
app: {{ .Values.label }}
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: {{ .Values.label }}
spec:
containers:
- image: {{ .Values.image }}
name: nginx
resources: {}
status: {}
EOF
cat > service.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: web
name: {{ .Release.Name }}-svc
spec:
ports:
- port: {{ .Values.port }}
protocol: TCP
targetPort: {{ .Values.port }}
selector:
app: {{ .Values.label }}
type: NodePort
status:
loadBalancer: {}
EOF
测试语法
#回到mychart同级目录
cd ~
#测试
helm install web1 mychart/ --dry-run
返回
NAME: web1
LAST DEPLOYED: Mon Feb 1 15:21:32 2021
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: web
name: web1-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: web1-deploy
name: web1-deploy #helm install 时起的名称加-deply
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
#返回语法正常
部署
[root@k8s-master01 ~]# helm install web1 mychart/
NAME: web1
LAST DEPLOYED: Mon Feb 1 15:22:58 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s-master01 ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
web1 default 1 2021-02-01 15:22:58.560611304 +0800 CST deployed mychart-0.1.0 1.16.0
那么什么是复用呢? 我们来多创建几个
[root@k8s-master01 ~]# helm install web2 mychart/
NAME: web2
LAST DEPLOYED: Mon Feb 1 15:24:24 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s-master01 ~]# helm install web3 mychart/
NAME: web3
LAST DEPLOYED: Mon Feb 1 15:24:28 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
#我们在只需要修改创建的helm的管理名称,就可以创建多个相同的pod
[root@k8s-master01 ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
web1 default 1 2021-02-01 15:22:58.560611304 +0800 CST deployed mychart-0.1.0 1.16.0
web2 default 1 2021-02-01 15:24:24.971268591 +0800 CST deployed mychart-0.1.0 1.16.0
web3 default 1 2021-02-01 15:24:28.612617947 +0800 CST deployed mychart-0.1.0 1.16.0
#每个都是一套独立的应用,互不影响
二. 更新部署的应用
1. 修改yaml
vi values.yaml
tag: 1.16 #修改为1.17版本
2.更新
helm upgrade -f values.yaml web1 ~/mychart/
返回
Release "web1" has been upgraded. Happy Helming!
NAME: web1
LAST DEPLOYED: Mon Feb 1 15:55:48 2021
NAMESPACE: default
STATUS: deployed
REVISION: 2
TEST SUITE: None
查看
[root@k8s-master01 mychart]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
web1 default 2 2021-02-01 15:55:48.693513647 +0800 CST deployed mychart-0.1.0 1.16.0
web2 default 1 2021-02-01 15:24:24.971268591 +0800 CST deployed mychart-0.1.0 1.16.0
#可以看到REVISION变为了2
3. 回滚
helm rollback web1 1
查看
[root@k8s-master01 mychart]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
web1 default 3 2021-02-01 16:00:42.142226076 +0800 CST deployed mychart-0.1.0 1.16.0
web2 default 1 2021-02-01 15:24:24.971268591 +0800 CST deployed mychart-0.1.0 1.16.0
#版本变更为了3
#当你不清楚那个版本更新了什么,你可以根据历史记录查看
#查看第一版本
helm get all --revision 1 web1
返回
NAME: web1
LAST DEPLOYED: Mon Feb 1 15:22:58 2021
NAMESPACE: default
STATUS: superseded
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
null
COMPUTED VALUES:
image: nginx
label: nginx
port: 80
replicaCount: 1
tag: 1.16
HOOKS:
MANIFEST:
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: web
name: web1-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: web1-deploy
name: web1-deploy #helm install 时起的名称加-deply
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
查看第二版本
[root@k8s-master01 mychart]# helm get all --revision 2 web1
NAME: web1
LAST DEPLOYED: Mon Feb 1 15:55:48 2021
NAMESPACE: default
STATUS: superseded
REVISION: 2
TEST SUITE: None
USER-SUPPLIED VALUES:
image: nginx
label: nginx
port: 80
replicaCount: 1
tag: 1.17 #可以看到镜像的标签版本不同,由此来确认回退的版本
COMPUTED VALUES:
image: nginx
label: nginx
port: 80
replicaCount: 1
tag: 1.17
HOOKS:
MANIFEST:
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: web
name: web1-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: web1-deploy
name: web1-deploy #helm install 时起的名称加-deply
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}