Kubernetes Helm Charts 2026: Hướng Dẫn Đóng Gói, Triển Khai và Câu Hỏi Phỏng Vấn

Tìm hiểu sâu về Helm Charts trong Kubernetes với hướng dẫn thực hành về tạo chart, templating, deployment và các câu hỏi phỏng vấn thường gặp nhất.

Kubernetes Helm Charts 2026: Hướng Dẫn Đóng Gói, Triển Khai và Câu Hỏi Phỏng Vấn

Kubernetes Helm Charts đơn giản hóa việc triển khai ứng dụng bằng cách đóng gói tất cả tài nguyên Kubernetes vào một artifact duy nhất có phiên bản. Helm 3, phiên bản ổn định hiện tại, loại bỏ nhu cầu Tiller và giới thiệu hỗ trợ OCI registry, giúp phân phối chart an toàn và chuẩn hóa hơn. Bài hướng dẫn này bao gồm việc tạo chart, templating, chiến lược deployment và các câu hỏi phỏng vấn phổ biến nhất từ các đội tuyển dụng.

Tham Khảo Nhanh Helm 3

Helm charts chứa Chart.yaml (metadata), values.yaml (giá trị mặc định), và thư mục templates/ với các manifest Kubernetes. Lệnh helm install render template với values và áp dụng chúng vào cluster.

Hiểu Cấu Trúc và Thành Phần của Helm Chart

Helm chart là một thư mục với cấu trúc được định nghĩa trước. Tên chart trở thành tên thư mục, và mỗi file phục vụ một mục đích cụ thể trong quá trình đóng gói và triển khai.

yaml
# Chart.yaml
apiVersion: v2
name: my-application
description: A production-ready web application
type: application
version: 1.0.0
appVersion: "2.1.0"
dependencies:
  - name: postgresql
    version: "15.x"
    repository: "https://charts.bitnami.com/bitnami"
    condition: postgresql.enabled

apiVersion: v2 cho biết tương thích với Helm 3. Trường version theo dõi các thay đổi của chart, trong khi appVersion phản ánh phiên bản ứng dụng được triển khai. Dependencies khai báo các chart khác mà chart này yêu cầu, với điều kiện tùy chọn để bật hoặc tắt chúng.

yaml
# values.yaml
replicaCount: 3

image:
  repository: myregistry.io/app
  tag: "2.1.0"
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 8080

resources:
  limits:
    cpu: 500m
    memory: 512Mi
  requests:
    cpu: 100m
    memory: 128Mi

postgresql:
  enabled: true
  auth:
    database: appdb

Values định nghĩa cấu hình mặc định mà người dùng có thể ghi đè trong quá trình cài đặt. Việc lồng values dưới các key có ý nghĩa giúp cấu hình được tổ chức và tự giải thích.

Tạo Kubernetes Deployments với Helm Templates

Helm templates sử dụng Go templating với các hàm Sprig để tạo manifest Kubernetes động. Đối tượng {{ .Values }} cung cấp quyền truy cập vào nội dung values.yaml, trong khi {{ .Release }} chứa metadata deployment.

yaml
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "my-application.fullname" . }}
  labels:
    {{- include "my-application.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "my-application.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "my-application.selectorLabels" . | nindent 8 }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.service.port }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}

Hàm nindent thêm newline và indentation, rất quan trọng cho việc tạo YAML hợp lệ. Sử dụng include với named templates (định nghĩa trong _helpers.tpl) giữ cho templates tuân thủ nguyên tắc DRY và dễ bảo trì.

yaml
# templates/_helpers.tpl
{{- define "my-application.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}

{{- define "my-application.labels" -}}
helm.sh/chart: {{ include "my-application.chart" . }}
app.kubernetes.io/name: {{ include "my-application.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

Helper templates chuẩn hóa các quy ước đặt tên và labels trên tất cả resources, đảm bảo nhất quán với các labels được Kubernetes khuyến nghị.

Helm Chart Dependencies và Subcharts

Các ứng dụng phức tạp thường phụ thuộc vào databases, caches, hoặc message queues. Helm dependencies cho phép nhúng các thành phần này như subcharts, được quản lý tự động trong quá trình cài đặt.

bash
# Cập nhật dependencies trước khi đóng gói
helm dependency update ./my-application

# Build dependencies (tạo thư mục charts/)
helm dependency build ./my-application

Lệnh helm dependency update tải dependencies vào thư mục charts/. Conditional dependencies sử dụng values để bật/tắt:

yaml
# values.yaml
postgresql:
  enabled: true

redis:
  enabled: false
yaml
# Chart.yaml
dependencies:
  - name: postgresql
    version: "15.x"
    repository: "https://charts.bitnami.com/bitnami"
    condition: postgresql.enabled
  - name: redis
    version: "19.x"
    repository: "https://charts.bitnami.com/bitnami"
    condition: redis.enabled

Pattern này cho phép cấu hình theo môi trường cụ thể, trong đó production sử dụng managed services còn development sử dụng databases in-cluster.

Hỗ Trợ OCI Registry

Helm 3.8+ hỗ trợ lưu trữ charts trong các OCI-compliant registries như Docker Hub, GitHub Container Registry, và AWS ECR. Push charts với helm push my-app-1.0.0.tgz oci://registry.example.com/charts.

Triển Khai Helm Charts với Release Management

Helm theo dõi deployments như releases, cho phép rollback và upgrade có phiên bản. Mỗi release duy trì history để audit và recovery.

bash
# Cài đặt chart với custom values
helm install my-release ./my-application \
  --namespace production \
  --create-namespace \
  --values production-values.yaml \
  --set image.tag=2.1.1

# Upgrade release hiện có
helm upgrade my-release ./my-application \
  --namespace production \
  --values production-values.yaml \
  --set image.tag=2.2.0 \
  --atomic \
  --timeout 5m

Flag --atomic đảm bảo các upgrade thất bại tự động rollback. Flag --timeout đặt thời gian Helm chờ resources sẵn sàng trước khi báo lỗi.

bash
# Xem release history
helm history my-release -n production

# Rollback về revision trước
helm rollback my-release 2 -n production

# Gỡ cài đặt release
helm uninstall my-release -n production --keep-history

Flag --keep-history giữ lại bản ghi release ngay cả sau khi gỡ cài đặt, hữu ích cho compliance và debugging.

Sẵn sàng chinh phục phỏng vấn DevOps?

Luyện tập với mô phỏng tương tác, flashcards và bài kiểm tra kỹ thuật.

Chiến Lược Testing và Validation Helm Chart

Kiểm thử Helm charts trước deployment giúp phát hiện lỗi template và vấn đề cấu hình sớm. Helm cung cấp các công cụ tích hợp cho linting và dry-run testing.

bash
# Lint chart để tìm lỗi và best practices
helm lint ./my-application

# Render templates locally mà không deploy
helm template my-release ./my-application \
  --values production-values.yaml \
  > rendered-manifests.yaml

# Dry-run với cluster API
helm install my-release ./my-application \
  --dry-run \
  --debug \
  --namespace production

Lệnh helm template render manifests locally, hữu ích cho CI pipelines validate YAML được tạo ra. Flag --dry-run gửi manifests đến Kubernetes API server để validation mà không tạo resources.

yaml
# templates/tests/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
  name: "{{ include "my-application.fullname" . }}-test"
  labels:
    {{- include "my-application.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": test
spec:
  containers:
    - name: curl
      image: curlimages/curl:8.7.1
      command: ['curl']
      args: ['{{ include "my-application.fullname" . }}:{{ .Values.service.port }}/health']
  restartPolicy: Never

Helm hooks với helm.sh/hook: test chạy sau helm test my-release, xác thực deployment hoạt động chính xác sau cài đặt.

Kỹ Thuật Helm Templating Nâng Cao

Production charts yêu cầu conditional logic, loops, và data transformation. Các hàm Sprig mở rộng Go templates với các tiện ích cho string manipulation, encoding, và flow control.

yaml
# templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "my-application.fullname" . }}-config
data:
  {{- range $key, $value := .Values.config }}
  {{ $key }}: {{ $value | quote }}
  {{- end }}
  
  {{- if .Values.features.enabled }}
  FEATURE_FLAGS: {{ .Values.features.flags | join "," | quote }}
  {{- end }}
yaml
# templates/secrets.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "my-application.fullname" . }}-secrets
type: Opaque
data:
  {{- range $key, $value := .Values.secrets }}
  {{ $key }}: {{ $value | b64enc }}
  {{- end }}

Hàm range lặp qua maps và lists. Hàm b64enc mã hóa base64 giá trị cho Kubernetes secrets. Pipe operators kết nối các hàm để biến đổi dữ liệu.

Quản Lý Secrets

Lưu trữ secrets trong values.yaml làm lộ chúng trong version control. Sử dụng external secret managers như HashiCorp Vault, AWS Secrets Manager với External Secrets Operator, hoặc files values được mã hóa SOPS cho production deployments.

Câu Hỏi và Trả Lời Phỏng Vấn Helm Charts

Phỏng vấn kỹ thuật thường kiểm tra kiến thức Helm thông qua các câu hỏi lý thuyết và thực hành. Phần sau bao gồm các chủ đề được hỏi phổ biến nhất.

Sự khác biệt giữa helm installhelm upgrade --install là gì?

helm install tạo release mới và thất bại nếu release đã tồn tại. helm upgrade --install (với flag --install) tạo release mới nếu chưa có, hoặc upgrade nếu đã có. CI/CD pipelines thường sử dụng helm upgrade --install cho idempotent deployments.

Làm thế nào để truyền dữ liệu nhạy cảm vào Helm charts?

Có ba cách tiếp cận: external secret managers (Vault, AWS Secrets Manager), files values được mã hóa (SOPS, sealed-secrets), hoặc Kubernetes secrets tạo bên ngoài Helm và tham chiếu trong templates. Tránh lưu secrets trong files values.yaml plain-text được commit vào version control.

Helm hooks là gì và khi nào nên sử dụng?

Hooks thực thi tại các điểm cụ thể trong lifecycle của release: pre-install, post-install, pre-upgrade, post-upgrade, pre-delete, post-delete, pre-rollback, post-rollback, và test. Các use cases phổ biến bao gồm database migrations trước upgrades, gửi notifications sau deployments, hoặc chạy integration tests.

Làm thế nào để xử lý chart versioning?

Trường version trong Chart.yaml theo dõi các thay đổi chart sử dụng semantic versioning. Trường appVersion theo dõi phiên bản ứng dụng được deploy. Tăng version cho mọi sửa đổi chart, ngay cả khi phiên bản ứng dụng giữ nguyên.

Mục đích của .helmignore là gì?

Tương tự .gitignore, .helmignore loại trừ files khỏi việc đóng gói chart. Các entries điển hình bao gồm *.tgz (tránh đóng gói builds trước đó), .git/, tài liệu *.md không cần thiết khi runtime, và files cấu hình CI.

Để chuẩn bị phỏng vấn Kubernetes thêm, khám phá các module câu hỏi Kubernetes BasicsKubernetes Advanced trên SharpSkill.

Kết Luận

Các điểm thực hành khi làm việc với Helm charts:

  • Cấu trúc charts với sự phân tách rõ ràng giữa metadata Chart.yaml, defaults values.yaml, và thư mục templates chứa Kubernetes manifests
  • Sử dụng helper templates trong _helpers.tpl để chuẩn hóa naming và labels trên tất cả resources
  • Quản lý dependencies thông qua Chart.yaml với conditional enablement cho cấu hình theo môi trường cụ thể
  • Test charts với helm lint, helm template, và helm install --dry-run trước khi deploy lên production
  • Theo dõi releases với tên có ý nghĩa và sử dụng flag --atomic cho automatic rollback khi upgrade thất bại
  • Lưu secrets trong external managers thay vì files values plain-text

Bắt đầu luyện tập!

Kiểm tra kiến thức với mô phỏng phỏng vấn và bài kiểm tra kỹ thuật.

Chia sẻ

Bài viết liên quan