- Install Docker and start the docker daemon.
$ git clone https://github.com/bharanitharanm/prometheus-demo.git $ cd prometheus-demo - Execute
./start.sh
Prometheus Console http://localhost:9090
Alert Manager Console http://localhost:9093
Node app http://localhost:3001
cAdvisor Console http://localhost:8080
- Download and Extract
$ wget https://github.com/prometheus/prometheus/releases/download/v2.18.0-rc.0/prometheus-2.18.0-rc.0.linux-amd64.tar.gz $ tar -xf prometheus-2.18.0-rc.0.linux-amd64.tar.gz - Start prometheus
$ cd prometheus-2.18.0-rc.0.linux-amd64/ $ ./prometheus - Prometheus console can be accessed by http://localhost:9090
- By default Prometheus will monitor itself http://localhost:9090/targets
- Install Node.js
$ wget https://nodejs.org/dist/v12.16.3/node-v12.16.3-linux-x64.tar.xz $ tar -xf node-v12.16.3-linux-x64.tar.xz - Set
node-v12.16.3-linux-x64/binto class path and verify usingnode -v - Start the node application
$ cd app/ $ npm install $ node index.js - Has two endpoints
"http://localhost:3001/" - Total number of requests to this endpoint will be monitored "http://localhost:3001/metrics" - Endpoint used by Prometheus to get the metrics $ curl http://localhost:3001/metrics # HELP total_http_request_count Total number of HTTP requests # TYPE total_http_request_count counter total_http_request_count 0 $ curl http://localhost:3001/ HelloWorld!! $ curl http://localhost:3001/metrics # HELP total_http_request_count Total number of HTTP requests # TYPE total_http_request_count counter total_http_request_count 1
- In
prometheus.ymladd the Node application for monitoring underscrape_configs- job_name: 'node-app' static_configs: - targets: ['localhost:3001'] - Now the
node-appapplication will be listed in targets http://localhost:9090/targets (Status will be shown asUPorDOWNbased on the service status)
Alerts will be generated by Prometheus based on the configured alert rules.
- Create Alert Rules in yml format.
Alert rule if application goes down
Alert Rule for every 5 new requests
- alert: InstanceDown expr: up == 0 for: 1m labels: severity: "critical" annotations: title: 'Instance {{ $labels.instance }} down' description: '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minute.'- alert: RequestLimit expr: total_http_request_count != 0 and total_http_request_count%5 == 0 for: 10s labels: severity: "critical" annotations: title: '5 New requests received' description: '5 New HTTP requests received for node-app' - Adding Alert rules to Prometheus. In
prometheus.ymlrule_files: - "alert_rules.yml"
Alerts generated by Prometheus server are sent to Alert Manager which manages the alerts and sends the nofications
- Download and Extract Alert Manager
$ wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertmanager-0.20.0.linux-amd64.tar.gz $ tar -xf alertmanager-0.20.0.linux-amd64.tar.gz - Start Alert Manager
$ cd alertmanager-0.20.0.linux-amd64/ $ ./alertmanager - Alert Manager console can be accessed by http://localhost:9093
- Configure Alert Manager in Prometheus. In
prometheus.ymladd the belowalerting: alertmanagers: - static_configs: - targets: - localhost:9093
- Alert manager can send the alert notifications to mail, webhook etc. Sample configuration for configuring GMail.
Generate Google app password https://support.google.com/accounts/answer/185833?hl=en
receivers: - name: 'gmail-notifications' email_configs: - to: <email> from: <email> smarthost: smtp.gmail.com:587 auth_username: <email> auth_identity: <email> auth_password: <google_app_password>
- Start cAdvisor docker
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:ro --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor gcr.io/google-containers/cadvisor:v0.35.0 - cAdvisor console can be accessed on
http://localhost:8080 - Configure prometheus to listen to cAdvisor
- job_name: 'cAdvisor' static_configs: - targets: ['localhost:8080']
Prometheus can be added as a datasource to Grafana.
- Install Grafana
wget https://dl.grafana.com/oss/release/grafana-6.7.3-1.x86_64.rpm sudo yum install grafana-6.7.3-1.x86_64.rpm - Start Grafana
sudo systemctl daemon-reload sudo systemctl start grafana-server sudo systemctl status grafana-server