创建第一个helm chart

在本节中,我们将:

  • 创建chart
  • 查看chart的文件结构
  • 从头开始编写chart的yaml文件
  • 部署chart
  • 查看chart
  • 删除chart

创建chart

mkdir helm & cd helm
helm create nginx-application

image-20211203155548931

在创建chart项目后,自动为我们创建了许多yaml文件:

File or Directory Description
charts/ Sub-charts that the chart depends on
Chart.yaml Information about your chart
values.yaml The default values for your templates
template/ The template files
template/deployment.yaml Basic manifest for creating Kubernetes Deployment objects
template/_helpers.tpl Used to define Go template helpers
template/hpa.yaml Basic manifest for creating Kubernetes Horizontal Pod Autoscaler objects
template/ingress.yaml Basic manifest for creating Kubernetes Ingress objects
template/NOTES.txt A plain text file to give users detailed information about how to use the newly installed chart
template/serviceaccount.yaml Basic manifest for creating Kubernetes ServiceAccount objects
template/service.yaml Basic manifest for creating Kubernetes Service objects
tests/ Directory of Test files
tests/test-connections.yaml Tests that validate that your chart works as expected when it is installed

在本节中,我们将部署一个nginx deployment和service,所以可以将不使用的资源文件删除,只保留以下四个文件:

image-20211203160012277

编写chart

values.yaml:

deployment:
  replicaCount: 3
  name: my-deployment
  image:
    app: nginx
    version: latest
  
service:
  name: my-service
  type: NodePort
  port: 80
  targetPort: 80
  nodePort: 32036
  

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.deployment.name }}
  labels:
    app: nginx
spec:
  replicas: {{ .Values.deployment.replicaCount }}
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: "{{ .Values.deployment.image.app }}:{{ .Values.deployment.image.version}}"

service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.service.name }}
  labels:
    app: nginx
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.targetPort }}
      nodePort: {{ .Values.service.nodePort }}
      protocol: TCP
      name: http
  selector:
    app: nginx

从上面三个文件的组织结构,我们能发现helm的使用是比较简单的,它将变量抽离出来放到values.yaml中,并在其他的资源文件中引用这些变量的值。

部署chart

在部署chart之前,可以执行helm template .命令来预览生成的yaml结构

kongpingfan:~/environment/helm/nginx-application $ helm template .
---
# Source: nginx-application/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 32036
      protocol: TCP
      name: http
  selector:
    app: nginx
---
# Source: nginx-application/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: "nginx:latest"

部署chart:

kongpingfan:~/environment/helm/nginx-application $ helm install my-nginx-chart .
NAME: my-nginx-chart
LAST DEPLOYED: Fri Dec  3 08:14:35 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

执行kubectl get deploy,service,确认nginx应用已安装成功。

查看chart

kongpingfan:~/environment/helm/nginx-application $ helm list 
NAME            NAMESPACE       REVISION        UPDATED                                    STATUS          CHART                   APP VERSION       
my-nginx-chart  default         1               2021-12-03 08:14:35.486488989 +0000 UTC    deployed        nginx-application-0.1.0 1.16.0

删除chart

kongpingfan:~/environment/helm/nginx-application $ helm uninstall my-nginx-chart
release "my-nginx-chart" uninstalled