> 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/public-api/getting-start-with-ncs-public-api/auto-scaling-openstack-instances-with-senlin-and-prometheus/installation-prometheus.md).

# Installation Prometheus

* สร้าง directory ใหม่ที่ path **/opt/prometheus-setup** เพื่อเก็บ file ที่ดาวน์โหลดมา

{% code lineNumbers="true" %}

```bash
sudo mkdir /opt/prometheus-setup
cd /opt/prometheus-setup
```

{% endcode %}

* สร้าง User ชื่อ prometheus

<pre class="language-bash"><code class="lang-bash"><strong>sudo useradd prometheus
</strong></code></pre>

* ใช้ wget ดาวน์โหลด Prometheus server จาก [Github](https://github.com/prometheus/) ซึ่งควรใช้เป็น [version](https://prometheus.io/download/) ล่าสุด (ตรง {version} ให้ใส่ version ล่าสุดลงไป)

```bash
wget https://github.com/prometheus/prometheus/releases/download/v_version_/prometheus-{version}.linux-amd64.tar.gz 
```

* ใช้ tar เเตกไฟล์ prometheus-{version}.linux-amd64.tar.gz

```bash
tar -xvzf prometheus-{version}.linux-amd64.tar.gz
```

* เพิ่มไฟล์ที่เเตกออกมาลงใน path เพื่อให้สามารถเข้าถึงได้ง่าย และ เปลี่ยนการจัดการในไฟล์เเละไดเรกทอรี กับ user prometheus

{% code overflow="wrap" lineNumbers="true" %}

```bash
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
```

{% endcode %}

* ลองเช็ค version Prometheus

```bash
prometheus --version
```

จะได้ผลลัพธ์ดังนี้

<figure><img src="/files/2XOPJpMj7LTXJbE98HV7" alt=""><figcaption></figcaption></figure>

### สร้าง service ของ Prometheus

* สร้าง service ของ prometheus&#x20;

```
sudo vi /etc/systemd/system/prometheus.service
```

จากนั้น add config ดังนี้

```yaml
[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
```

* ใช้ systemd daemon เเละ start Prometheus service

{% code lineNumbers="true" %}

```bash
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus
```

{% endcode %}

เมื่อติดตั้ง Prometheus เสร็จเเล้วเราสามารถทดสอบใช้งานได้จากหัวข้อ **Test usage**

## Test usage (optional)

ในหัวข้อนี้จะเป็นการทดสอบใช้งานเท่านั้น สามารถทำได้เเต่ไม่จำเป็นต้องทำก็ได้

### ติดตั้ง Node Exporter

* ใช้ wget ดาวโหลด node exporter ซึ่งควรใช้เป็น [version](https://prometheus.io/download/) ล่าสุด ([github](https://github.com/prometheus/node_exporter))

```bash
wget https://github.com/prometheus/node_exporter/releases/download/v_version_/node_exporter-{version}.linux-amd64.tar.gz
```

* ใช้ tar เเตกไฟล์ node\_exporter-version.linux-amd64.tar.gz เเละใช้ mv ย้ายไฟล์

{% code lineNumbers="true" %}

```bash
tar -xvzf node_exporter-{version}.linux-amd64.tar.gz
sudo mv node_exporter-{version}.linux-amd64 node-exporter
```

{% endcode %}

* ย้ายไฟล์ไปที่ **/usr/bin/**

```bash
sudo mv node-exporter/node_exporter /usr/bin/
```

* สร้าง service node exporter

```
sudo vi /etc/systemd/system/node_exporter.service
```

จากนั้น add config ดังนี้&#x20;

```yaml
[Unit]
Description=Node Exporter

[Service]
User=prometheus
ExecStart=/usr/bin/node_exporter

[Install]
WantedBy=default.target
```

* start node exporter service รวมถึงเช็ค status

{% code lineNumbers="true" %}

```bash
sudo systemctl daemon-reload
sudo systemctl enable node_exporter.service
sudo systemctl start node_exporter.service
```

{% endcode %}

### Start Prometheus Server with a new node

เข้าไปเเก้ config ของ prometheus&#x20;

```bash
sudo vi /etc/prometheus/prometheus.yml
```

โดยใช้ config ดังนี้

```yaml
# 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: ['localhost:9100']
```

* Restart service prometheus

```bash
sudo systemctl restart prometheus
```

* ใช้ browser เข้าไปดู หน้า homepage ของ Prometheus ที่ <http://your\\_server\\_ip:9090> และ ดู metrics ที่มาจาก node exporter ของ Prometheus ได้ที่ <http://your\\_server\\_ip:9100>

\
CREDIT: <https://iamabhishek-dubey.medium.com/prometheus-overview-and-setup-dc0ee20791fb>
