helm-charts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHelm Chart Development
Helm Chart 开发
Create, organize, and manage Helm charts for Kubernetes applications.
创建、整理和管理Kubernetes应用的Helm Chart。
Chart Structure
Chart 结构
my-app/
├── Chart.yaml # Chart metadata
├── values.yaml # Default values
├── charts/ # Dependencies
├── templates/
│ ├── NOTES.txt # Post-install notes
│ ├── _helpers.tpl # Template helpers
│ ├── deployment.yaml
│ ├── service.yaml
│ └── ingress.yaml
└── .helmignoremy-app/
├── Chart.yaml # Chart元数据
├── values.yaml # 默认配置值
├── charts/ # 依赖项
├── templates/
│ ├── NOTES.txt # 安装后说明
│ ├── _helpers.tpl # 模板助手
│ ├── deployment.yaml
│ ├── service.yaml
│ └── ingress.yaml
└── .helmignoreChart.yaml
Chart.yaml
yaml
apiVersion: v2
name: my-app
description: A Helm chart for My Application
version: 1.0.0
appVersion: "2.1.0"
dependencies:
- name: postgresql
version: "12.0.0"
repository: "https://charts.bitnami.com/bitnami"
condition: postgresql.enabledyaml
apiVersion: v2
name: my-app
description: A Helm chart for My Application
version: 1.0.0
appVersion: "2.1.0"
dependencies:
- name: postgresql
version: "12.0.0"
repository: "https://charts.bitnami.com/bitnami"
condition: postgresql.enabledvalues.yaml Structure
values.yaml 结构
yaml
image:
repository: myapp
tag: "1.0.0"
pullPolicy: IfNotPresent
replicaCount: 3
service:
type: ClusterIP
port: 80
ingress:
enabled: false
hosts:
- host: app.example.com
paths:
- path: /
pathType: Prefix
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
autoscaling:
enabled: false
minReplicas: 2
maxReplicas: 10yaml
image:
repository: myapp
tag: "1.0.0"
pullPolicy: IfNotPresent
replicaCount: 3
service:
type: ClusterIP
port: 80
ingress:
enabled: false
hosts:
- host: app.example.com
paths:
- path: /
pathType: Prefix
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
autoscaling:
enabled: false
minReplicas: 2
maxReplicas: 10Template Helpers (_helpers.tpl)
模板助手 (_helpers.tpl)
yaml
{{- define "my-app.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- define "my-app.labels" -}}
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/name: {{ include "my-app.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}yaml
{{- define "my-app.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name .Chart.Name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- define "my-app.labels" -}}
helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
app.kubernetes.io/name: {{ include "my-app.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}Deployment Template
部署模板
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "my-app.fullname" . }}
labels:
{{- include "my-app.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "my-app.selectorLabels" . | nindent 6 }}
template:
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
resources:
{{- toYaml .Values.resources | nindent 12 }}yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "my-app.fullname" . }}
labels:
{{- include "my-app.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "my-app.selectorLabels" . | nindent 6 }}
template:
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
resources:
{{- toYaml .Values.resources | nindent 12 }}Multi-Environment Configuration
多环境配置
my-app/
├── values.yaml # Defaults
├── values-dev.yaml # Development
├── values-staging.yaml # Staging
└── values-prod.yaml # ProductionInstall with environment:
bash
helm install my-app ./my-app -f values-prod.yaml -n productionmy-app/
├── values.yaml # 默认配置
├── values-dev.yaml # 开发环境
├── values-staging.yaml # 预发布环境
└── values-prod.yaml # 生产环境按环境安装:
bash
helm install my-app ./my-app -f values-prod.yaml -n productionCommon Patterns
常见模式
Conditional Resources
条件化资源
yaml
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
...
{{- end }}yaml
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
...
{{- end }}Iterating Lists
列表遍历
yaml
env:
{{- range .Values.env }}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end }}yaml
env:
{{- range .Values.env }}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end }}Validation Commands
验证命令
bash
helm lint my-app/
helm template my-app ./my-app --dry-run
helm install my-app ./my-app --dry-run --debugbash
helm lint my-app/
helm template my-app ./my-app --dry-run
helm install my-app ./my-app --dry-run --debugBest Practices
最佳实践
- Use semantic versioning
- Document all values with comments
- Use template helpers for repeated logic
- Pin dependency versions explicitly
- Include NOTES.txt with usage instructions
- Add labels consistently
- Test in all environments
- 使用语义化版本控制
- 为所有配置值添加注释说明
- 使用模板助手处理重复逻辑
- 显式固定依赖版本
- 包含NOTES.txt并添加使用说明
- 保持标签一致性
- 在所有环境中进行测试