Ở bài trước, Kustomize đã gom manifest của TaskFlow thành base và overlay, nhưng mọi thứ trong đó vẫn do mình tự viết, kể cả PostgreSQL và Redis. Bài này mình sẽ giới thiệu với mọi người về Helm: cài Helm 4.3, tìm chart trong repository và OCI registry, xem trước chart, cài chart có sẵn bằng file values của mình, rồi upgrade, rollback, uninstall. Mọi thứ chạy trong namespace thử helm-thu, ứng dụng trong taskflow vẫn giữ Kustomize.
1. Helm là gì?
Helm là trình quản lý package của Kubernetes: ứng dụng được đóng gói thành chart (template manifest + giá trị mặc định), cài lên cụm thành release, và Helm theo dõi từng lần cài, nâng cấp của release đó.
- Chart: thư mục (hoặc file
.tgz) gồmChart.yaml(tên,versioncủa chart,appVersioncủa ứng dụng),values.yaml(giá trị mặc định) vàtemplates/(manifest viết bằng Go template). - Release: một lần cài chart vào một namespace với một tên. Mỗi lần
install,upgrade,rollbacktạo một revision mới. - Repository: nơi chứa chart, là web server có file
index.yamlhoặc OCI registry (GHCR, Docker Hub...) như chứa image. - Values: giá trị các bạn truyền vào (
-f values.yaml,--set key=value), trộn đè lênvalues.yamlcủa chart rồi điền vào template.
Khác Kustomize chỉ vá YAML có sẵn, Helm dựng manifest từ template nên chart có thể ghép chuỗi, bật tắt cả một resource bằng if. Helm chạy ở client, không có thành phần nào trong cụm.
Helm 4.0 ra mắt tháng 11/2025, bản mới nhất lúc viết là 4.3.2, Helm 3 chỉ còn bảo trì. Chart của Helm 3 (apiVersion: v2) vẫn chạy nguyên, thay đổi chủ yếu ở CLI: release mới được apply bằng server-side apply; --wait dùng kstatus để chờ resource sẵn sàng; --atomic đổi tên thành --rollback-on-failure, --force thành --force-replace (tên cũ vẫn chạy kèm cảnh báo); plugin có thể chạy bằng WebAssembly; cài chart OCI theo digest. Chi tiết ở Helm 4 Overview.
2. Cài Helm.
Như kubectl và kind ở Bài 3, mình tải binary đúng version kèm checksum (máy ARM thay amd64 bằng arm64, macOS là darwin):
curl -LO https://get.helm.sh/helm-v4.3.2-linux-amd64.tar.gz
curl -LO https://get.helm.sh/helm-v4.3.2-linux-amd64.tar.gz.sha256sum
sha256sum -c helm-v4.3.2-linux-amd64.tar.gz.sha256sum
# helm-v4.3.2-linux-amd64.tar.gz: OK
tar -xzf helm-v4.3.2-linux-amd64.tar.gz
sudo install -m 0755 linux-amd64/helm /usr/local/bin/helm
helm version
# version.BuildInfo{Version:"v4.3.2", GitCommit:"8346aaa55e7161f7c547212d0a87f01367976727", GitTreeState:"clean", GoVersion:"go1.27.4", KubeClientVersion:"v1.37"}
helm env | grep -E "KUBECONTEXT|NAMESPACE"
# HELM_KUBECONTEXT=""
# HELM_NAMESPACE="taskflow"
Helm đọc cùng kubeconfig với kubectl, dùng context hiện tại và cả namespace mặc định của nó. Context của chúng ta trỏ vào taskflow từ Bài 8, nên quên -n là chart thử cài thẳng vào namespace của ứng dụng; trong bài mọi lệnh helm đều có -n helm-thu.
3. Tìm chart: repository và OCI registry.
Chart dạng repository phải helm repo add trước, Helm tải index.yaml về cache để tìm:
helm repo add valkey https://valkey.io/valkey-helm
# "valkey" has been added to your repositories
helm repo add cnpg https://cloudnative-pg.github.io/charts
# "cnpg" has been added to your repositories
helm repo update
# ...
helm search repo valkey
# NAME CHART VERSION APP VERSION DESCRIPTION
# valkey/valkey 1.0.0 9.1.3 Valkey, an open source, high-performance in-mem...
# ...
helm search repo chỉ tìm trong repo đã thêm (--versions liệt kê cả bản cũ); helm search hub valkey tìm trên Artifact Hub và trả về 56 chart Valkey của đủ loại người đóng gói. Chart trong OCI registry thì không cần repo add, Helm kéo chart như kéo image:
helm show chart oci://ghcr.io/traefik/helm/traefik --version 43.0.0
# Pulled: ghcr.io/traefik/helm/traefik:43.0.0
# Digest: sha256:304dd81e4a9b6a942ad64ea0171579f4caff61d93cf91a722a248291777be8d7
# ...
# appVersion: v3.8.0
# ...
# version: 43.0.0
Dù cách nào, luôn ghi --version: không ghi thì Helm lấy bản mới nhất, và cùng một lệnh chạy hai lần có thể cài ra hai thứ khác nhau.
Chọn chart nào?
Trước đây câu trả lời mặc định là Bitnami. Từ 28/08/2025, Broadcom thu hẹp catalog miễn phí: image có version chuyển sang bitnamilegacy và không còn cập nhật, bản miễn phí chỉ còn ít image tag latest dành cho phát triển. Chart vẫn còn: helm show values oci://registry-1.docker.io/bitnamicharts/valkey (chart 6.3.19) cho ra image bitnami/valkey tag latest, không ghim được version, nên mình không dùng. Tiêu chí chọn: do chính dự án (hoặc tổ chức lớn) duy trì, còn phát hành đều, image chính thức có tag version. Trong bài:
valkey/valkey: chart chính thức của Valkey, bản fork mã nguồn mở (BSD) của Redis thuộc Linux Foundation, tương thích giao thức Redis; đơn giản, hợp để học lệnh Helm và so với Redis ở Bài 19.cnpg/cloudnative-pg,cnpg/cluster: chart của CloudNativePG, operator PostgreSQL thuộc CNCF.
4. Xem chart trước khi cài.
Chart là code của người khác sẽ chạy trên cụm của mình. helm show chart in Chart.yaml, helm show values in toàn bộ values.yaml có comment (chart Valkey có 972 dòng, đây là "tài liệu API" của chart), helm show readme in hướng dẫn, còn helm template dựng YAML ra màn hình mà không cài, giống kubectl kustomize. File values của mình:
- valkey-values.yaml
# Valkey một instance (Deployment), dữ liệu trên PVC như Redis ở Bài 19
auth:
enabled: true
aclUsers:
# user mặc định, mật khẩu mẫu (giá trị thật nên lấy từ Secret có sẵn, xem phần 5)
default:
permissions: "~* &* +@all"
password: doi-mat-khau-nay
persistence:
enabled: true
size: 1Gi
resources:
requests:
cpu: 50m
memory: 32Mi
limits:
memory: 128Mi
helm template valkey valkey/valkey --version 1.0.0 -n helm-thu -f valkey-values.yaml | grep -E "^kind:"
# kind: ServiceAccount
# kind: Secret
# kind: ConfigMap
# kind: PersistentVolumeClaim
# kind: Service
# kind: Deployment
# kind: Pod
Trong đó:
- Không có
-f, kết quả chỉ có ServiceAccount, ConfigMap, Service, Deployment: mặc định chart không bật mật khẩu và để dữ liệu trênemptyDir. Values quyết định cả việc Secretvalkey-authvà PVC có tồn tại hay không. - Tên release (
valkey) và namespace là tham số vì template đặt tên resource theo chúng. Podcuối là test hook (annotationhelm.sh/hook: test), chỉ chạy khi gọihelm test.helm-thukhông có ResourceQuota hay LimitRange (chỉtaskflowcó, Bài 16), nhưng mình vẫn đặt requests, limits như mọi workload của series.
Thêm --show-only templates/deploy_valkey.yaml để xem riêng Deployment: label có thêm app.kubernetes.io/instance: valkey (tên release) và app.kubernetes.io/managed-by: Helm, hai label series chưa từng đặt tay; Pod template có annotation checksum/initconfig, checksum/secret là hash của ConfigMap và Secret, đổi cấu hình là đổi Pod template nên Deployment tự rollout, cùng ý tưởng với hash suffix của Kustomize.
5. Cài, nâng cấp và rollback một release.
helm install valkey valkey/valkey --version 1.0.0 -n helm-thu --create-namespace -f valkey-values.yaml --wait --timeout 3m
# NAME: valkey
# LAST DEPLOYED: Wed Nov 25 17:38:59 2026
# NAMESPACE: helm-thu
# STATUS: deployed
# REVISION: 1
# DESCRIPTION: Install complete
# NOTES:
# ...
kubectl get all,pvc,secret -n helm-thu
# NAME READY STATUS RESTARTS AGE
# pod/valkey-64bfc97d68-sfwhp 1/1 Running 0 31s
# ...
# persistentvolumeclaim/valkey Bound pvc-b8f323dc-b9a8-4151-a1be-179593d87040 1Gi RWO standard <unset> 31s
# ...
# secret/sh.helm.release.v1.valkey.v1 helm.sh/release.v1 1 32s
# secret/valkey-auth Opaque 1 32s
--create-namespace tạo helm-thu nếu chưa có; nhiều -f thì file sau đè file trước, --set đè tất cả. --wait chờ Pod sẵn sàng, PVC Bound... trong --timeout; không có nó thì deployed chỉ nghĩa là API server đã nhận manifest. NOTES là hướng dẫn kết nối do chart in ra (xem lại bằng helm status), helm list -n helm-thu liệt kê release.
Mình ghi thử key ghi-chu bằng valkey-cli --user default -a doi-mat-khau-nay qua kubectl exec, và helm test valkey -n helm-thu chạy Pod test của chart, báo Phase: Succeeded.
Helm nhớ gì về release?
helm get values valkey -n helm-thu
# USER-SUPPLIED VALUES:
# auth:
# aclUsers:
# default:
# password: doi-mat-khau-nay
# ...
kubectl -n helm-thu get secret sh.helm.release.v1.valkey.v1 -o jsonpath="{.metadata.labels}"; echo
# {"modifiedAt":"1795603169","name":"valkey","owner":"helm","status":"deployed","version":"1"}
Mỗi revision là một Secret sh.helm.release.v1.<release>.v<revision> chứa toàn bộ release đã nén: chart, values, manifest đã dựng. helm get values (thêm --all để có cả giá trị mặc định) và helm get manifest đọc từ đây. Hệ quả: mật khẩu ghi trong values nằm trong Secret của release. Chart tử tế cho trỏ tới Secret có sẵn thay vì ghi mật khẩu vào values (chart Valkey có auth.usersExistingSecret), Bài 24 sẽ làm như vậy cho TaskFlow. Server-side apply cũng thấy được: managedFields của Deployment có manager helm với operation Apply.
Upgrade, history và rollback
Thêm vào cuối valkey-values.yaml:
# nối thêm vào valkey.conf: giới hạn bộ nhớ dữ liệu dưới memory limit của container
extraConfig: |
maxmemory 96mb
helm upgrade valkey valkey/valkey --version 1.0.0 -n helm-thu -f valkey-values.yaml --wait --timeout 3m
# Release "valkey" has been upgraded. Happy Helming!
# ...
# REVISION: 2
helm history valkey -n helm-thu giờ có revision 1 (superseded) và 2 (deployed). Checksum ConfigMap đổi nên Pod được tạo lại, config get maxmemory trả 100663296, key ghi-chu vẫn còn vì nằm trên PVC. Trong CI hay dùng helm upgrade --install: chưa có release thì cài.
Chú ý: helm upgrade chỉ dùng values truyền ở lần đó. Dựng thử helm upgrade ... --set valkeyLogLevel=warning --dry-run=server không kèm -f valkey-values.yaml thì Secret valkey-auth và maxmemory biến mất, tức là tắt luôn mật khẩu. Luôn truyền đủ file values (để trong git) thay vì trông vào --reuse-values.
Giờ thử bản nâng cấp hỏng (image tag không tồn tại) với --rollback-on-failure, flag này tự bật --wait:
helm upgrade valkey valkey/valkey --version 1.0.0 -n helm-thu -f valkey-values.yaml --set image.tag=9.9.9 --rollback-on-failure --timeout 90s
# level=WARN msg="upgrade failed" name=valkey error="resource Deployment/helm-thu/valkey not ready. status: InProgress, message: Available: 0/1\ncontext deadline exceeded"
# Error: UPGRADE FAILED: release valkey failed, and has been rolled back due to rollback-on-failure being set: resource Deployment/helm-thu/valkey not ready. status: InProgress, message: Available: 0/1
# context deadline exceeded
helm history valkey -n helm-thu
# ...
# 3 Wed Nov 25 17:41:00 2026 failed valkey-1.0.0 9.1.3 Upgrade "valkey" failed: resource Deployment/helm-thu/valkey not ready. status: InProgress, message: Available: ...
# 4 Wed Nov 25 17:42:32 2026 deployed valkey-1.0.0 9.1.3 Rollback to 2
helm rollback valkey 1 -n helm-thu --wait --timeout 3m
# Rollback was a success! Happy Helming!
Revision 3 hỏng, Helm tự tạo revision 4 có nội dung của revision 2. Rollback không quay ngược thời gian: Deployment này là Recreate (như Redis ở Bài 19, vì volume RWO), Pod cũ đã dừng trước khi Pod mới kẹt ImagePullBackOff, nên Valkey ngừng phục vụ suốt 90 giây. Lệnh helm rollback valkey 1 sau đó đưa về cấu hình không có extraConfig (maxmemory về 0) dưới dạng revision 5. Helm giữ tối đa 10 revision (--history-max); khác kubectl rollout undo ở Bài 10, helm rollback lùi mọi resource của release.
So với Redis tự viết ở Bài 19
Chart cho sẵn những thứ mình chưa làm: securityContext chặt (user 1000, readOnlyRootFilesystem, bỏ mọi capability), ServiceAccount không mount token, ACL nhiều user, checksum để tự rollout, test hook; đổi values là có replication, Sentinel, metrics exporter. Ngược lại, valkey.conf mặc định không bật AOF như Bài 19, chỉ có snapshot RDB (key còn sau restart vì Valkey ghi RDB khi tắt); muốn AOF phải thêm appendonly yes vào extraConfig. Dùng chart vẫn phải đọc values và kiểm tra cấu hình thật. Gỡ release trước khi sang chart tiếp theo cho cụm kind nhẹ:
helm uninstall valkey -n helm-thu --wait
# release "valkey" uninstalled
kubectl get all,pvc,secret -n helm-thu
# No resources found in helm-thu namespace.
6. PostgreSQL với CloudNativePG.
Với PostgreSQL mình dùng một operator: controller chạy trong cụm, theo dõi custom resource Cluster và tự tạo Pod, PVC, Service, Secret, replication, failover. Chart cloudnative-pg cài operator và CRD, chart cluster tạo một Cluster.
File cnpg-values.yaml chỉ có config.clusterWide: false (operator chỉ theo dõi namespace của release) và requests 50m/64Mi, limit memory 256Mi:
helm install cnpg cnpg/cloudnative-pg --version 0.29.2 -n helm-thu --create-namespace -f cnpg-values.yaml --wait --timeout 5m
# ...
# STATUS: deployed
helm get manifest cnpg -n helm-thu | grep "^kind:" | sort | uniq -c
# 3 kind: ClusterRole
# 1 kind: ClusterRoleBinding
# 2 kind: ConfigMap
# 11 kind: CustomResourceDefinition
# ...
# 1 kind: ValidatingWebhookConfiguration
Release nằm trong helm-thu nhưng chart cài 11 CRD, ClusterRole và webhook ở phạm vi cả cụm: cần quyền admin cụm, và helm template trước khi cài cho thấy điều đó. Tiếp theo là database:
- pg-values.yaml
# PostgreSQL 18 một instance, volume 1Gi, database và user taskflow như Bài 19
version:
postgresql: "18"
cluster:
instances: 1
storage:
size: 1Gi
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
memory: 512Mi
initdb:
database: taskflow
owner: taskflow
helm install pg cnpg/cluster --version 0.9.0 -n helm-thu -f pg-values.yaml --wait --timeout 5m
# ...
# STATUS: deployed
kubectl -n helm-thu wait --for=condition=Ready cluster/pg-cluster --timeout=300s
# cluster.postgresql.cnpg.io/pg-cluster condition met
kubectl get cluster,pods,pvc -n helm-thu
# NAME AGE INSTANCES READY STATUS PRIMARY
# cluster.postgresql.cnpg.io/pg-cluster 56s 1 1 Cluster in healthy state pg-cluster-1
# ...
# pod/pg-cluster-1 1/1 Running 0 12s
# pod/pg-cluster-1-initdb-4c8nc 0/1 Completed 0 55s
# ...
helm install --wait trả về ngay khi Cluster vừa được tạo, lúc chưa có Pod nào: Helm chỉ chờ được resource nó hiểu trạng thái, database sẵn sàng hay chưa là việc của operator, nên phải chờ thêm condition Ready của Cluster. Khác với Bài 19, không có StatefulSet: operator tự tạo Pod, PVC (owner của Pod pg-cluster-1 là Cluster/pg-cluster), chạy Job initdb một lần, tạo ba Service pg-cluster-rw (primary), -ro (replica), -r (mọi instance). Secret pg-cluster-app có mật khẩu ngẫu nhiên và key uri (postgresql://taskflow:***@pg-cluster-rw.helm-thu:5432/taskflow) ghép từ đúng mật khẩu đó, việc Kustomize không làm được ở Bài 22.
Ở Bài 19, tăng replicas của StatefulSet chỉ cho ra các database rỗng, độc lập. Ở đây đổi instances: 1 thành 2 rồi upgrade:
helm upgrade pg cnpg/cluster --version 0.9.0 -n helm-thu -f pg-values.yaml
# Release "pg" has been upgraded. Happy Helming!
# ...
kubectl -n helm-thu wait --for=jsonpath="{.status.readyInstances}"=2 cluster/pg-cluster --timeout=300s
# cluster.postgresql.cnpg.io/pg-cluster condition met
kubectl -n helm-thu exec pg-cluster-1 -c postgres -- psql -U postgres -tAc "select application_name, state from pg_stat_replication"
# pg-cluster-2|streaming
kubectl -n helm-thu delete pod pg-cluster-1
# pod "pg-cluster-1" deleted from helm-thu namespace
kubectl -n helm-thu get cluster pg-cluster
# NAME AGE INSTANCES READY STATUS PRIMARY
# pg-cluster 2m15s 2 2 Cluster in healthy state pg-cluster-2
pg-cluster-2 là replica nhận WAL qua streaming replication. Xóa Pod primary thì chưa tới 20 giây sau operator đã nâng pg-cluster-2 lên primary, Service pg-cluster-rw trỏ sang nó, pg-cluster-1 quay lại làm replica.
Dù vậy series vẫn giữ PostgreSQL tự viết của Bài 19 cho TaskFlow: cụm kind chỉ có 8 GB, và operator, CRD, webhook là thêm nhiều thứ phải hiểu khi có lỗi. Hệ thống thật cần HA và backup tự động thì nên cân nhắc operator như CloudNativePG trước khi tự viết StatefulSet.
7. Traefik qua chart chính thức.
Traefik ở Bài 17 được cài bằng hai file trong platform/traefik/. Mình chỉ dựng thử chart chính thức để so, không thay Traefik đang chạy. File traefik-values.yaml làm cho chart giống bản của mình: tắt provider kubernetesCRD, kubernetesIngress và ingressClass, bật providers.kubernetesGateway.enabled, gateway.enabled: false (Gateway taskflow đã nằm trong kustomize/base), gatewayClass.name: traefik, service.spec.type: NodePort với ports.web.nodePort: 30080, không expose websecure.
Lần đầu mình viết service.type và nhận lỗi at '/service': additional properties 'type' not allowed: chart có values.schema.json nên Helm kiểm tra values trước khi dựng, sai tên key là dừng ngay thay vì bị bỏ qua lặng lẽ.
helm template traefik oci://ghcr.io/traefik/helm/traefik --version 43.0.0 -n traefik -f traefik-values.yaml > traefik-chart.yaml; grep -E "^kind:" traefik-chart.yaml | sort | uniq -c
# ...
# 1 kind: ClusterRole
# 1 kind: ClusterRoleBinding
# 1 kind: Deployment
# 1 kind: GatewayClass
# ...
Kết quả gần như trùng platform/traefik/ (thiếu Namespace vì Helm dùng --create-namespace): cùng image traefik:v3.8.0, --entryPoints.web.address=:8000/tcp, nodePort: 30080, nhưng dài 294 dòng so với 199 dòng của mình, và --include-crds cho thấy chart còn mang 25 CRD của Traefik và Traefik Hub, cài cùng release dù không dùng. Muốn chuyển thật thì so file dựng ra với manifest đang chạy, xóa bản cũ rồi helm install, chấp nhận vài giây gián đoạn. Quyết định cho các bài sau:
- Traefik trên kind giữ manifest của Bài 17: đang chạy ổn, ngắn, mọi dòng đã được giải thích. Trên k3s (Bài 31), Traefik đi kèm k3s và được k3s tự cài bằng Helm chart, mình chỉ chỉnh values của nó.
- kube-prometheus-stack (Bài 30), cert-manager (Bài 31), Argo CD (Bài 32) cài bằng chart chính thức qua OCI (
oci://ghcr.io/prometheus-community/charts/kube-prometheus-stack,oci://quay.io/jetstack/charts/cert-manager,oci://ghcr.io/argoproj/argo-helm/argo-cd), ghim version, file values để trongplatform/. Đó là hàng nghìn dòng YAML, tự viết không có lợi gì.
8. Dọn namespace thử.
Gỡ database trước rồi mới gỡ operator, để operator kịp xóa Pod và PVC của Cluster:
helm uninstall pg -n helm-thu --wait
# release "pg" uninstalled
helm uninstall cnpg -n helm-thu --wait
# These resources were kept due to the resource policy:
# [CustomResourceDefinition] poolers.postgresql.cnpg.io
# ...
# release "cnpg" uninstalled
kubectl get crd -o name | grep cnpg.io | xargs kubectl delete
# customresourcedefinition.apiextensions.k8s.io "backups.postgresql.cnpg.io" deleted
# ...
kubectl delete namespace helm-thu
# namespace "helm-thu" deleted
CRD có annotation helm.sh/resource-policy: keep nên uninstall giữ lại: xóa CRD là xóa mọi Cluster trên cả cụm, chart không làm việc đó thay các bạn. Namespace taskflow không bị đụng tới: vẫn 6/20 Pod, kubectl diff -k kustomize/overlays/dev trả exit 0, /ready trả ok. README của repo chỉ thêm Helm 4.3 vào danh sách công cụ cần có.
Code đầy đủ của series sau bài này nằm ở tag bai-23 trên GitHub; các thay đổi của riêng bài này: so sánh bai-22...bai-23.
9. Lời kết.
Như vậy mình đã giới thiệu với mọi người về Helm ở vai trò người dùng chart: chart, release, repository, values, thay đổi của Helm 4, tìm chart qua repo và OCI, đọc chart bằng helm show, helm template, vòng đời install, upgrade, rollback, uninstall, Secret lưu release, cùng hai chart thật là Valkey và CloudNativePG. Bài sau mình sẽ tự viết chart cho TaskFlow: template, values.yaml, helper, ghép DATABASE_URL từ một mật khẩu duy nhất, hook cho Job migration, helm lint, và so sánh Kustomize với Helm.
Đăng ký nhận tin.
Chúng tôi chỉ gửi tối đa 2 lần trên 1 tháng. Tuyên bố không spam mail!





0 Comments