2023-01-31 23:59:53 +02:00
|
|
|
{ cluster, config, depot, lib, pkgs, ... }:
|
2022-08-05 00:57:54 +03:00
|
|
|
|
|
|
|
let
|
|
|
|
inherit (cluster.config) vars;
|
|
|
|
|
|
|
|
getMeshIp = name: vars.mesh.${name}.meshIp;
|
|
|
|
|
|
|
|
net = vars.meshNet.cidr;
|
|
|
|
|
|
|
|
pg = pkgs.postgresql_14;
|
|
|
|
|
|
|
|
baseDir = "/srv/storage/database/postgres-ha";
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
imports = [
|
2023-01-31 23:59:53 +02:00
|
|
|
depot.nixosModules.patroni
|
2022-08-05 00:57:54 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
age.secrets = lib.mapAttrs (_: file: {
|
|
|
|
inherit file;
|
|
|
|
mode = "0400";
|
|
|
|
owner = "patroni";
|
|
|
|
group = "patroni";
|
|
|
|
}) vars.patroni.passwords;
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [ "d '${baseDir}' 0700 patroni patroni - -" ];
|
|
|
|
services.patroni = {
|
|
|
|
enable = true;
|
|
|
|
name = vars.hostName;
|
|
|
|
postgresqlPackage = pg;
|
|
|
|
postgresqlDataDir ="${baseDir}/${pg.psqlSchema}";
|
|
|
|
postgresqlPort = cluster.config.links.patroni-pg-internal.port;
|
|
|
|
restApiPort = cluster.config.links.patroni-api.port;
|
|
|
|
scope = "poseidon";
|
|
|
|
namespace = "/patroni";
|
|
|
|
|
|
|
|
nodeIp = getMeshIp vars.hostName;
|
|
|
|
otherNodesIps = map getMeshIp cluster.config.services.patroni.otherNodes.worker;
|
|
|
|
raft = false;
|
|
|
|
softwareWatchdog = true;
|
|
|
|
settings = {
|
2023-03-05 21:51:20 +02:00
|
|
|
consul.host = "127.0.0.1:8500";
|
2022-08-05 00:57:54 +03:00
|
|
|
bootstrap.dcs = {
|
|
|
|
ttl = 30;
|
|
|
|
loop_wait = 10;
|
|
|
|
retry_timeout = 10;
|
|
|
|
maximum_lag_on_failover = 1024 * 1024;
|
|
|
|
};
|
|
|
|
postgresql = {
|
|
|
|
use_pg_rewind = true;
|
|
|
|
use_slots = true;
|
|
|
|
authentication = {
|
|
|
|
replication.username = "patronirep";
|
|
|
|
rewind.username = "patronirew";
|
|
|
|
superuser.username = "postgres";
|
|
|
|
};
|
|
|
|
parameters = {
|
|
|
|
listen_addresses = getMeshIp vars.hostName;
|
|
|
|
wal_level = "replica";
|
|
|
|
hot_standby_feedback = "on";
|
|
|
|
unix_socket_directories = "/tmp";
|
|
|
|
};
|
|
|
|
pg_hba = [
|
|
|
|
"host replication patronirep ${net} scram-sha-256"
|
|
|
|
"host all patronirew ${net} scram-sha-256"
|
|
|
|
"host all postgres ${net} scram-sha-256"
|
2022-08-07 20:57:35 +03:00
|
|
|
"host all all ${net} scram-sha-256"
|
2022-08-05 00:57:54 +03:00
|
|
|
"host all all 127.0.0.1/32 scram-sha-256"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
environmentFiles = lib.mapAttrs (n: _: config.age.secrets.${n}.path) vars.patroni.passwords;
|
|
|
|
};
|
2023-03-06 01:27:29 +02:00
|
|
|
|
|
|
|
consul.services.patroni = {
|
|
|
|
mode = "external";
|
2023-03-06 17:42:14 +02:00
|
|
|
definition = rec {
|
2023-03-06 01:27:29 +02:00
|
|
|
name = "patroni";
|
|
|
|
address = getMeshIp vars.hostName;
|
|
|
|
port = cluster.config.links.patroni-pg-internal.port;
|
2023-03-06 17:44:33 +02:00
|
|
|
checks = [
|
|
|
|
{
|
2023-03-06 19:05:04 +02:00
|
|
|
name = "Patroni API";
|
|
|
|
id = "service:patroni";
|
2023-03-06 17:44:33 +02:00
|
|
|
interval = "5s";
|
2023-03-06 19:05:04 +02:00
|
|
|
http = "http://${address}:${cluster.config.links.patroni-api.portStr}/liveness";
|
2023-03-06 17:44:33 +02:00
|
|
|
}
|
|
|
|
{
|
2023-03-06 19:05:04 +02:00
|
|
|
name = "Patroni Leader";
|
|
|
|
id = "service:patroni:leader";
|
|
|
|
interval = "5s";
|
|
|
|
http = "http://${address}:${cluster.config.links.patroni-api.portStr}/leader";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "PostgreSQL";
|
|
|
|
id = "service:patroni:postgres";
|
2023-03-06 17:44:33 +02:00
|
|
|
interval = "120s";
|
|
|
|
tcp = "${address}:${cluster.config.links.patroni-pg-internal.portStr}";
|
|
|
|
}
|
|
|
|
];
|
2023-03-06 01:27:29 +02:00
|
|
|
};
|
|
|
|
};
|
2022-08-05 00:57:54 +03:00
|
|
|
}
|