ติดตั้งและใช้งาน Kubernetes Dashboard บน Cloud VPS
Kubernetes Dashboard เป็น Web UI ที่มีประโยชน์มากสำหรับการบริหารจัดการ Kubernetes Cluster ด้วยอินเทอร์เฟซที่เป็นมิตรต่อผู้ใช้งาน บทความนี้จะอธิบายวิธีติดตั้งและใช้งาน Kubernetes Dashboard บน Cloud VPS ของท่าน
Kubernetes Dashboard คืออะไร?
Kubernetes Dashboard เป็น Web-based interface ที่ออกแบบมาเพื่อให้ผู้บริหาร cluster สามารถติดตามสถานะของ cluster ได้อย่างง่ายดาย ผ่าน Dashboard ท่านสามารถ:
- ดูสถานะของ Nodes, Pods, Services
- สร้างและจัดการ Deployments
- ตรวจสอบ Logs ของ Pods
- จัดการ Namespaces
- Monitor resource usage
ข้อกำหนดเบื้องต้น
ก่อนติดตั้ง Kubernetes Dashboard ท่านต้องมี:
- Cloud VPS ที่มี Kubernetes Cluster ติดตั้งแล้ว
- kubectl ติดตั้งและตั้งค่าให้สามารถเชื่อมต่อกับ cluster ได้
- Admin access ในการ execute commands
วิธีติดตั้ง Kubernetes Dashboard
ขั้นตอนที่ 1: ติดตั้ง Dashboard
ติดตั้ง Kubernetes Dashboard ด้วยคำสั่ง kubectl:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
ขั้นตอนที่ 2: ตรวจสอบการติดตั้ง
ตรวจสอบว่า Dashboard Pods ได้ทำงานแล้ว:
kubectl get pods -n kubernetes-dashboard
ท่านจะได้ผลลัพธ์แบบนี้:
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-c79fbf474-w7q7l 1/1 Running 0 2m
kubernetes-dashboard-7fd68cb5d5-glhmz 1/1 Running 0 2m
วิธีเข้าถึง Kubernetes Dashboard
ตัวเลือกที่ 1: ใช้ kubectl port-forward
วิธีที่ง่ายที่สุดคือใช้ kubectl port-forward:
kubectl port-forward -n kubernetes-dashboard svc/kubernetes-dashboard 8443:443
จากนั้นเปิด URL ในเบราว์เซอร์: https://localhost:8443
ตัวเลือกที่ 2: ใช้ NodePort
แก้ไข Service type เป็น NodePort:
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
เปลี่ยน type: ClusterIP เป็น type: NodePort และ save
ตรวจสอบ port ที่ได้รับการ assign:
kubectl get svc -n kubernetes-dashboard
สร้าง Service Account สำหรับ Dashboard
สำหรับการเข้าถึง Dashboard อย่างปลอดภัย ให้สร้าง Service Account:
kubectl create serviceaccount admin-user -n kubernetes-dashboard
kubectl create clusterrolebinding admin-user --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:admin-user
รับ Access Token
รับ Token สำหรับเข้าถึง Dashboard:
kubectl -n kubernetes-dashboard create token admin-user
คำสั่งนี้จะแสดง Token ที่ใช้สำหรับ login ในหน้า Dashboard
ล็อกอินเข้า Dashboard
เมื่อเปิด Dashboard ท่านจะเห็นหน้า login:
- เลือก “Token” option
- วาง Token ที่ได้จากคำสั่งข้างต้น
- คลิก “Sign in”
การใช้งาน Kubernetes Dashboard
ดูข้อมูล Cluster
ในหน้า Dashboard แสดง Overview ของ cluster:
- Namespaces: จำนวน namespaces และ workloads
- Node Status: สถานะของ nodes ในระบบ
- Pod Status: จำนวน pods ในแต่ละสถานะ
สร้าง Deployment ใหม่
ผ่าน Dashboard ท่านสามารถสร้าง Deployment ใหม่:
- ไปที่ “Deployments” menu
- คลิก “Create” button
- ใส่ข้อมูล deployment (image, replicas, port)
ดู Logs
ดู logs ของ Pod:
- ไปที่ “Pods” หรือ “Workloads”
- เลือก Pod ที่ต้องการ
- คลิก “Logs” เพื่อดู logs in real-time
Best Practices สำหรับ Kubernetes Dashboard
- ใช้ RBAC: สร้าง Service Accounts ที่มี Limited Permissions
- ล็อก Access: ตรวจสอบ audit logs เพื่อเห็นว่าใครเข้าถึง cluster
- ใช้ TLS: เพื่อให้การเชื่อมต่อปลอดภัย
- Restrict Access: จำกัดการเข้าถึง Dashboard เฉพาะ authorized users
- Update รัฐเฉพาะ: ตรวจสอบการ update dashboard เพื่อได้ security patches
Troubleshooting
ปัญหา: Dashboard Pods ไม่ขึ้น
ตรวจสอบ logs:
kubectl logs -n kubernetes-dashboard -l k8s-app=kubernetes-dashboard
ปัญหา: ไม่สามารถเข้าถึง Dashboard
ตรวจสอบ port-forward:
kubectl port-forward -n kubernetes-dashboard svc/kubernetes-dashboard 8443:443 --address 0.0.0.0
ปัญหา: Token หมดอายุ
สร้าง Token ใหม่:
kubectl -n kubernetes-dashboard create token admin-user
สรุป
Kubernetes Dashboard เป็นเครื่องมือสำคัญที่ช่วยให้การบริหารจัดการ Kubernetes Cluster บน Cloud VPS ของท่านง่ายขึ้น ด้วยการติดตั้งอย่างถูกต้องและสร้าง Service Account ที่เหมาะสม ท่านสามารถใช้ Dashboard เพื่อติดตามและจัดการ cluster ได้อย่างมีประสิทธิภาพ
