helm 通过全局变量模板,可以实现yaml高效的复用。

1 创建chart

# 创建自定义chart
[root@k8smaster helm]# helm create mychart
Creating mychart
[root@k8smaster helm]# ls
mychart  web-char  web-deploy.yaml  web-service.yaml
[root@k8smaster helm]# ls mychart/
charts  Chart.yaml  templates  values.yaml
[root@k8smaster helm]# rm -rf mychart/templates/*

2 修改模板

# 修改mychart中的values.yaml
添加 配置
replicas: 1
image: nginx
tag: 1.16
label: nginx
port: 80

拷贝web-deploy.yaml  web-service.yaml 到 templates目录下作为模板

# 原web-deploy.yaml
[root@k8smaster helm]# cat web-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
# 修改后的web-deploy.yaml
[root@k8smaster templates]# cat web-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: {{ .Values.label}}
  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: {}
# 原web-service.yaml
[root@k8smaster helm]# cat web-service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
  type: NodePort
status:
  loadBalancer: {}
# 修改后的web-service.yaml
[root@k8smaster templates]# cat web-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: 80
  selector:
    app: {{ .Values.label}}
  type: NodePort
status:
  loadBalancer: {}
# 尝试运行 查看模板填错后内容
[root@k8smaster helm]# helm install --dry-run myngin001 mychart/
NAME: myngin001
LAST DEPLOYED: Thu Jun  3 20:01:33 2021
NAMESPACE: default
STATUS: pending-install
REVISION: 1
TEST SUITE: None
HOOKS:
MANIFEST:
---
# Source: mychart/templates/web-service.yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: myngin001-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
status:
  loadBalancer: {}
---
# Source: mychart/templates/web-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: myngin001-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

3 安装chart

[root@k8smaster helm]# helm install mynginx001 mychart/
NAME: mynginx001
LAST DEPLOYED: Thu Jun  3 20:02:47 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

# 查看pod和服务
[root@k8smaster helm]# kubectl get pods,svc
NAME                                    READY   STATUS              RESTARTS   AGE
pod/mynginx001-deploy-f89759699-mxwdj   0/1     ContainerCreating   0          25s

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP        4h6m
service/mynginx001-svc   NodePort    10.103.215.139   <none>        80:30241/TCP   25s

此时可以正常访问nginx

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐