Damn I Love Docker: Docker Swarm Metrics
Table of Contents
Introduction
Here we discuss one way to monitor the nodes in a docker swarm1 cluster using Prometheus,2 Grafana,3 and node-exporter.4
Metrics Stack
NOTE: This guide assumes you already have a docker swarm
1 setup.
Node-Exporter Service
The following command will create a docker service
on your docker swarm
composed of the node-exporter application written by Prometheus.2
docker service create --mode global \
--name=metrics_node-exporter \
--network=host \
prom/node-exporter \
--path.rootfs=/host
Prometheus/Grafana Stack
The following command will deploy a docker stack
5 composed of two
docker services6: Prometheus2, and Grafana.3 Before we look at
how to create the docker stack we need to look at the config file for
Prometheus:
$ cat prometheus_grafana/prometheus.yml
global:
scrape_interval: 1m
scrape_timeout: 10s
evaluation_interval: 1m
scrape_configs:
- job_name: node-exporter
static_configs:
- targets:
- 192.168.100.30:9100
- 192.168.100.31:9100
- 192.168.100.32:9100
Next we will write a stack file which has the same syntax as a docker-compose file7:
$ cat prometheus_grafana/metrics-stack.yml
version: "3.7"
volumes:
prometheus-data:
grafana-data:
configs:
prometheus-config:
file: ./prometheus.yaml
services:
prometheus:
deploy:
placement:
constraints:
- node.role == manager
hostname: prometheus
configs:
- source: prometheus-config
target: /prometheus.yaml
volumes:
- prometheus-data:/prometheus
ports:
- 9090:9090
image: prom/prometheus
command: [
--config.file, /prometheus.yaml,
--storage.tsdb.path, /prometheus
]
grafana:
deploy:
placement:
constraints:
- node.role == manager
hostname: grafana
volumes:
- grafana-data:/var/lib/grafana
ports:
- 3000:3000
image: grafana/grafana
Finally we can deploy the stack as follows:
$ docker stack deploy -c prometheus_grafana/metrics_stack.yml metrics