Build a Monitor and Log system using Docker

Tram Ho

Monitoring is a topic that is no longer strange and still very hot to us :v. Especially when working in monitoring , most of you will probably remember the divine duo Prometheus and Grafana .

Today, I will demo setup this monitor using docker and add logging using Loki (also a product of GrafanaLabs). Loki: like Prometheus, but for logs. Loki ‘s intro :v reads it and gets to work :v :v :v

In this article, I will install the following components:

  • Prometheus (metrics database): http://<host-ip>:9090
  • Prometheus-pushgateway (push acceptor for ephemeral and batch jobs): http://<host-ip>:9091
  • AlertManager (alerts management): http://<host-ip>:9093
  • Grafana (visualize metrics): http://<host-ip>:3000
  • Loki (likes prometheus, but for logs): http://<host-ip>:3100
  • Promtail (is the agent, responsible for gathering logs and sending them to Loki)
  • NodeExporter (host metrics collector)
  • cAdvisor (containers metrics collector)
  • Caddy (reverse proxy and basic auth provider for prometheus and alertmanager)

My source code is here

For convenience in the article, I will call this demo domolo for you to follow easily (domolo: docker – monitor -log).

Ok!!! Come in

Setup

First, install docker on your computer. After that, you clone the source code to:

The structure of the domolo looks like this:

Grafana

In Grafana I will setup 2 main parts:

  • Datasources
  • Dashboards

First, you edit the config file to set up user vs password variables to your liking:

In the datasources section we will preconfigure prometheus and loki :

This will help us not need to add these 2 guys after building the cluster. The dashboards of Grafana I will present below.

Prometheus + Node Exporter

Prometheus ( metrics database ): Responsible for getting metrics information from targets and saving them. The main configuration section of prometheus is here , the parameters myself use:

  • global : Used to set up the periodic period of prometheus pull metrics, and the external_labels parameter to be able to get metrics information from external systems about prometheus .
  • alerting : Connection information to Alert Manager.
  • rule_files : This is the important part of warning handling. You can set rules depending on your needs, for example, high CPU or RAM load warnings. You can follow some sample rules here
  • scrape_configs : Connect to targets to get metric .

Direct monitoring of the prometheus system:

Monitor other systems:

Node Exporter ( host metrics collector ): Responsible for collecting metrics from servers. Here, I will get metrics information at /proc , /sys :

To setup node_exporter on the server, you need to monitor that you run the file docker-compose.agents.yml

In addition, in the docker-compose.agents.yml file, we will set up cAdvisor (same as node_exporter but different, cAdvisor gets the metrics of containers), and promtail I will show below.

Grafana Dashboards

In this part, you can create your own dashboard to suit the needs of your project, or you can go to Grafana Dashboards to find templates quickly (so beautiful, vl)

In this section, I used a few dashboards :

NodeExporter

Monitor Services

MonitorServices

Docker Host

DockerHost

Loki + Promtail

Loki is inspired by prometheus , a system for collecting, storing, and analyzing logs. We can easily query logs from Loki and limit the amount of logs received and time to process old logs. You can refer to my loki config file here . Or go directly to the github page to see :v

Promtail tool to get logs and send them to Loki server . In domolo I use promtail to collect logs of containers and the system:

  • Scape logs containers:

  • Scrape logs systems:

Ok!!! So that’s the end of the theory part of Loki and promtail . Next, I’ll try to build it up and see how it looks.

Demo

First, I created a small tool to automatically generate logs. You guys can watch it here . This tool I wrote in bash script simply prints out some miscellaneous and colorful messages :v

After that, I close it into a Dockerfile and add it in the docker-compose file or you can use the docker-compose.with-flogs.yml file and then proceed to build …

Done and then access grafana and select Explore :

Explore

Next, select labels to view logs:

Labels

Ex: Select label container and view the newly created fake-logs container log:

LabelFlog

LogsFlog

More logs: logs system, other containers, ….

SystemLogs

ContainersLogs

It looks fine too :v

Conclude

So I just showed how I set up a monitoring and logging system. If there is anything wrong or poorly presented, please give me more suggestions.

Thank you guys for reading!!!

Share the news now

Source : Viblo