1. 程式人生 > 其它 >Helm系列(二) 自建立Chart

Helm系列(二) 自建立Chart

一、配置Chart

之前的Chart是通過從設定的倉庫中拉取,那麼如何自定義Chart呢?

1、建立Chart

[root@k8smaster ~]# helm create mychart
Creating mychart
[root@k8smaster ~]# ll mychart/
總用量 8
drwxr-xr-x 2 root root    6 6月  27 23:58 charts
-rw-r--r-- 1 root root 1143 6月  27 23:58 Chart.yaml
drwxr-xr-x 3 root root  162 6月  27 23:58 templates
-rw-r--r-- 1 root root 1874 6月 27 23:58 values.yaml
  • Chart.yaml 當前Chart屬性配置資訊
  • templates 自定義yaml檔案存放的目錄
  • values.yaml 全域性變數定義的檔案

2、自定義yaml檔案

再templates目錄中新建兩個yaml檔案:

  • deployment.yaml
  • service.yaml

上面中的通過helm create建立了模板,裡面已經有兩個檔案,如果自己的deployment,可以進行替換。

deployment.yaml檔案:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include 
"mychart.fullname" . }} labels: {{- include "mychart.labels" . | nindent 4 }} spec: {{- if not .Values.autoscaling.enabled }} replicas: {{ .Values.replicaCount }} {{- end }} selector: matchLabels: {{- include "mychart.selectorLabels" . | nindent 6 }} template: metadata: {{
- with .Values.podAnnotations }} annotations: {{- toYaml . | nindent 8 }} {{- end }} labels: {{- include "mychart.selectorLabels" . | nindent 8 }} spec: {{- with .Values.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} serviceAccountName: {{ include "mychart.serviceAccountName" . }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} containers: - name: {{ .Chart.Name }} securityContext: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: 80 protocol: TCP livenessProbe: httpGet: path: / port: http readinessProbe: httpGet: path: / port: http resources: {{- toYaml .Values.resources | nindent 12 }} {{- with .Values.nodeSelector }} nodeSelector: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.affinity }} affinity: {{- toYaml . | nindent 8 }} {{- end }} {{- with .Values.tolerations }} tolerations: {{- toYaml . | nindent 8 }} {{- end }}

service.yaml檔案:

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mychart.fullname" . }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "mychart.selectorLabels" . | nindent 4 }}

可以看到上面兩個檔案中一些變數是以 {{ }} 進行書寫,而這些檔案中變數來自於values.yaml檔案中:

# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: nginx
  pullPolicy: IfNotPresent
  # Overrides the image tag whose default is the chart appVersion.
  tag: ""

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  # Specifies whether a service account should be created
  create: true
  # Annotations to add to the service account
  annotations: {}
  # The name of the service account to use.
  # If not set and create is true, a name is generated using the fullname template
  name: ""

podAnnotations: {}

podSecurityContext: {}
  # fsGroup: 2000

securityContext: {}
  # capabilities:
  #   drop:
  #   - ALL
  # readOnlyRootFilesystem: true
  # runAsNonRoot: true
  # runAsUser: 1000

service:
  type: NodePort
  port: 80

ingress:
  enabled: false
  className: ""
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  hosts:
    - host: chart-example.local
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #   cpu: 100m
  #   memory: 128Mi
  # requests:
  #   cpu: 100m
  #   memory: 128Mi

autoscaling:
  enabled: false
  minReplicas: 1
  maxReplicas: 100
  targetCPUUtilizationPercentage: 80
  # targetMemoryUtilizationPercentage: 80

nodeSelector: {}

tolerations: []

affinity: {}

所以如果需要部署不同的deployment.yaml,只需要修改values.yaml檔案中的變數,這樣也就進行了yaml檔案的複用。values.yaml檔案中可以定義這幾個變數,一般這是不同的:

  • image
  • tag
  • label
  • port
  • replicas

二、安裝Chart

[root@k8smaster ~]# helm install web mychart
NAME: web
LAST DEPLOYED: Mon Jun 28 00:47:03 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
...

檢視Service:

[root@k8smaster ~]# kubectl get svc
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes     ClusterIP   10.96.0.1      <none>        443/TCP        2m55s
web2-mychart   NodePort    10.108.84.36   <none>        80:31334/TCP   9s