Monitor Cost บน GKE Cluster ง่าย ๆ ด้วย Kubecost
แนะนำ Tool น่าสนใจตัวนึงที่สามารถแจง Cost ของส่วนต่าง ๆ ของ Workload บน Kubernetes ซึ่งสามารถแนะนำและ predict issue ต่าง ๆ บน Cluster ได้อีกด้วย ซึ่งมีประโยชน์มากในการวิเคราะห์การใช้งานและลดค่าใช้จ่ายของ Cluster
สิ่งที่ต้องเตรียม:
- GKE Cluster ที่ใช้งานอยู่
- kubectl : รายละเอียดการติดตั้ง https://kubernetes.io/docs/tasks/tools/install-kubectl/
- helm client : รายละเอียดการติดตั้ง https://helm.sh/docs/using_helm/
ขั้นตอนการติดตั้ง:
- สร้าง Service Account สำหรับ helm
ตั้งเป็น cluster-admin role เพื่อให้ tiller pod ต่อกับ kubernetes api ได้
kubectl apply -f ไฟล์ yaml ตามข้างล่าง
จากนั้น RUN
helm init — service-account helm
แล้วเช็คความเรียบร้อยโดยดูว่ามี deployment และ service ของ tiller-deploy ใน kube-system namespace หรือไม่
2. เปิด Billing API ของ Google Cloud
ไปที่หน้า Console ของ Google Cloud > เลือก navigation menu ที่มุมซ้ายบน > APIs & Services > Library > search แล้วเลือกไปที่ Cloud Billing API แล้วทำการ enable
3. ติดตั้ง Kubecost ผ่าน helm
เพิ่ม helm repo
helm repo add kubecost https://kubecost.github.io/cost-analyzer/
Install kubecost
โดย default จะลงทั้ง Prometheus กับ Grafana ด้วย กรณีจะลงแบบ custom สามารถเช็ค option ได้ที่ https://github.com/kubecost/cost-analyzer-helm-chart ตัวอย่าง เช่น ใช้กับ Prometheus ที่ลงไว้อยู่แล้ว
ตรง token ด้านล่างเช็คจากเว็บหลัก https://kubecost.com
helm install kubecost/cost-analyzer — namespace kubecost — name kubecost — set kubecostToken=<token>
4. ใช้งาน Dashboard
สามารถใช้วิธี port-forward มาที่ localhost ด้วยคำสั่ง
kubectl port-forward — namespace kubecost deployment/kubecost-cost-analyzer 9090
หรือจะ expose service ผ่าน ingress หรือ nodeport เพื่อให้ใช้งานจาก public ก็ได้