Consul Service Discovery

How Prometheus discovery monitoring targets with consul service registry

When prometheus_sd_method == ‘consul’, Prometheus will query consul for monitoring targets.

This article describes the mechanism used by the Pigsty monitor system Prometheus to discover monitor objects.

The basis of service discovery is identity. For more identity information, please refer to the section entity.

It would help if you also associated monitor targets with the identity in the monitor system, and Pigsty provides two implementations.

The static file is the default service discovery mechanism. Before v1.0.0, Consul was the default service discovery method, and the discovery mechanism could be configured via parameters.

Identity

All instances have an Identity, and the Identifier is the metadata associated with the instance to identify it.

An Identity parameter is a unique identifier that must be defined for any cluster with an instance.

namevariablesabbreviationtypedescription
clusterpg_clusterclsCore identity parametersCluster name, top-level namespace for resources within the cluster
rolepg_rolerolecore identity parametersinstance role, primary, replica, offline,…
seqpg_seqseqcore identity parametersInstance serial number, positive integer, unique within the cluster.
instancepg_instanceinsderived identity parameters${pg_cluster}-${pg_seq}
servicepg_servicesvcderived identity parameters${pg_cluster}-${pg_role}

Consul SD - 图1

Attach Identity

After naming the objects in the system, you also need to associate identity information to specific instances.

Identity information is business-given metadata, and the database instance is not aware of this identity information.

The most straightforward way to associate identities is Operator’s memory: the DBA remembers in his mind that the instance on IP 10.2.3.4 is the one used for payments. In contrast, the instance on the other one is used for user management. A better way to manage the identity of cluster members is through the config file or by using service discovery.

Pigsty provides both ways of identity management: service discovery based on the Consul and service discovery based on the config file.

The parameter prometheus_sd_method controls this behavior.

  • consul: service discovery based on Consul, default config.
  • static: service discovery based on local config files.

Pigsty recommends using static service discovery, which is more concise and more reliable as the monitor system does not need to rely on Consul.

Static File Service Discovery

Static file service discovery is the default method of monitor object discovery, and Pigsty pulls the config using the following config by default.

  1. #------------------------------------------------------------------------------
  2. # job: pgsql (database monitoring)
  3. # node_exporter | pg_exporter | pgbouncer_exporter | haproxy(exporter)
  4. # labels: [cls, ins, instance]
  5. # path: targets/pgsql/*.yml
  6. #------------------------------------------------------------------------------
  7. - job_name: pgsql
  8. metrics_path: /metrics
  9. file_sd_configs:
  10. - refresh_interval: 10s
  11. files: [ /etc/prometheus/targets/pgsql/*.yml ]

The /etc/prometheus/targets dir holds the monitor object definition files generated by Pigsty, and pgsql is the name of the default environment.

Each instance is register by a standalone file:

  1. /etc/prometheus/targets/pgsql
  2. ^-----pg-meta-1.yml
  3. ^-----pg-test-1.yml
  4. ^-----pg-test-2.yml
  5. ^-----pg-test-3.yml

Its content is the identifier on a single instance, with monitor objects.

  1. # pg-meta-1 [primary] @ 10.10.10.10
  2. - labels: { cls: pg-meta, ins: pg-meta-1 }
  3. targets: [10.10.10.10:9630, 10.10.10.10:9100, 10.10.10.10:9631, 10.10.10.10:9101]

Maintenance Document Service Discovery

When using static file service discovery, these config files are automatically maintained for all cluster expansion and downsize.

The config files will be regenerated for all instances in the environment using the following command.

  1. ./pgsql.yml -t register_prometheus

Default Targets

Each managed Postgres instance includes several capture ports.

  • Node Exporter for capturing machine node metrics.
  • PG Exporter for capturing database metrics.
  • PGBouncer Exporter for capturing connection pool metrics (uses the same binary as PG Exporter).
  • Patroni for capturing HA components.
  • HAProxy for capturing LB metrics (built-in support, no separate deployment required).

Consul SD - 图2

Prometheus on the meta node captures these capture ports.

In addition, the optional Promtail for collecting Postgres, Patroni, and Pgbouncer logs.

All monitoring ports are registered to Consul by default, but Prometheus manages these tasks by default using static file service discovery. Users can use Consul service discovery by configuring prometheus_sd_method as consul to manage instances dynamically.

Consul Service Discovery

Pigsty has built-in DCS-based config management and automatic service discovery, which provides a visual overview of all nodes and services in the system and their health status. All services in Pigsty are automatically registered with DCS.

Users can also use the DNS and service discovery mechanism provided by Consul to achieve automatic DNS-based traffic switching.

Consul uses a Client/Server architecture, with one to five Consul Servers in the entire environment for the actual metadata storage. Consul Agent is deployed on all nodes to proxy the communication between local services and the Consul Server. Pigsty registers services by default using local Consul config files.

Service Registration

A consul agent is running on each node, and services are registered to DCS by the consul agent using JSON config files.

The default location of the JSON config file is /etc/consul.d/, using the naming pattern of svc-<service>.json, taking postgres as an example.

  1. {
  2. "service": {
  3. "name": "postgres",
  4. "port": 5432,
  5. "tags": [
  6. "pgsql",
  7. "primary",
  8. "pg-meta"
  9. ],
  10. "meta": {
  11. "type": "postgres",
  12. "role": "primary",
  13. "seq": "1",
  14. "instance": "pg-meta-1",
  15. "service": "pg-meta-primary",
  16. "cluster": "pg-meta",
  17. "version": "13"
  18. },
  19. "check": {
  20. "args": ["/usr/pgsql/bin/pg_isready", "-p", "5432", "-U", "dbuser_monitor"],
  21. "interval": "15s",
  22. "timeout": "1s"
  23. }
  24. }
  25. }

Where the meta and tags sections are the metadata of the service and store the identity information of the instance.

Service Inquiry

Users can discover services registered to Consul through the DNS service provided by Consul, or by calling the Consul API directly.

See Consul doc for ways to consult consul services using the DNS API.

Service Discovery

Prometheus automatically discovers monitor objects in the env via consul_sd_configs. Services tagged with both pg and exporter are automatically identified as crawlers.

  1. - job_name: pg
  2. # https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config
  3. consul_sd_configs:
  4. - server: localhost:8500
  5. refresh_interval: 5s
  6. tags:
  7. - pg
  8. - exporter

Figure: In services discovered by Prometheus, identity information has been associated with the metric dimension of the instance.

Service Maintenance

A database primary-replica switch occurs, resulting in a registered role that is different from the actual role of the instance. This is when such anomalies need to be handled through the anti-entropy process. Patroni-based failover can fix the registered roles normally through callback logic, but manually done role switchover requires manual intervention to take it. Service registrations to the database can be detected and fixed automatically using the following script. It is recommended to configure Crontab on the database instance or set up periodic patrol tasks on the meta node.

  1. /pg/bin/pg-register $(pg-role)

Labels

Either through Consul or static file service discovery. The end effect is to achieve an association between identity information and instance metrics.

This correlation is achieved through the dimensional labels of the monitoring metrics.

However, all original metrics related to database clusters in Pigsty must have both cls and ins tags and remain immutable throughout their lifecycle.

Identity parametersDimension labelsSample values
pg_clusterclspg-test
pg_instanceinspg-test-1
node_ipip10.10.10.11

Read the next section of metrics to learn how labels organize these metrics.

Last modified 2022-06-04: fill en docs (5a858d3)