📘 KUBERNETES: HỆ THỐNG ĐIỀU PHỐI CONTAINER
🎯 Mục tiêu tổng quát
- Hiểu được nguyên lý hoạt động và kiến trúc của Kubernetes
- Thành thạo việc cài đặt và cấu hình Kubernetes
- Biết cách triển khai và quản lý ứng dụng container trên Kubernetes
- Hiểu được các thành phần cơ bản của Kubernetes
- Triển khai ứng dụng có tính sẵn sàng cao và khả năng mở rộng
🧑🏫 Bài 1: Giới thiệu về Kubernetes
Kubernetes là gì?
- Kubernetes (K8s) là một nền tảng mã nguồn mở để tự động hóa việc triển khai, mở rộng và quản lý ứng dụng container
- Được phát triển bởi Google, dựa trên kinh nghiệm của họ với hệ thống Borg
- Hiện nay được duy trì bởi Cloud Native Computing Foundation (CNCF)
Lịch sử phát triển
- 2014: Google công bố Kubernetes như một dự án mã nguồn mở
- 2015: Kubernetes v1.0 được phát hành, CNCF được thành lập
- 2016-nay: Kubernetes trở thành tiêu chuẩn chính cho điều phối container
Lợi ích của Kubernetes
- Tự động hóa triển khai: Triển khai ứng dụng một cách đáng tin cậy và nhất quán
- Self-healing: Tự động khởi động lại containers khi chúng bị lỗi
- Mở rộng tự động: Tự động mở rộng/thu hẹp số lượng container dựa trên tải
- Cân bằng tải: Phân phối lưu lượng mạng để đảm bảo triển khai ổn định
- Service discovery: Containers có thể tìm nhau thông qua DNS nội bộ
Các giải pháp thay thế cho Kubernetes
- Docker Swarm: Đơn giản hơn, tích hợp chặt chẽ với Docker
- Apache Mesos: Tập trung vào việc chạy workloads đa dạng (không chỉ containers)
- Amazon ECS: Dịch vụ quản lý container của AWS
- Nomad: Từ HashiCorp, đơn giản và nhẹ hơn
Use cases phổ biến
- Microservices: Quản lý ứng dụng phức tạp với nhiều thành phần nhỏ
- CI/CD: Triển khai liên tục với zero-downtime
- DevOps: Hỗ trợ quy trình DevOps tự động
- Big Data: Xử lý dữ liệu lớn với khả năng mở rộng
- Hybrid Cloud: Chạy workloads trên nhiều môi trường cloud khác nhau
🧑🏫 Bài 2: Kiến trúc Kubernetes
Tổng quan kiến trúc
text
+-------------------------------------------------------+
| Kubernetes Cluster |
| |
| +--------------------+ +--------------------+ |
| | | | | |
| | Control Plane | | Worker Nodes | |
| | | | | |
| | +--------------+ | | +--------------+ | |
| | | API Server | | | | Kubelet | | |
| | +--------------+ | | +--------------+ | |
| | | | | |
| | +--------------+ | | +--------------+ | |
| | | Scheduler | | | | Kube-proxy | | |
| | +--------------+ | | +--------------+ | |
| | | | | |
| | +--------------+ | | +--------------+ | |
| | | Controller | | | | Container | | |
| | | Manager | | | | Runtime | | |
| | +--------------+ | | +--------------+ | |
| | | | | |
| | +--------------+ | | | |
| | | etcd | | | | |
| | +--------------+ | | | |
| +--------------------+ +--------------------+ |
+-------------------------------------------------------+Control Plane Components
API Server (kube-apiserver):
- Điểm cuối HTTP API để tương tác với Kubernetes
- Cổng chính để kiểm soát cluster
- Xác thực và phân quyền tất cả các requests
Scheduler (kube-scheduler):
- Quan sát các pods mới chưa được gán node
- Lựa chọn node phù hợp để chạy pod
- Cân nhắc tài nguyên, ràng buộc, affinity, anti-affinity...
Controller Manager (kube-controller-manager):
- Chạy các quy trình controller
- Điều khiển trạng thái của cluster
- Chứa nhiều loại controllers: Node Controller, Replication Controller, Endpoint Controller,...
etcd:
- Cơ sở dữ liệu phân tán key-value
- Lưu trữ tất cả dữ liệu của cluster
- Đảm bảo tính nhất quán và sẵn sàng cao
Node Components
Kubelet:
- Agent chạy trên mỗi node
- Đảm bảo containers chạy trong pod
- Báo cáo tình trạng node lên control plane
Kube-proxy:
- Duy trì network rules trên node
- Cho phép giao tiếp mạng đến pods từ trong hoặc ngoài cluster
- Thực hiện chức năng load balancing cho services
Container Runtime:
- Phần mềm chịu trách nhiệm chạy containers
- Ví dụ: Docker, containerd, CRI-O
Các Add-ons quan trọng
- CoreDNS: Cung cấp DNS cho cluster
- Dashboard: Giao diện UI cho quản trị Kubernetes
- Ingress Controller: Quản lý traffic từ bên ngoài vào services
- CNI (Container Network Interface): Plugin quản lý mạng giữa các pods
Mô hình hoạt động
- Khi có yêu cầu (ví dụ: triển khai ứng dụng), client gửi request đến API Server
- API Server xác thực và xử lý request, lưu trạng thái vào etcd
- Controllers phát hiện thay đổi trạng thái và thực hiện các hành động
- Scheduler quyết định pod sẽ chạy trên node nào
- Kubelet trên node nhận thông tin và tạo pod
- Kube-proxy cấu hình mạng cho pod
🧑🏫 Bài 3: Cài đặt và Cấu hình Kubernetes
Các phương pháp cài đặt Kubernetes
- Minikube: Cho môi trường phát triển, chạy Kubernetes cục bộ
- kubeadm: Công cụ chính thức để cài đặt và cấu hình Kubernetes
- kind (Kubernetes IN Docker): Chạy Kubernetes trên Docker containers
- Dịch vụ quản lý: EKS (AWS), GKE (Google), AKS (Azure)
Cài đặt Minikube cho môi trường phát triển
bash
# Cài đặt Minikube trên Linux
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/
# Khởi động cluster
minikube start
# Kiểm tra trạng thái
minikube statusCài đặt kubectl - công cụ CLI để tương tác với Kubernetes
bash
# Linux
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
# Kiểm tra phiên bản
kubectl version --clientCài đặt cluster với kubeadm
bash
# 1. Cài đặt container runtime (ví dụ: Docker)
# 2. Cài đặt kubeadm, kubelet và kubectl
apt-get update
apt-get install -y apt-transport-https ca-certificates curl
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
add-apt-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
apt-get update
apt-get install -y kubelet kubeadm kubectl
# 3. Khởi tạo control plane
kubeadm init --pod-network-cidr=10.244.0.0/16
# 4. Cấu hình kubectl cho user
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 5. Cài đặt network plugin (ví dụ: Calico)
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
# 6. Join worker nodes
# Sử dụng lệnh từ output của kubeadm init
kubeadm join <control-plane-ip>:<port> --token <token> --discovery-token-ca-cert-hash <hash>Xác nhận cài đặt Kubernetes
bash
# Kiểm tra trạng thái các nodes
kubectl get nodes
# Kiểm tra các pods trong namespace kube-system
kubectl get pods -n kube-system
# Kiểm tra phiên bản server và client
kubectl versionCấu hình Kubernetes
Contexts và Clusters:
bash# Liệt kê contexts kubectl config get-contexts # Chuyển đổi context kubectl config use-context my-cluster # Xem cấu hình hiện tại kubectl config viewCác file cấu hình quan trọng:
/etc/kubernetes/: Chứa cấu hình của cluster~/.kube/config: Cấu hình của kubectl/etc/systemd/system/kubelet.service.d/: Cấu hình kubelet
Roles và RBAC (Điều khiển truy cập dựa trên vai trò):
bash# Tạo Role kubectl create role pod-reader --verb=get,list,watch --resource=pods # Tạo RoleBinding kubectl create rolebinding read-pods --role=pod-reader --user=jane # Kiểm tra quyền kubectl auth can-i list pods --as janeNamespace:
bash
# Tạo namespace
kubectl create namespace my-namespace
# Liệt kê namespaces
kubectl get namespaces
# Thực hiện lệnh trong namespace cụ thể
kubectl get pods -n my-namespace🧑🏫 Bài 4: Kubernetes Objects và Workloads
Kubernetes Objects là gì?
- Các thực thể bền vững trong hệ thống Kubernetes
- Đại diện cho trạng thái của cluster
- Được mô tả bằng files YAML hoặc JSON
Các Objects phổ biến
- Pods: Đơn vị nhỏ nhất có thể deploy trong Kubernetes
- ReplicaSets: Đảm bảo số lượng Pods mong muốn
- Deployments: Quản lý ReplicaSets, hỗ trợ cập nhật và rollback
- Services: Định nghĩa cách truy cập vào Pods
- ConfigMaps và Secrets: Cấu hình và dữ liệu nhạy cảm
- Volumes: Lưu trữ dữ liệu cho Pods
- Namespaces: Phân chia cluster thành nhiều môi trường ảo
Pod
- Nhóm các containers chia sẻ storage và network
- Mô hình "sidecar" phổ biến: container chính + container phụ trợ
yaml
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
- name: log-sidecar
image: busybox
command: ["sh", "-c", "tail -f /var/log/nginx/access.log"]
volumeMounts:
- name: logs-volume
mountPath: /var/log/nginx
volumes:
- name: logs-volume
emptyDir: {}Deployment
- Mức cao hơn Pod và ReplicaSet
- Quản lý việc deploy và update ứng dụng
- Hỗ trợ rollout và rollback
yaml
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80Service
- Cung cấp một endpoint ổn định để truy cập Pods
- Cân bằng tải giữa nhiều Pods
yaml
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: ClusterIPDaemonSet
- Đảm bảo tất cả (hoặc một số) Nodes chạy một bản sao của Pod
- Thường dùng cho logging, monitoring, storage
yaml
# daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: prometheus-node-exporter
spec:
selector:
matchLabels:
app: node-exporter
template:
metadata:
labels:
app: node-exporter
spec:
containers:
- name: node-exporter
image: prom/node-exporter
ports:
- containerPort: 9100StatefulSet
- Quản lý các Pods có định danh duy nhất
- Duy trì thứ tự và tính duy nhất của Pods
- Thích hợp cho databases và ứng dụng stateful
yaml
# statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb
spec:
serviceName: "mongodb"
replicas: 3
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo:4.4
ports:
- containerPort: 27017
volumeMounts:
- name: data
mountPath: /data/db
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1GiJob và CronJob
- Job: Chạy Pod đến khi hoàn thành
- CronJob: Job chạy theo lịch trình
yaml
# cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: backup-database
spec:
schedule: "0 1 * * *" # Mỗi ngày lúc 1 giờ sáng
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: mybackup:1.0
command: ["/bin/sh", "-c", "backup.sh"]
restartPolicy: OnFailureQuản lý Kubernetes Objects
bash
# Tạo từ file YAML
kubectl apply -f deployment.yaml
# Cập nhật image
kubectl set image deployment/nginx-deployment nginx=nginx:1.20
# Rollback
kubectl rollout undo deployment/nginx-deployment
# Scale
kubectl scale deployment/nginx-deployment --replicas=5
# Xóa
kubectl delete deployment nginx-deployment🧑🏫 Bài 5: Networking trong Kubernetes
Mô hình networking của Kubernetes
- Flat network: Pods giao tiếp với nhau không cần NAT
- Mỗi Pod có IP duy nhất
- Containers trong Pod chia sẻ IP
Các thành phần networking
- Pod Network: Giao tiếp giữa các pods
- Service Network: Truy cập đến các pods
- Cluster DNS: Service discovery
- Ingress: Routing HTTP/HTTPS từ bên ngoài vào cluster
Network Plugins (CNI)
- Calico: Hiệu suất cao, hỗ trợ network policy
- Flannel: Đơn giản, dễ cài đặt
- Weave Net: Dễ sử dụng, mã hóa
- Cilium: Dựa trên eBPF, hiệu năng cao
Service ()
- Cung cấp địa chỉ IP và DNS cố định
- Load balancing giữa các pods
Các loại Services
ClusterIP: (mặc định)
- IP nội bộ trong cluster
- Chỉ truy cập được từ bên trong cluster
yamlapiVersion: v1 kind: Service metadata: name: backend-service spec: selector: app: backend ports: - port: 80 targetPort: 8080 type: ClusterIPNodePort:
Mở port cố định trên tất cả các nodes
Truy cập từ bên ngoài qua
<NodeIP>:<NodePort>yamlapiVersion: v1 kind: Service metadata: name: web-service spec: selector: app: web ports: - port: 80 targetPort: 8080 nodePort: 30080 # Port 30000-32767 type: NodePort
LoadBalancer:
- Sử dụng load balancer của cloud provider
- Cung cấp IP công khai
yamlapiVersion: v1 kind: Service metadata: name: web-service spec: selector: app: web ports: - port: 80 targetPort: 8080 type: LoadBalancerExternalName:
- CNAME record tới service bên ngoài
yaml
apiVersion: v1
kind: Service
metadata:
name: external-db
spec:
type: ExternalName
externalName: database.example.comIngress
- Layer 7 (HTTP) load balancer
- Định tuyến traffic dựa trên URL path hoặc hostname
- Cần Ingress Controller (nginx, traefik, ...)
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
spec:
rules:
- host: app.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80Network Policies
- Kiểm soát traffic giữa Pods
- Tương tự như firewall rules
yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-policy
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
role: backend
ports:
- protocol: TCP
port: 5432Debugging Network Issues
bash
# Kiểm tra service
kubectl get svc my-service
# Debug DNS
kubectl run -i --tty --rm debug --image=busybox -- sh
# Trong container debug
nslookup my-service
# Kiểm tra endpoints
kubectl get endpoints my-service
# Xem network policies
kubectl get networkpolicies🧑🏫 Bài 6: Storage và Persistence
Persistent Storage trong Kubernetes
- Dữ liệu tồn tại độc lập với vòng đời của Pod
- Kubernetes abstraction để quản lý storage
Volumes
- Storage gắn vào Pod
- Tồn tại trong suốt vòng đời của Pod
Các loại Volumes phổ biến
- emptyDir: Thư mục tạm thời, bị xóa khi Pod bị xóa
- hostPath: Sử dụng path trên Node
- PersistentVolume (PV): Storage độc lập với Pod
- ConfigMap/Secret as Volume: Mount cấu hình/bí mật
emptyDir
yaml
apiVersion: v1
kind: Pod
metadata:
name: cache-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: cache-volume
mountPath: /cache
volumes:
- name: cache-volume
emptyDir: {}hostPath
yaml
apiVersion: v1
kind: Pod
metadata:
name: log-pod
spec:
containers:
- name: app
image: nginx
volumeMounts:
- name: log-volume
mountPath: /var/log/nginx
volumes:
- name: log-volume
hostPath:
path: /var/log/pods
type: DirectoryOrCreatePersistent Storage Architecture
- PersistentVolume (PV): Tài nguyên storage thực tế
- PersistentVolumeClaim (PVC): Yêu cầu sử dụng storage
- StorageClass: Định nghĩa loại storage và provisioner
PersistentVolume (PV)
yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-storage
spec:
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /data/pv0001PersistentVolumeClaim (PVC)
yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-storage-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: standardSử dụng PVC trong Pod
yaml
apiVersion: v1
kind: Pod
metadata:
name: db-pod
spec:
containers:
- name: db
image: postgres:13
volumeMounts:
- name: db-data
mountPath: /var/lib/postgresql/data
volumes:
- name: db-data
persistentVolumeClaim:
claimName: db-storage-claimStorageClass
- Cung cấp storage động theo yêu cầu
- Tích hợp với cloud providers
yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
fsType: ext4
reclaimPolicy: Delete
allowVolumeExpansion: trueVolume Snapshots
- Sao lưu dữ liệu từ PVC
- Khôi phục từ snapshot
yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: db-snapshot
spec:
volumeSnapshotClassName: csi-hostpath-snapclass
source:
persistentVolumeClaimName: db-storage-claimStatefulSet với Storage
yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: "postgres"
replicas: 3
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:13
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: "standard"
resources:
requests:
storage: 10GiBest Practices
- Sử dụng PVs và PVCs thay vì volumes trực tiếp
- Định nghĩa StorageClass phù hợp cho từng loại workload
- Cấu hình backup và disaster recovery
- Sử dụng StatefulSets với volumeClaimTemplates cho ứng dụng stateful
- Giám sát dung lượng và hiệu suất storage
🧑🏫 Bài 7: ConfigMaps và Secrets
ConfigMaps
- Lưu trữ dữ liệu cấu hình dạng key-value
- Tách cấu hình ra khỏi image container
Tạo ConfigMap
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
database.host: "mysql"
database.port: "3306"
ui.theme: "dark"
config.json: |
{
"log_level": "info",
"debug": false,
"features": {
"billing": true,
"notifications": false
}
}Sử dụng ConfigMap
Biến môi trường:
yamlapiVersion: v1 kind: Pod metadata: name: web spec: containers: - name: web image: nginx env: - name: DB_HOST valueFrom: configMapKeyRef: name: app-config key: database.host - name: DB_PORT valueFrom: configMapKeyRef: name: app-config key: database.portenvFrom - tất cả keys làm biến môi trường:
yamlapiVersion: v1 kind: Pod metadata: name: web spec: containers: - name: web image: nginx envFrom: - configMapRef: name: app-configVolume mount:
yamlapiVersion: v1 kind: Pod metadata: name: web spec: containers: - name: web image: nginx volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: app-config
Secrets
- Lưu trữ dữ liệu nhạy cảm (passwords, tokens, keys)
- Tương tự ConfigMap nhưng an toàn hơn
- Mã hóa Base64 (không phải mã hóa mạnh)
Tạo Secret
yaml
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
data:
# Giá trị phải ở dạng base64
username: YWRtaW4= # admin
password: c2VjcmV0 # secretTạo Secret từ command line
bash
# Tạo từ file
kubectl create secret generic ssl-cert --from-file=cert.pem --from-file=key.pem
# Tạo từ literal
kubectl create secret generic api-keys --from-literal=api_key=123456 --from-literal=secret_key=abcdefSử dụng Secret
Biến môi trường:
yamlapiVersion: v1 kind: Pod metadata: name: db-client spec: containers: - name: app image: myapp env: - name: DB_USERNAME valueFrom: secretKeyRef: name: db-credentials key: username - name: DB_PASSWORD valueFrom: secretKeyRef: name: db-credentials key: passwordVolume mount:
yamlapiVersion: v1 kind: Pod metadata: name: db-client spec: containers: - name: app image: myapp volumeMounts: - name: secret-volume mountPath: /etc/secrets readOnly: true volumes: - name: secret-volume secret: secretName: db-credentials
Secret Types
- Opaque: Default, dữ liệu tùy ý
- kubernetes.io/service-account-token: Service account token
- kubernetes.io/dockerconfigjson: Docker registry auth
- kubernetes.io/tls: TLS certificates
Docker Registry Secret
yaml
apiVersion: v1
kind: Secret
metadata:
name: docker-registry-cred
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: <base64-encoded-docker-config>Sử dụng Docker Registry Secret
yaml
apiVersion: v1
kind: Pod
metadata:
name: private-app
spec:
containers:
- name: app
image: myprivate/repo:tag
imagePullSecrets:
- name: docker-registry-credBest Practices (Bí quyết bảo mật Secrets)
- Không lưu trữ Secrets trong git repositories
- Hạn chế access đến Secrets bằng RBAC
- Sử dụng solutions bên ngoài như Vault cho quản lý bí mật
- Thiết lập network policies cho Pods với Secrets
- Encrypt etcd để bảo vệ Secrets khi lưu trữ
🧑🏫 Bài 8: Helm - Package Manager cho Kubernetes
Helm là gì?
- Package manager cho Kubernetes
- Giúp định nghĩa, cài đặt và nâng cấp ứng dụng phức tạp
- Tương tự npm, pip hoặc apt nhưng cho Kubernetes
Khái niệm cơ bản của Helm
- Chart: Package của Helm, chứa tất cả tài nguyên Kubernetes
- Repository: Nơi lưu trữ và chia sẻ charts
- Release: Instance của chart đã được deploy
Cài đặt Helm
bash
# Linux
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# macOS
brew install helm
# Windows
choco install kubernetes-helmCấu trúc của một Helm Chart
text
mychart/
Chart.yaml # Thông tin về chart
values.yaml # Giá trị mặc định cho templates
templates/ # Thư mục chứa templates
deployment.yaml
service.yaml
ingress.yaml
_helpers.tpl # Partial templates
charts/ # Charts phụ thuộc
templates/NOTES.txt # Notes hiển thị sau khi cài đặtChart.yaml
yaml
apiVersion: v2
name: myapp
version: 1.0.0
description: My Application Helm Chart
type: application
appVersion: "1.0.0"
dependencies:
- name: mysql
version: 8.8.5
repository: https://charts.bitnami.com/bitnamivalues.yaml
yaml
# Default values
replicaCount: 2
image:
repository: nginx
tag: 1.19.0
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
ingress:
enabled: false
hosts:
- host: chart-example.local
paths: ["/"]Template file (deployment.yaml)
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: { { include "myapp.fullname" . } }
labels: { { - include "myapp.labels" . | nindent 4 } }
spec:
replicas: { { .Values.replicaCount } }
selector:
matchLabels: { { - include "myapp.selectorLabels" . | nindent 6 } }
template:
metadata:
labels: { { - include "myapp.selectorLabels" . | nindent 8 } }
spec:
containers:
- name: { { .Chart.Name } }
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: { { .Values.image.pullPolicy } }
ports:
- name: http
containerPort: 80
protocol: TCPHelm Commands
bash
# Tìm kiếm charts
helm search hub wordpress
# Thêm repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
# Cài đặt chart
helm install my-release bitnami/wordpress
# Xem các releases đã cài đặt
helm list
# Kiểm tra trạng thái
helm status my-release
# Nâng cấp release
helm upgrade my-release bitnami/wordpress --values=custom-values.yaml
# Rollback
helm rollback my-release 1
# Gỡ cài đặt
helm uninstall my-releaseTạo Helm Chart mới
bash
# Tạo chart mới
helm create mychart
# Kiểm tra cấu trúc chart
helm lint mychart
# Đóng gói chart
helm package mychart
# Cài đặt chart local
helm install my-app ./mychart
# Cài đặt với custom values
helm install my-app ./mychart -f my-values.yamlHelm Template Functions
yaml
# Quote
app: {{ .Values.appName | quote }}
# Default
replicas: {{ .Values.replicas | default 1 }}
# Indent
data:
{{- .Values.configuration | nindent 2 }}
# toYaml
labels:
{{- toYaml .Values.labels | nindent 4 }}
# if/else
{{- if .Values.ingress.enabled }}
# ingress configuration
{{- end }}Chart Hooks
pre-install,post-installpre-delete,post-deletepre-upgrade,post-upgradepre-rollback,post-rollbacktest
yaml
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "mychart.fullname" . }}-db-init
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "0"
"helm.sh/hook-delete-policy": hook-succeeded
spec:
template:
spec:
containers:
- name: db-init
image: postgres
command: ["psql", "--command", "CREATE DATABASE app"]
restartPolicy: NeverBest Practices (Bí quyết sử dụng Helm)
- Sử dụng Helm repo để quản lý charts
- Phân chia values.yaml theo các thành phần logic
- Đặt thực nghiệm với templates trong values.yaml
- Sử dụng helpers để tái sử dụng code
- Thêm NOTES.txt để hướng dẫn người dùng
🧪 BÀI TẬP LỚN CUỐI PHẦN: Xây dựng và triển khai ứng dụng microservices trên Kubernetes
Mô tả bài toán
Xây dựng hệ thống microservices hoàn chỉnh và triển khai trên Kubernetes cluster, với các thành phần:
- Frontend SPA (Single Page Application)
- API Gateway
- 2-3 Microservices backend
- Database (SQL hoặc NoSQL)
- Hệ thống xác thực/phân quyền
Yêu cầu
- Xây dựng Docker images cho từng microservice
- Tạo các Kubernetes manifests cho tất cả các components
- Cấu hình Services, Ingress để quản lý traffic
- Thiết lập PersistentVolumes cho database
- Cấu hình ConfigMaps và Secrets
- Triển khai Prometheus và Grafana để monitoring
- Cấu hình Horizontal Pod Autoscaler
- Tạo Helm chart cho toàn bộ ứng dụng
- Viết scripts cho CI/CD pipeline
Kết quả đạt được
- Ứng dụng chạy ổn định trên Kubernetes
- Tài liệu triển khai chi tiết và kiến trúc hệ thống
- Khả năng mở rộng tự động theo tải
- Monitoring và alerting đầy đủ
- CI/CD pipeline cho việc cập nhật ứng dụng
