Kubernetes Helm Chart开发最佳实践:打造可复用的应用包
引言
Helm是Kubernetes的包管理工具,通过Helm Chart可以将应用打包成可复用的包。今天就来分享一下Helm Chart开发的最佳实践。
Chart结构
标准Chart结构
一个标准的Chart结构如下:
mychart/
├── Chart.yaml # Chart元数据
├── values.yaml # 默认配置值
├── charts/ # 依赖的子Chart
├── templates/ # Kubernetes资源模板
│ ├── NOTES.txt # 部署说明
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── ingress.yaml
│ ├── configmap.yaml
│ ├── secret.yaml
│ └── _helpers.tpl # 辅助模板函数
├── templates/NOTES.txt # 部署后显示的说明信息
├── README.md # Chart说明文档
├── LICENSE # 许可证文件
└── .helmignore # 忽略文件列表
Chart.yaml配置
Chart.yaml包含Chart的元数据:
yaml
apiVersion: v2
name: myapp
description: A production-ready web application
type: application
version: 1.2.3
appVersion: "2.0.0"
kubeVersion: ">=1.24.0"
keywords:
- web
- api
- microservice
home: https://myapp.example.com
sources:
- https://github.com/example/myapp
maintainers:
- name: Wanli Hou
email: wanli@example.com
icon: https://myapp.example.com/icon.png
deprecated: false
Chart.yaml字段说明:
apiVersion:Chart API版本,v1适用于Helm 2,v2适用于Helm 3name:Chart名称,必须唯一version:Chart版本,使用语义化版本appVersion:应用版本kubeVersion:兼容的Kubernetes版本description:Chart描述type:Chart类型,application或librarykeywords:关键词列表maintainers:维护者信息
values.yaml配置
values.yaml包含Chart的默认配置值:
yaml
# 全局配置
global:
imageRegistry: ""
imagePullSecrets: []
storageClass: ""
# 应用配置
replicaCount: 3
image:
repository: myapp
pullPolicy: IfNotPresent
tag: "2.0.0"
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
serviceAccount:
create: true
annotations: {}
name: ""
podAnnotations: {}
podSecurityContext: {}
securityContext: {}
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
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
targetCPUUtilizationPercentage: 80
targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}
values.yaml最佳实践:
- 使用层次化结构组织配置
- 提供合理的默认值
- 添加注释说明配置项的用途
- 支持global配置用于共享值
.helmignore文件
.helmignore文件用于指定打包时需要忽略的文件:
# .helmignore
.git/
.gitignore
*.tgz
*.tar.gz
node_modules/
vendor/
常见忽略项:
- 版本控制目录(.git)
- 依赖目录(node_modules, vendor)
- 构建产物(*.tgz)
- 编辑器配置(.vscode, .idea)
模板开发
使用辅助函数
yaml
# templates/_helpers.tpl
{{- define "myapp.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
条件渲染
yaml
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "myapp.fullname" . }}
spec:
rules:
- host: {{ .Values.ingress.host }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ include "myapp.fullname" . }}
port:
number: {{ .Values.service.port }}
{{- end }}
最佳实践
配置验证
使用schema验证配置:
json
{
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"replicaCount": {
"type": "integer",
"minimum": 1,
"maximum": 10
}
}
}
版本管理
使用语义化版本管理:
yaml
version: 1.2.3
# 主版本.次版本.修订版本
结语
Helm Chart是Kubernetes应用打包的标准方式。通过遵循最佳实践,我们可以创建高质量的Chart。
本文作者:侯万里(万里侯),致力于Helm Chart开发的工程师