> For the complete documentation index, see [llms.txt](https://docs-epc.gitbook.io/ncs-documents/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-epc.gitbook.io/ncs-documents/load-balancing/load-balancer/monitoring-load-balancer-using-prometheus.md).

# Monitoring Load Balancer Using Prometheus

## Prerequisite

* มียอดคงเหลือใน Project มากกว่า 0 บาท ([ขั้นตอนการเติมเงินเข้าสู่ Project](/ncs-documents/nipa-cloud-space-overview/billing-and-wallet/topup-a-project-wallet.md))
* มี Load Balancer ที่ต้องการ monitor ด้วย Prometheus และ Grafana

## Instructions

ขั้นตอนในการ monitor Load Balancer ด้วย Prometheus และ Grafana แบ่งออกเป็น 3 ขั้นตอน&#x20;

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

<figure><img src="/files/DRo5x0Ng80gus1TituZN" alt=""><figcaption><p>Load Balancer list</p></figcaption></figure>

#### 2. เข้าหน้าสร้าง Listener

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

<figure><img src="/files/l3xwzBFZ8lF22pemHPwP" alt=""><figcaption><p>Load Balancer detail</p></figcaption></figure>

#### 3. สร้าง Listener ที่มี protocol เป็น Prometheus

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

<figure><img src="/files/aJBXgClNY8igCwfJJROO" alt=""><figcaption><p>Load Balancer - Listener list</p></figcaption></figure>

### 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](https://github.com/prometheus/) ซึ่งควรใช้เป็น [version](https://prometheus.io/download/) ล่าสุด (ตรง {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
```

โดยผลลัพท์ควรจะเป็นดังนี้

<figure><img src="/files/PtMXcGStnsfzPSByoUgU" alt=""><figcaption></figcaption></figure>

#### 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&#x20;

ใช้ browser เข้าไปดู หน้า homepage ของ Prometheus ที่ URL http\://{your\_server\_ip}:9090 และ ดู metrics ที่มาจาก load balancer ได้

<figure><img src="/files/AsvcLic5V6vBJEKRBAXi" alt=""><figcaption></figcaption></figure>

### 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 โดยใช้&#x20;

Username: admin

Password: admin

<figure><img src="/files/neHAE8gJhjrfnZDzSFGn" alt=""><figcaption></figcaption></figure>

#### 10. เพิ่ม datasource จาก Prometheus

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

<figure><img src="/files/ydRkioN05wNebVyTLohE" alt=""><figcaption></figcaption></figure>

เลือก Prometheus และกำหนด Prometheus server URL เป็น <http://localhost:9090>

#### 11. ปรับแต่ง Grafana dashboard

ปรับแต่ง Grafana dashboard ตามต้องการ<br>

<figure><img src="/files/9XR7YSH3OOH5GWYbwkDa" alt=""><figcaption></figcaption></figure>
