Kubernetes เต็มรูปแบบซับซ้อนเกินจำเป็นสำหรับ VPS เดียว แต่ k3s คือ Lightweight Kubernetes ที่ติดตั้งได้ใน 30 วินาที ใช้ RAM น้อย แต่ compatible 100% กับ kubectl และ Helm เหมาะสำหรับ Developer ที่ต้องการเรียนรู้ Kubernetes หรือรัน production workload บน VPS ราคาประหยัด
ทำไมต้องใช้ k3s แทน Docker Compose?
Docker Compose เพียงพอสำหรับ Container ไม่กี่ตัว แต่เมื่อ Application เติบโต Kubernetes ให้ความสามารถที่ Docker Compose ทำไม่ได้:
- Auto-healing — restart pod ที่ crash โดยอัตโนมัติ
- Rolling Update — อัปเดต version โดยไม่มี downtime
- Resource Limits — กำหนด CPU/RAM ต่อ pod ป้องกัน OOM
- Declarative Config — ทุกอย่างเป็น YAML ใน git
- Helm Charts — ติดตั้ง Software ซับซ้อนด้วย command เดียว
Requirements
- VPS Ubuntu 20.04+ / RAM 2GB+ (แนะนำ 4GB)
- CPU 2 Core+
- Port 6443 เปิดสำหรับ kubectl remote access
ขั้นที่ 1 — ติดตั้ง k3s
# ติดตั้ง k3s Server (Master Node)
curl -sfL https://get.k3s.io | sh -
# รอ 1-2 นาที แล้วตรวจสอบ
sudo k3s kubectl get nodes
ถ้าเห็น Node status Ready แสดงว่า k3s ทำงานแล้ว
ขั้นที่ 2 — ตั้งค่า kubectl จากเครื่อง Local
# บน VPS: copy kubeconfig
sudo cat /etc/rancher/k3s/k3s.yaml
# บนเครื่อง Local: สร้างไฟล์ config
mkdir -p ~/.kube
# วาง content จาก VPS แล้วแทนที่ 127.0.0.1 ด้วย VPS IP
nano ~/.kube/config-k3s
# ตั้ง Environment Variable
export KUBECONFIG=~/.kube/config-k3s
# ทดสอบ
kubectl get nodes
ขั้นที่ 3 — Deploy Application แรก
สร้าง Deployment
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "128Mi"
cpu: "500m"
kubectl apply -f nginx-deployment.yaml
kubectl get pods -w # รอดู status
สร้าง Service
# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
kubectl apply -f nginx-service.yaml
สร้าง Ingress ด้วย Traefik (k3s มาให้ default)
# nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: websecure
traefik.ingress.kubernetes.io/router.tls.certresolver: letsencrypt
spec:
rules:
- host: nginx.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80
kubectl apply -f nginx-ingress.yaml
ขั้นที่ 4 — ติดตั้ง Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
helm version
ติดตั้ง Application ด้วย Helm
# ตัวอย่าง: ติดตั้ง cert-manager
helm repo add jetstack https://charts.jetstack.io
helm repo update
helm install cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--set installCRDs=true
ขั้นที่ 5 — ConfigMap และ Secret
# สร้าง ConfigMap
kubectl create configmap app-config \
--from-literal=DB_HOST=postgres \
--from-literal=DB_PORT=5432
# สร้าง Secret (base64 encoded)
kubectl create secret generic app-secret \
--from-literal=DB_PASSWORD=mysecretpass \
--from-literal=API_KEY=myapikey
# ใช้ใน Deployment
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: DB_HOST
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: app-secret
key: DB_PASSWORD
Persistent Volume สำหรับ Database
# postgres-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: local-path # k3s built-in storage class
Commands ที่ใช้บ่อย
# ดู resource ทั้งหมด
kubectl get all -A
# ดู log ของ pod
kubectl logs pod-name -f
# exec เข้า pod
kubectl exec -it pod-name -- /bin/sh
# scale deployment
kubectl scale deployment nginx --replicas=3
# rolling update image
kubectl set image deployment/nginx nginx=nginx:1.25
# rollback
kubectl rollout undo deployment/nginx
# ดู events
kubectl get events --sort-by=.metadata.creationTimestamp
Tip — ดู Resource Usage: ติดตั้ง metrics-server เพื่อใช้ kubectl top nodes และ kubectl top pods: kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
k3s กับ Docker Compose เปรียบเทียบ
| ฟีเจอร์ | k3s | Docker Compose |
|---|---|---|
| Auto-restart | ✅ Liveness Probe | restart: unless-stopped |
| Zero-downtime deploy | ✅ Rolling Update | ❌ |
| Resource Limit | ✅ CPU/Memory | จำกัด |
| ความซับซ้อน | สูง (learning curve) | ต่ำ (เรียนรู้ง่าย) |
| RAM overhead | ~512MB | ~50MB |
| Helm support | ✅ | ❌ |
ต้องการ VPS สำหรับรัน Kubernetes?
AsiaGB มี VPS 4-8GB RAM เหมาะสำหรับ k3s Production Cluster เริ่มต้น 500 บาท/เดือน
ดู VPS Plans