Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

haproxy: enable direct connections apart from pgbouncer ones (#548) #549

Merged
merged 9 commits into from
Jan 15, 2024
64 changes: 64 additions & 0 deletions roles/confd/templates/haproxy.tmpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 %}
Expand All @@ -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 %}
Expand All @@ -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 %}

71 changes: 71 additions & 0 deletions roles/haproxy/templates/haproxy.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 %}

59 changes: 59 additions & 0 deletions roles/upgrade/templates/haproxy-no-http-checks.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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 %}
7 changes: 7 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down