feat(database): use service discovery for primary address
Related to #890 (closed)
What does this MR do?
It introduces a dns
package that can resolve SRV
and A
types of records from a DNS server.
It also adds a new discovery
section to the database
configuration to enable service discovery for a primary database host.
How to test locally
The setup requires a DNS server that can reply to SRV
and A
requests. The easiest way is to run a PostgreSQL container and install consul
:
- Create a volume
docker volume create postgres_data
docker run --name postgres-registry -d --restart=always -p 55432:5432 -e POSTGRES_USER=registry -e POSTGRES_PASSWORD=apassword -e POSTGRES_DB=registry_dev -e PGDATA=/var/lib/postgresql/pgdata -v postgres_data:/var/lib/postgresql/pgdata postgres:12-alpine
- ensure the database
registry_dev
exists, e.g. usingpsql
:PGPASSWORD=apassword psql -h 127.0.0.1 -U registry -p 55432 -d registry_dev -c 'select 1';
- Create a consul directory and add some configuration settings:
mkdir -p consul/data
cd consul
touch consul.json
- Add the following to
consul.json
{
"data_dir": "consul/data",
"enable_local_script_checks": true,
"services": [
{
"id": "primary-55432",
"name": "registry-primary",
"port": 55432,
"address": "127.0.0.1",
"tags": [
"primary"
],
"checks": [
{
"args": ["psql", "-U", "registry", "-h", "127.0.0.1", "-p", "55432", "-d", "registry_dev", "-c", "select 1"],
"interval": "2s"
}
]
}
]
}
- Consul will run in the terminal and the logs can be seen when a query is attempted:
dig +short @127.0.0.1 -p 8600 primary.registry-primary.service.consul -t SRV
1 1 55432 7f000001.addr.dc1.consul.
2023-04-05T17:54:09.221+1000 [DEBUG] agent.dns: request served from client: name=primary.registry-primary.service.consul. type=SRV class=IN latency="260.625µs" client=127.0.0.1:59990 client_network=udp
2023-04-05T17:54:10.818+1000 [DEBUG] agent: Check status updated: check=service:primary-55432 status=passing
2023-04-05T17:54:12
Now add the following to your registry config.yml
file under the database
section
database:
enabled: true
password: "apassword"
dbname: "registry_dev"
sslmode: "disable"
discovery:
enabled: true
nameserver: 127.0.0.1
primaryrecord: "primary.registry-primary.service.consul."
recordtype: SRV
port: 8600
- Run the migrations!
make bin/registry && ./bin/registry database migrate up ./myenv/config.yml
OK: applied 132 migrations in 3.778s
Edited by Jaime Martinez