Helm Chart 基操

作者: Ju4t

得知道的事

  • 虽然都只是基础操作,但你要修改yaml必须得会kubernetes
  • 更多Chart参考 https://artifacthub.io/ 目前模版是开源的

创建模版

$ helm create helm-chart

$ tree
.
└── helm-chart
    ├── Chart.yaml
    ├── charts
    ├── templates
    │   ├── NOTES.txt
    │   ├── _helpers.tpl
    │   ├── deployment.yaml
    │   ├── hpa.yaml
    │   ├── ingress.yaml
    │   ├── service.yaml
    │   ├── serviceaccount.yaml
    │   └── tests
    │       └── test-connection.yaml
    └── values.yaml

修改

# 自定根据业务修改相关内容
code .

调试

调试模板可能很棘手,因为渲染后的模板发送给了Kubernetes API server,可能会以格式化以外的原因拒绝YAML文件。

以下命令有助于调试:

  • helm lint 是验证chart是否遵循最佳实践的首选工具。
  • helm template --debug 在本地测试渲染chart模板。
  • helm install --dry-run --debug:我们已经看到过这个技巧了,这是让服务器渲染模板的好方法,然后返回生成的清单文件。
  • helm get manifest: 这是查看安装在服务器上的模板的好方法。
$ helm template --debug test ./helm-chart

install.go:178: [debug] Original chart version: ""
install.go:195: [debug] CHART PATH: /Volumes/Data/***/helm-chart

---
# Source: helm-chart/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: test-helm-chart
  labels:
    helm.sh/chart: helm-chart-0.1.0
    app.kubernetes.io/name: helm-chart
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
---
# Source: helm-chart/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: test-helm-chart
  labels:
    helm.sh/chart: helm-chart-0.1.0
    app.kubernetes.io/name: helm-chart
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: helm-chart
    app.kubernetes.io/instance: test
---
# Source: helm-chart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-helm-chart
  labels:
    helm.sh/chart: helm-chart-0.1.0
    app.kubernetes.io/name: helm-chart
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: helm-chart
      app.kubernetes.io/instance: test
  template:
    metadata:
      labels:
        app.kubernetes.io/name: helm-chart
        app.kubernetes.io/instance: test
    spec:
      serviceAccountName: test-helm-chart
      securityContext:
        {}
      containers:
        - name: helm-chart
          securityContext:
            {}
          image: "nginx:1.16.0"
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {}
---
# Source: helm-chart/templates/tests/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
  name: "test-helm-chart-test-connection"
  labels:
    helm.sh/chart: helm-chart-0.1.0
    app.kubernetes.io/name: helm-chart
    app.kubernetes.io/instance: test
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
  annotations:
    "helm.sh/hook": test
spec:
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args: ['test-helm-chart:80']
  restartPolicy: Never

打包

$ helm package ./helm-chart/
Successfully packaged chart and saved it to: /Volumes/Data/***/helm-chart-0.1.0.tgz

安装

$ helm install test helm-chart-0.1.0.tgz