# Setup

#### Setting up Prometheus

Now that we have the basic understanding of Prometheus,let’s get a Prometheus server up and start scraping some metrics.&#x20;

<figure><img src="https://github.com/zen-class/zen-class-devops-documentation/assets/36299748/7778c253-9567-4812-a4a9-d5bc84c382ab" alt=""><figcaption></figcaption></figure>

#### Installing node\_exporter

Download the Node Exporter on all machines :

```bash
wget https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz
Extract the downloaded archive

tar -xf node_exporter-0.15.2.linux-amd64.tar.gz
Move the node_exporter binary to /usr/local/bin:

sudo mv node_exporter-0.15.2.linux-amd64/node_exporter /usr/local/bin
Remove the residual files with:

rm -r node_exporter-0.15.2.linux-amd64*

```

#### Create users and service files for node\_exporter.

For security reasons, it is always recommended to run any services/daemons in separate accounts of their own. Thus, we are going to create an user account for node\_exporter. We have used the -r flag to indicate it is a system account, and set the default shell to /bin/false using -s to prevent logins.

```bash
sudo useradd -rs /bin/false node_exporter
```

#### Create a systemd unit file so that node\_exporter can be started at boot.

```bash
sudo nano /etc/systemd/system/node_exporter.service
```

```bash
[Unit]
Description=Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
```

Since we have created a new unit file, we must reload the systemd daemon, set the service to always run at boot and start it :

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

#### Installing Prometheus

The next step is to download and install Prometheus only on the Prometheus Server.

```bash
wget https://github.com/prometheus/prometheus/releases/download/v2.1.0/prometheus-2.1.0.linux-amd64.tar.gz
```

#### Extract the Prometheus archive :

```bash
tar -xf prometheus-2.1.0.linux-amd64.tar.gz
```

#### Move the binaries to /usr/local/bin:

```bash
sudo mv prometheus-2.1.0.linux-amd64/prometheus prometheus-2.1.0.linux-amd64/promtool /usr/local/bin
```

#### Create directories for configuration files and other prometheus data.

```bash
sudo mkdir /etc/prometheus /var/lib/prometheus

```

#### Move the configuration files to the directory we made previously:

```bash
sudo mv prometheus-2.1.0.linux-amd64/consoles prometheus-2.1.0.linux-amd64/console_libraries /etc/prometheus
```

#### Delete the leftover files as we do not need them any more:

```bash
rm -r prometheus-2.1.0.linux-amd64*
```

#### Configuring Prometheus

After having installed Prometheus, we have to configure Prometheus to let it know about the HTTP endpoints it should monitor. Prometheus uses the YAML format for its configuration.

Go to /etc/hosts and add the following lines, replace x.x.x.x with the machine’s corresponding IP address

x.x.x.x prometheus-target-1 x.x.x.x prometheus-target-2 We will use /etc/prometheus/prometheus.yml as our configuration file

```bash
global:
  scrape_interval: 10s

scrape_configs:
  - job_name: 'prometheus_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9100','prometheus-target-1:9100','prometheus-target-2:9100']
```

Finally, we will also change the ownership of files that Prometheus will use:

```
sudo useradd -rs /bin/false prometheus
sudo chown -R prometheus: /etc/prometheus /var/lib/prometheus
```

Then, we will create a systemd unit file in /etc/systemd/system/prometheus.service with the following contents :

```bash
[Unit]
Description=Prometheus
After=network.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
```

Finally, we will reload systemd:

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

Prometheus provides a web UI for running basic queries located at http\://\<your\_server\_IP>:9090/. This is how it looks like in a web browser:&#x20;

<figure><img src="https://github.com/zen-class/zen-class-devops-documentation/assets/36299748/35f72835-daf7-4e41-a79d-ed0042f3a166" alt=""><figcaption></figcaption></figure>

Script Setup

1. Run Prometheus.sh for installing Prometheus server

```bash
#!/bin/bash
PROMETHEUS_VERSION="2.52.0"
wget https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz
tar -xzvf prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz
cd prometheus-${PROMETHEUS_VERSION}.linux-amd64/
# if you just want to start prometheus as root
#./prometheus --config.file=prometheus.yml

# create user
useradd --no-create-home --shell /bin/false prometheus 

# create directories
mkdir -p /etc/prometheus
mkdir -p /var/lib/prometheus

# set ownership
chown prometheus:prometheus /etc/prometheus
chown prometheus:prometheus /var/lib/prometheus

# copy binaries
cp prometheus /usr/local/bin/
cp promtool /usr/local/bin/

chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool

# copy config
cp -r consoles /etc/prometheus
cp -r console_libraries /etc/prometheus
cp prometheus.yml /etc/prometheus/prometheus.yml

chown -R prometheus:prometheus /etc/prometheus/consoles
chown -R prometheus:prometheus /etc/prometheus/console_libraries

# setup systemd
echo '[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target' > /etc/systemd/system/prometheus.service

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

```bash
chmod +x prometheus.sh
sudo ./prometheus.sh 
```

{% hint style="info" %}
Only run as root user for this script method installation.
{% endhint %}

2. Install Node\_Exporter

```bash
#!/bin/bash
NODE_EXPORTER_VERSION="1.8.1"
wget https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz
tar -xzvf node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz
cd node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64
cp node_exporter /usr/local/bin

# create user
useradd --no-create-home --shell /bin/false node_exporter

chown node_exporter:node_exporter /usr/local/bin/node_exporter

echo '[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target' > /etc/systemd/system/node_exporter.service

# enable node_exporter in systemctl
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
```

```bash
chmod +x Node.sh
sudo ./Node.sh 
```
