selfhosted-apps-docker/prometheus_grafana/readme.md

372 lines
11 KiB
Markdown
Raw Normal View History

2020-04-26 00:48:03 +00:00
# Prometheus+Grafana in docker
2020-05-18 22:49:18 +00:00
###### guide-by-example
2020-04-26 00:48:03 +00:00
![logo](https://i.imgur.com/e03aF8d.png)
2020-04-26 23:30:52 +00:00
# Purpose
2020-04-26 00:48:03 +00:00
Monitoring of the host and the running cointaners.
* [Official site](https://prometheus.io/)
* [Github](https://github.com/prometheus)
* [DockerHub](https://hub.docker.com/r/prom/prometheus/)
2020-07-11 18:24:40 +00:00
[Good youtube overview](https://youtu.be/h4Sl21AKiDg) of Prometheus.</br>
2020-04-27 20:25:06 +00:00
Everything here is based on the magnificent
2020-05-10 16:51:30 +00:00
[stefanprodan/dockprom](https://github.com/stefanprodan/dockprom),</br>
So maybe just go get that.
---
Prometheus is an open source system application used for monitoring and alerting.
It collects metrics from configured targets at given intervals,
2020-07-11 18:24:40 +00:00
exposes collected metrics for visualization, evaluates rule expressions,
2020-05-10 16:51:30 +00:00
and can trigger alerts if some condition is observed to be true.
Prometheus is relatively new project, it is a **pull type** monitoring
and consists of several components.
* **Prometheus Server** is the core of the system, responsible for
* pulling new metrics
* storing the metrics in a database and evaluating them
* making metrics available through PromQL API
* **Targets** - machines, services, applications that are monitored.</br>
2020-07-11 18:24:40 +00:00
These need to have an **exporter**.
2020-05-10 17:10:36 +00:00
* **exporter** - a script or a service that gathers metrics on the target,
2020-07-11 18:24:40 +00:00
converts them to prometheus server format,
2020-05-10 16:51:30 +00:00
and exposes them at an endpoint so they can be pulled
* **AlertManager** - responsible for handling alerts from Prometheus Server,
2020-05-10 17:10:36 +00:00
and sending notifications through email, slack, pushover,..
2020-05-10 16:51:30 +00:00
* **pushgateway** - allows push type of monitoring.
2020-05-10 17:10:36 +00:00
Should not be overused as it goes against the pull philosophy of prometheus.
Most commonly it is used to collect data from batch jobs, or from services
that have short execution time. Like a backup script.
2020-05-10 16:51:30 +00:00
* **Grafana** - for web UI visualization of the collected metrics
[glossary](https://prometheus.io/docs/introduction/glossary/)
![prometheus components](https://i.imgur.com/AxJCg8C.png)
2020-04-26 00:48:03 +00:00
2020-04-26 23:30:52 +00:00
# Files and directory structure
2020-04-26 00:48:03 +00:00
```
2020-05-01 09:38:43 +00:00
/home/
└── ~/
└── docker/
└── prometheus/
2020-04-26 00:48:03 +00:00
2020-05-01 09:38:43 +00:00
├── grafana/
│ └── provisioning/
│ ├── dashboards/
│ │ ├── dashboard.yml
│ │ ├── docker_host.json
│ │ ├── docker_containers.json
│ │ └── monitor_services.json
2020-04-26 00:48:03 +00:00
│ │
2020-05-01 09:38:43 +00:00
│ └── datasources/
│ └── datasource.yml
2020-04-26 00:48:03 +00:00
2020-05-01 09:38:43 +00:00
├── grafana-data/
├── prometheus-data/
2020-04-26 00:48:03 +00:00
2020-05-01 09:38:43 +00:00
├── .env
├── docker-compose.yml
└── prometheus.yml
2020-04-26 00:48:03 +00:00
```
2020-05-10 17:10:36 +00:00
* `grafana/` - a directory containing grafanas configs and dashboards
* `grafana-data/` - a directory where grafana stores its data
* `prometheus-data/` - a directory where prometheus stores its database and data
2020-05-22 16:05:03 +00:00
* `.env` - a file containing environment variables for docker compose
2020-05-22 16:22:45 +00:00
* `docker-compose.yml` - a docker compose file, telling docker how to run the containers
2020-05-10 17:10:36 +00:00
* `prometheus.yml` - a configuration file for prometheus
2020-05-10 21:48:51 +00:00
All files must be provided.</br>
As well as `grafana` directory and its subdirectories and files.
2020-05-10 17:10:36 +00:00
the directories `grafana-data` and `prometheus-data` are created
by docker compose on the first run.
2020-04-26 23:30:52 +00:00
# docker-compose
2020-04-26 00:48:03 +00:00
2020-04-27 21:01:48 +00:00
Four containers to spin up.</br>
2020-05-10 16:51:30 +00:00
While [stefanprodan/dockprom](https://github.com/stefanprodan/dockprom)
2020-05-10 17:10:36 +00:00
also got alertmanager and pushgateway, this is a simpler setup for now.</br>
Just want pretty graphs.
2020-05-10 16:51:30 +00:00
* **Prometheus** - prometheus server, pulling, storing, evaluating metrics
* **Grafana** - web UI visualization of the collected metrics
in nice dashboards
* **NodeExporter** - an exporter for linux machines,
in this case gathering the metrics of the linux machine runnig docker,
2020-04-27 21:01:48 +00:00
like uptime, cpu load, memory use, network bandwidth use, disk space,...
2020-05-10 16:51:30 +00:00
* **cAdvisor** - exporter for gathering docker **containers** metrics,
2020-04-27 21:01:48 +00:00
showing cpu, memory, network use of each container
2020-04-26 00:48:03 +00:00
`docker-compose.yml`
```yml
version: '3'
services:
# MONITORING SYSTEM AND THE METRICS DATABASE
prometheus:
image: prom/prometheus
container_name: prometheus
hostname: prometheus
restart: unless-stopped
user: root
depends_on:
- cadvisor
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--storage.tsdb.retention.time=200h'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.enable-lifecycle'
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus_data:/prometheus
labels:
org.label-schema.group: "monitoring"
# WEB BASED UI VISUALISATION OF THE METRICS
grafana:
image: grafana/grafana
container_name: grafana
hostname: grafana
restart: unless-stopped
user: root
environment:
- GF_SECURITY_ADMIN_USER
- GF_SECURITY_ADMIN_PASSWORD
- GF_USERS_ALLOW_SIGN_UP
volumes:
- ./grafana_data:/var/lib/grafana
- ./grafana/provisioning:/etc/grafana/provisioning
labels:
org.label-schema.group: "monitoring"
# HOSTS METRICS COLLECTOR
nodeexporter:
image: prom/node-exporter
container_name: nodeexporter
hostname: nodeexporter
restart: unless-stopped
command:
- '--path.procfs=/host/proc'
- '--path.rootfs=/rootfs'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
labels:
org.label-schema.group: "monitoring"
# DOCKER CONTAINERS METRICS COLLECTOR
cadvisor:
image: google/cadvisor
container_name: cadvisor
hostname: cadvisor
restart: unless-stopped
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker:/var/lib/docker:ro
- /cgroup:/cgroup:ro
labels:
org.label-schema.group: "monitoring"
networks:
default:
external:
2020-05-20 18:29:12 +00:00
name: $DOCKER_MY_NETWORK
2020-04-26 00:48:03 +00:00
```
`.env`
```bash
# GENERAL
2020-05-16 13:18:21 +00:00
MY_DOMAIN=example.com
2020-05-20 18:29:12 +00:00
DOCKER_MY_NETWORK=caddy_net
2020-05-02 20:48:23 +00:00
TZ=Europe/Bratislava
2020-04-26 00:48:03 +00:00
# GRAFANA
GF_SECURITY_ADMIN_USER=admin
GF_SECURITY_ADMIN_PASSWORD=admin
GF_USERS_ALLOW_SIGN_UP=false
```
**All containers must be on the same network**.</br>
2020-05-10 16:51:30 +00:00
Which is named in the `.env` file.</br>
2020-04-26 00:48:03 +00:00
If one does not exist yet: `docker network create caddy_net`
2020-05-10 16:51:30 +00:00
# Prometheus configuration
2020-04-27 20:25:06 +00:00
2020-04-26 23:30:52 +00:00
#### prometheus.yml
2020-05-10 16:51:30 +00:00
* /prometheus/**prometheus.yml**
[Official documentation.](https://prometheus.io/docs/prometheus/latest/configuration/configuration/)
2020-04-27 20:25:06 +00:00
2020-04-26 23:30:52 +00:00
A config file for prometheus, bind mounted in to prometheus container.</br>
2020-05-10 16:51:30 +00:00
Contains the bare minimum setup of targets from where metrics are to be pulled.
2020-04-26 00:48:03 +00:00
`prometheus.yml`
```yml
global:
scrape_interval: 15s
evaluation_interval: 15s
# A scrape configuration containing exactly one endpoint to scrape.
scrape_configs:
- job_name: 'nodeexporter'
scrape_interval: 5s
static_configs:
- targets: ['nodeexporter:9100']
- job_name: 'cadvisor'
scrape_interval: 5s
static_configs:
- targets: ['cadvisor:8080']
- job_name: 'prometheus'
scrape_interval: 10s
static_configs:
- targets: ['localhost:9090']
```
2020-04-26 23:30:52 +00:00
2020-05-10 16:51:30 +00:00
# Grafana configuration
Some of the grafana config files could be ommited
and info passed on the first run, or through settings.
But setting it through GUI wont generate these files which hinders backup
and ease of migration.
2020-04-27 20:25:06 +00:00
#### datasource.yml
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
* /prometheus/grafana/provisioning/datasources/**datasource.yml**
[Official documentation.](https://grafana.com/docs/grafana/latest/administration/provisioning/#datasources)
2020-04-26 00:48:03 +00:00
2020-04-27 20:25:06 +00:00
Grafana's datasources config file, from where it suppose to get metrics.</br>
2020-05-10 16:51:30 +00:00
In this case it points at the prometheus container.
2020-04-26 00:48:03 +00:00
`datasource.yml`
```yml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
orgId: 1
url: http://prometheus:9090
basicAuth: false
isDefault: true
2020-04-27 20:25:06 +00:00
editable: false
2020-04-26 00:48:03 +00:00
```
2020-04-27 20:25:06 +00:00
#### dashboard.yml
2020-04-26 23:30:52 +00:00
2020-05-10 16:51:30 +00:00
* /prometheus/grafana/provisioning/dashboards/**dashboard.yml**
[Official documentation](https://grafana.com/docs/grafana/latest/administration/provisioning/#dashboards)
2020-04-26 00:48:03 +00:00
2020-04-27 20:25:06 +00:00
Config file telling grafana from where to load dashboards.
2020-04-26 00:48:03 +00:00
`dashboard.yml`
```yml
apiVersion: 1
providers:
- name: 'Prometheus'
orgId: 1
folder: ''
type: file
disableDeletion: false
2020-04-27 20:25:06 +00:00
editable: false
allowUiUpdates: false
2020-04-26 00:48:03 +00:00
options:
path: /etc/grafana/provisioning/dashboards
```
2020-04-27 20:25:06 +00:00
#### \<dashboards>.json
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
* /prometheus/grafana/provisioning/dashboards/**<dashboards.json>**
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
[Official documentation.](https://grafana.com/docs/grafana/latest/reference/dashboard/)
The dashboards files are in
[the dashboards](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/prometheus_grafana/dashboards)
2020-04-27 21:43:14 +00:00
directory of this repository.
2020-05-10 16:51:30 +00:00
Preconfigured dashboards from
2020-04-27 20:25:06 +00:00
[stefanprodan/dockprom](https://github.com/stefanprodan/dockprom).</br>
2020-05-10 16:51:30 +00:00
Mostly unchanged, except for the default time range shown,
changed from 15min to 1hour,
2020-04-27 20:25:06 +00:00
and [a fix](https://github.com/stefanprodan/dockprom/issues/18#issuecomment-487023049)
for host network monitoring not showing traffick.
2020-04-26 00:48:03 +00:00
2020-04-27 20:34:35 +00:00
* **docker_host.json** - dashboard showing linux host metrics
* **docker_containers.json** - dashboard showing docker containers metrics,
except the ones labeled as `monitoring` in the compose file
* **monitoring_services.json** - dashboar showing docker containers metrics
2020-05-10 16:51:30 +00:00
of containers that are labeled `monitoring`, which are this repo containers.
2020-04-27 20:34:35 +00:00
2020-04-27 20:25:06 +00:00
# Reverse proxy
2020-04-26 00:48:03 +00:00
2020-05-01 09:51:20 +00:00
Caddy v2 is used, details
[here](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/caddy_v2).</br>
2020-04-26 00:48:03 +00:00
2020-04-27 20:25:06 +00:00
The setup is accessed through grafana.
But occasionally there might be need to check with prometheus,
2020-05-10 16:51:30 +00:00
which will be available on \<docker-host-ip>:9090.</br>
For that to work, Caddy will also need port 9090 published.
2020-05-02 20:48:23 +00:00
2020-04-27 20:25:06 +00:00
`Caddyfile`
```
grafana.{$MY_DOMAIN} {
reverse_proxy grafana:3000
}
2020-04-26 00:48:03 +00:00
2020-04-27 20:25:06 +00:00
:9090 {
reverse_proxy prometheus:9090
}
```
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
*Extra info:* `:9090` is short notation for `localhost:9090`
2020-04-27 21:43:14 +00:00
2020-04-26 00:48:03 +00:00
---
2020-05-10 16:54:33 +00:00
![interface-pic](https://i.imgur.com/wzwgBkp.png)
2020-04-26 00:48:03 +00:00
2020-04-26 23:30:52 +00:00
# Update
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
[Watchtower](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/watchtower)
updates the image automatically.
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
Manual image update:
- `docker-compose pull`</br>
- `docker-compose up -d`</br>
- `docker image prune`
2020-04-26 00:48:03 +00:00
2020-04-26 23:30:52 +00:00
# Backup and restore
2020-04-26 00:48:03 +00:00
2020-05-10 16:51:30 +00:00
#### Backup
Using [borg](https://github.com/DoTheEvo/selfhosted-apps-docker/tree/master/borg_backup)
that makes daily snapshot of the entire directory.
2020-04-27 20:34:35 +00:00
2020-05-10 16:51:30 +00:00
#### Restore
* down the prometheus containers `docker-compose down`</br>
* delete the entire prometheus directory</br>
* from the backup copy back the prometheus directory</br>
* start the containers `docker-compose up -d`