fix deployment and document root ssh authorization
[pgpool-online-recovery] / README.md
1 pgpool-online-recovery
2 ======================
3
4 This simple project aims to automate and make easy the online recovery process of a failed pgpool's backend node in master/slave mode.
5
6 This version is work-in-progress using Centos7 and upstream packages. It doesn't require psmisc package, making Centos7 minimal installation sufficient for scripts to run, since it uses systemd to manage postgresql-9.6 installed in /var/lib/pgsql/9.6/data/
7
8 Hardware configuration is 3 nodes:
9
10 10.200.1.60 edozvola-pgpool
11 10.200.1.61 edozvola-db-01
12 10.200.1.62 edozvola-db-02
13
14 Deployment script ./t/1-init-cluster.sh assumes that management machine from which it's run is 10.200.1.1
15 which is added in pg_hba.conf as authorized to be able to deploy cluster. It also assumes that management machine
16 has ssh access to nodes of clustetr using ssh keys or you will need to type passwords multiple times.
17
18 You can run cluster creation it with:
19
20 make init
21
22 This will destroy all databases on all nodes, archive logs, etc, so don't do this if you need your old data later.
23
24 On the other hand this will also create setup whole cluster, and you can examine it's status using:
25
26 make
27
28 If you edited local files, push changes to all nodes using:
29
30 make push
31
32 To restart pgpool (and cleanup it's state) do:
33
34 make restart
35
36 If you want to see systemd status of services just type:
37
38 make status
39
40
41 Now you can start './t/80-insert-test.sh' in one terminal to create insert and select load on cluster and
42 kill one of nodes with 'echo b > /proc/sysrq-trigger'
43
44 For example, kill slave:
45
46 ssh root@10.200.1.62 'echo b > /proc/sysrq-trigger'
47
48 pgpool should detect broken back-end and remove it.
49 You can verify that using just 'make' and see that one node is down.
50 To issue online recovery, you can use:
51
52 make fix
53
54 now, try to kill master:
55
56 ssh root@10.200.1.61 'echo b > /proc/sysrq-trigger'
57
58 FIXME: pgpool is stuck and needs to be restarted
59
60
61 If installing on existing streaming replication you will need to tell pgpool where current master is with:
62
63 echo 0 > /tmp/postgres_master
64
65 You can also force re-check of nodes by removing status file and restarting pgool:
66
67 rm /var/log/pgpool_status
68 systemctl restart pgpool
69
70
71
72 Requirements
73 ============
74
75 There are two requirements to these scripts to work.
76
77 * The first one is [pgpool-II](http://www.pgpool.net) (v3.6.5) available for [Centos7 from upstream](http://www.pgpool.net/yum/rpms/3.6/redhat/rhel-7-x86_64/pgpool-II-pg96-3.6.5-1pgdg.rhel7.x86_64.rpm).
78 * The second one is obviously Postgres server (v9.6) also for [Centos7 from upstream](https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm)
79
80 There are several tutorials about setting up pgpool2 and postgres servers with [Streaming Replication](http://wiki.postgresql.org/wiki/Streaming_Replication) and this readme is far to be a howto for configuring both of them.
81
82 Installation and configuration
83 ==============================
84 What about the given scripts and config files ?
85
86 **pgpool.conf** : This is a sample config file for pgpool that activates master/slave mode, loadbalancing, backends health check, failover, ...
87
88 **postgresql.conf.master** : A config file for postgres master node.
89
90 **postgresql.conf.slave** : A config file for postgres slave node.
91
92 **recovery.conf** : A config file used by postgres slave for streaming replication process.
93
94 **failover.sh** : This script will be executed automatically when a pgpool's backend node (postgres node) fails down. It'll switch the standby node (slave) to master (new master).
95
96 **online-recovery.sh** : This is the bash script which you'll execute manually in order to :
97 * Reboot, sync and reattach slave node to pgpool if it fails.
98 * Setup new master and new slave, sync and reattach them to pgpool if current master fails.
99 This script will invoque remotely the script streaming-replication.sh (in the new slave node) to start the [online recovery process](http://www.postgresql.org/docs/8.1/static/backup-online.html) within the standby node.
100 PS : When a node (master or slave) fails, pgpool still running and DBs remain available. Otherwise, pgpool will detach this node for data consistancy reasons.
101
102 **streaming-replication.sh** : This script can be executed manually to synchronize a slave node with a given master node (master name/ip must be passed as argument to streaming-replication.sh). Otherwise, this same script is triggred be online-recovery.sh via ssh during failback process.
103
104 Installation
105 ------------
106
107 The installation steps are simple. You just need to copy provided bash scripts and config files as follow.
108
109 **In pgpool node** :
110 * Copy pgpool.conf to /etc/pgpool-II/. This is an optional operation and in this case you have to edit the default pgpool.conf file in order to looks like the config file we provided.
111 * Copy failover.sh into /etc/pgpool-II/ and online-recovery.sh to same directory or another directory that will be easily accessible.
112
113 **In the master and slave postgres nodes** :
114 * Copy streaming-replication.sh script into /var/lib/pgsql/ (postgres homedir).
115 * Copy postgresql.conf.master and postgresql.conf.slave files to /var/lib/pgsql/9.6/data/.
116 * Finally copy recovery.conf into /var/lib/postgresql/9.1/main/.
117
118 PS : All similar old files must be backed up to be able to rollback in case of risk (e.g: cp -p /etc/pgpool-II/pgpool.conf /etc/pgpool-II/pgpool.conf.backup).
119 Make sure that :
120 - All scripts are executable and owned by the proper users. 
121 - /var/lib/pgsql/9.6/archive directory is created (used to archive WAL files). This folder must be owned by postgres user !
122 - Do not forge to edit pg_hba.conf in each postgres server to allow access to cluster's nodes.
123
124 Not enough ! It remains only the configuration steps and we'll be done :)
125
126 Configuration
127 -------------
128
129 To do, just follow these steps :
130
131 1- First of all make sure you have created a postgres user in pgpool node with SSH access to all Postgres nodes. All cluster's nodes have to be able to ssh each other. You can put "config" file with "StrictHostKeyChecking=no" option under .ssh/ directory of postgres user. This is a best practice (essencially when automating a bunch of operations) that allows postgres to ssh remote machine for the first time without prompting and validating Yes/No authorization question.
132
133 2- In Pgpool node set up pgpool.conf file for instance the parameters :
134
135         # Controls various backend behavior for instance master and slave(s).
136         backend_hostname0='master.foo.bar'
137         backend_port0 = 5432
138         backend_weight0 = 1
139         backend_data_directory0 = '/var/lib/postgres/9.1/main/'
140         backend_flag0 = 'ALLOW_TO_FAILOVER'
141         backend_hostname1='slave.foo.bar'
142         backend_port1 = 5432
143         backend_weight1 = 1
144         backend_data_directory1 = '/var/lib/postgres/9.1/main/'
145         backend_flag1 = 'ALLOW_TO_FAILOVER'
146         # Pool size
147         num_init_children = 32
148         max_pool = 4
149         # Master/Slave and load balancing (replication mode must be off)
150         load_balance_mode = on
151         master_slave_mode = on
152         master_slave_sub_mode = 'stream'
153         #Health check (must be set up to detecte postgres server status up/down)
154         health_check_period = 30
155         health_check_user = 'postgres'
156         health_check_password = 'postgrespass'
157         # - Special commands -
158         follow_master_command = 'echo %M > /tmp/postgres_master'
159         # Failover command
160         failover_command = '/path/to/failover.sh %d %H %P /tmp/trigger_file'
161
162 3- In failover.sh script, specify the proper ssh private key to postgres user to access new master  node via SSH.
163
164         ssh -i /var/lib/postgresql/.ssh/id_rsa postgres@$new_master "touch $trigger_file"
165
166 4- Idem for online-recovery.sh you have juste to change if needed the postgres's private key, the rest of params is set automatically when the script runs. Magic hein ! :)
167
168 5- Change the primary_conninfo access parameters (to master) in recovery.conf file in slave side :
169
170         primary_conninfo = 'host=master-or-slave.foo.bar port=5432 user=postgres password=nopass'
171
172 6- Rename recovery.conf to recovery.done in master side.
173
174 7- Setup postgres master node (after backup of postgresql.conf) :
175
176         cp -p postgresql.conf.master postgresql.conf
177         /etc/init.d/postgresql restart
178
179 8- Setup postgres slave node (after backup of postgresql.conf) :
180
181         cp -p postgresql.conf.slave postgresql.conf
182
183 9- Start first slave synchronisation with master by executing streaming-replication.sh as postgres user :
184
185         su postgres
186         cd ~
187         ./streaming-replication.sh master.foo.bar
188
189 10- Restart pgpool :
190
191         /etc/init.d/pgpool2 restart
192
193 At his stage slave node is connected to master and both of them are connected to pgpool. If the master fails down, pgpool detach it from the pool and perform failover process (slave become master) automatically.
194
195 Tests
196 =====
197
198 Test PCP interface (as root) :
199
200         #retrieves the node information
201         pcp_node_info 10 localhost 9898 postgres "postgres-pass" "postgres-id"
202         #detaches a node from pgpool
203         pcp_detach_node 10 localhost 9898 postgres "postgres-pass" "postgres-id"
204         #attaches a node to pgpool
205         pcp_attach_node 10 localhost 9898 postgres "postgres-pass" "postgres-id"
206
207 After starting pgpool, try to test this two scenarios :
208
209 **1. When a slave fails down** :
210
211 Open pgpool log file 'journalctl -u pgpool -f'
212
213 Stop slave node 'sudo systemctl stop postgresql-9.6'
214
215 After exceeding health_check_period, you should see this log message :
216
217         [INFO] Slave node is down. Failover not triggred !
218
219 Now, start slave failback process (as root) :
220
221         # ./online-recovery.sh
222
223 **2. When a master fails down** :
224
225 Idem, open pgpool log file.
226
227 Stop master node '/etc/init.d/postgres stop'.
228
229 After exceeding health_check_period, you should see this log message :
230
231         [INFO] Master node is down. Performing failover...
232
233 Start failback process (as root) to switch master(new slave) and slave(new master) roles :
234
235         # ./online-recovery.sh