Kubernetes K3s ติดตั้งบน VPS

เริ่มต้น Kubernetes บน VPS: ติดตั้ง K3s ใน 15 นาที

มิถุนายน 2026 · อ่าน 10 นาที

Kubernetes เป็น container orchestration platform ที่ทรงพลัง แต่ full installation ต้องการ resource มาก K3s คือ Kubernetes แบบ lightweight ที่ Rancher พัฒนา ใช้ RAM แค่ ~500MB รันบน VPS เครื่องเดียวได้ รองรับ workload จริง พร้อม kubectl ทุกคำสั่ง และ upgrade ไปหา K8s full-scale ได้ในอนาคต

ความต้องการระบบ

ติดตั้ง K3s

# ติดตั้ง K3s แบบ single-node (master + worker ในเครื่องเดียว)
curl -sfL https://get.k3s.io | sh -

# ตรวจสอบสถานะ
systemctl status k3s

# ดู node
kubectl get nodes
# ผลลัพธ์ที่ควรเห็น
NAME     STATUS   ROLES                  AGE   VERSION
vps-01   Ready    control-plane,master   1m    v1.29.x+k3s1
kubeconfig: K3s เก็บ config ที่ /etc/rancher/k3s/k3s.yaml — copy ไปที่ ~/.kube/config ถ้าต้องการรัน kubectl จาก user ทั่วไป: mkdir -p ~/.kube && cp /etc/rancher/k3s/k3s.yaml ~/.kube/config && chown $(id -u):$(id -g) ~/.kube/config

Concept พื้นฐาน Kubernetes

Deploy App แรก: Nginx

# สร้าง deployment
kubectl create deployment nginx --image=nginx:alpine --replicas=2

# expose ด้วย service
kubectl expose deployment nginx --port=80 --type=ClusterIP

# ดูสถานะ
kubectl get pods
kubectl get deployments
kubectl get services
# หรือใช้ YAML manifest (แนะนำสำหรับ production)
nano 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: "50m"
          limits:
            memory: "128Mi"
            cpu: "200m"
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
kubectl apply -f nginx-deployment.yaml

ตั้งค่า Ingress Controller (Traefik)

K3s มาพร้อม Traefik ingress controller อยู่แล้ว สร้าง Ingress resource เพื่อ route domain:

nano nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-svc
            port:
              number: 80
kubectl apply -f nginx-ingress.yaml

# ทดสอบ (ต้อง DNS ชี้มาที่ IP VPS ก่อน)
curl -H "Host: myapp.example.com" http://YOUR_VPS_IP

SSL อัตโนมัติด้วย cert-manager

# ติดตั้ง cert-manager
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/latest/download/cert-manager.yaml

# รอจนพร้อม
kubectl wait --for=condition=ready pod -l app=cert-manager -n cert-manager --timeout=120s

# สร้าง ClusterIssuer สำหรับ Let's Encrypt
cat <

คำสั่ง kubectl ที่ใช้บ่อย

# ดู resource ทั้งหมด
kubectl get all

# ดู log ของ pod
kubectl logs POD_NAME -f

# exec เข้าไปใน container
kubectl exec -it POD_NAME -- sh

# scale replicas
kubectl scale deployment nginx --replicas=3

# ลบ resource
kubectl delete deployment nginx
kubectl delete -f nginx-deployment.yaml

# describe รายละเอียด
kubectl describe pod POD_NAME
kubectl describe node vps-01

Monitor Resource Usage

# ติดตั้ง metrics-server (มีใน K3s แล้ว)
kubectl top nodes
kubectl top pods

K3s ทำให้ Kubernetes เข้าถึงได้สำหรับ VPS ทั่วไป — เหมาะมากสำหรับ microservices ขนาดเล็กถึงกลาง หรือทีมที่ต้องการเรียนรู้ Kubernetes ก่อน scale ขึ้น production cluster เต็มรูปแบบในภายหลัง

ตรวจสอบว่า K3s ทำงานถูกต้อง

# ตรวจสอบ node status
kubectl get nodes
# ต้องแสดง: STATUS = Ready

# ตรวจสอบ pods ทั้งหมดใน namespace
kubectl get pods --all-namespaces

# ตรวจสอบ service ทำงาน
kubectl get services

# ทดสอบ cluster DNS
kubectl run test-dns --image=busybox --rm -it --restart=Never -- nslookup kubernetes
Tip: ถ้า kubectl ไม่ทำงาน ให้ตั้ง KUBECONFIG: export KUBECONFIG=/etc/rancher/k3s/k3s.yaml หรือ copy ไป ~/.kube/config เพื่อใช้ได้โดย user ทั่วไปโดยไม่ต้อง sudo

แก้ปัญหาที่พบบ่อย

Error: "the server doesn't have a resource type" หลังติดตั้ง K3s

K3s ยังไม่พร้อม หรือ kubectl ไม่ได้ connect กับ cluster ที่ถูกต้อง:

# ตรวจสอบว่า K3s รันอยู่
systemctl status k3s

# ตั้ง kubeconfig
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
kubectl cluster-info

# ดู K3s logs ถ้า service ไม่ start
journalctl -u k3s -n 50

Error: Pod ติดค้างที่ "Pending" ไม่เปลี่ยนเป็น Running

มักเกิดจาก resource ไม่พอ หรือ node ยังไม่ Ready:

# ดูรายละเอียดปัญหา
kubectl describe pod POD_NAME
# ดูใน Events section ด้านล่าง

# ตรวจสอบ resource ของ node
kubectl describe node
# ดู Capacity vs Allocatable

# ตรวจสอบว่า node Ready
kubectl get nodes -o wide

Error: Pod ติดอยู่ที่ "CrashLoopBackOff"

Container crash แล้ว restart วนซ้ำ ให้ดู log ว่า error คืออะไร:

# ดู log ของ pod ที่ crash
kubectl logs POD_NAME
kubectl logs POD_NAME --previous   # log จาก run ก่อนหน้า

# ดู exit code ของ container
kubectl describe pod POD_NAME | grep -A3 "Last State"

Error: Ingress ตั้งค่าแล้วแต่ไม่ redirect traffic

ต้องตรวจสอบว่า Traefik ingress controller ทำงาน และ DNS ชี้ถูก:

# ตรวจสอบ ingress resource
kubectl get ingress

# ตรวจสอบ Traefik pod
kubectl get pods -n kube-system | grep traefik

# ทดสอบ manual (DNS อาจยังไม่ propagate)
curl -H "Host: myapp.example.com" http://YOUR_VPS_IP

Error: K3s ใช้ RAM มากเกินคาด

K3s รวม etcd, API server, controller manager ทั้งหมดในเครื่องเดียว บน VPS RAM 1GB อาจตึง:

# ดู resource usage ของ K3s components
kubectl top pods -n kube-system

# ถ้า RAM น้อย ลด resource ของ system pods
# หรือใช้ containerd แทน docker: K3s ใช้ containerd เป็น default แล้ว

# ดู node resource
kubectl describe node | grep -A5 "Allocated resources"

คำถามที่พบบ่อย

K3s ต่างจาก K8s (Kubernetes เต็ม) อย่างไร?

K3s เป็น Kubernetes ที่ผ่านการ slim down สำหรับ resource ที่จำกัด โดย Rancher ลบ cloud provider plugins ที่ไม่จำเป็น ใช้ SQLite แทน etcd (หรือ etcd embedded), และรวม components เป็น binary เดียว ใช้ kubectl เหมือนกันทุกคำสั่ง และ upgrade ไป full K8s cluster ได้ในอนาคต

VPS 1 เครื่องเหมาะกับ K3s จริงไหม?

เหมาะสำหรับ development, staging, microservices ขนาดเล็ก หรือ workload ที่ไม่ต้องการ HA สูง VPS RAM 2GB+ แนะนำสำหรับ production หลายๆ app ข้อจำกัดคือไม่มี failover ถ้า VPS ล้ม ทุก pod หยุดหมด สำหรับ production สำคัญควรใช้ multi-node cluster

จะ backup state ของ K3s cluster ได้อย่างไร?

K3s ใช้ SQLite เก็บ cluster state ที่ /var/lib/rancher/k3s/server/db/ backup ด้วย cp -r /var/lib/rancher/k3s/server/db/ /backup/k3s-db-$(date +%Y%m%d) และ backup YAML manifest ทุกไฟล์ที่ใช้ deploy ด้วย เพราะ manifest คือ source of truth สำหรับ restore