Monitoring PostgreSQL with Prometheus + grafana in docker environment

Time:2021-5-3

design sketch

Monitoring PostgreSQL with Prometheus + grafana in docker environment

Environmental preparation

Prepare the docker environment, including docker compose. Here, you can directly use the docker compose configuration file.

$ mkdir docker-composes
$ git clone https://github.com/docker-composes/postgres.git
$ git clone https://github.com/docker-composes/grafana.git
$ git clone https://github.com/docker-composes/prometheus.git

Note: the following operations start from the docker compositions directory.

Container configuration

Postgres exporter configuration

vim postgres/exporter/docker-compose.yml

Set the Postgres database connection information to data_ SOURCE_ Name environment variable. If it is a container under the same network, use the container name as the host. In this way, use the container postgressingleton under the same network (local net).

DATA_SOURCE_NAME=postgresql://root:[email protected]:5432/postgres?sslmode=disable

prometheus.yml

vim prometheus/singleton/config/prometheus.yml

On the screen_ Add under configs scope:

  # Postgres
  - job_name: 'postgres'
    static_configs:
      - targets: ['postgres-exporter-singleton:9187']
        labels:
          instance: 'postgres-01'
          platform: 'ec2'

Start container

$ docker network create local-net
$ cd grafana && docker-compose up -d 
$ cd prometheus/signleton && docker-compose up -d 
$ cd postgres/signleton && docker-compose up -d
$ cd postgres/expoter && docker-compose up -d

Configure grafana

visithttp://localhost:3000

Configure data sources

stayConfiguration -> Data SourcesAdd data source:

Monitoring PostgreSQL with Prometheus + grafana in docker environment

Import dashboard

copyDashboardID: 9628, import:
Monitoring PostgreSQL with Prometheus + grafana in docker environment

Monitoring PostgreSQL with Prometheus + grafana in docker environment

If the import is successful, you can open the dashboard to view the effect.

epilogue

  1. Understand the network connection between docker containers.
  2. Understand the configuration file mount in docker-compose.yml.
  3. Be patient with grafana.

After running through, you can study the query of various indicators and the statistical function of grafana, which will benefit a lot.