Skip to content

Commit 176a36a

Browse files
committed
VIP: make the cluster ip address as optional.
Now, you can deploy your custom schemes without using a VIP (cluster IP address) if necessary.
1 parent 7079672 commit 176a36a

14 files changed

+126
-29
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ proxy_env:
155155
http_proxy: http://proxy_server_ip:port
156156
https_proxy: http://proxy_server_ip:port
157157
```
158-
- `cluster_vip`
158+
- `cluster_vip` # for client access to databases in the cluster (optional)
159159
- `patroni_cluster_name`
160160
- `with_haproxy_load_balancing` `'true'` (Type A) or `'false'`/default (Type B)
161161
- `postgresql_version`
162+
- `postgresql_data_dir`
162163

163164

164165
5. Run playbook:

add_balancer.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
- import_tasks: tasks/hostname.yml
2020
when: hostname is defined and hostname | length > 0
2121
tags: hostname
22-
22+
2323
- import_tasks: tasks/sysctl.yml
2424
tags: [ sysctl, kernel ]
25-
25+
2626
- name: Make sure the gnupg package is present
2727
apt:
2828
name: gnupg

add_pgnode.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
tags: patroni
7676

7777
- import_tasks: tasks/vip-manager.yml
78-
when: with_haproxy_load_balancing != "true"
78+
when: with_haproxy_load_balancing != "true" and (cluster_vip is defined and cluster_vip | length > 0)
7979
tags: [ vip, vip_manager ]
8080

8181
# finish (info)

deploy_pgcluster.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
tags: [ sysctl, kernel ]
100100

101101
- import_tasks: tasks/haproxy.yml
102-
when: with_haproxy_load_balancing == "true" and cluster_vip | length > 0
102+
when: with_haproxy_load_balancing == "true"
103103
tags: load_balancing
104104

105105

@@ -137,7 +137,7 @@
137137
tags: patroni
138138

139139
- import_tasks: tasks/vip-manager.yml
140-
when: with_haproxy_load_balancing != "true" and cluster_vip | length > 0
140+
when: with_haproxy_load_balancing != "true" and (cluster_vip is defined and cluster_vip | length > 0)
141141
tags: [ vip, vip_manager ]
142142

143143
# optional

tasks/confd.yml

+21
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,29 @@
8686
backrefs: yes
8787
loop:
8888
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
89+
- { regexp: '^.*bind.*:5000$', line: ' bind {{ cluster_vip }}:5000' }
90+
- { regexp: '^.*bind.*:5001$', line: ' bind {{ cluster_vip }}:5001' }
91+
- { regexp: '^.*bind.*:5002$', line: ' bind {{ cluster_vip }}:5002' }
92+
- { regexp: '^.*bind.*:5003$', line: ' bind {{ cluster_vip }}:5003' }
8993
loop_control:
9094
label: "{{ item.line }}"
95+
when: cluster_vip is defined and cluster_vip | length > 0
96+
97+
- name: confd | prepare haproxy.tmpl template file (replace "bind" for stats)
98+
lineinfile:
99+
path: /etc/confd/templates/haproxy.tmpl
100+
regexp: '{{ item.regexp }}'
101+
line: '{{ item.line }}'
102+
backrefs: yes
103+
loop:
104+
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
105+
- { regexp: '^.*bind.*:5000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5000' }
106+
- { regexp: '^.*bind.*:5001$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5001' }
107+
- { regexp: '^.*bind.*:5002$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5002' }
108+
- { regexp: '^.*bind.*:5003$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5003' }
109+
loop_control:
110+
label: "{{ item.line }}"
111+
when: cluster_vip is not defined or cluster_vip | length < 1
91112
when: add_balancer is defined and add_balancer|bool
92113
tags: [ confd_conf, confd ]
93114

tasks/deploy_finish.yml

+7-13
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@
4747
- port 5002 (read only) synchronous replica only
4848
- port 5003 (read only) asynchronous replicas only
4949
- +------------------------------------------------+
50-
when: with_haproxy_load_balancing == "true" and synchronous_mode == "true"
51-
ignore_errors: yes
52-
tags: conn_info
50+
when: with_haproxy_load_balancing == "true" and synchronous_mode == "true"
5351

54-
- block:
5552
- name: PostgreSQL Cluster connection info
5653
run_once: true
5754
debug:
@@ -61,11 +58,8 @@
6158
- port 5000 (read/write) master
6259
- port 5001 (read only) all replicas
6360
- +------------------------------------------------+
64-
when: with_haproxy_load_balancing == "true" and synchronous_mode != "true"
65-
ignore_errors: yes
66-
tags: conn_info
61+
when: with_haproxy_load_balancing == "true" and synchronous_mode != "true"
6762

68-
- block:
6963
- name: PostgreSQL Cluster connection info
7064
run_once: true
7165
debug:
@@ -74,11 +68,8 @@
7468
- address (VIP) {{ cluster_vip }}
7569
- port {{ pgbouncer_listen_port }} (pgbouncer)
7670
- +------------------------------------------------+
77-
when: with_haproxy_load_balancing == "false" and install_pgbouncer == "true"
78-
ignore_errors: yes
79-
tags: conn_info
71+
when: with_haproxy_load_balancing == "false" and install_pgbouncer == "true"
8072

81-
- block:
8273
- name: PostgreSQL Cluster connection info
8374
run_once: true
8475
debug:
@@ -87,7 +78,8 @@
8778
- address (VIP) {{ cluster_vip }}
8879
- port {{ postgresql_port }}
8980
- +------------------------------------------------+
90-
when: with_haproxy_load_balancing == "false" and install_pgbouncer != "true"
81+
when: with_haproxy_load_balancing == "false" and install_pgbouncer != "true"
82+
when: cluster_vip is defined and cluster_vip | length > 0
9183
ignore_errors: yes
9284
tags: conn_info
9385

@@ -102,4 +94,6 @@
10294
debug:
10395
msg: "Cluster ip address (VIP) {{ cluster_vip }} is running on server {{ ansible_hostname }}"
10496
when: man_ip is defined and man_ip == cluster_vip
97+
when: cluster_vip is defined and cluster_vip | length > 0
98+
ignore_errors: yes
10599
tags: [ vip_owner, vip_status ]

tasks/haproxy.yml

+22-1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,22 @@
345345
group: haproxy
346346
register: haproxy_conf_result
347347

348+
- name: haproxy | prepare haproxy.cfg conf file (replace "bind")
349+
lineinfile:
350+
path: /etc/haproxy/haproxy.cfg
351+
regexp: '{{ item.regexp }}'
352+
line: '{{ item.line }}'
353+
backrefs: yes
354+
loop:
355+
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
356+
- { regexp: '^.*bind.*:5000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5000' }
357+
- { regexp: '^.*bind.*:5001$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5001' }
358+
- { regexp: '^.*bind.*:5002$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5002' }
359+
- { regexp: '^.*bind.*:5003$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5003' }
360+
loop_control:
361+
label: "{{ item.line }}"
362+
when: cluster_vip is not defined or cluster_vip | length < 1
363+
348364
- name: haproxy | prepare haproxy.cfg conf file (replace "bind" for stats)
349365
lineinfile:
350366
path: /etc/haproxy/haproxy.cfg
@@ -353,9 +369,13 @@
353369
backrefs: yes
354370
loop:
355371
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
372+
- { regexp: '^.*bind.*:5000$', line: ' bind {{ cluster_vip }}:5000' }
373+
- { regexp: '^.*bind.*:5001$', line: ' bind {{ cluster_vip }}:5001' }
374+
- { regexp: '^.*bind.*:5002$', line: ' bind {{ cluster_vip }}:5002' }
375+
- { regexp: '^.*bind.*:5003$', line: ' bind {{ cluster_vip }}:5003' }
356376
loop_control:
357377
label: "{{ item.line }}"
358-
register: haproxy_conf_prepare_result
378+
when: cluster_vip is defined and cluster_vip | length > 0
359379
when: add_balancer is defined and add_balancer|bool
360380
tags: [ haproxy, haproxy_conf ]
361381

@@ -437,4 +457,5 @@
437457

438458
# virtual ip address (and haproxy health checking)
439459
- import_tasks: ../tasks/keepalived.yml
460+
when: cluster_vip is defined and cluster_vip | length > 0
440461
tags: keepalived

tasks/patroni.yml

+17
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@
278278
- { regexp: '^ connect_address: ((?!8008).)*$', line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ postgresql_port }}' }
279279
loop_control:
280280
label: "{{ item.line }}"
281+
when: with_haproxy_load_balancing == "true" or install_pgbouncer == "true" or (cluster_vip is not defined or cluster_vip | length < 1)
282+
283+
- name: Patroni | prepare patroni.yml conf file (replace "name","listen","connect_address")
284+
lineinfile:
285+
path: /etc/patroni/patroni.yml
286+
regexp: '{{ item.regexp }}'
287+
line: '{{ item.line }}'
288+
backrefs: yes
289+
loop:
290+
- { regexp: '^name:', line: 'name: {{ ansible_hostname }}' }
291+
- { regexp: '^ listen: .*:8008$', line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }}:8008' }
292+
- { regexp: '^ connect_address: .*:8008$', line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:8008' }
293+
- { regexp: '^ listen: ((?!8008).)*$', line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}' }
294+
- { regexp: '^ connect_address: ((?!8008).)*$', line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ postgresql_port }}' }
295+
loop_control:
296+
label: "{{ item.line }}"
297+
when: with_haproxy_load_balancing != "true" and install_pgbouncer != "true" and (cluster_vip is defined and cluster_vip | length > 0)
281298
when: existing_pgcluster is defined and existing_pgcluster|bool
282299
tags: [ patroni, patroni_conf ]
283300

tasks/pgbouncer.yml

+13
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@
111111
- { regexp: '^listen_addr =', line: 'listen_addr = {{ hostvars[inventory_hostname].inventory_hostname }}' }
112112
loop_control:
113113
label: "{{ item.line }}"
114+
when: with_haproxy_load_balancing == 'true' or (cluster_vip is not defined or cluster_vip | length < 1)
115+
116+
- name: PgBouncer | prepare pgbouncer.ini conf file (replace "listen_addr")
117+
lineinfile:
118+
path: /etc/pgbouncer/pgbouncer.ini
119+
regexp: '{{ item.regexp }}'
120+
line: '{{ item.line }}'
121+
backrefs: yes
122+
loop:
123+
- { regexp: '^listen_addr =', line: 'listen_addr = {{ hostvars[inventory_hostname].inventory_hostname }},{{ cluster_vip }}' }
124+
loop_control:
125+
label: "{{ item.line }}"
126+
when: with_haproxy_load_balancing != 'true' and (cluster_vip is defined and cluster_vip | length > 0 )
114127
when: existing_pgcluster is defined and existing_pgcluster|bool
115128
tags: [ pgbouncer_conf, pgbouncer ]
116129

templates/haproxy.cfg.j2

+16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ listen stats
2626
stats uri /
2727

2828
listen master
29+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
2930
bind {{ cluster_vip }}:5000
31+
{% else %}
32+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5000
33+
{% endif %}
3034
maxconn 10000
3135
option tcplog
3236
option httpchk OPTIONS /master
@@ -44,7 +48,11 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
4448
{% endif %}
4549

4650
listen replicas
51+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
4752
bind {{ cluster_vip }}:5001
53+
{% else %}
54+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5001
55+
{% endif %}
4856
maxconn 10000
4957
option tcplog
5058
option httpchk OPTIONS /replica
@@ -63,7 +71,11 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
6371
{% endif %}
6472

6573
listen replicas_sync
74+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
6675
bind {{ cluster_vip }}:5002
76+
{% else %}
77+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5002
78+
{% endif %}
6779
maxconn 10000
6880
option tcplog
6981
option httpchk OPTIONS /sync
@@ -82,7 +94,11 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
8294
{% endif %}
8395

8496
listen replicas_async
97+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
8598
bind {{ cluster_vip }}:5003
99+
{% else %}
100+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5003
101+
{% endif %}
86102
maxconn 10000
87103
option tcplog
88104
option httpchk OPTIONS /async

templates/haproxy.tmpl.j2

+16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ listen stats
2626
stats uri /
2727

2828
listen master
29+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
2930
bind {{ cluster_vip }}:5000
31+
{% else %}
32+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5000
33+
{% endif %}
3034
maxconn 10000
3135
option tcplog
3236
option httpchk OPTIONS /master
@@ -43,7 +47,11 @@ listen master
4347

4448

4549
listen replicas
50+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
4651
bind {{ cluster_vip }}:5001
52+
{% else %}
53+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5001
54+
{% endif %}
4755
maxconn 10000
4856
option tcplog
4957
option httpchk OPTIONS /replica
@@ -61,7 +69,11 @@ listen replicas
6169

6270

6371
listen replicas_sync
72+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
6473
bind {{ cluster_vip }}:5002
74+
{% else %}
75+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5002
76+
{% endif %}
6577
maxconn 10000
6678
option tcplog
6779
option httpchk OPTIONS /sync
@@ -79,7 +91,11 @@ listen replicas_sync
7991

8092

8193
listen replicas_async
94+
{% if cluster_vip is defined and cluster_vip | length > 0 %}
8295
bind {{ cluster_vip }}:5003
96+
{% else %}
97+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5003
98+
{% endif %}
8399
maxconn 10000
84100
option tcplog
85101
option httpchk OPTIONS /async

templates/patroni.yml.j2

+3-4
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ bootstrap:
8181

8282

8383
postgresql:
84-
{% if with_haproxy_load_balancing == "true" or install_pgbouncer == "true" %}
85-
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},127.0.0.1:{{ postgresql_port }}
86-
{% endif %}
87-
{% if with_haproxy_load_balancing != "true" and install_pgbouncer != "true" %}
84+
{% if with_haproxy_load_balancing != "true" and install_pgbouncer != "true" and (cluster_vip is defined and cluster_vip | length > 0) %}
8885
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}
86+
{% else %}
87+
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},127.0.0.1:{{ postgresql_port }}
8988
{% endif %}
9089
connect_address: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ postgresql_port }}
9190
use_unix_socket: true

templates/pgbouncer.ini.j2

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
[pgbouncer]
99
logfile = {{ pgbouncer_log_dir }}/pgbouncer.log
1010
pidfile = /var/run/pgbouncer/pgbouncer.pid
11-
{% if with_haproxy_load_balancing == 'true' %}
12-
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }}
13-
{% endif %}
14-
{% if with_haproxy_load_balancing != 'true' %}
11+
{% if with_haproxy_load_balancing != 'true' and cluster_vip is defined and cluster_vip | length > 0 %}
1512
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }}
13+
{% else %}
14+
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }}
1615
{% endif %}
1716
listen_port = {{ pgbouncer_listen_port | default(6432) }}
1817
unix_socket_dir = /var/run/postgresql

vars/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ proxy_env: {}
77
# -------------------------------------------
88

99
# Cluster variables
10-
cluster_vip: "10.128.64.145" # for client access to databases in the cluster
10+
cluster_vip: "10.128.64.145" # for client access to databases in the cluster (optional)
1111
vip_interface: "{{ ansible_default_ipv4.interface }}" # interface name (ex. "ens32")
1212

1313
patroni_cluster_name: "postgres-cluster" # specify the cluster name

0 commit comments

Comments
 (0)