1. Machine grafana/Promteheus
- hostmachine : Node-gpsql
- IP : 192.168.1.113/24
- OS : Rocky Linux 8.5
- RAM : 2Go
- CPU : 1
2. Mise à jour
[root@node-gpsql ~]# dnf update -y
3. Ajout des hosts PosgreSQL
[root@node-gpsql ~]# echo "192.168.1.103 node-psql01" >> /etc/hosts [root@node-gpsql ~]# echo "192.168.1.104 node-psql02" >> /etc/hosts [root@node-gpsql ~]# echo "192.168.1.104 node-psql03" >> /etc/hosts [root@node-gpsql ~]# echo "192.168.1.113 node-pgsql" >> /etc/hosts
4. Désactiver SELinux
[root@node-gpsql ~]# setenforce 0 [root@node-gpsql ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
5. Synchroniser DateTime sur le fuseau de PARIS
[root@node-gpsql ~]# timedatectl set-timezone Europe/Paris
6. Installation Grafana
Installation repository
[root@node-gpsql ~]# vi /etc/yum.repos.d/grafana.repo [grafana] name=grafana baseurl=https://packages.grafana.com/oss/rpm repo_gpgcheck=1 enabled=1 gpgcheck=1 gpgkey=https://packages.grafana.com/gpg.key sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt
[root@node-gpsql ~]# dnf update -y
Installation grafana
[root@node-gpsql ~]# dnf install grafana -y
Start les service grafana
[root@node-gpsql ~]# systemctl start grafana-server [root@node-gpsql ~]# systemctl enable grafana-server [root@node-gpsql ~]# systemctl status grafana-server

Règle Firewall
[root@node-gpsql ~]# firewall-cmd --add-port=3000/tcp --permanent && firewall-cmd --reload
Accès Grafana

- login : admin
- password : admin

7. Installation prometheus
Création user
[root@node-gpsql ~]# adduser -M -r -s /sbin/nologin prometheus
Création structure
[root@node-gpsql ~]# mkdir /etc/prometheus [root@node-gpsql ~]# mkdir /var/lib/prometheus
[root@node-gpsql ~]# chown prometheus:prometheus /etc/prometheus [root@node-gpsql ~]# chown prometheus:prometheus /var/lib/prometheus
Récupération des sources


[root@node-gpsql ~]# cd /tmp [root@node-gpsql tmp]# dnf install wget tar -y [root@node-gpsql tmp]# wget https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz [root@node-gsql tmp]# tar xzvf prometheus-2.42.0.linux-amd64.tar.gz
Copier les binaires sur la structure Prometheus
[root@node-gpsql tmp]# cp prometheus-2.42.0.linux-amd64/prometheus /usr/local/bin/ [root@node-gsql tmp]# cp prometheus-2.42.0.linux-amd64/promtool /usr/local/bin/
[root@node-gpsql tmp]# chown prometheus:prometheus /usr/local/bin/prometheus [root@node-gsql tmp]# chown prometheus:prometheus /usr/local/bin/promtool
Copier les fichiers conf sur la structure Prometheus
[root@node-gpsql tmp]# cp -r prometheus-2.42.0.linux-amd64/consoles /etc/prometheus [root@node-gpsql tmp]# cp -r prometheus-2.42.0.linux-amd64/console_libraries /etc/prometheus
[root@node-gpsql tmp]# chown -R prometheus:prometheus /etc/prometheus/consoles [root@node-gpsql tmp]# chown -R prometheus:prometheus /etc/prometheus/console_libraries
Création du service Prometheus
[root@node-gsql tmp]# vi /etc/systemd/system/prometheus.service [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
[root@node-gsql tmp]# systemctl daemon-reload
8. Installation exporter PostgreSQL (Sur les nodes postgreSQL)
Installation prérequis (3 nœuds postgreSQL)
[root@node-psql0x ~]# yum -y install wget tar curl git
Récupération exporter postgreSQL (3 nœuds postgreSQL)
[root@node-psql0x ~]# cd /home [root@node-psql0x home]# git clone https://github.com/prometheus-community/postgres_exporter.git
Installation exporter postgreSQL (3 nœuds postgreSQL)
Installation go

[root@node-psql0x home]# wget https://go.dev/dl/go1.20.2.linux-amd64.tar.gz [root@node-psql0x home]# tar -zxvf go1.20.2.linux-amd64.tar.gz -C /usr/local/
[root@node-psql0x home]# echo 'export GOROOT=/usr/local/go' | sudo tee -a /etc/profile export GOROOT=/usr/local/go [root@node-psql0x home]# echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile export PATH=$PATH:/usr/local/go/bin [root@node-psql0x home]# source /etc/profile
[root@node-psql0x home]# go version go version go1.20.2 linux/amd64

Compiler « postgres_esporter »
[root@node-psql0x home]# cd postgres_exporter/ [root@node-psql0x postgres_exporter]# make build

Copier le binaire « postgres_esporter » sur /usr/local/bin
[root@node-psql0x postgres_exporter]# ls -al |grep postgres_exporter

[root@node-psql0x postgres_exporter]# cp postgres_exporter /usr/local/bin
Configurer exporter postgreSQL (3 nœuds postgreSQL)
- User : chris
- Password : chris2023
- Dbase : chris2023
[root@node-psql0x postgres_exporter]# vi postgres_exporter.env DATA_SOURCE_NAME="postgresql://chris:chris2023@localhost:5432/chris2023?sslmode=disable"
Configurer le service exporter postgreSQL (3 nœuds postgreSQL)
Création utilisateur et droits postgres_exporter
[root@node-psql0x postgres_exporter]# adduser -M -r -s /sbin/nologin postgres_exporter [root@node-psql0x postgres_exporter]# chown -R postgres_exporter:postgres_exporter /home/postgres_exporter
Création du service exporter
[root@node-psql01 postgres_exporter]# vi /etc/systemd/system/postgres_exporter.service [Unit] Description=Prometheus exporter for Postgresql Wants=network-online.target After=network-online.target [Service] User=postgres_exporter Group=postgres_exporter WorkingDirectory=/home/postgres_exporter EnvironmentFile=/home/postgres_exporter/postgres_exporter.env ExecStart=/usr/local/bin/postgres_exporter --web.listen-address=node-psql0x:9187 --web.telemetry-path=/metrics Restart=always [Install] WantedBy=multi-user.target
Démarrage du service exporter
[root@node-psql0x postgres_exporter]# systemctl daemon-reload [root@node-psql0x postgres_exporter]# systemctl start postgres_exporter && systemctl enable postgres_exporter
[root@node-psql01 postgres_exporter]# systemctl status postgres_exporter

[root@node-psql01 postgres_exporter]# netstat -antp |grep 9187

Ouverture port firewall pour l’exporter
[root@node-psql01 postgres_exporter]# firewall-cmd --add-port=9187/tcp --permanent && firewall-cmd --reload
Check connexion aux metrics postgreSQL
- http://IP_node-psqlx:9187/metrics

9. Configurer prometheus ⬄ Cluster postgreSQL
[root@node-gpsql tmp]#vi /etc/prometheus/prometheus.yml global: scrape_interval: 10s scrape_configs: - job_name: postgreSQL scrape_interval: 5s static_configs: - targets: ['node-psql01:9187'] - targets: ['node-psql02:9187'] - targets: ['node-psql03:9187']

[root@node-gpsql tmp]# promtool check config /etc/prometheus/prometheus.yml

Démarrage du service Promotheus
[root@node-gpsql tmp]# systemctl start prometheus && systemctl enable prometheus
[root@node-gpsql tmp]# systemctl status prometheus

Régle firewall
[root@node-gpsql ~]# firewall-cmd --zone=public --add-port=9090/tcp --permanent [root@node-gpsql ~]# firewall-cmd --reload
Connexion au service Prometheus

« Status » puis « Targets »

Le Cluster postgreSQL est bien présent et « Up » sur Promotheus.
10. Interfacer prometheus / Grafana

« DATA SOURCES »

Choisir « Prometheus»

Dans URL préciser l’adresse http de la base Prometheus.
Ajouter un dashboard

« DASHBOARD » puis « + Import »
https://grafana.com/grafana/dashboards/?search=postgres
- ID : 9628
- ID : 455
- ID : 3742
- ID : 4164
« Ajouter l’ID souhaité » puis « Load »

« Import »
Générer du trafic SQL/PostgreSQL
Prérequis
[root@node-gsql ~]# dnf -y install postgresql python3-psycopg2 python3
[root@node-gsql ~]# cd /home [root@node-gsql home]# echo "192.168.1.110 node-sql" >> /etc/hosts
Récupération du script
[root@node-gsql home]# curl -LO https://raw.githubusercontent.com/jobinau/pgscripts/main/patroni/HAtester.py

[root@node-gsql home]# chmod -x HAtester.py
Configuration du script
[root@node-gsql home]# vi HAtester.py # CONNECTION DETAILS host = "node-sql" dbname = "chris2023" user = "chris" password = "chris2023"
Prérequis du script
[root@node-gsql home]# psql -U chris -h node-sql -p 5000 -d chris2023 -t -c "CREATE TABLE HATEST (TM TIMESTAMP);" [root@node-gsql home]# psql -U chris -h node-sql -p 5000 -d chris2023 -t -c "CREATE UNIQUE INDEX idx_hatext ON hatest (tm desc);"

Générer trafic « MASTER »
[root@node-gpsql home]# ./HAtester.py 5000

Générer trafic « REPLICA »
[root@node-gsql home]# ./HAtester.py 5001

Monitoring Grafana – Cluster postgreSQL

Check des hosts monitorés

Check des bases monitorées
Sur 1 heures

Node-sql01

Node-sql02

Node-sql03


Node-sql01

Node-sql02

Node-sql02
Sur 12heures


Node-psql01


Node-psql02

Node-psql03
Views: 3
Node-psql03