diff --git a/roles/confd/templates/haproxy.tmpl.j2 b/roles/confd/templates/haproxy.tmpl.j2 index e07a63a8d..d5bd3d0e6 100644 --- a/roles/confd/templates/haproxy.tmpl.j2 +++ b/roles/confd/templates/haproxy.tmpl.j2 @@ -45,6 +45,21 @@ listen master {{end}}{% endraw %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.master_direct is defined %} +listen master_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.master_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.master_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.master }} + 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 +{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}} +{{end}}{% endraw %} +{% endif %} listen replicas {% if cluster_vip is defined and cluster_vip | length > 0 %} @@ -67,6 +82,22 @@ listen replicas {{end}}{% endraw %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_direct is defined %} +listen replicas_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + option httpchk OPTIONS /replica + balance roundrobin + http-check expect status 200 + default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions +{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}} +{{end}}{% endraw %} +{% endif %} listen replicas_sync {% if cluster_vip is defined and cluster_vip | length > 0 %} @@ -89,6 +120,22 @@ listen replicas_sync {{end}}{% endraw %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_sync_direct is defined %} +listen replicas_sync_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_sync_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_sync_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + option httpchk OPTIONS /sync + balance roundrobin + http-check expect status 200 + default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions +{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}} +{{end}}{% endraw %} +{% endif %} listen replicas_async {% if cluster_vip is defined and cluster_vip | length > 0 %} @@ -111,3 +158,20 @@ listen replicas_async {{end}}{% endraw %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_async_direct is defined %} +listen replicas_async_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_async_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + option httpchk OPTIONS /async + balance roundrobin + http-check expect status 200 + default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions +{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}} +{{end}}{% endraw %} +{% endif %} + diff --git a/roles/haproxy/templates/haproxy.cfg.j2 b/roles/haproxy/templates/haproxy.cfg.j2 index 98b64da17..f0282ae25 100644 --- a/roles/haproxy/templates/haproxy.cfg.j2 +++ b/roles/haproxy/templates/haproxy.cfg.j2 @@ -47,6 +47,23 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos {% endfor %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.master_direct is defined %} +listen master_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.master_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.master_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.master }} + 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 + {% for host in groups['postgres_cluster'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port {{ patroni_restapi_port }} + {% endfor %} +{% endif %} + listen replicas {% if cluster_vip is defined and cluster_vip | length > 0 %} bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas }} @@ -70,6 +87,24 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos {% endfor %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_direct is defined %} +listen replicas_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + option httpchk OPTIONS /replica + balance roundrobin + http-check expect status 200 + default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions + {% for host in groups['postgres_cluster'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port {{ patroni_restapi_port }} + {% endfor %} +{% endif %} + listen replicas_sync {% if cluster_vip is defined and cluster_vip | length > 0 %} bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_sync }} @@ -93,6 +128,24 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos {% endfor %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_sync_direct is defined %} +listen replicas_sync_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_sync_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_sync_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + option httpchk OPTIONS /sync + balance roundrobin + http-check expect status 200 + default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions + {% for host in groups['postgres_cluster'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port {{ patroni_restapi_port }} + {% endfor %} +{% endif %} + listen replicas_async {% if cluster_vip is defined and cluster_vip | length > 0 %} bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async }} @@ -116,3 +169,21 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos {% endfor %} {% endif %} +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_async_direct is defined %} +listen replicas_async_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_async_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + option httpchk OPTIONS /async + balance roundrobin + http-check expect status 200 + default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions + {% for host in groups['postgres_cluster'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port {{ patroni_restapi_port }} + {% endfor %} +{% endif %} + diff --git a/roles/upgrade/templates/haproxy-no-http-checks.cfg.j2 b/roles/upgrade/templates/haproxy-no-http-checks.cfg.j2 index 687b1c3b6..d662e77c1 100644 --- a/roles/upgrade/templates/haproxy-no-http-checks.cfg.j2 +++ b/roles/upgrade/templates/haproxy-no-http-checks.cfg.j2 @@ -37,6 +37,20 @@ listen master server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} {% endfor %} +{% if haproxy_listen_port.master_direct is defined %} +listen master_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.master_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.master_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.master }} + option tcplog + {% for host in groups['primary'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} + {% endfor %} +{% endif %} + listen replicas {% if cluster_vip is defined and cluster_vip | length > 0 %} bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas }} @@ -50,6 +64,21 @@ listen replicas server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} {% endfor %} +{% if haproxy_listen_port.replicas_direct is defined %} +listen replicas_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + balance roundrobin + {% for host in groups['secondary'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} + {% endfor %} +{% endif %} + listen replicas_sync {% if cluster_vip is defined and cluster_vip | length > 0 %} bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_sync }} @@ -63,6 +92,21 @@ listen replicas_sync server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} {% endfor %} +{% if haproxy_listen_port.replicas_sync_direct is defined %} +listen replicas_sync_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_sync_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_sync_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + balance roundrobin + {% for host in groups['secondary'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} + {% endfor %} +{% endif %} + listen replicas_async {% if cluster_vip is defined and cluster_vip | length > 0 %} bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async }} @@ -75,3 +119,18 @@ listen replicas_async {% for host in groups['secondary'] %} server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} {% endfor %} + +{% if haproxy_listen_port.replicas_async_direct is defined %} +listen replicas_async_direct +{% if cluster_vip is defined and cluster_vip | length > 0 %} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async_direct }} +{% else %} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_async_direct }} +{% endif %} + maxconn {{ haproxy_maxconn.replica }} + option tcplog + balance roundrobin + {% for host in groups['secondary'] %} +server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} + {% endfor %} +{% endif %} \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index dc7e2a758..e8fd77711 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -33,6 +33,13 @@ haproxy_listen_port: replicas: 5001 replicas_sync: 5002 replicas_async: 5003 +# The following ('_direct') ports are used for direct connections to the PostgreSQL database, +# bypassing the PgBouncer connection pool (if 'pgbouncer_install' is 'true'). +# Uncomment the relevant lines if you need to set up direct connections. +# master_direct: 6000 +# replicas_direct: 6001 +# replicas_sync_direct: 6002 +# replicas_async_direct: 6003 stats: 7000 haproxy_maxconn: global: 100000