Monitoring Load Balancer Using Prometheus
monitor การใช้งาน Load Balancer ให้ละเอียดยิ่งขึ้น ด้วยการใช้งาน Prometheus และ Grafana
Prerequisite
มียอดคงเหลือใน Project มากกว่า 0 บาท (ขั้นตอนการเติมเงินเข้าสู่ Project)
มี Load Balancer ที่ต้องการ monitor ด้วย Prometheus และ Grafana
Instructions
ขั้นตอนในการ monitor Load Balancer ด้วย Prometheus และ Grafana แบ่งออกเป็น 3 ขั้นตอน
a. สร้าง Listener ที่มี protocol เป็น Prometheus
b. ติดตั้ง Prometheus
b. ติดตั้ง Grafana
a. สร้าง Prometheus Listener
ขั้นตอนในการสร้าง Listener ที่มี protocol เป็น Prometheus มีดังนี้
1. เข้าหน้า Load Balancer detail
เข้าหน้า Load Balancer detail โดยการ click ที่ชื่อ Load Balancer ในหน้า Load Balancer list

2. เข้าหน้าสร้าง Listener
เปิดหน้าสร้าง Listener จากการ click ปุ่มที่แถบ menu bar

3. สร้าง Listener ที่มี protocol เป็น Prometheus
ในหน้าสร้าง Listener เลือก protocol เป็น Prometheus และเลือก port เป็นเลขที่ต้องการ และ click ปุ่ม CREATE เพื่อ confirm การสร้าง

b. ติดตั้ง Prometheus
ติดตั้ง Prometheus ใน server โดยในตัวอย่างนี้ จะเลือกติดตั้งบน Compute Instance
1. สร้าง directory ใหม่
สร้าง directory ใหม่ที่ path /opt/prometheus-setup เพื่อเก็บ file ที่จะดาวน์โหลด โดยใช้ command
sudo mkdir /opt/prometheus-setup
cd /opt/prometheus-setup
2. สร้าง user ชื่อ prometheus
sudo useradd prometheus
3. ดาวน์โหลด Prometheus server
ใช้ wget ดาวน์โหลด Prometheus server จาก Github ซึ่งควรใช้เป็น version ล่าสุด (ตรง {version} ให้ใส่ version ล่าสุดลงไป)
wget https://github.com/prometheus/prometheus/releases/download/v{version}/prometheus-{version}.linux-amd64.tar.gz
4. เเตกไฟล์ที่ download มา
ใช้ tar เเตกไฟล์ prometheus-{version}.linux-amd64.tar.gz
tar -xvzf prometheus-{version}.linux-amd64.tar.gz
5. จัดการ file และกำหนดสิทธิเข้าถึง
เพิ่มไฟล์ที่เเตกออกมาลงใน path เพื่อให้สามารถเข้าถึงได้ง่าย และ เปลี่ยนการจัดการในไฟล์เเละไดเรกทอรี กับ user prometheus
sudo mv prometheus-{version}.linux-amd64 prometheus
sudo mv prometheus/prometheus /usr/bin/
sudo chown prometheus:prometheus /usr/bin/prometheus
sudo mkdir /etc/prometheus
sudo cp prometheus/prometheus.yml /etc/prometheus/
sudo chown -R prometheus:prometheus /etc/prometheus
sudo chown -R prometheus:prometheus /opt/prometheus-setup
6. ทดสอบ version ของ Prometheus server
ทดสอบการติดตั้ง Prometheus โดยการใช้ command นี้ เพื่อดู version ของ Prometheus
prometheus --version
โดยผลลัพท์ควรจะเป็นดังนี้

7. สร้าง service ของ Prometheus
สร้าง service ของ Prometheus
sudo vi /etc/systemd/system/prometheus.service
จากนั้น add config ดังนี้
[Unit]
Description=Prometheus
[Service]
User=prometheus
ExecStart=/usr/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /opt/prometheus-setup/
[Install]
WantedBy=multi-user.target
8. start service Prometheus
ใช้ systemd daemon เเละ start Prometheus service
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
9. แก้ไข config source ของ Prometheus
เปิด file config
sudo vi /etc/prometheus/prometheus.yml
โดยใช้ config และระบุ IP address ของ Load Balancer และ port ของ Listener ดังนี้
# my global configuration which means it will applicable for all jobs in file
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. scrape_interval should be provided for scraping data from exporters
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. Evaluation interval checks at particular time is there any update on alerting rules or not.
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. Here we will define our rules file path
#rule_files:
# - "node_rules.yml"
# - "db_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape: In the scrape config we can define our job definitions
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'node-exporter'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
# target are the machine on which exporter are running and exposing data at particular port.
static_configs:
- targets: ['{Load Balancer IP address}:{Listener port}']
10. restart Prometheus service
sudo systemctl restart prometheus
11. ตรวจสอบการทำงานของ Prometheus
ใช้ browser เข้าไปดู หน้า homepage ของ Prometheus ที่ URL http://{your_server_ip}:9090 และ ดู metrics ที่มาจาก load balancer ได้

b. ติดตั้ง Grafana
ติดตั้ง Grafana ใน Compute Instance เดียวกันกับ Prometheus
1. update และ upgrade apt
สั่ง update และ upgrade apt ด้วย command นี้
sudo apt update -y && sudo apt upgrade -y
2. ติดตั้ง package ที่จำเป็นของ Grafana
ติดตั้ง package ที่จำเป็นของ Grafana โดยใช้ command นี้
sudo apt install -y apt-transport-https software-properties-common wget
3. ติดตั้ง Grafana GPG
download และ จัดเก็บ Grafana GPG ด้วย command นี้
sudo mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
4. เพิ่ม Grafana APT repository
เพิ่ม Grafana APT repository ด้วย command นี้
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
แล้วต่อด้วย command
sudo apt update
5. ติดตั้ง Grafana
ติดตั้ง Grafana โดยใช้ command นี้
sudo apt install grafana
6. เปิดใช้งาน Grafana
ตรวจสอบ version ของ Grafana ด้วย command นี้
sudo grafana-server -v
เปิดการทำงานของ Grafana ด้วย command นี้
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
7. ตรวจสอบการทำงานของ Grafana service
ตรวจสอบการทำงานของ Grafana service ด้วย command นี้
sudo systemctl status grafana-server
8. เข้าใช้งาน Grafana ผ่าน UI
เข้า URL http://{your_server_ip}:3000 เพื่อเปิด Grafana UI โดยใช้
Username: admin
Password: admin

10. เพิ่ม datasource จาก Prometheus
เพิ่ม datasource จาก Prometheus จาก connections > data sources

เลือก Prometheus และกำหนด Prometheus server URL เป็น http://localhost:9090
11. ปรับแต่ง Grafana dashboard
ปรับแต่ง Grafana dashboard ตามต้องการ

Last updated
Was this helpful?