Kubernetes Helm 套件管理工具

Kubernetes Helm Package Manager Guide

Helm 概述與核心概念

Helm 是 Kubernetes 的套件管理工具,類似於 Linux 系統中的 apt 或 yum。它能夠簡化 Kubernetes 應用程式的部署、升級與管理流程。透過 Helm,我們可以將複雜的 Kubernetes 資源定義打包成可重複使用的 Chart,大幅提升部署效率。

為什麼需要 Helm?

  • 簡化部署:將多個 Kubernetes 資源打包成單一 Chart
  • 版本控制:追蹤應用程式的版本與歷史記錄
  • 可重複使用:透過參數化設定,同一 Chart 可部署到不同環境
  • 便於分享:透過 Repository 分享與取得社群維護的 Chart

安裝 Helm

使用腳本安裝(Linux/macOS)

1
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

使用 Homebrew 安裝(macOS)

1
brew install helm

使用 apt 安裝(Debian/Ubuntu)

1
2
3
4
curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm

驗證安裝

1
helm version

核心概念:Chart、Release、Repository

Chart

Chart 是 Helm 的套件格式,包含了部署 Kubernetes 應用程式所需的所有資源定義。一個 Chart 可以包含 Deployment、Service、ConfigMap、Secret 等多種資源。

Release

Release 是 Chart 在 Kubernetes 叢集中的一個執行實例。每次安裝 Chart 都會建立一個新的 Release,同一個 Chart 可以在同一叢集中安裝多次,產生多個 Release。

Repository

Repository 是存放和分享 Chart 的地方,類似於 Docker Hub 之於 Docker Image。

新增與管理 Helm Repository

新增 Repository

1
2
3
4
5
6
7
8
# 新增官方穩定版 Repository
helm repo add bitnami https://charts.bitnami.com/bitnami

# 新增 Prometheus 社群 Repository
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

# 新增 Ingress-Nginx Repository
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx

管理 Repository

1
2
3
4
5
6
7
8
# 列出所有已新增的 Repository
helm repo list

# 更新 Repository 索引
helm repo update

# 移除 Repository
helm repo remove bitnami

搜尋與安裝 Chart

搜尋 Chart

1
2
3
4
5
# 從已新增的 Repository 搜尋
helm search repo nginx

# 從 Artifact Hub 搜尋
helm search hub wordpress

安裝 Chart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 基本安裝
helm install my-nginx bitnami/nginx

# 指定命名空間
helm install my-nginx bitnami/nginx -n web-apps --create-namespace

# 使用自訂 values 檔案
helm install my-nginx bitnami/nginx -f custom-values.yaml

# 透過命令列設定參數
helm install my-nginx bitnami/nginx --set service.type=NodePort

查看與管理 Release

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 列出所有 Release
helm list -A

# 查看 Release 狀態
helm status my-nginx

# 查看 Release 歷史
helm history my-nginx

# 升級 Release
helm upgrade my-nginx bitnami/nginx --set replicaCount=3

# 回滾到指定版本
helm rollback my-nginx 1

# 解除安裝 Release
helm uninstall my-nginx

自訂 values.yaml

每個 Chart 都有預設的 values.yaml,我們可以透過自訂 values 來覆蓋預設設定。

查看 Chart 預設 values

1
helm show values bitnami/nginx > default-values.yaml

建立自訂 values.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# custom-values.yaml
replicaCount: 3

image:
  registry: docker.io
  repository: bitnami/nginx
  tag: latest

service:
  type: LoadBalancer
  port: 80

resources:
  limits:
    cpu: 200m
    memory: 256Mi
  requests:
    cpu: 100m
    memory: 128Mi

ingress:
  enabled: true
  hostname: nginx.example.com
  annotations:
    kubernetes.io/ingress.class: nginx
  tls: true

使用自訂 values 安裝

1
helm install my-nginx bitnami/nginx -f custom-values.yaml

建立自訂 Chart

建立新的 Chart

1
helm create my-app

Chart 目錄結構

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
my-app/
├── Chart.yaml          # Chart 的基本資訊
├── values.yaml         # 預設設定值
├── charts/             # 依賴的子 Chart
├── templates/          # Kubernetes 資源模板
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   ├── hpa.yaml
│   ├── serviceaccount.yaml
│   ├── _helpers.tpl    # 模板輔助函數
│   ├── NOTES.txt       # 安裝後顯示的說明
│   └── tests/          # 測試檔案
└── .helmignore         # 打包時忽略的檔案

Chart.yaml 範例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
apiVersion: v2
name: my-app
description: A Helm chart for my application
type: application
version: 0.1.0
appVersion: "1.0.0"
maintainers:
  - name: Your Name
    email: your@email.com
dependencies:
  - name: postgresql
    version: "12.x.x"
    repository: https://charts.bitnami.com/bitnami
    condition: postgresql.enabled

templates/deployment.yaml 範例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 }}"
          ports:
            - containerPort: {{ .Values.service.port }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}

Helm 常用指令

指令說明
helm repo add <name> <url>新增 Repository
helm repo update更新 Repository 索引
helm search repo <keyword>搜尋 Chart
helm install <release> <chart>安裝 Chart
helm upgrade <release> <chart>升級 Release
helm rollback <release> <revision>回滾 Release
helm uninstall <release>解除安裝 Release
helm list列出所有 Release
helm status <release>查看 Release 狀態
helm history <release>查看 Release 歷史
helm show values <chart>查看 Chart 預設值
helm template <release> <chart>渲染模板(不部署)
helm lint <chart>檢查 Chart 語法
helm package <chart>打包 Chart
helm create <name>建立新 Chart

進階技巧

使用 –dry-run 測試

1
helm install my-app ./my-app --dry-run --debug

渲染模板檢視結果

1
helm template my-app ./my-app > rendered.yaml

更新 Chart 依賴

1
helm dependency update ./my-app

參考資料

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy