Skip to content

Commit fd6b0ec

Browse files
committed
Do not listen to all ip addresses.
If the server has a public IP address, then listening to all ip addresses is not secure. Now we only listen to ip addresses specified in the inventory file. More details: * If the scheme "Type B" (by default) is used, then the connection pooller (pgbouncer) will listen in addition to the ip address specified in the inventory file also will be used the cluster ip address ("cluster_vip" variable). Postgresql will be listen for the ip address from the inventory file and the local ip 127.0.0.1. * If you do not want to install and configure the pgbouncer (install_pgbouncer: 'false'), then postgresql will also listen on the cluster ip address ("cluster_vip" variable). * In the scheme "Type A" (with_haproxy_load_balancing: 'true '), the haproxy listen only for the cluster ip address ("cluster_vip" variable).
1 parent 739b30a commit fd6b0ec

File tree

9 files changed

+32
-22
lines changed

9 files changed

+32
-22
lines changed

inventory

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This is example inventory file!
22
# Please specify the ip addresses and connection settings for your environment
3+
# The specified ip addresses will be used to listen by the cluster components.
34

45
# "postgresql_exists='true'" if PostgreSQL is already exists and runing
56
# "hostname=" variable is optional (used to change the server name)

tasks/haproxy.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@
138138
enabled: yes
139139
state: restarted
140140

141-
- name: haproxy | wait for port 5000 to become open on the host
141+
- name: haproxy | check HAProxy is started and accepting connections
142142
wait_for:
143-
port: 5000
144-
host: 127.0.0.1
143+
port: 7000
144+
host: "{{ hostvars[inventory_hostname]['inventory_hostname'] }}"
145145
state: started
146146
timeout: 120
147147
delay: 10

tasks/patroni.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@
281281
- name: Patroni | wait for port 8008 to become open on the host
282282
wait_for:
283283
port: 8008
284-
host: "{{ ansible_ssh_host }}"
284+
host: "{{ hostvars[inventory_hostname]['inventory_hostname'] }}"
285285
state: started
286286
timeout: 120
287287
delay: 10
@@ -361,7 +361,7 @@
361361
- name: Patroni | wait for port 8008 to become open on the host
362362
wait_for:
363363
port: 8008
364-
host: "{{ ansible_ssh_host }}"
364+
host: "{{ hostvars[inventory_hostname]['inventory_hostname'] }}"
365365
state: started
366366
timeout: 120
367367
delay: 10

tasks/pgbouncer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
- name: PgBouncer | wait for port "{{ pgbouncer_listen_port }}" to become open on the host
7777
wait_for:
7878
port: "{{ pgbouncer_listen_port }}"
79-
host: "{{ ansible_ssh_host }}"
79+
host: "{{ hostvars[inventory_hostname]['inventory_hostname'] }}"
8080
state: started
8181
timeout: 300
8282
delay: 5

templates/haproxy.cfg.j2

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ defaults
2121

2222
listen stats
2323
mode http
24-
bind 0.0.0.0:7000
24+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:7000
2525
stats enable
2626
stats uri /
2727

2828
listen master
29-
bind 0.0.0.0:5000
29+
bind {{ cluster_vip }}:5000
3030
maxconn 10000
3131
option tcplog
3232
option httpchk OPTIONS /master
@@ -44,7 +44,7 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
4444
{% endif %}
4545

4646
listen replicas
47-
bind 0.0.0.0:5001
47+
bind {{ cluster_vip }}:5001
4848
maxconn 10000
4949
option tcplog
5050
option httpchk OPTIONS /replica
@@ -63,7 +63,7 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
6363
{% endif %}
6464

6565
listen replicas_sync
66-
bind 0.0.0.0:5002
66+
bind {{ cluster_vip }}:5002
6767
maxconn 10000
6868
option tcplog
6969
option httpchk OPTIONS /sync
@@ -82,7 +82,7 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
8282
{% endif %}
8383

8484
listen replicas_async
85-
bind 0.0.0.0:5003
85+
bind {{ cluster_vip }}:5003
8686
maxconn 10000
8787
option tcplog
8888
option httpchk OPTIONS /async

templates/haproxy.tmpl.j2

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ defaults
2121

2222
listen stats
2323
mode http
24-
bind 0.0.0.0:7000
24+
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:7000
2525
stats enable
2626
stats uri /
2727

2828
listen master
29-
bind 0.0.0.0:5000
29+
bind {{ cluster_vip }}:5000
3030
maxconn 10000
3131
option tcplog
3232
option httpchk OPTIONS /master
@@ -43,7 +43,7 @@ listen master
4343

4444

4545
listen replicas
46-
bind 0.0.0.0:5001
46+
bind {{ cluster_vip }}:5001
4747
maxconn 10000
4848
option tcplog
4949
option httpchk OPTIONS /replica
@@ -61,7 +61,7 @@ listen replicas
6161

6262

6363
listen replicas_sync
64-
bind 0.0.0.0:5002
64+
bind {{ cluster_vip }}:5002
6565
maxconn 10000
6666
option tcplog
6767
option httpchk OPTIONS /sync
@@ -79,7 +79,7 @@ listen replicas_sync
7979

8080

8181
listen replicas_async
82-
bind 0.0.0.0:5003
82+
bind {{ cluster_vip }}:5003
8383
maxconn 10000
8484
option tcplog
8585
option httpchk OPTIONS /async

templates/patroni.yml.j2

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ name: {{ ansible_hostname }}
55
namespace: /service/
66

77
restapi:
8-
listen: {{ ansible_ssh_host }}:8008
9-
connect_address: {{ ansible_ssh_host }}:8008
8+
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:8008
9+
connect_address: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:8008
1010
# certfile: /etc/ssl/certs/ssl-cert-snakeoil.pem
1111
# keyfile: /etc/ssl/private/ssl-cert-snakeoil.key
1212
# authentication:
@@ -108,8 +108,13 @@ bootstrap:
108108

109109

110110
postgresql:
111-
listen: 0.0.0.0:{{ postgresql_port }}
112-
connect_address: {{ ansible_ssh_host }}:{{ postgresql_port }}
111+
{% if with_haproxy_load_balancing == "true" or install_pgbouncer == "true" %}
112+
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},127.0.0.1:{{ postgresql_port }}
113+
{% endif %}
114+
{% if with_haproxy_load_balancing != "true" and install_pgbouncer != "true" %}
115+
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}
116+
{% endif %}
117+
connect_address: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ postgresql_port }}
113118
use_unix_socket: true
114119
data_dir: {{ postgresql_data_dir }}
115120
bin_dir: {{ postgresql_bin_dir }}

templates/pgbouncer.ini.j2

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
[pgbouncer]
77
logfile = {{ pgbouncer_log_dir }}/pgbouncer.log
88
pidfile = /var/run/pgbouncer/pgbouncer.pid
9-
listen_addr = {{ pgbouncer_listen_address | default('*') }}
9+
{% if with_haproxy_load_balancing == 'true' %}
10+
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }}
11+
{% endif %}
12+
{% if with_haproxy_load_balancing != 'true' %}
13+
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }}
14+
{% endif %}
1015
listen_port = {{ pgbouncer_listen_port | default(6432) }}
1116
unix_socket_dir = /var/run/postgresql
1217
auth_type = md5

vars/main.yml

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ postgresql_pg_hba:
144144
install_pgbouncer: 'true' # or 'false' if you do not want to install and configure the pgbouncer service
145145
pgbouncer_conf_dir: "/etc/pgbouncer"
146146
pgbouncer_log_dir: "/var/log/pgbouncer"
147-
pgbouncer_listen_address: '0.0.0.0'
148147
pgbouncer_listen_port: 6432
149148
max_client_conn: 10000
150149
max_db_connections: 1000

0 commit comments

Comments
 (0)