ติดตั้ง Monitoring Stack (Prometheus + Grafana) บน Kubernetes

ทำไม Monitoring สำคัญสำหรับ Kubernetes

การจัดการ Kubernetes Cluster ที่มีจำนวน Pod และ Node มากมายต้องมีระบบ Monitoring ที่แข็งแอ่ง เพื่อติดตามสถานะของระบบ ประสิทธิภาพการทำงาน และจัดการปัญหาได้อย่างทันที่หมุ เมื่อใช้บริการ Cloud VPS ของ ผู้ให้บริการโฮสติ้ง ที่รองรับ Kubernetes คุณสามารถติดตั้ง Stack Monitoring ได้อย่างสะดวก

บทความนี้จะแนะนำการติดตั้ง Prometheus และ Grafana บน Kubernetes ด้วย Helm Chart ที่มีความสมบูรณ์ เพื่อใหคุณมีระบบ Monitoring ที่ครอบคลุม

Prometheus คืออะไร

Prometheus เป็นระบบเก็บข้อมูล Time Series Database ที่ออกแบบมาเพื่อ Monitoring ที่มีความยืดหยุ่นสูง ข้อดีของ Prometheus ได้แก่:

  • เก็บเมตริกส์เป็น Time Series Data ที่ใช้เวลาเป็นตัวชี
  • มี Query Language (PromQL) ที่ทรงพลัง
  • มีระบบ Alerting ที่สามารถตั้งค่า Rule ได้อย่างยืดหยุ่น
  • รองรับ Service Discovery ของ Kubernetes โดยอัตโนมัติ
  • Lightweight และใช้ทรัพยากรน้อย

Grafana คืออะไร

Grafana เป็นแพลตฟอร์มสำหรับสร้าง Dashboard และ Visualization ที่มีความสามารถสูง ข้อดีของ Grafana ได้แก่:

  • สร้าง Dashboard ที่สวยงามและสัญชาตญานสืง
  • สนับสนูน Data Source หลากหลาย (Prometheus, Elasticsearch, InfluxDB เป็นต้น)
  • มี Alerting ที่ส่งการแจ้งเตือนผ่าน Email, Slack, PagerDuty และอื่นๆ
  • สามารถแช่ร Dashboard และสร้าง Report ได้อย่างง่าย

ติดตั้ง Prometheus + Grafana ด้วย Helm

การติดตั้งด้วย Helm Chart เป็นวิธีที่แนะนำมากที่สุด เนื่องจากมีการกำหนดค่าไว้ล่วงหน้าอย่างเหมาะสม

ขั้นตอนที่ 1: เพิ่ม Prometheus Community Helm Repository

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

ขั้นตอนที่ 2: สร้าง Namespace

kubectl create namespace monitoring

ขั้นตอนที่ 3: สร้าง Values File สำหรับ Kube-Prometheus-Stack

cat > prometheus-values.yaml << 'EOF'
prometheus:
  prometheusSpec:
    retention: 30d
    storageSpec:
      volumeClaimTemplate:
        spec:
          accessModes: ["ReadWriteOnce"]
          resources:
            requests:
              storage: 50Gi
    resources:
      requests:
        cpu: 500m
        memory: 2Gi

grafana:
  enabled: true
  adminPassword: "your-secure-password"
  persistence:
    enabled: true
    size: 10Gi
  datasources:
    datasources.yaml:
      apiVersion: 1
      datasources:
      - name: Prometheus
        type: prometheus
        url: http://prometheus-operated:9090
        access: proxy
        isDefault: true
EOF

ขั้นตอนที่ 4: ติดตั้ง Kube-Prometheus-Stack

helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  -f prometheus-values.yaml

ขั้นตอนที่ 5: ตรวจสอบการติดตั้ง

kubectl get pods -n monitoring
kubectl get svc -n monitoring

เข้าถึง Grafana Dashboard

หลังจากติดตั้งเสร็จ คุณสามารถเข้าถึง Grafana โดยใช้ Port Forward:

kubectl port-forward -n monitoring svc/kube-prometheus-stack-grafana 3000:80

จากนั้นเปิด http://localhost:3000 ด้วย Username "admin" และ Password ที่คุณตั้งไว้

กำหนดค่า Alert Rules

Prometheus จะ Monitor Node CPU, Memory, และ Disk Space โดยอัตโนมัติ แต่คุณสามารถสร้าง Custom Alert Rules:

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: custom-alerts
  namespace: monitoring
spec:
  groups:
  - name: kubernetes.rules
    interval: 30s
    rules:
    - alert: HighPodCPU
      expr: |
        rate(container_cpu_usage_seconds_total[5m]) > 0.8
      for: 5m
      annotations:
        summary: "High CPU usage on pod {{ $labels.pod }}"

Best Practices สำหรับ Monitoring Kubernetes

  • ตั้งค่า Retention Policy: กำหนดระยะเวลาเก็บข้อมูล Metrics เพื่อประหยัด Storage
  • สร้าง Alert ที่มีความหมาย: Alert ควรมี Action Plan ชัดเจน เพื่อให DevOps Team รู้จะทำอะไร
  • ใช้ Grafana Dashboard Templates: Community มีหลาย Dashboard Template ที่สามารถนำมาใช้ได้ทันที
  • Monitor ที่สำคัญ: เน้นชอนการ Monitor ที่เกี่ยวิเสาะหท้แอปลิเคชันหรือ SLA ของแอปพลิเคชัน
  • ทดสอบ Alert: ทำการทดสอบ Alert Rule เพื่อใหแน่ใจว่าการแจ้งเตือทำงานได้ถูกต้อง

Troubleshooting Monitoring Issues

หากเกิดปัญหาต่อ Monitoring Stack ลองตรวจสอบเหล่านี้:

  • Prometheus ไม่ Scrape Metrics: ตรวจสอบ Service Monitor Configuration และ Network Policy
  • Grafana ไม่สามารถเชื่อมต่อ Prometheus: ตรวจสอบ DNS Resolution และ Network Connectivity
  • Alert ไม่ส่งไปยัง Slack: ตรวจสอบ Alertmanager Configuration และ Webhook URL

สรุป

การติดตั้ง Prometheus และ Grafana บน Kubernetes ด้วย Helm Chart เป็นวิธีที่รวดเร็วและมีประสิทธิภาพ ระบบ Monitoring นี้จะช่วยใหคุณสามารถติดตามสถานะของ Cluster ได้อย่างชาติแลาชัด ตรวจจับปัญหาได้อย่างรวดเร็ว และตัดสินใจการ Scale Out/In ได้อย่างมีข้อมูล

บริการ Cloud VPS จาก ผู้ให้บริการโฮสติ้ง รองรับการติดตั้ง Kubernetes Cluster ที่มี Resource เพิยงพอต้องสำหรับการรัน Monitoring Stack ขนาดใหญ่ หากต้องการความช่วยเหลือในการ Setup Monitoring หรือ Optimization ยินดีใหบริการ