Deploy Grafana and Prometheus via Docker

Grafana and Prometheus are good monitoring tools. With the use of Docker, the deployment of these two gets way easier.

1. Install Docker

Just use the installer you made before.

2. Install Grafana

1
2
3
mkdir -p /data/grafana/storage
chmod 777 /data/grafana/storage
docker run -d -p 3000:3000 --name=grafana --restart=always -v /data/grafana/storage:/var/lib/grafana grafana/grafana

Default username: admin
Default password: admin

3. Install node exporter on the MONITORED SERVER

Remember to update the version number in your code if needed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
tar xzvf node_exporter-1.4.0.linux-amd64.tar.gz
mv node_exporter-1.4.0.linux-amd64 /usr/local/bin/node_exporter

groupadd prometheus
useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus
mkdir /usr/local/prometheus
chown prometheus:prometheus -R /usr/local/prometheus

cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=node_exporter-1.4.0
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/node_exporter/node_exporter --collector.processes --collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($|/)
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl restart node_exporter.service
systemctl enable node_exporter.service

systemctl start node_exporter.service
systemctl status node_exporter

4. Install Prometheus on the GRAFANA SERVER

1
2
3
mkdir -p /data/prometheus
chmod 777 /data/prometheus
vim prometheus.yml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
global:
  scrape_interval:     10s
  evaluation_interval: 10s

scrape_configs:
  - job_name: prometheus
	static_configs:
	  - targets: ['本机ip:9090']
		labels:
		  instance: prometheus
  - job_name: Monitoring
	static_configs:
	  - targets: ['被监控机器1ip:9100']
		labels:
		  instance: 名字1
	  - targets: ['被监控机器1ip:9100']
		labels:
		  instance: 名字2
	  - targets: ['被监控机器1ip:9100']
		labels:
		  instance: 名字3
1
docker run -d --restart=always --name=prometheus -p 9090:9090 -v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --storage.tsdb.retention.time=20d --config.file=/etc/prometheus/prometheus.yml --web.console.libraries=/usr/share/prometheus/console_libraries --web.console.templates=/usr/share/prometheus/consoles

DO NOT put prom/prometheus behind those custom flags like storage.tsdb.retention.time. Doing so will run into an error, because those flags are not generic docker flags. Thus, specifying the docker image first!

Notice if prometheus.yml is changed, the container should be restarted.

1
docker restart {Prometheus container id}

Credits

Chris’ blog on how to install Grafana and Prometheus using Docker

Built with Hugo
Theme Stack designed by Jimmy