vitabaks / postgresql_cluster

PostgreSQL High-Availability Cluster (based on "Patroni" and DCS "etcd" or "consul"). Automating with Ansible.
MIT License
1.27k stars 340 forks source link

Add the ability to set tags for load balancers #613

Closed vitabaks closed 1 month ago

vitabaks commented 1 month ago

Variable: balancer_tags

Example (/etc/haproxy/haproxy.cfg ):

global
    maxconn 100000
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    mode               tcp
    log                global
    retries            2
    timeout queue      5s
    timeout connect    5s
    timeout client     60m
    timeout server     60m
    timeout check      15s

listen stats
    mode http
    bind 10.172.0.20:7000
    stats enable
    stats uri /

listen master
    bind 10.172.0.20:5000
    maxconn 10000
    option tcplog
    option httpchk OPTIONS /primary
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
 server pgnode01 10.172.0.20:6432 check port 8008
 server pgnode02 10.172.0.21:6432 check port 8008
 server pgnode03 10.172.0.22:6432 check port 8008

listen replicas
    bind 10.172.0.20:5001
    maxconn 10000
    option tcplog
    option httpchk OPTIONS /replica?lag=100MB&tag_datacenter=dc1
    balance roundrobin
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
 server pgnode01 10.172.0.20:6432 check port 8008
 server pgnode02 10.172.0.21:6432 check port 8008
 server pgnode03 10.172.0.22:6432 check port 8008

listen replicas_sync
    bind 10.172.0.20:5002
    maxconn 10000
    option tcplog
    option httpchk OPTIONS /sync?tag_datacenter=dc1
    balance roundrobin
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
 server pgnode01 10.172.0.20:6432 check port 8008
 server pgnode02 10.172.0.21:6432 check port 8008
 server pgnode03 10.172.0.22:6432 check port 8008

listen replicas_async
    bind 10.172.0.20:5003
    maxconn 10000
    option tcplog
    option httpchk OPTIONS /async?lag=100MB&tag_datacenter=dc1
    balance roundrobin
    http-check expect status 200
    default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
 server pgnode01 10.172.0.20:6432 check port 8008
 server pgnode02 10.172.0.21:6432 check port 8008
 server pgnode03 10.172.0.22:6432 check port 8008

To separate load balancers across different data centers

  1. To separate load balancers across different data centers (each has its VIP), just create separate inventory files (e.q. cluster.hosts, balancers_dc1.hosts, balancers_dc2.hosts). See an example here
  2. To separate the read-only traffic across different data centers, add the datacenter=<name> tag to the ‘patroni_tags’ and ‘balancer_tags’ variables in the inventory for the respective hosts. See an example here