Update README.md
[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 Requirements
7 ============
8
9 There are two requirements to these scripts to work.
10
11 * The first one is [pgpool2](http://www.pgpool.net) (v3.1.3) available in [Debian Wheezy](http://packages.debian.org/stable/database/pgpool2). We assume that pgpool2 is installed, set up in master/slave mode with loadbalacing and manageable via PCP interface.
12 * The second one is obviously Postgres server (v9.1) also available in Wheezy packages repository.
13
14 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. You can check out [this tutorial](https://aricgardner.com/databases/postgresql/pgpool-ii-3-0-5-with-streaming-replication/) which describes really all the steps needed.
15
16 Installation and configuration
17 ==============================
18 What about the given scripts and config files ?
19
20 **pgpool.conf** : This is a sample config file for pgpool that activates master/slave mode, loadbalancing, backends health check, failover, ...
21
22 **postgresql.conf.master** : A config file for postgres master node.
23
24 **postgresql.conf.slave** : A config file for postgres slave node.
25
26 **recovery.conf** : A config file used by postgres slave for streaming replication process.
27
28 **failover.sh** : This script will be executed automatically when a pgpool's backend node (postgres node) fall down. It'll switch the standby node (slave) to master (new master).
29
30 **online-recovery.sh** : This is the bash script which you'll execute manually in order to :
31 * Reboot, sync and reattach slave node to pgpool if it fails.
32 * Setup new master and new slave, sync and reattach them to pgpool if current master fails.
33 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.
34 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.
35
36 **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.
37
38 Installation
39 ------------
40
41 The installation steps are simple. You just need to copy provided bash scripts and config files as follow.
42
43 **In pgpool node** :
44 * Copy pgpool.conf to /etc/pgpool2/. 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.
45 * Copy failover.sh into /usr/local/bin/ and online-recovery.sh to your home or another directory that will be easily accessible.
46
47 **In the master and slave postgres nodes** :
48 * Copy streaming-replication.sh script into /var/lib/postgresql/ (postgres homedir).
49 * Copy postgresql.conf.master and postgresql.conf.slave files to /etc/postgresql/9.1/main/.
50 * Finally copy recovery.conf into /var/lib/postgresql/9.1/main/.
51
52 PS : All similar old files must be backed up to be able to rollback in case of risk (e.g: cp -p /etc/pgpool2/pgpool.conf /etc/pgpool2/pgpool.conf.backup).
53 Make sure that :
54 - All scripts are executable and owned by the proper users. 
55 - /var/lib/postgresql/9.1/archive directory is created (used to archive WAL files). This folder must be owned by postgres user !
56 - Do not forge to edit pg_hba.conf in each postgres server to allow access to cluster's nodes.
57
58 Not enough ! It remains only the configuration steps and we'll be done :)
59
60 Configuration
61 -------------
62
63 To do, just follow these steps :
64
65 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.
66
67 2- In Pgpool node set up pgpool.conf file for instance the parameters :
68
69         # Controls various backend behavior for instance master and slave(s).
70         backend_hostname0='master.foo.bar'
71         backend_port0 = 5432
72         backend_weight0 = 1
73         backend_data_directory0 = '/var/lib/postgres/9.1/main/'
74         backend_flag0 = 'ALLOW_TO_FAILOVER'
75         backend_hostname1='slave.foo.bar'
76         backend_port1 = 5432
77         backend_weight1 = 1
78         backend_data_directory1 = '/var/lib/postgres/9.1/main/'
79         backend_flag1 = 'ALLOW_TO_FAILOVER'
80         # Pool size
81         num_init_children = 32
82         max_pool = 4
83         # Master/Slave and load balancing (replication mode must be off)
84         load_balance_mode = on
85         master_slave_mode = on
86         master_slave_sub_mode = 'stream'
87         #Health check (must be set up to detecte postgres server status up/down)
88         health_check_period = 30
89         health_check_user = 'postgres'
90         health_check_password = 'postgrespass'
91         # Failover command
92         failover_command = '/path/to/failover.sh %d %H %P /tmp/trigger_file'
93
94 3- In failover.sh script, specify the proper ssh private key to postgres user to access new master  node via SSH.
95
96         ssh -i /var/lib/postgresql/.ssh/id_rsa postgres@$new_master "touch $trigger_file"
97
98 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 ! :)
99
100 5- Change the primary_conninfo access parameters (to master) in recovery.conf file in slave side :
101
102         primary_conninfo = 'host=master-or-slave.foo.bar port=5432 user=postgres password=nopass'
103
104 6- Rename recovery.conf to recovery.done in master side.
105
106 7- Setup postgres master node (after backup of postgresql.conf) :
107
108         cp -p postgresql.conf.master postgresql.conf
109         /etc/init.d/postgresql restart
110
111 8- Setup postgres slave node (after backup of postgresql.conf) :
112
113         cp -p postgresql.conf.slave postgresql.conf
114
115 9- Start first slave synchronisation with master by executing streaming-replication.sh as postgres user :
116
117         su postgres
118         cd ~
119         ./streaming-replication.sh master.foo.bar
120
121 10- Restart pgpool :
122
123         /etc/init.d/pgpool2 restart
124
125 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.
126
127 Tests
128 =====
129
130 Test PCP interface (as root) :
131
132         #retrieves the node information
133         pcp_node_info 10 localhost 9898 postgres "postgres-pass" "postgres-id"
134         #detaches a node from pgpool
135         pcp_detach_node 10 localhost 9898 postgres "postgres-pass" "postgres-id"
136         #attaches a node to pgpool
137         pcp_attach_node 10 localhost 9898 postgres "postgres-pass" "postgres-id"
138
139 After starting pgpool, try to test this two scenarios :
140
141 **1. When a slave fall down** :
142
143 Open pgpool log file 'tail -f /var/log/pgpool2/pgpool.log'.
144
145 Stop slave node '/etc/init.d/postgres stop'.
146
147 After exceeding health_check_period, you should see this log message :
148
149         [INFO] Slave node is down. Failover not triggred !
150
151 Now, start slave failback process (as root) :
152
153         # ./online-recovery.sh
154
155 **2. When a master fall down** :
156
157 Idem, open pgpool log file.
158
159 Stop master node '/etc/init.d/postgres stop'.
160
161 After exceeding health_check_period, you should see this log message :
162
163         [INFO] Master node is down. Performing failover...
164
165 Start failback process (as root) to switch master(new slave) and slave(new master) roles :
166
167         # ./online-recovery.sh