实现yaml高效复用,通过传递参数,动态渲染模板,yaml内容动态传入参数生成
cd mychartcat >> values.yaml << EOFreplicas: 1image: nginxtag: 1.16label: web1port: 80serviceAccount:# Specifies whether a service account should be createdcreate: true# Annotations to add to the service accountannotations: {}# The name of the service account to use.# If not set and create is true, a name is generated using the fullname templatename: ""podAnnotations: {}podSecurityContext: {}# fsGroup: 2000securityContext: {}service:type: ClusterIPport: 80ingress:enabled: falseannotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths: []tls: []# - secretName: chart-example-tls# hosts:# - chart-example.localresources: {}autoscaling:enabled: falseminReplicas: 1maxReplicas: 100targetCPUUtilizationPercentage: 80# targetMemoryUtilizationPercentage: 80nodeSelector: {}tolerations: []affinity: {}EOF
在templates的yaml文件中使用values.yaml定义变量
- 通过表达式使用全局变量:{{ .Values.变量名称}}
- 获取当前版本:{{ .Release.Name}}
apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: {{ .Values.label}} name: {{ .Release.Name}}-deployment spec: replicas: {{ .Values.replicas}} selector: matchLabels: app: {{ .Values.label}} strategy: {} template: metadata: creationTimestamp: null labels: app: web1 spec: containers: - image: {{ .Values.image}} name: nginx resources: {} status: {}
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: {}
尝试运行,会打印所有yaml文件
[root@k8s-master01 helm]# helm install web2 myChart --dry-run
NAME: web2
LAST DEPLOYED: Tue May 4 18:01:07 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: web1
name: web2-svc
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web1
type: NodePort
status:
loadBalancer: {}
---
# Source: myChart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web1
name: web2-deployment
spec:
replicas: 1
selector:
matchLabels:
app: web1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web1
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
启动
helm install web2 myChart
查看部署情况:
kubectl get pods,svc

查看helm list
[root@k8s-master01 helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
ui default 1 2021-05-04 16:14:42.544918429 +0800 CST deployed weave-scope-1.1.12 1.12.0
web1 default 1 2021-05-04 17:49:36.858245738 +0800 CST deployed myChart-0.2.0 1.16.0
web2 default 1 2021-05-04 18:02:09.10007188 +0800 CST deployed myChart-0.2.0 1.16.0
