相依性與子圖表

🔥 Vibe Prompt

「Helm Chart 包含 Postgres(子圖表)、Redis(子圖表)和我的應用。覆寫子圖表的值。」

Chart.yaml 加入相依性

apiVersion: v2
name: fullstack
version: 1.0.0
dependencies:
  - name: postgresql
    version: "12.x"
    repository: https://charts.bitnami.com/bitnami
    condition: postgresql.enabled
  - name: redis
    version: "18.x"
    repository: https://charts.bitnami.com/bitnami
    condition: redis.enabled
# 下載相依性
helm dependency update ./fullstack
# 這會建立 charts/postgresql-12.x.tgz

覆寫子圖表的值

# values.yaml
postgresql:
  enabled: true
  auth:
    database: myapp
    username: myuser
    password: securepass
  primary:
    resources:
      limits:
        memory: "1Gi"

redis:
  enabled: true
  architecture: standalone
  auth:
    enabled: true
    password: redispass

全域值

# 所有圖表共用
global:
  environment: production
  monitoring:
    enabled: true

子圖表目錄結構

fullstack/
├── Chart.yaml        # 包含相依性
├── values.yaml       # 覆寫子圖表值
├── templates/
│   └── app.yaml
└── charts/
    ├── postgresql-12.x.tgz
    └── redis-18.x.tgz

常用指令

helm dep update ./fullstack   # 下載/更新相依性
helm dep list ./fullstack     # 列出相依性
helm install full ./fullstack # 部署所有元件
helm get values full          # 查看目前值

關鍵要點

  • ✅ 請根據本章主題補充具體的學習重點
  • ✅ 建議加入比較表格、程式碼範例或流程圖

為什麼需要子 Chart?

在真實的 Kubernetes 部署中,你的應用很少只靠自己運作。你可能需要:

  • PostgreSQL 資料庫
  • Redis 快取
  • Ingress Controller
  • Prometheus 監控

你可以手動撰寫這些資源的 YAML,但更聰明的方式是用**子 Chart(Subchart)**來管理。

Helm Dependency 的運作流程

你的 Chart(my-app)
├── Chart.yaml  → 宣告 dependencies:
│     - name: postgresql
│       version: 12.x
│       repository: https://charts.bitnami.com/bitnami
│
├── helm dependency update  → 下載子 Chart 到 charts/ 目錄
│
└── helm install my-app ./my-app  → 一次部署你的應用 + PostgreSQL

透過 helm dependency update,Helm 會自動下載指定版本的 PostgreSQL Chart 到 charts/ 目錄中。當你執行 helm install 時,你的應用和資料庫會同時部署

子 Chart 的實用技巧

| 技巧 | 作法 | 效果 | |:----|:----|:----| | 全域 Values | 使用 global. 前綴 | 所有子 Chart 共享同一組參數 | | 條件式部署 | condition + tags | 只在需要時才部署特定子 Chart | | 版本鎖定 | 指定 version 範圍 | 防止意外升級造成不相容 | | 覆寫 Values | 在父 Chart values.yaml 中設定 | 不需修改子 Chart 原始碼就能調整參數 |

下一章預告:Hooks 與生命週期

Dependency 解決了「部署哪些元件」的問題。下一章的 Hooks 則解決「部署順序」的問題——如何讓資料庫初始化完成後才啟動應用程式,以及如何在解除安裝前執行清理工作。

解鎖完整教學內容

本章為付費內容。加入專案即可解鎖超過 5000 字的深度解析,包含 10 個以上神級 Prompt 與真實 Source Code 範例!