Helm เป็นเครื่องมือยอดนิยมสำหรับจัดการ configuration file ของ Kubernetes (K8s) ช่วยให้เราสามารถสร้าง template สำหรับ deployment ที่สามารถนำกลับมาใช้ซ้ำได้ ในบทความนี้จะพาไปรู้จักแนวคิดของ Helm Chart พร้อมตัวอย่างการสร้าง chart จริงสำหรับ phpMyAdmin ที่เชื่อมต่อกับ MySQL
สารบัญ
- Helm Chart คืออะไร?
- เตรียมเครื่องมือ
- สร้าง Helm Chart แรกของเรา
- เพิ่ม dependency (MySQL)
- ตัวอย่าง Deployment และ Service Template
- ตั้งค่า Values
- ติดตั้ง Chart
- Upgrade Chart
- Uninstall Chart
- Best Practices
- สรุป
Helm Chart คืออะไร?
Helm คือ package manager ของ Kubernetes ที่ช่วยให้การ deploy application หรือ service ต่าง ๆ ลง K8s ง่ายขึ้น โดยเราสามารถสร้างไฟล์ template และกำหนด value เฉพาะที่ต้องการเปลี่ยนแปลงในแต่ละ environment ได้สะดวก
Chart คือชื่อเรียก repository ที่เก็บ template/config ต่าง ๆ สำหรับใช้งานบน K8s
เตรียมเครื่องมือ
ก่อนเริ่มต้น ต้องเตรียมเครื่องมือต่อไปนี้:
- Kubernetes cluster - ในที่นี้ใช้ minikube สำหรับ local development
- Helm - Package manager สำหรับ Kubernetes
- kubectl - Command line tool สำหรับจัดการ Kubernetes
การติดตั้งบน macOS
# ติดตั้ง Helm และ minikube
brew install helm minikube kubectl
# เริ่มต้น minikube
minikube start
# ตรวจสอบการติดตั้ง
helm version
kubectl cluster-info
การติดตั้งบน Linux/Windows
# Linux (Ubuntu/Debian)
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
# Windows (ใช้ Chocolatey)
choco install kubernetes-helm minikube
สร้าง Helm Chart แรกของเรา
เริ่มจากสร้าง chart repository:
helm create my-example-chart
cd my-example-chart
จะได้โครงสร้างไฟล์ประมาณนี้:
my-example-chart/
├── Chart.yaml # ข้อมูล metadata ของ chart
├── charts/ # directory สำหรับ dependencies
├── templates/ # Kubernetes manifest templates
│ ├── NOTES.txt # ข้อความแสดงหลังการติดตั้ง
│ ├── _helpers.tpl # template helpers และ functions
│ ├── deployment.yaml # deployment template
│ ├── hpa.yaml # horizontal pod autoscaler
│ ├── ingress.yaml # ingress template
│ ├── service.yaml # service template
│ ├── serviceaccount.yaml # service account template
│ └── tests/
│ └── test-connection.yaml # connection test
└── values.yaml # default values สำหรับ templates
อธิบายไฟล์สำคัญ
- Chart.yaml: เก็บ metadata ของ chart เช่น name, version, description
- values.yaml: เก็บค่า default ที่จะถูกใช้ใน templates
- templates/: เก็บ Kubernetes manifest templates ที่ใช้ Go template syntax
เพิ่ม dependency (MySQL)
สมมติว่าเราต้องการ deploy phpMyAdmin ที่เชื่อมต่อกับ MySQL:
แก้ไข Chart.yaml
:
apiVersion: v2
name: my-example-chart
version: 0.0.1
description: my chart for test phpmyadmin connect to mysql
type: application
appVersion: "5.2.0"
dependencies:
- name: mysql
version: "9.19.1"
repository: oci://registry-1.docker.io/bitnamicharts
maintainers:
- name: trwkc
url: https://github.com/trwkc
จากนั้นรัน:
helm dep build
Helm จะดึง dependency (mysql) มาเก็บไว้ใน charts/
ให้โดยอัตโนมัติ
ตัวอย่าง Deployment และ Service Template
templates/deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.name }}
template:
metadata:
labels:
app: {{ .Values.name }}
release: {{ .Release.Name }}
spec:
containers:
- name: {{ .Values.name }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: {{ .Values.service.port }}
env:
- name: PMA_HOST
value: {{ .Release.Name }}-mysql
- name: MYSQL_ROOT_PASSWORD
value: {{ .Values.mysql.auth.rootPassword }}
templates/service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: {{ .Values.name }}
labels:
app: {{ .Values.name }}
release: {{ .Release.Name }}
spec:
type: {{ .Values.service.type }}
ports:
- port: 80
targetPort: 80
selector:
app: {{ .Values.name }}
ตั้งค่า Values
values.yaml
(บางส่วน):
name: my-example
replicaCount: 1
image:
repository: phpmyadmin
pullPolicy: IfNotPresent
tag: "5.2.0"
service:
type: ClusterIP
port: 80
mysql:
auth:
rootPassword: pass12345
ติดตั้ง Chart
helm install --debug my-example .
เมื่อติดตั้งสำเร็จ จะสามารถเข้า phpMyAdmin และลอง insert ข้อมูลใน MySQL ได้
Upgrade Chart
ลองเปลี่ยน version ของ phpmyadmin เป็น 5.2.1
ที่ values.yaml
และปรับ Chart.yaml
เป็น version: 0.0.2
, appVersion: 5.2.1
จากนั้นอัพเกรดด้วย
helm upgrade --reuse-values --set image.tag=5.2.1 --debug my-example .
ข้อมูลเดิมของเรายังคงอยู่หลังการ upgrade release!
Uninstall Chart
เมื่อต้องการลบ service ออกทั้งหมด:
helm uninstall my-example
Best Practices
เมื่อสร้าง Helm Chart ควรคำนึงถึงแนวทางปฏิบัติที่ดีต่อไปนี้:
1. การตั้งชื่อและ Versioning
# Chart.yaml
apiVersion: v2
name: my-app # ใช้ชื่อที่ชัดเจนและสั้น
version: 1.0.0 # ใช้ semantic versioning
appVersion: "2.1.4" # version ของ application
2. การจัดการ Values
# values.yaml - จัดกลุ่มและเพิ่ม comments
# Application settings
app:
name: my-app
version: "2.1.4"
# Container settings
image:
repository: nginx
tag: "1.21"
pullPolicy: IfNotPresent
# Service configuration
service:
type: ClusterIP
port: 80
targetPort: 8080
3. การใช้ Templates อย่างมีประสิทธิภาพ
# ใช้ labels และ selectors ที่สอดคล้องกัน
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "chart.fullname" . }}
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
selector:
matchLabels:
{{- include "chart.selectorLabels" . | nindent 6 }}
4. การจัดการ Security
# ใช้ Security Context
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
# จำกัด resources
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
5. การทดสอบ
# ทดสอบ template rendering
helm template my-example .
# ทดสอบ values
helm lint .
# dry-run ก่อนติดตั้งจริง
helm install --dry-run --debug my-example .
สรุป
Helm Chart เป็นเครื่องมือที่ทรงพลังสำหรับการจัดการ Kubernetes applications ช่วยให้เราสามารถ:
- Package และ Deploy applications ได้อย่างง่ายดาย
- Version Control และ Rollback ได้อย่างปลอดภัย
- Template configuration ให้สามารถนำไปใช้ใน environment ต่าง ๆ
- จัดการ Dependencies ระหว่าง services ได้อย่างมีระบบ
ขั้นตอนต่อไป
- ลองสร้าง Chart ของ application ที่คุณใช้งานอยู่
- เรียนรู้ Advanced Features เช่น Hooks, Tests, และ Subcharts
- ศึกษา Helm Hub เพื่อหา Charts ที่พร้อมใช้งาน
- ฝึกฝน CI/CD integration กับ Helm
แหล่งข้อมูลเพิ่มเติม
- ตัวอย่าง Chart: github.com/trwkc/my-example-chart
- Helm Documentation: https://helm.sh/docs/
- Chart Best Practices: https://helm.sh/docs/chart_best_practices/
- Helm Hub: https://artifacthub.io/
- Kubernetes Documentation: https://kubernetes.io/docs/
หวังว่าบทความนี้จะช่วยให้คุณเริ่มต้นการใช้งาน Helm Chart ได้อย่างมั่นใจ! 🚀