一、helm-chart高效复用(动态渲染yaml文件部署)
二、helm-chart高效复用实验
### --- 定义全局变量的配置文件
[root@k8s-master ~]# cd mychart/
[root@k8s-master mychart]# ls
values.yaml
### --- 在values.yaml定义变量和值
[root@k8s-master ~]# cd mychart/
[root@k8s-master mychart]# vim values.yaml
replicas: 1
image: nginx
tag: 1.16
label: nginx
port: 80
### --- 在具体yaml文件,获取变量定义值
~~~ yaml文件大体有几个地方不同
~~~ image:镜像不一致
~~~ tag:镜像版本
~~~ label:label名称不一致
~~~ port:端口号不一致
~~~ replicats:副本数不一致
~~~ #在templates的yaml文件使用values.yaml定义变量
~~~ 通过表达式形式使用全局变量
~~~ {{ .Values.变量名称}}
~~~ {{ .Release.Names}}
~~~ #第二种方案,取到当前版本的名称
[root@k8s-master mychart]# cd templates/
[root@k8s-master templates]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name}}-deploy
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: {}
[root@k8s-master templates]# vim service.yaml
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name}}-svc
spec:
ports:
- port: {{ .Values.port}}
protocol: TCP
targetPort: 80
selector:
app: {{ .Values.label}}
type: NodePort
status:
loadBalancer: {}
### --- 创建生成yaml文件,
[root@k8s-master ~]# helm install --dry-run web2 mychart/
NAME: web2
LAST DEPLOYED: Tue Feb 23 21:39:51 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
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:
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: {}
### --- 通过动态渲染yaml文件,部署应用
[root@k8s-master ~]# helm install web2 mychart
NAME: web2
LAST DEPLOYED: Tue Feb 23 21:41:50 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@k8s-master ~]# kubectl get pods
web2-deploy-f89759699-q2hzd 1/1 Running 0 32s
[root@k8s-master ~]# kubectl get svc
web2-svc NodePort 10.111.251.89 <none> 80:31700/TCP 74s