Saturday, April 17, 2021

How To install Grafana & Prometheus on Ubuntu 20.04

 


We need fresh an ubuntu srv 20.04 with ssh only, pls prepare him first.
Reference:


1. Install Grafana:

sudo apt-get install -y apt-transport-https

sudo apt-get install -y software-properties-common wget

wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

After you add the repository:

sudo apt-get update

sudo apt-get install grafana

To start the service and verify that the service has started:

sudo systemctl daemon-reload

sudo systemctl start grafana-server

sudo systemctl status grafana-server

sudo systemctl enable grafana-server.service

To log in to Grafana for the first time: 

Open your web browser and go to http://X.X.X.X:3000/  . On the login page, enter admin for username and password ( default: admin / admin )


If login is successful, then you will see a prompt to change the admin's password.


Its time to create first dashboard

Click the + icon on the left panel, select Create Dashboard, and then click Add An Empty panel



Select -- Grafana -- under query tab > click Apply


Fill dashboard name then click save


Disable anonymous access on our Grafana server. Change parameter look like bellow in Grafana configuration file:

[auth.anonymous]
# enable anonymous access
enabled = false

2. Install Prometheus:

You can check version at https://prometheus.io/download/

Download Prometheus:
wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz
Extract Prometheus: 
tar xvzf prometheus-2.26.0.linux-amd64.tar.gz

Create a Prometheus user & Prometheus group.
useradd -rs /bin/false prometheus

Copy prometheus promtool to /usr/local/bin

cd  prometheus-2.26.0.linux-amd64
cp prometheus promtool /usr/local/bin

Set permissions

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

Create folder /etc/prometheus folder for Prometheus and move the console files, console libraries and the prometheus configuration file to this folder.

mkdir /etc/prometheus
cp -R consoles/ console_libraries/ prometheus.yml /etc/prometheus

Create a data folder for prometheus:

mkdir -p /data/prometheus

Set permission on both folders /data/prometheus & /etc/prometheus

 chown -R prometheus:prometheus /data/prometheus /etc/prometheus/*

 Create a new prometheus.service

cd /lib/systemd/system
touch prometheus.service

vi prometheus.service 

then paste parameters bellow into this file:

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]

Type=simple
User=prometheus
Group=prometheus
ExecStart=/usr/local/bin/prometheus \

  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path="/data/prometheus" \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries \
  --web.listen-address=0.0.0.0:9090 \
  --web.enable-admin-api

Restart=always

[Install]
WantedBy=multi-user.target


 Enable at startup, and start Prometheus service.

systemctl enable prometheus
systemctl start prometheus

systemctl status prometheus 

At this time you can access to your prometheus with the link : http://X.X.X.X:9090/ 

 



Go back to your Gafana to add prometheus data source



In configure your Prometheus data source windows, input URL http://localhost:9090 & select Skip TLS Verify then click Save & Test button.

3. Installing and running the node_exporter(all step are the same with prometheus at above):

you can get the download link for your platform at https://prometheus.io/download/

wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
tar xvzf node_exporter-1.1.2.linux-amd64.tar.gz
cd node_exporter-1.1.2.linux-amd64
cp node_exporter /usr/local/bin
useradd -rs /bin/false node_exporter
chown node_exporter:node_exporter /usr/local/bin/node_exporter
cd /lib/systemd/system
touch node_exporter.service
vi node_exporter.service 
then paste parameter to your node_exporter.service

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

[Service]
Type=simple
User=node_exporter
Group=node_exporter
ExecStart=/usr/local/bin/node_exporter \
    --collector.mountstats \
    --collector.logind \
    --collector.processes \
    --collector.ntp \
    --collector.systemd \
    --collector.tcpstat \
    --collector.wifi

Restart=always

[Install]
WantedBy=multi-user.target

 


Enable node exporter:

systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter

then check everything is ok:

journalctl -f -u node_exporter.service
curl http://localhost:9100/metrics
curl http://localhost:9100/metrics | grep "node_"

Add 'localhost:9100' to Prometheus configuration file

vi /etc/prometheus/prometheus.yml
systemctl restart prometheus


 at this moment, you can see http://localhost:9100/metrics is UP when access to http://X.X.X.X:9090/targets 

4. Import Grafana Dashboard to our server:

Access to grafana at http://X.X.X.X:3000 > Click Menu + > select Import


Input id 10242 then click Load button


Select Prometheus data source then click Import


Great, now you can play with a ton of data visualization :) 


Thansk you :) .

No comments:

Post a Comment

Windows Server 2019 - LAB08 Implementing storage solutions (Part3) Implement Storage Spaces Direct

Part 3:  Implement Storage Spaces Direct What is Storage Spaces Direct? Storage Spaces Direct is the evolution of Storage Spaces . It lever...