Helm Chart 結構
Vibe Prompt
「幫我建立一個 Helm Chart 目錄結構,用於部署 Next.js 應用,包含 Deployment、Service、Ingress、HPA。」
目錄
my-chart/
├── Chart.yaml # 中繼資料
├── values.yaml # 預設值
├── templates/ # Go Template 檔案
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── ingress.yaml
│ ├── hpa.yaml
│ └── _helpers.tpl # 共用函式
└── .helmignore # 忽略檔案
Chart.yaml
apiVersion: v2
name: my-app
version: 0.1.0
appVersion: "1.0.0"
description: A Helm chart for deploying my Next.js app
type: application
values.yaml
replicaCount: 2
image:
repository: ghcr.io/myorg/my-app
tag: latest
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
ingress:
enabled: true
host: myapp.com
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 70
常用指令
helm create my-chart # 建立新 Chart
helm lint my-chart # 檢查語法
helm template my-chart # 渲染預覽
helm install release-name my-chart # 安裝
helm upgrade release-name my-chart # 升級
helm rollback release-name 1 # 回滾
關鍵要點
- ✅ 請根據本章主題補充具體的學習重點
- ✅ 建議加入比較表格、程式碼範例或流程圖
- ✅ 確保內容扎實且有價值
Helm Chart 目錄結構
my-chart/
├── Chart.yaml # 版本資訊、相依性
├── values.yaml # 預設配置值
├── values.schema.json # (選填) values 的 JSON Schema 驗證
├── charts/ # 子 Chart(手動或透過 helm dependency)
├── crds/ # Custom Resource Definitions
└── templates/ # Go Template YAML 檔案
├── _helpers.tpl # 共用模板函數
├── deployment.yaml
├── service.yaml
├── ingress.yaml
├── configmap.yaml
├── secrets.yaml
├── hpa.yaml
├── serviceaccount.yaml
└── tests/ # Helm Test Pods
└── test-connection.yaml
| 檔案 | 必要? | 說明 |
|------|:------:|------|
| Chart.yaml | ✅ 必要 | API 版本、名稱、描述、版本號 |
| values.yaml | ✅ 必要 | 預設配置值,使用者可覆蓋 |
| templates/ | ✅ 必要 | Kubernetes 資源的 Go Template |
| _helpers.tpl | ❌ 選填 | 共用的命名、標籤模板函數 |
| crds/ | ❌ 選填 | CRD 定義,helm install 時自動安裝 |
| charts/ | ❌ 選填 | 子 Chart 相依性 |
Chart.yaml 範例
apiVersion: v2
name: my-app
description: A production-ready web application
version: 1.2.0
appVersion: "2.5.0"
kubeVersion: ">=1.25.0-0"
type: application
keywords:
- web
- api
maintainers:
- name: Developer Team
email: dev@example.com
dependencies:
- name: postgresql
version: "12.x"
repository: https://charts.bitnami.com/bitnami
condition: postgresql.enabled
Helm Chart 結構的設計哲學
Helm 之所以採用這樣的目錄結構,不是為了增加複雜度,而是為了解決 Kubernetes 部署中最常見的問題——「這個 YAML 是給哪個環境用的?」
為什麼需要 Chart 結構?
my-app/
├── Chart.yaml # 版本、依賴、描述
├── values.yaml # 所有可調整的參數
├── values-production.yaml # 正式環境覆寫
├── templates/ # Go Template 樣板
│ ├── deployment.yaml
│ └── service.yaml
└── charts/ # 子依賴(如 Redis、MySQL)
這個結構讓你可以:
- 環境隔離:
helm install --values values-production.yaml就是正式環境 - 版本追溯:Chart.yaml 的 version 欄位可以追蹤每次變更
- 依賴管理:不用手動安裝 Redis,Helm 自動管理子 Chart
Helm vs Kustomize:誰比較好?
| 比較 | Helm | Kustomize |
|:----|:----|:---------|
| 學習曲線 | 較陡(需學 Go Template) | 平緩(純 YAML patch) |
| 版本管理 | 內建(Helm Release) | 需仰賴 Git |
| 依賴管理 | ✅ 內建 | ❌ 沒有 |
| 回滾 | ✅ helm rollback | 需手動 |
| 適合場景 | 複雜應用、需要 CI/CD 整合 | 簡單 overlay、不想學新工具 |
下一章預告:Go Template 與 Values
看懂 Chart 結構只是第一步。下一章你會學到 Chart 的靈魂——Go Template。如何用 {{ .Values.replicaCount }} 讓同一個 Chart 在不同環境產生不同的 YAML,以及 template 函式庫的實戰技巧。