实现yaml高效复用,通过传递参数,动态渲染模板,yaml内容动态传入参数生成

    1. cd mychart
    2. cat >> values.yaml << EOF
    3. replicas: 1
    4. image: nginx
    5. tag: 1.16
    6. label: web1
    7. port: 80
    8. serviceAccount:
    9. # Specifies whether a service account should be created
    10. create: true
    11. # Annotations to add to the service account
    12. annotations: {}
    13. # The name of the service account to use.
    14. # If not set and create is true, a name is generated using the fullname template
    15. name: ""
    16. podAnnotations: {}
    17. podSecurityContext: {}
    18. # fsGroup: 2000
    19. securityContext: {}
    20. service:
    21. type: ClusterIP
    22. port: 80
    23. ingress:
    24. enabled: false
    25. annotations: {}
    26. # kubernetes.io/ingress.class: nginx
    27. # kubernetes.io/tls-acme: "true"
    28. hosts:
    29. - host: chart-example.local
    30. paths: []
    31. tls: []
    32. # - secretName: chart-example-tls
    33. # hosts:
    34. # - chart-example.local
    35. resources: {}
    36. autoscaling:
    37. enabled: false
    38. minReplicas: 1
    39. maxReplicas: 100
    40. targetCPUUtilizationPercentage: 80
    41. # targetMemoryUtilizationPercentage: 80
    42. nodeSelector: {}
    43. tolerations: []
    44. affinity: {}
    45. 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
    

    image.png

    查看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