Skip to content

Commit 5daf05b

Browse files
committed
Scaling: add new haproxy balancer node.
Add playbook "add_balancer.yml". See more in README.md file -> "Scaling: add new haproxy balancer node".
1 parent dbad28f commit 5daf05b

File tree

5 files changed

+110
-10
lines changed

5 files changed

+110
-10
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ proxy_env:
172172
See the vars/[main.yml](./vars/main.yml), [system.yml](./vars/system.yml) and ([Debian.yml](./vars/Debian.yml) or [RedHat.yml](./vars/RedHat.yml)) files for more details.
173173

174174

175-
## Scaling: add a new node to an existing postgres cluster
175+
## Scaling: add new postgresql node to existing cluster
176+
<details><summary>Click here to expand...</summary><p>
177+
176178
After you successfully deployed your PostgreSQL HA cluster, you may need to scale it further. \
177179
Use the `add_pgnode.yml` playbook for this.
178180

@@ -201,6 +203,40 @@ Variables that should be the same on all cluster nodes: \
201203

202204
`ansible-playbook add_pgnode.yml`
203205

206+
</p></details>
207+
208+
209+
## Scaling: add new haproxy balancer node
210+
<details><summary>Click here to expand...</summary><p>
211+
212+
Use the `add_balancer.yml` playbook for this.
213+
214+
During the run this playbook, the new balancer node will be prepared in the same way as when first deployment the cluster. But unlike the initial deployment, **all necessary configuration files will be copied from the server specified in the [master] group**.
215+
216+
> :heavy_exclamation_mark: Please test it in your test enviroment before using in a production.
217+
218+
###### Steps to add a new banlancer node:
219+
220+
1. Go to the playbook directory
221+
222+
2. Edit the inventory file
223+
224+
Specify the ip address of one of the existing balancer nodes in the [master] group, and the new balancer node (which you want to add) in the [balancers] group.
225+
226+
> :heavy_exclamation_mark: Attention! The list of Firewall ports is determined dynamically based on the group in which the host is specified. \
227+
If you adding a new haproxy balancer node to one of the existing nodes from the [etcd_cluster] or [master]/[replica] groups, you can rewrite the iptables rules! \
228+
See firewall_allowed_tcp_ports_for.balancers variable in the system.yml file.
229+
230+
3. Edit the `main.yml` variable file
231+
232+
Specify `with_haproxy_load_balancing: 'true'`
233+
234+
4. Run playbook:
235+
236+
`ansible-playbook add_balancer.yml`
237+
238+
</p></details>
239+
204240

205241
## Maintenance
206242
Please note that the original design goal of this playbook was more concerned with the initial deploiment of a PostgreSQL HA Cluster and so it does not currently concern itself with performing ongoing maintenance of a cluster.

add_balancer.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
3+
- name: Add haproxy balancer node
4+
hosts: balancers
5+
become: true
6+
become_method: sudo
7+
any_errors_fatal: true
8+
gather_facts: true
9+
vars_files:
10+
- vars/main.yml
11+
- vars/system.yml
12+
- "vars/{{ ansible_os_family }}.yml"
13+
vars:
14+
add_balancer: true
15+
16+
pre_tasks:
17+
- import_tasks: tasks/check_system.yml
18+
19+
- import_tasks: tasks/hostname.yml
20+
when: hostname is defined and hostname | length > 0
21+
tags: hostname
22+
23+
- import_tasks: tasks/sysctl.yml
24+
tags: [ sysctl, kernel ]
25+
26+
- name: Make sure the gnupg package is present
27+
apt:
28+
name: gnupg
29+
state: present
30+
update_cache: yes
31+
environment: '{{ proxy_env | default({}) }}'
32+
when: ansible_os_family == "Debian" and installation_method == "repo"
33+
tags: [ add_repo, install_packages, install_postgres ]
34+
35+
- name: Firewall | build a firewall_ports_dynamic_var
36+
set_fact:
37+
firewall_ports_dynamic_var: "{{ firewall_ports_dynamic_var |default([]) }} + {{ firewall_allowed_tcp_ports_for[item] }}"
38+
loop: '{{ hostvars[inventory_hostname].group_names }}'
39+
when: firewall_enabled_at_boot|bool
40+
tags: firewall
41+
42+
- name: Firewall | build a firewall_rules_dynamic_var
43+
set_fact:
44+
firewall_rules_dynamic_var: "{{ firewall_rules_dynamic_var |default([]) }} + {{ firewall_additional_rules_for[item] }}"
45+
loop: '{{ hostvars[inventory_hostname].group_names }}'
46+
when: firewall_enabled_at_boot|bool
47+
tags: firewall
48+
49+
roles:
50+
- role: ansible-role-firewall
51+
environment: '{{ proxy_env | default({}) }}'
52+
vars:
53+
firewall_allowed_tcp_ports: '{{ firewall_ports_dynamic_var | unique }}'
54+
firewall_additional_rules: '{{ firewall_rules_dynamic_var | unique }}'
55+
when: firewall_enabled_at_boot|bool
56+
tags: firewall
57+
58+
tasks:
59+
- meta: flush_handlers
60+
61+
- import_tasks: tasks/haproxy.yml
62+
when: with_haproxy_load_balancing == "true"
63+
tags: load_balancing
64+

tasks/confd.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
src: templates/haproxy.tmpl.j2
5151
dest: /etc/confd/templates/haproxy.tmpl
5252
tags: haproxy_tmpl
53-
when: existing_pgcluster is not defined or not existing_pgcluster|bool
53+
when: add_balancer is not defined or not add_balancer|bool
5454
tags: [ confd_conf, confd ]
5555

56-
- block: # for add_pgnode.yml
56+
- block: # for add_balancer.yml
5757
- name: confd | fetch confd.toml, haproxy.toml, haproxy.tmpl conf files from master
5858
run_once: true
5959
fetch:
@@ -88,7 +88,7 @@
8888
- { regexp: '^.*bind.*:7000$', line: ' bind {{ hostvars[inventory_hostname].inventory_hostname }}:7000' }
8989
loop_control:
9090
label: "{{ item.line }}"
91-
when: existing_pgcluster is defined and existing_pgcluster|bool
91+
when: add_balancer is defined and add_balancer|bool
9292
tags: [ confd_conf, confd ]
9393

9494
- name: confd | copy systemd service file

tasks/haproxy.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@
324324
owner: haproxy
325325
group: haproxy
326326
register: haproxy_conf_result
327-
when: existing_pgcluster is not defined or not existing_pgcluster|bool
327+
when: add_balancer is not defined or not add_balancer|bool
328328
tags: [ haproxy, haproxy_conf ]
329329

330-
- block: # for add_pgnode.yml
330+
- block: # for add_balancer.yml
331331
- name: haproxy | fetch haproxy.cfg file from master
332332
run_once: true
333333
fetch:
@@ -356,7 +356,7 @@
356356
loop_control:
357357
label: "{{ item.line }}"
358358
register: haproxy_conf_prepare_result
359-
when: existing_pgcluster is defined and existing_pgcluster|bool
359+
when: add_balancer is defined and add_balancer|bool
360360
tags: [ haproxy, haproxy_conf ]
361361

362362
- name: haproxy | selinux | make sure the libsemanage-python, policycoreutils-python packages is present

tasks/keepalived.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
src: templates/keepalived.conf.j2
4444
dest: /etc/keepalived/keepalived.conf
4545
register: keepalived_conf_result
46-
when: existing_pgcluster is not defined or not existing_pgcluster|bool
46+
when: add_balancer is not defined or not add_balancer|bool
4747
tags: [ keepalived_conf, keepalived ]
4848

49-
- block: # for add_pgnode.yml
49+
- block: # for add_balancer.yml
5050
- name: keepalived | fetch keepalived.conf conf file from master
5151
run_once: true
5252
fetch:
@@ -73,7 +73,7 @@
7373
loop_control:
7474
label: "{{ item.line }}"
7575
register: keepalived_conf_prepare_result
76-
when: existing_pgcluster is defined and existing_pgcluster|bool
76+
when: add_balancer is defined and add_balancer|bool
7777
tags: [ keepalived_conf, keepalived ]
7878

7979
- name: keepalived | selinux | change the keepalived_t domain to permissive

0 commit comments

Comments
 (0)