Kubernetes Helm Chart开发与最佳实践构建可复用的应用包一、Helm概述Helm是Kubernetes的包管理工具用于简化应用的部署和管理。Helm Chart是一个预配置的Kubernetes资源集合可以方便地进行版本管理和分发。1.1 Helm架构flowchart TD subgraph Helm客户端 A[Helm CLI] -- B[Chart Repository] B -- C[Tiller] end C -- D[Kubernetes Cluster] subgraph Kubernetes Cluster D -- E[Release] E -- F[Deployment] E -- G[Service] E -- H[Ingress] E -- I[ConfigMap] E -- J[Secret] end1.2 Helm组件组件功能Chart应用包定义ReleaseChart的部署实例RepositoryChart仓库Values配置参数二、Chart结构2.1 标准Chart目录结构my-chart/ ├── Chart.yaml # Chart元数据 ├── values.yaml # 默认配置值 ├── charts/ # 依赖的子Chart ├── templates/ # Kubernetes资源模板 │ ├── deployment.yaml │ ├── service.yaml │ ├── ingress.yaml │ ├── configmap.yaml │ └── secret.yaml └── templates/ └── NOTES.txt # 部署后提示信息2.2 Chart.yaml配置apiVersion: v2 name: my-app description: A Helm chart for Kubernetes type: application version: 1.0.0 appVersion: 1.0.0 keywords: - my-app - web home: https://example.com sources: - https://github.com/example/my-app maintainers: - name: John Doe email: johnexample.com dependencies: - name: redis version: 17.3.0 repository: https://charts.bitnami.com/bitnami三、模板开发3.1 Deployment模板apiVersion: apps/v1 kind: Deployment metadata: name: {{ include my-app.fullname . }} labels: {{- include my-app.labels . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: {{- include my-app.selectorLabels . | nindent 6 }} template: metadata: labels: {{- include my-app.selectorLabels . | nindent 8 }} spec: containers: - name: {{ .Chart.Name }} image: {{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }} imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: {{ .Values.service.port }} protocol: TCP resources: {{- toYaml .Values.resources | nindent 12 }}3.2 Service模板apiVersion: v1 kind: Service metadata: name: {{ include my-app.fullname . }} labels: {{- include my-app.labels . | nindent 4 }} spec: type: {{ .Values.service.type }} ports: - port: {{ .Values.service.port }} targetPort: http protocol: TCP name: http selector: {{- include my-app.selectorLabels . | nindent 4 }}3.3 Ingress模板{{- if .Values.ingress.enabled -}} apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: {{ include my-app.fullname . }} labels: {{- include my-app.labels . | nindent 4 }} annotations: {{- toYaml .Values.ingress.annotations | nindent 4 }} spec: {{- if .Values.ingress.tls }} tls: {{- range .Values.ingress.tls }} - hosts: {{- range .hosts }} - {{ . | quote }} {{- end }} secretName: {{ .secretName }} {{- end }} {{- end }} rules: {{- range .Values.ingress.hosts }} - host: {{ .host | quote }} http: paths: {{- range .paths }} - path: {{ .path }} pathType: {{ .pathType }} backend: service: name: {{ include my-app.fullname $ }} port: number: {{ $.Values.service.port }} {{- end }} {{- end }} {{- end }}四、Values配置4.1 values.yaml配置replicaCount: 1 image: repository: my-app pullPolicy: IfNotPresent tag: imagePullSecrets: [] nameOverride: fullnameOverride: service: type: ClusterIP port: 80 ingress: enabled: false className: annotations: {} hosts: - host: chart-example.local paths: - path: / pathType: ImplementationSpecific tls: [] resources: limits: cpu: 100m memory: 128Mi requests: cpu: 100m memory: 128Mi nodeSelector: {} tolerations: [] affinity: {}4.2 自定义Valuesenv: - name: DB_HOST value: {{ .Values.database.host }} - name: DB_PORT value: {{ .Values.database.port | quote }} database: host: db.example.com port: 5432 username: admin password: secret五、Chart模板函数5.1 常用模板函数{{ .Release.Name }} # Release名称 {{ .Release.Namespace }} # Release命名空间 {{ .Release.Revision }} # Release版本号 {{ .Chart.Name }} # Chart名称 {{ .Chart.Version }} # Chart版本 {{ .Values.key }} # 获取Values值 {{ include my-app.fullname . }} # 调用模板 {{ tpl .Values.template . }} # 模板渲染 {{ toYaml .Values }} # 转换为YAML {{ toJson .Values }} # 转换为JSON {{ quote .Values.string }} # 添加引号 {{ int .Values.number }} # 转换为整数5.2 条件判断{{- if .Values.ingress.enabled }} # 仅在ingress.enabled为true时渲染 {{- end }} {{- if eq .Values.env production }} replicaCount: 3 {{- else }} replicaCount: 1 {{- end }} {{- if or .Values.a .Values.b }} # a或b为true时渲染 {{- end }}5.3 循环遍历{{- range .Values.extraEnv }} - name: {{ .name }} value: {{ .value }} {{- end }} {{- range $key, $value : .Values.labels }} {{ $key }}: {{ $value }} {{- end }}六、Chart部署与管理6.1 安装Charthelm install my-release ./my-chart helm install my-release ./my-chart --values custom-values.yaml helm install my-release ./my-chart --set replicaCount36.2 升级Charthelm upgrade my-release ./my-chart helm upgrade my-release ./my-chart --values custom-values.yaml helm upgrade my-release ./my-chart --reuse-values6.3 回滚Charthelm rollback my-release helm rollback my-release 16.4 删除Releasehelm uninstall my-release七、Chart测试7.1 模板测试helm template ./my-chart helm template ./my-chart --values test-values.yaml output.yaml7.2 lint检查helm lint ./my-chart helm lint ./my-chart --values test-values.yaml7.3 安装测试helm install --dry-run ./my-chart helm install --debug ./my-chart八、Chart最佳实践8.1 Chart打包helm package ./my-chart helm package ./my-chart --version 1.1.08.2 Chart仓库helm repo add my-repo https://charts.example.com helm repo update helm search repo my-repo8.3 依赖管理helm dependency update helm dependency build九、总结Helm Chart开发实践包括Chart结构遵循标准目录结构模板开发使用Go模板语法Values配置提供合理的默认值条件渲染使用if/else控制渲染循环遍历处理列表数据Chart测试确保模板正确建议使用helm lint和helm template进行测试确保Chart质量。参考资料Helm文档Helm Chart模板指南Helm最佳实践