Skip to content

Commit

Permalink
VIP: make the cluster ip address as optional.
Browse files Browse the repository at this point in the history
Now, you can deploy your custom schemes without using a VIP (cluster IP address) if necessary.
  • Loading branch information
vitabaks committed Feb 7, 2020
1 parent 7079672 commit 176a36a
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 29 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ proxy_env:
http_proxy: http://proxy_server_ip:port
https_proxy: http://proxy_server_ip:port
```
- `cluster_vip`
- `cluster_vip` # for client access to databases in the cluster (optional)
- `patroni_cluster_name`
- `with_haproxy_load_balancing` `'true'` (Type A) or `'false'`/default (Type B)
- `postgresql_version`
- `postgresql_data_dir`


5. Run playbook:
Expand Down
4 changes: 2 additions & 2 deletions add_balancer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
- import_tasks: tasks/hostname.yml
when: hostname is defined and hostname | length > 0
tags: hostname

- import_tasks: tasks/sysctl.yml
tags: [ sysctl, kernel ]

- name: Make sure the gnupg package is present
apt:
name: gnupg
Expand Down
2 changes: 1 addition & 1 deletion add_pgnode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
tags: patroni

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

# finish (info)
Expand Down
4 changes: 2 additions & 2 deletions deploy_pgcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
tags: [ sysctl, kernel ]

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


Expand Down Expand Up @@ -137,7 +137,7 @@
tags: patroni

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

# optional
Expand Down
21 changes: 21 additions & 0 deletions tasks/confd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,29 @@
backrefs: yes
loop:
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
- { regexp: '^.*bind.*:5000$', line: ' bind {{ cluster_vip }}:5000' }
- { regexp: '^.*bind.*:5001$', line: ' bind {{ cluster_vip }}:5001' }
- { regexp: '^.*bind.*:5002$', line: ' bind {{ cluster_vip }}:5002' }
- { regexp: '^.*bind.*:5003$', line: ' bind {{ cluster_vip }}:5003' }
loop_control:
label: "{{ item.line }}"
when: cluster_vip is defined and cluster_vip | length > 0

- name: confd | prepare haproxy.tmpl template file (replace "bind" for stats)
lineinfile:
path: /etc/confd/templates/haproxy.tmpl
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
backrefs: yes
loop:
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
- { regexp: '^.*bind.*:5000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5000' }
- { regexp: '^.*bind.*:5001$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5001' }
- { regexp: '^.*bind.*:5002$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5002' }
- { regexp: '^.*bind.*:5003$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5003' }
loop_control:
label: "{{ item.line }}"
when: cluster_vip is not defined or cluster_vip | length < 1
when: add_balancer is defined and add_balancer|bool
tags: [ confd_conf, confd ]

Expand Down
20 changes: 7 additions & 13 deletions tasks/deploy_finish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@
- port 5002 (read only) synchronous replica only
- port 5003 (read only) asynchronous replicas only
- +------------------------------------------------+
when: with_haproxy_load_balancing == "true" and synchronous_mode == "true"
ignore_errors: yes
tags: conn_info
when: with_haproxy_load_balancing == "true" and synchronous_mode == "true"

- block:
- name: PostgreSQL Cluster connection info
run_once: true
debug:
Expand All @@ -61,11 +58,8 @@
- port 5000 (read/write) master
- port 5001 (read only) all replicas
- +------------------------------------------------+
when: with_haproxy_load_balancing == "true" and synchronous_mode != "true"
ignore_errors: yes
tags: conn_info
when: with_haproxy_load_balancing == "true" and synchronous_mode != "true"

- block:
- name: PostgreSQL Cluster connection info
run_once: true
debug:
Expand All @@ -74,11 +68,8 @@
- address (VIP) {{ cluster_vip }}
- port {{ pgbouncer_listen_port }} (pgbouncer)
- +------------------------------------------------+
when: with_haproxy_load_balancing == "false" and install_pgbouncer == "true"
ignore_errors: yes
tags: conn_info
when: with_haproxy_load_balancing == "false" and install_pgbouncer == "true"

- block:
- name: PostgreSQL Cluster connection info
run_once: true
debug:
Expand All @@ -87,7 +78,8 @@
- address (VIP) {{ cluster_vip }}
- port {{ postgresql_port }}
- +------------------------------------------------+
when: with_haproxy_load_balancing == "false" and install_pgbouncer != "true"
when: with_haproxy_load_balancing == "false" and install_pgbouncer != "true"
when: cluster_vip is defined and cluster_vip | length > 0
ignore_errors: yes
tags: conn_info

Expand All @@ -102,4 +94,6 @@
debug:
msg: "Cluster ip address (VIP) {{ cluster_vip }} is running on server {{ ansible_hostname }}"
when: man_ip is defined and man_ip == cluster_vip
when: cluster_vip is defined and cluster_vip | length > 0
ignore_errors: yes
tags: [ vip_owner, vip_status ]
23 changes: 22 additions & 1 deletion tasks/haproxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,22 @@
group: haproxy
register: haproxy_conf_result

- name: haproxy | prepare haproxy.cfg conf file (replace "bind")
lineinfile:
path: /etc/haproxy/haproxy.cfg
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
backrefs: yes
loop:
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
- { regexp: '^.*bind.*:5000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5000' }
- { regexp: '^.*bind.*:5001$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5001' }
- { regexp: '^.*bind.*:5002$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5002' }
- { regexp: '^.*bind.*:5003$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:5003' }
loop_control:
label: "{{ item.line }}"
when: cluster_vip is not defined or cluster_vip | length < 1

- name: haproxy | prepare haproxy.cfg conf file (replace "bind" for stats)
lineinfile:
path: /etc/haproxy/haproxy.cfg
Expand All @@ -353,9 +369,13 @@
backrefs: yes
loop:
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
- { regexp: '^.*bind.*:5000$', line: ' bind {{ cluster_vip }}:5000' }
- { regexp: '^.*bind.*:5001$', line: ' bind {{ cluster_vip }}:5001' }
- { regexp: '^.*bind.*:5002$', line: ' bind {{ cluster_vip }}:5002' }
- { regexp: '^.*bind.*:5003$', line: ' bind {{ cluster_vip }}:5003' }
loop_control:
label: "{{ item.line }}"
register: haproxy_conf_prepare_result
when: cluster_vip is defined and cluster_vip | length > 0
when: add_balancer is defined and add_balancer|bool
tags: [ haproxy, haproxy_conf ]

Expand Down Expand Up @@ -437,4 +457,5 @@

# virtual ip address (and haproxy health checking)
- import_tasks: ../tasks/keepalived.yml
when: cluster_vip is defined and cluster_vip | length > 0
tags: keepalived
17 changes: 17 additions & 0 deletions tasks/patroni.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,23 @@
- { regexp: '^ connect_address: ((?!8008).)*$', line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ postgresql_port }}' }
loop_control:
label: "{{ item.line }}"
when: with_haproxy_load_balancing == "true" or install_pgbouncer == "true" or (cluster_vip is not defined or cluster_vip | length < 1)

- name: Patroni | prepare patroni.yml conf file (replace "name","listen","connect_address")
lineinfile:
path: /etc/patroni/patroni.yml
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
backrefs: yes
loop:
- { regexp: '^name:', line: 'name: {{ ansible_hostname }}' }
- { regexp: '^ listen: .*:8008$', line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }}:8008' }
- { regexp: '^ connect_address: .*:8008$', line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:8008' }
- { regexp: '^ listen: ((?!8008).)*$', line: ' listen: {{ hostvars[inventory_hostname].inventory_hostname }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}' }
- { regexp: '^ connect_address: ((?!8008).)*$', line: ' connect_address: {{ hostvars[inventory_hostname].inventory_hostname }}:{{ postgresql_port }}' }
loop_control:
label: "{{ item.line }}"
when: with_haproxy_load_balancing != "true" and install_pgbouncer != "true" and (cluster_vip is defined and cluster_vip | length > 0)
when: existing_pgcluster is defined and existing_pgcluster|bool
tags: [ patroni, patroni_conf ]

Expand Down
13 changes: 13 additions & 0 deletions tasks/pgbouncer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@
- { regexp: '^listen_addr =', line: 'listen_addr = {{ hostvars[inventory_hostname].inventory_hostname }}' }
loop_control:
label: "{{ item.line }}"
when: with_haproxy_load_balancing == 'true' or (cluster_vip is not defined or cluster_vip | length < 1)

- name: PgBouncer | prepare pgbouncer.ini conf file (replace "listen_addr")
lineinfile:
path: /etc/pgbouncer/pgbouncer.ini
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
backrefs: yes
loop:
- { regexp: '^listen_addr =', line: 'listen_addr = {{ hostvars[inventory_hostname].inventory_hostname }},{{ cluster_vip }}' }
loop_control:
label: "{{ item.line }}"
when: with_haproxy_load_balancing != 'true' and (cluster_vip is defined and cluster_vip | length > 0 )
when: existing_pgcluster is defined and existing_pgcluster|bool
tags: [ pgbouncer_conf, pgbouncer ]

Expand Down
16 changes: 16 additions & 0 deletions templates/haproxy.cfg.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ listen stats
stats uri /

listen master
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5000
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5000
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /master
Expand All @@ -44,7 +48,11 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
{% endif %}

listen replicas
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5001
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5001
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /replica
Expand All @@ -63,7 +71,11 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
{% endif %}

listen replicas_sync
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5002
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5002
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /sync
Expand All @@ -82,7 +94,11 @@ server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hos
{% endif %}

listen replicas_async
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5003
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5003
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /async
Expand Down
16 changes: 16 additions & 0 deletions templates/haproxy.tmpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ listen stats
stats uri /

listen master
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5000
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5000
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /master
Expand All @@ -43,7 +47,11 @@ listen master


listen replicas
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5001
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5001
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /replica
Expand All @@ -61,7 +69,11 @@ listen replicas


listen replicas_sync
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5002
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5002
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /sync
Expand All @@ -79,7 +91,11 @@ listen replicas_sync


listen replicas_async
{% if cluster_vip is defined and cluster_vip | length > 0 %}
bind {{ cluster_vip }}:5003
{% else %}
bind {{ hostvars[inventory_hostname]['inventory_hostname'] }}:5003
{% endif %}
maxconn 10000
option tcplog
option httpchk OPTIONS /async
Expand Down
7 changes: 3 additions & 4 deletions templates/patroni.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ bootstrap:


postgresql:
{% if with_haproxy_load_balancing == "true" or install_pgbouncer == "true" %}
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},127.0.0.1:{{ postgresql_port }}
{% endif %}
{% if with_haproxy_load_balancing != "true" and install_pgbouncer != "true" %}
{% if with_haproxy_load_balancing != "true" and install_pgbouncer != "true" and (cluster_vip is defined and cluster_vip | length > 0) %}
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }},127.0.0.1:{{ postgresql_port }}
{% else %}
listen: {{ hostvars[inventory_hostname]['inventory_hostname'] }},127.0.0.1:{{ postgresql_port }}
{% endif %}
connect_address: {{ hostvars[inventory_hostname]['inventory_hostname'] }}:{{ postgresql_port }}
use_unix_socket: true
Expand Down
7 changes: 3 additions & 4 deletions templates/pgbouncer.ini.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
[pgbouncer]
logfile = {{ pgbouncer_log_dir }}/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid
{% if with_haproxy_load_balancing == 'true' %}
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }}
{% endif %}
{% if with_haproxy_load_balancing != 'true' %}
{% if with_haproxy_load_balancing != 'true' and cluster_vip is defined and cluster_vip | length > 0 %}
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }},{{ cluster_vip }}
{% else %}
listen_addr = {{ hostvars[inventory_hostname]['inventory_hostname'] }}
{% endif %}
listen_port = {{ pgbouncer_listen_port | default(6432) }}
unix_socket_dir = /var/run/postgresql
Expand Down
2 changes: 1 addition & 1 deletion vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ proxy_env: {}
# -------------------------------------------

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

patroni_cluster_name: "postgres-cluster" # specify the cluster name
Expand Down

0 comments on commit 176a36a

Please sign in to comment.