Chuyển đến nội dung chính

Bài 4: Helm & Kustomize

Helm charts, releases, values.yaml và upgrade/rollback workflow. Kustomize overlays và bases. Phân biệt khi nào dùng Helm vs Kustomize cho CKAD.

Helm vs Kustomize — Chart structure, template engine, overlays

1. Helm Core Concepts

Helm là Kubernetes package manager. Nó đóng gói các Kubernetes manifests vào Charts và quản lý deployments dưới dạng Releases.

Helm Architecture:

  values.yaml          Chart templates
       │                    │
       ▼                    ▼
  ┌──────────────────────────────┐
  │   Helm Template Engine       │
  │   Renders YAML manifests     │
  └──────────────┬───────────────┘
                 │
                 ▼ kubectl apply
         Kubernetes Cluster
          (stored as Release)
TermĐịnh nghĩa
ChartPackage của Helm — bao gồm templates + default values
ReleaseInstance của Chart đã được deploy lên cluster
RepositoryNơi lưu trữ Charts (như artifact hub, bitnami)
ValuesConfiguration parameters để customize Chart
RevisionMỗi install/upgrade tạo ra một revision mới

2. Helm Commands

# Add repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update

# Search charts
helm search repo bitnami/nginx
helm search hub wordpress

# Install chart
helm install my-release bitnami/nginx
helm install my-release bitnami/nginx --values custom-values.yaml
helm install my-release bitnami/nginx --set image.tag=1.25

# List releases
helm list
helm list -n production

# Upgrade release
helm upgrade my-release bitnami/nginx --set replicaCount=3

# Rollback to previous revision
helm rollback my-release 1   # rollback to revision 1
helm rollback my-release     # rollback to previous revision

# Uninstall
helm uninstall my-release

# View rendered templates (dry-run)
helm template my-release bitnami/nginx
helm install my-release bitnami/nginx --dry-run

Exam tip: CKAD thường test helm install với flag --set (override values trực tiếp) và --values file.yaml (override từ file). Cũng test helm upgrade và helm rollback. Nhớ rằng --set override trumps --values file.

3. Kustomize

Kustomize là tool built vào kubectl cho phép customize Kubernetes manifests mà không cần templates hoặc parameters. Dùng overlay pattern.

Kustomize Structure:
  base/
  ├── kustomization.yaml    # Base kustomization
  ├── deployment.yaml
  └── service.yaml

  overlays/
  ├── development/
  │   ├── kustomization.yaml  # Patches for dev
  │   └── replica-patch.yaml
  └── production/
      ├── kustomization.yaml  # Patches for prod
      └── replica-patch.yaml
# base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml
  - service.yaml

# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
  - ../../base
patches:
  - path: replica-patch.yaml
images:
  - name: myapp
    newTag: "2.0"
# Apply với kustomize
kubectl apply -k overlays/production/

# Preview rendered output
kubectl kustomize overlays/production/

4. Helm vs Kustomize

Tiêu chíHelmKustomize
ApproachTemplate-based (Go templates)Overlay/patching (plain YAML)
Learning curveCao hơn (template syntax)Thấp hơn (YAML patches)
Package mgmtCó (charts, repos, versioning)Không
Release historyCó (upgrade/rollback)Không built-in
Built into kubectlKhông (separate binary)Có (kubectl apply -k)
Best forPhân phối (distribute) appsEnv-specific customization

5. Cheat Sheet

TaskCommand
Install chart với custom valueshelm install rel chart --values f.yaml
Override một valuehelm install rel chart --set key=val
Rollback Helm releasehelm rollback release-name 2
Apply kustomize overlaykubectl apply -k overlays/prod/
Preview kustomize outputkubectl kustomize overlays/prod/

6. Practice Questions

Q1: You need to deploy a Helm chart from the "stable" repo with a custom replica count of 5. Which command accomplishes this?

  • A) helm deploy myapp stable/nginx --replicas=5
  • B) helm install myapp stable/nginx --set replicaCount=5 ✓
  • C) helm install myapp stable/nginx -e replicaCount=5
  • D) helm apply myapp stable/nginx --values replicaCount=5

Explanation: helm install uses --set flag to override values. The syntax is --set key=value. The exact key name (replicaCount) depends on the chart's values.yaml, but --set is the correct flag for inline value overrides.

Q2: A team uses Kustomize with a base configuration and production/staging overlays. Which command applies the production overlay?

  • A) kubectl apply -f overlays/production/
  • B) kubectl kustomize overlays/production/ | kubectl apply -f -
  • C) kubectl apply -k overlays/production/ ✓
  • D) kustomize apply overlays/production/

Explanation: kubectl apply -k (note -k not -f) is the built-in way to apply a kustomization directory. Option B also works but is more verbose. The -k flag tells kubectl to process the directory as a Kustomize configuration.

Q3: After a Helm upgrade introduces a bug, you need to revert to the previous working state. What is the correct approach?

  • A) kubectl rollout undo deployment/myapp
  • B) helm install --replace myapp stable/nginx
  • C) helm rollback myapp ✓
  • D) helm upgrade myapp --version=previous

Explanation: helm rollback reverts a release to a previous revision. Without specifying a revision number, it rolls back to the previous one. This undoes all the changes made by the failed upgrade, including ConfigMaps, Secrets, and other resources managed by the chart.