From 3ff643a5e8cc17981023890fb358eff24ed6edb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Mom=C4=8Dilovi=C4=87?= Date: Thu, 11 Jan 2024 23:48:02 +0100 Subject: [PATCH] haproxy: individual optional _direct variables for every type of connection, commented out by default (#548) --- roles/confd/templates/haproxy.tmpl.j2 | 73 ++++++++++++----- roles/haproxy/templates/haproxy.cfg.j2 | 80 ++++++++++++++----- .../templates/haproxy-no-http-checks.cfg.j2 | 64 +++++++++++---- vars/main.yml | 10 ++- 4 files changed, 166 insertions(+), 61 deletions(-) diff --git a/roles/confd/templates/haproxy.tmpl.j2 b/roles/confd/templates/haproxy.tmpl.j2 index 2b7e37d15..7f3335528 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 | default('') | length > 0 %} +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 | default('') | length > 0 %} +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 | default('') | length > 0 %} +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,32 +158,16 @@ listen replicas_async {{end}}{% endraw %} {% endif %} - -{% if with_haproxy_direct_connections|bool and pgbouncer_install|bool %} -listen master_direct +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_async_direct | default('') | length > 0 %} +listen replicas_async_direct {% if cluster_vip is defined and cluster_vip | length > 0 %} - bind {{ cluster_vip }}:{{ haproxy_listen_port.master_direct }} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async_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 %} - - -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 }} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_async_direct }} {% endif %} maxconn {{ haproxy_maxconn.replica }} option tcplog - option httpchk OPTIONS /replica + 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 diff --git a/roles/haproxy/templates/haproxy.cfg.j2 b/roles/haproxy/templates/haproxy.cfg.j2 index 2e74ba765..0d127ca5a 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 | default('') | length > 0 %} +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 | default('') | length > 0 %} +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 | default('') | length > 0 %} +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,35 +169,20 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos {% endfor %} {% endif %} -{% if with_haproxy_direct_connections|bool and pgbouncer_install|bool %} -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 %} - -listen replicas_direct +{% if pgbouncer_install|bool and haproxy_listen_port.replicas_async_direct | default('') | length > 0 %} +listen replicas_async_direct {% if cluster_vip is defined and cluster_vip | length > 0 %} - bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_direct }} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async_direct }} {% else %} - bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_direct }} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_async_direct }} {% endif %} maxconn {{ haproxy_maxconn.replica }} option tcplog - option httpchk OPTIONS /replica + 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 %} +{% endif %} \ No newline at end of file diff --git a/roles/upgrade/templates/haproxy-no-http-checks.cfg.j2 b/roles/upgrade/templates/haproxy-no-http-checks.cfg.j2 index f6d05c458..505bdceaa 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 | default('') | length > 0 %} +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 | default('') | length > 0 %} +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 | default('') | length > 0 %} +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 }} @@ -76,24 +120,12 @@ listen replicas_async server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} {% endfor %} -{% if with_haproxy_direct_connections|bool %} -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 %} - -listen replicas_direct +{% if haproxy_listen_port.replicas_async_direct | default('') | length > 0 %} +listen replicas_async_direct {% if cluster_vip is defined and cluster_vip | length > 0 %} - bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_direct }} + bind {{ cluster_vip }}:{{ haproxy_listen_port.replicas_async_direct }} {% else %} - bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_direct }} + bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ haproxy_listen_port.replicas_async_direct }} {% endif %} maxconn {{ haproxy_maxconn.replica }} option tcplog diff --git a/vars/main.yml b/vars/main.yml index 483843bf6..e8fd77711 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -28,14 +28,18 @@ synchronous_node_count: 1 # number of synchronous standby databases # Load Balancing with_haproxy_load_balancing: false # or 'true' if you want to install and configure the load-balancing -with_haproxy_direct_connections: false # or 'true' if you want to enable haproxy ports that skip pgbouncer (and if pgbouncer_install is 'true') haproxy_listen_port: master: 5000 - master_direct: 6000 replicas: 5001 replicas_sync: 5002 replicas_async: 5003 - replicas_direct: 6001 +# 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