Skip to content

Commit

Permalink
haproxy: individual optional _direct variables for every type of conn…
Browse files Browse the repository at this point in the history
…ection, commented out by default (vitabaks#548)
  • Loading branch information
jimnydev committed Jan 11, 2024
1 parent 0e22a96 commit 3ff643a
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 61 deletions.
73 changes: 52 additions & 21 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 | 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 %}
Expand All @@ -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 %}
Expand All @@ -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 %}
Expand All @@ -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
Expand Down
80 changes: 59 additions & 21 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 | 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 }}
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 | 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 }}
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 | 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 }}
Expand All @@ -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 %}
64 changes: 48 additions & 16 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 | 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 }}
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 | 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 }}
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 | 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 }}
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ff643a

Please sign in to comment.