ใช้ Helm Chart จัดการ Kubernetes Application อย่างมืออาชีพ
Helm Chart คือเครื่องมือที่ช่วยให้การจัดการ Kubernetes application ง่ายขึ้น โดยให้สามารถกำหนดค่าทำซ้ำและปล่อยไปยัง environment ต่าง ๆ ได้อย่างสมบูรณ์ เรียนรู้วิธีสร้าง Helm Chart ที่มีคุณภาพสูงและจัดการ release management อย่างมืออาชีพ
Helm Chart คืออะไร
- Package Manager: Helm ช่วยจัดเก็บ Kubernetes manifests ในลักษณะที่เป็นเหมือนแพ็คเก็จ
- Templating: ใช้ templates เพื่อให้ข้อมูลสามารถนำมาใช้ซ้ำได้
- Version Control: จัดการเวอร์ชันของ releases ได้ง่าย
- Rollback Support: สามารถย้อนกลับไปเวอร์ชันก่อนหน้าได้
- Repository: แชร์ Charts ผ่าน Helm repositories
Helm Chart Structure
my-chart/ ├── Chart.yaml # ข้อมูล Chart ├── values.yaml # Default values ├── values-dev.yaml # Dev environment values ├── values-prod.yaml # Production values ├── templates/ │ ├── deployment.yaml # Deployment template │ ├── service.yaml # Service template │ ├── configmap.yaml # ConfigMap template │ ├── ingress.yaml # Ingress template │ ├── _helpers.tpl # Helper templates │ └── NOTES.txt # Release notes └── README.md # Documentation
สร้าง Helm Chart ขั้นพื้นฐาน
Step 1: สร้าง Chart
helm create my-app
Step 2: แก้ไข Chart.yaml
apiVersion: v2
name: my-app
description: A Helm chart for my application
type: application
version: 1.0.0
appVersion: 1.0.0
maintainers:
- name: Your Name
email: [email protected]
Step 3: ปรับแต่ง values.yaml
replicaCount: 3
image:
repository: my-registry/my-app
tag: "1.0.0"
pullPolicy: IfNotPresent
service:
type: LoadBalancer
port: 80
targetPort: 8080
ingress:
enabled: true
className: "nginx"
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
hosts:
- host: myapp.example.com
paths:
- path: /
pathType: Prefix
tls:
- secretName: myapp-tls
hosts:
- myapp.example.com
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
autoscaling:
enabled: true
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 80
Step 4: เขียน Deployment Template
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "my-app.fullname" . }}
labels:
{{- include "my-app.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
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 }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.service.targetPort }}
livenessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: http
initialDelaySeconds: 5
periodSeconds: 5
resources:
{{- toYaml .Values.resources | nindent 10 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
Release Management ด้วย Helm
ติดตั้ง Release
helm install my-release ./my-app -f values-prod.yaml -n production --create-namespace
ตรวจสอบ Release
helm list -n production helm status my-release -n production helm get values my-release -n production
อัปเดต Release (Upgrade)
helm upgrade my-release ./my-app -f values-prod.yaml -n production
ย้อนกลับ Release (Rollback)
helm history my-release -n production helm rollback my-release 1 -n production
ลบ Release
helm uninstall my-release -n production
Values Override ตามสภาพแวดล้อม
# values-dev.yaml
replicaCount: 1
image:
tag: "latest"
resources:
limits:
cpu: 100m
memory: 128Mi
# values-prod.yaml
replicaCount: 5
image:
tag: "v1.0.0"
resources:
limits:
cpu: 1000m
memory: 1Gi
autoscaling:
enabled: true
minReplicas: 5
maxReplicas: 20
ใช้ Helm Hooks สำหรับ Lifecycle Events
Helm hooks อนุญาตให้คุณดำเนินการ tasks ในขั้นตอนสำคัญของ release lifecycle:
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "my-app.fullname" . }}-db-migrate
annotations:
"helm.sh/hook": pre-upgrade,pre-install
"helm.sh/hook-weight": "-5"
spec:
template:
spec:
containers:
- name: db-migrate
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
command: ["php", "artisan", "migrate", "--force"]
restartPolicy: Never
Push Chart ไปยัง Repository
# Package Chart helm package ./my-app # Push ไปยัง ChartMuseum หรือ Harbor helm push my-app-1.0.0.tgz oci://registry.example.com/helm/charts # ติดตั้งจาก repository helm repo add my-repo https://charts.example.com helm install my-release my-repo/my-app
Best Practices
- ให้ Chart ใช้ default values ที่สมเหตุสมผล
- ใช้ named templates เพื่อให้ code ใหม่ซ้ำ
- เขียน tests สำหรับ templates ของคุณ
- จัดระเบียบ values ตามหัวข้อตรรมชาติ
- ใช้ semantic versioning สำหรับ Chart versions
- จัดให้มี documentation ชัดเจนใน NOTES.txt
- ใช้ lint เพื่อตรวจสอบ Chart ก่อนปล่อย
บทสรุป
Helm Chart ให้วิธีมืออาชีพในการจัดการ Kubernetes deployments ด้วยการจัดเก็บ templating และ version control ที่มี ด้วยการฝึกอบรม Helm ให้ดี คุณสามารถทำให้กระบวนการ deployment เป็นอัตโนมัติและเชื่อถือได้
