PostgreSQL+pgpooll+Keepalived雙機HA方案

PostgreSQL+pgpooll+Keepalived雙機HA方案node

(注:本文檔中的全部操做在沒有說明的狀況下都應該使用postgres用戶進行操做,postgres用戶沒有權限的操做方可採用root用戶進行操做,能夠在本方案中使用postgres用戶及該用戶的密碼替換replcia及replica用戶的密碼)python

 

方案邏輯圖linux

 

192.168.100.3的pgpool故障圖sql

 

192.168.100.4的pgpool故障圖數據庫

 

192.168.100.3的Master故障圖vim

 

1、環境信息bash

操做系統:CentOS6.6服務器

數據庫:PostgreSQL9.3.5dom

物理機兩臺:node1,node2ssh

Keepalived:keepalived-1.2.15

pgpool:pgpool-II-3.3.3

1、     安裝Linux操做系統CentOS6.6(兩臺物理機操做)

主節點:node1:IP:192.168.100.3

備節點:node2:IP:192.168.2.4

VIP:192.168.100.4

2、     安裝完Linux操做系統以後的系統調整(兩臺物理機操做)

2.1主備節點時鐘同步:

/usr/sbin/ntpdate asia.pool.ntp.org && /sbin/hwclock –systohc

 

2.2給兩個機器分別命名:node1,node2

 

第一臺機器命名爲node1

2.2.1 修改/etc/sysconfig/network中的hostname
vi /etc/sysconfig/network
HOSTNAME=node1 #修改localhost.localdomain爲node1

2.2.2 修改/etc/hosts文件
vi /etc/hosts
127.0.0.1 node1 localhost #修改localhost.localdomain爲node1
shutdown -r now #最後,重啓服務器便可

 

第二臺機器命名爲node2

2.2.3 修改/etc/sysconfig/network中的hostname
vi /etc/sysconfig/network
HOSTNAME=node2 #修改localhost.localdomain爲node2

2.2.4 修改/etc/hosts文件
vi /etc/hosts
127.0.0.1 node2 localhost #修改localhost.localdomain爲node2
shutdown -r now #最後,重啓服務器便可。

 

2.3.OS資源調整

時區調整: (若是已經調好同步不須要作)

vi /etc/sysconfig/clock

ZONE="Asia/Shanghai"

UTC=false

ARC=false

vi /etc/sysconfig/i18n

LANG="en_US.UTF-8"

 

vi /etc/sysctl.conf

kernel.shmmni = 4096

kernel.sem = 50100 64128000 50100 1280

fs.file-max = 7672460

net.ipv4.ip_local_port_range = 9000 65000

net.core.rmem_default = 1048576

net.core.rmem_max = 4194304

net.core.wmem_default = 262144

net.core.wmem_max = 1048576

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_max_syn_backlog = 4096

net.core.netdev_max_backlog = 10000

vm.overcommit_memory = 0

net.ipv4.ip_conntrack_max = 655360

fs.aio-max-nr = 1048576

net.ipv4.tcp_timestamps = 0

使文件修改生效

sysctl –p

 

vi /etc/security/limits.conf

* soft nofile 131072

* hard nofile 131072

* soft nproc 131072

* hard nproc 131072

* soft core unlimited

* hard core unlimited

* soft memlock 50000000

* hard memlock 50000000

 

vi /etc/sysconfig/selinux

SELINUX=disabled

setenforce 0

 

防火牆根據須要配置

 

vi /etc/sysconfig/iptables

-A INPUT -s 192.168.0.0/16 -j ACCEPT

-A INPUT -s 10.0.0.0/8 -j ACCEPT

-A INPUT -s 172.16.0.0/16 -j ACCEPT

# or

-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

 

service iptables restart

 

2.4.給兩個節點分別建立postgres用戶並設置密碼

useradd postgres

password:postgres

 

2.5.配置兩臺機器的ssh免密鑰登陸

[postgres@node1]$ ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.

Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.

The key fingerprint is:

[postgres@node1]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

[postgres@node1]$ chmod go-rwx ~/.ssh/*

[postgres@node2$ ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.

Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.

The key fingerprint is:

[postgres@node2$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

[postgres@node2$ chmod go-rwx ~/.ssh/*

 

[postgres@node1]$ cd ~/.ssh

[postgres@node1]$ scp id_rsa.pub  postgres@node2:/home/postgres/.ssh/id_rsa.pub1

[postgres@node2] cd ~/.ssh

[postgres@node2] cat id_rsa.pub1 | authorized_keys

[postgres@node2]scp id_rsa.pub postgres@node1:/home/postgres/.ssh/id_rsa.pub2

[postgres@node1] cat id_rsa.pub2| authorized_keys

2.6.建立源碼包存儲目錄

在兩個節點的/opt/目錄下分別建立soft_bak目錄用於存放軟件安裝包如:postgresql-9.3.5.tar.gz

mkdir /opt/soft_bak

設置相關目錄的所屬主

postgresql必須用postgres 用戶安裝,安裝以前先改變postgresql的將要安裝的目錄的權限

chown –R postgres:postgres  /opt/soft_bak

chown –R postgres:postgres  /usr/local/

 

3、安裝PG的依賴軟件包:

yum -y install lrzsz sysstat e4fsprogs ntp readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel python-devel tcl-devel gcc make flex bison

 

4、下載PostgreSQL 9.3.5 pgpool-II-3.3.3.tar.gz keepalived-1.2.15

在安裝PG以前

/opt/soft_bak文件夾下應該有postgresql-9.3.5.tar.gz pgpool-II-3.4.0.tar.gz 等

 

5、安裝PostgreSQL

主庫(postgres用戶安裝)

cd /opt/soft_bak/

tar zxvf postgresql-9.3.5.tar.gz

cd /opt/soft_bak/postgresql-9.3.5

./configure  --with-pgport=5432 --with-perl --with-tcl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --with-blocksize=8

gmake world

gmake install-world

 

備庫(postgres用戶安裝 備節點只須要安裝軟件,不須要init數據庫)

cd /opt/soft_bak/

tar zxvf postgresql-9.3.5.tar.gz

cd /opt/soft_bak/postgresql-9.3.5

./configure  --with-pgport=5432 --with-perl --with-tcl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --with-blocksize=8

gmake world

gmake install-world

 

6、主備節點設置相關環境變量(主備節點應該一致)

vi /home/postgres/.bash_profile

export PGPORT=5432

export PGDATA=/file/data

export LANG=en_US.utf8

export PGHOME=/usr/local/pgsql

export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH

export DATE=`date +"%Y%m%d%H%M"`

export PATH=$PGHOME/bin:$PATH:.

export MANPATH=$PGHOME/share/man:$MANPATH

export PGUSER=postgres

export PGHOST=$PGDATA

export PGDATABASE=postgres

alias rm='rm -i'

alias ll='ls -lh'

 

在/home/postgres/目錄下建立.pgpass(用於主備之間數據傳輸的流複製用戶)

格式:

IP:端口:replication:用戶:密碼

(master)(在不建立replica用戶的狀況下可使用postgres用戶和postgres用戶的密碼)

192.168.100.3:5432:replication:replica:replica

192.168.2.4:5432:replication:replica:replica

(standby) (在不建立replica用戶的狀況下可使用postgres用戶和postgres用戶的密碼)

192.168.100.3:5432:replication:replica:replica

192.168.2.4:5432:replication:replica:replica

 

7、主節點初始化postgreSQL實例

su - postgres

initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W

8、PostgreSQL配置文件調整

cd $PGDATA

vi postgresql.conf

listen_addresses = '0.0.0.0'

max_connections = 1000

superuser_reserved_connections = 13

unix_socket_directory = '$PGDATA'

unix_socket_permissions = 0700

tcp_keepalives_idle = 60

tcp_keepalives_interval = 10

tcp_keepalives_count = 10

shared_buffers = 2GB

maintenance_work_mem = 512MB

max_stack_depth = 8MB

vacuum_cost_delay = 10ms

vacuum_cost_limit = 10000

bgwriter_delay = 10ms

wal_level = hot_standby

wal_buffers = 16384kB
checkpoint_segments = 64

checkpoint_timeout = 5min

archive_mode = on

archive_command = '/bin/date'

max_wal_senders = 32

hot_standby = on

random_page_cost = 2.0

effective_cache_size = 12000MB

log_destination = 'csvlog'

logging_collector = on

log_directory = 'pg_log'

log_truncate_on_rotation = on

log_rotation_age = 1d

log_rotation_size = 10MB

log_min_duration_statement = 1s

log_checkpoints = on

log_lock_waits = on

deadlock_timeout = 1s

log_connections = on

log_disconnections = on

log_error_verbosity = verbose

log_statement = 'ddl'

track_activity_query_size = 2048

autovacuum = on

log_autovacuum_min_duration = 0

log_timezone = 'PRC'

datestyle = 'iso, mdy'

timezone = 'PRC'

lc_messages = 'C'                               

lc_monetary = 'C'                               

lc_numeric = 'C'                                

lc_time = 'C'                                   

default_text_search_config = 'pg_catalog.english'

shared_preload_libraries = 'pg_stat_statements,auto_explain'

pg_stat_statements.max = 1000

pg_stat_statements.track = all

pg_stat_statements.track_utility = off

track_io_timing = off

custom_variable_class = ‘pg_stat_statements,auto_explain’

pg_stat_statements.max = 1000

pg_stat_statements.track = all

auto_explain.log_min_duration = 10ms

auto_explain.log_analyze = false

auto_explain.log_verbose = false

auto_explain.log_nested_statements = false

 

9、使系統具備監控SQL功能

su - postgres

pg_clt –D $PGDATA start

psql –h node1 –p 5432 –U postgres –d postgres

postgres=#create extention pg_stat_statements;

postgres=#select pg_stat_statements_reset();

//select * from pg_stat_statements;

 

10、主備方案postgresql.conf pg_hba.conf的配置

vim postgres.conf

  listen_addresses='*'

  wal_level = 'hot_standby'

  archive_mode = on

  archive_command = 'cd ./'  # we can also use exit 0

  max_wal_senders = 10

  wal_keep_segments = 5000   # 80 GB required on pg_xlog

  hot_standby = on

vim pg_hba.conf

host    all             all               192.168.2.4/32          trust

host    postgres        postgres          192.168.2.4/32          trust

host    replication      replica            192.168.2.4/32          trust

host    all             all               192.168.100.3/32        trust

host    postgres        postgres          192.168.100.3/32        trust

host    replica         replica           192.168.100.3/32        trust

 

十一 主備節點標誌文件的配置

su – postgres

cp /usr/local/pgsql/share/recovery.conf.sample  /file/data/recovery.done

standby_mode = on

recovery_target_timeline =’latest’時間線

primary_conninfo = ‘host=node2 port=5432 user=postgres’主節點信息

trigger_file = ‘/file/data/trigger_file’

12、生成備庫實例(node2上執行)

su – postgres

pg_basebackup –D $PGDATA –Fp –Xs –v –P –h node1 –p主庫端口號 –U replica(postgres)

十3、修改備庫實例的備庫標識文件

cd /file/data

mv recovery.done recovery.conf

vim recovery.conf

standby_mode = on

recovery_target_timeline =’latest’時間線

primary_conninfo = ‘host=node1 port=5432 user=postgres’主節點信息

trigger_file = ‘/file/data/trigger_file’

 

啓動備庫

chmod 0700 /file/data

pg_ctl –D $PGDATA

查看備庫進程

ps –ef|grep postgres

查看主庫進程

ps –ef|grep postgres

 

十4、安裝pgpool-II3.3.3(兩個節點操做)

將pgpool-II3.3.3下載到/opt/soft_bak

建立pgpool的安裝目錄

mkdir /opt/pgpool

su - postgres

tar zxvf pgpool-II-3.3.3.tar.gz

cd pgpool-II-3.3.3

mkdir -p /opt/pgpool

./configure --prefix=/opt/pgpool/ --with-pgsql=/usr/local/pgsql/ --with-pgsql-libdir=/opt/pgsql/lib/ --with-pgsql-includedir=/opt/pgsql/include/

make

make install

cd src/sql

[postgres@node1 sql]$ cd pgpool_adm/

[postgres@node1 pgpool_adm]$ make

[postgres@node1 pgpool_adm]$ make install

[postgres@node1 pgpool_adm]$ cd ..

[postgres@node1 sql]$ cd pgpool-recovery/

[postgres@node1 pgpool-recovery]$ make

[postgres@node1 pgpool-recovery]$ make install

[postgres@node1 pgpool-recovery]$ cd ..

[postgres@node1 sql]$ cd pgpool-regclass/

[postgres@node1 pgpool-regclass]$ make

[postgres@node1 pgpool-regclass]$ make install

 

十5、安裝 pgpool 相關函數

su – postgres

cd pgpool-II-3.3.3/sql

make

make install

cd pgpool-recovery

make install

cd pgpool-regclass

make install

 

echo 「export PATH=\」 \$PATH:/opt/pgpool/bin\」 " >>/home/postgres/.bashrc

source /home/postgres/.bashrc

 

安裝完之後/usr/local/pgsql/share/extension/目錄下應該有以下文件

pgpool_recovery--1.0.sql
pgpool_recovery.control
pgpool-recovery.sql
pgpool_regclass--1.0.sql
pgpool_regclass.control
pgpool-regclass.sql

su - postgres

psql template1   (備節點不須要建立pgpool_regclass,pgpool_recovery)

template1=# create extension pgpool_regclass;
CREATE EXTENSION
template1=# CREATE EXTENSION pgpool_recovery;
CREATE EXTENSION

查看新增長的  recovery 管理函數
template1=# \df
                                                     List of functions
 Schema |        Name         | Result data type |                      Argument data types                       |  Type  
--------+---------------------+------------------+----------------------------------------------------------------+--------
 public | pgpool_pgctl        | boolean          | action text, stop_mode text                                    | normal
 public | pgpool_recovery     | boolean          | script_name text, remote_host text, remote_data_directory text | normal
 public | pgpool_remote_start | boolean          | remote_host text, remote_data_directory text                   | normal
 public | pgpool_switch_xlog  | text             | arcive_dir text                                                | normal
(4 rows)

 

十6、配置 pgpool-II ( 兩節點操做)
--配置  pcp.conf

cd /opt/pgpool/etc
cp pcp.conf.sample pcp.conf
pg_md5 -u postgres -p
password: postgres
ba777e4c2f15c11ea8ac3be7e0440aa0

vim pcp.conf

--編寫 pcp.conf 文件,寫入如下

# USERID:MD5PASSWD
postgres:ba777e4c2f15c11ea8ac3be7e0440aa0

--配置 ifconfig, arping 執行權限(由root用戶執行)

# chmod u+s /sbin/ifconfig
# chmod u+s /usr/sbin/

--配置 pgpool.conf

cd /opt/pgpool/etc         
cp pgpool.conf.sample pgpool.conf

--主節點的 pgpool.conf(配置文件中的用戶名和配置能用postgres用戶就用postgres用戶,儘可能減小用戶)
listen_addresses = '*'
port = 9999
socket_dir = '/opt/pgpool'
pcp_port = 9898
pcp_socket_dir = '/opt/pgpool'
backend_hostname0 = 'node1'   ##配置數據節點 node1
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = 'node2'   ##配置數據節點  node2
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = on
pool_passwd = 'pool_passwd'
authentication_timeout = 60
ssl = off
num_init_children = 32
max_pool = 4
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 0
log_destination = 'syslog'
print_timestamp = on
log_connections = on
log_hostname = on
log_statement = on
log_per_node_statement = off
log_standby_delay = 'none'
syslog_facility = 'LOCAL0'
syslog_ident = 'pgpool'
debug_level = 0
pid_file_name = '/opt/pgpool/pgpool.pid'
logdir = '/tmp'
connection_cache = on
reset_query_list = 'ABORT; DISCARD ALL'
replication_mode = off
replicate_select = off
insert_lock = on
lobj_lock_table = ''
replication_stop_on_mismatch = off
failover_if_affected_tuples_mismatch = off
load_balance_mode = on
ignore_leading_white_space = on
white_function_list = ''
black_function_list = 'nextval,setval'
master_slave_mode = on # 設置流複製模式
master_slave_sub_mode = 'stream' # 設置流複製模式
sr_check_period = 5
sr_check_user = 'replica'
sr_check_password = 'replica'
delay_threshold = 16000
follow_master_command = ''
parallel_mode = off
pgpool2_hostname = ''
system_db_hostname  = 'localhost'
system_db_port = 5432
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = ''
health_check_period = 5
health_check_timeout = 20
health_check_user = 'replica'
health_check_password = 'replcia'
health_check_max_retries = 3
health_check_retry_delay = 1
failover_command = '/opt/pgpool/failover_stream.sh  %d %H /file/data/trigger_file' 
failback_command = ''
fail_over_on_backend_error = on
search_primary_node_timeout = 10
recovery_user = 'nobody'
recovery_password = ''
recovery_1st_stage_command = ''
recovery_2nd_stage_command = ''
recovery_timeout = 90
client_idle_limit_in_recovery = 0
use_watchdog = on
trusted_servers = ''
ping_path = '/bin'
wd_hostname = 'node1'
wd_port = 9000
wd_authkey = ''
delegate_IP = '' 
ifconfig_path = '/sbin'  
if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'
if_down_cmd = 'ifconfig eth0:0 down'
arping_path = '/usr/sbin'           # arping command path
arping_cmd = 'arping -U $_IP_$ -w 1'
clear_memqcache_on_escalation = on
wd_escalation_command = ''
wd_lifecheck_method = 'heartbeat'
wd_interval = 10
wd_heartbeat_port = 9694
wd_heartbeat_keepalive = 2
wd_heartbeat_deadtime = 30
heartbeat_destination0 = 'node2'   # 配置對端的 hostname
heartbeat_destination_port0 = 9694
heartbeat_device0 = 'eth0'
wd_life_point = 3
wd_lifecheck_query = 'SELECT 1'
wd_lifecheck_dbname = 'template1'
wd_lifecheck_user = 'nobody'
wd_lifecheck_password = ''
other_pgpool_hostname0 = 'node2'   ## 配置對端的 pgpool
other_pgpool_port0 = 9999
other_wd_port0 = 9000
relcache_expire = 0
relcache_size = 256
check_temp_table = on
memory_cache_enabled = off
memqcache_method = 'shmem'
memqcache_memcached_host = 'localhost'
memqcache_memcached_port = 11211
memqcache_total_size = 67108864
memqcache_max_num_cache = 1000000
memqcache_expire = 0
memqcache_auto_cache_invalidation = on
memqcache_maxcache = 409600
memqcache_cache_block_size = 1048576
memqcache_oiddir = '/opt/pgpool/oiddir'(須要如今/opt/pgpool目錄下建立oiddr)
white_memqcache_table_list = ''
black_memqcache_table_list = ''

 

--備節點的 pgpool.conf

pgpool.conf
listen_addresses = '*'
port = 9999
socket_dir = '/opt/pgpool'
pcp_port = 9898
pcp_socket_dir = '/opt/pgpool'
backend_hostname0 = 'node1'
backend_port0 = 5432
backend_weight0 = 1
backend_flag0 = 'ALLOW_TO_FAILOVER'
backend_hostname1 = 'node2'
backend_port1 = 5432
backend_weight1 = 1
backend_flag1 = 'ALLOW_TO_FAILOVER'
enable_pool_hba = on
pool_passwd = 'pool_passwd'
authentication_timeout = 60
ssl = off
num_init_children = 32
max_pool = 4
child_life_time = 300
child_max_connections = 0
connection_life_time = 0
client_idle_limit = 0
log_destination = 'syslog'
print_timestamp = on
log_connections = on
log_hostname = on
log_statement = on
log_per_node_statement = off
log_standby_delay = 'none'
syslog_facility = 'LOCAL0'
syslog_ident = 'pgpool'
debug_level = 0
pid_file_name = '/opt/pgpool/pgpool.pid'
logdir = '/tmp'
connection_cache = on
reset_query_list = 'ABORT; DISCARD ALL'
replication_mode = off
replicate_select = off
insert_lock = on
lobj_lock_table = ''
replication_stop_on_mismatch = off
failover_if_affected_tuples_mismatch = off
load_balance_mode = on
ignore_leading_white_space = on
white_function_list = ''
black_function_list = 'nextval,setval'
master_slave_mode = on
master_slave_sub_mode = 'stream'
sr_check_period = 0
sr_check_user = 'replica'
sr_check_password = 'replica'
delay_threshold = 16000
follow_master_command = ''
parallel_mode = off
pgpool2_hostname = ''
system_db_hostname  = 'localhost'
system_db_port = 5432
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = ''
health_check_period = 0
health_check_timeout = 20
health_check_user = 'nobody'
health_check_password = ''
health_check_max_retries = 0
health_check_retry_delay = 1
failover_command = '/opt/pgpool/failover_stream.sh  %d %H /file/data/trigger/file'
failback_command = ''
fail_over_on_backend_error = on
search_primary_node_timeout = 10
recovery_user = 'nobody'
recovery_password = ''
recovery_1st_stage_command = ''
recovery_2nd_stage_command = ''
recovery_timeout = 90
client_idle_limit_in_recovery = 0
use_watchdog = off
trusted_servers = ''
ping_path = '/bin'
wd_hostname = ' '
wd_port = 9000
wd_authkey = ''
delegate_IP = ' '
ifconfig_path = '/sbin'
if_up_cmd = 'ifconfig eth0:0 inet $_IP_$ netmask 255.255.255.0'
if_down_cmd = 'ifconfig eth0:0 down'
arping_path = '/usr/sbin'           # arping command path
arping_cmd = 'arping -U $_IP_$ -w 1'
clear_memqcache_on_escalation = on
wd_escalation_command = ''
wd_lifecheck_method = 'heartbeat'
wd_interval = 10
wd_heartbeat_port = 9694
wd_heartbeat_keepalive = 2
wd_heartbeat_deadtime = 30
heartbeat_destination0 = 'node1'
heartbeat_destination_port0 = 9694
heartbeat_device0 = 'eth0'
wd_life_point = 3
wd_lifecheck_query = 'SELECT 1'
wd_lifecheck_dbname = 'template1'
wd_lifecheck_user = 'nobody'
wd_lifecheck_password = ''
other_pgpool_hostname0 = 'node1'
other_pgpool_port0 = 9999
other_wd_port0 = 9000
relcache_expire = 0
relcache_size = 256
check_temp_table = on
memory_cache_enabled = off
memqcache_method = 'shmem'
memqcache_memcached_host = 'localhost'
memqcache_memcached_port = 11211
memqcache_total_size = 67108864
memqcache_max_num_cache = 1000000
memqcache_expire = 0
memqcache_auto_cache_invalidation = on
memqcache_maxcache = 409600
memqcache_cache_block_size = 1048576
memqcache_oiddir = '/var/log/pgpool/oiddir'
white_memqcache_table_list = ''
black_memqcache_table_list = ''

failover_stream.sh文件內容

#! /bin/sh

# Failover command for streaming replication.

# This script assumes that DB node 0 is primary, and 1 is standby.

#

# If standby goes down, do nothing. If primary goes down, create a

# trigger file so that standby takes over primary node.

#

# Arguments: $1: failed node id. $2: new master hostname. $3: path to

# trigger file.

 

failed_node=$1

new_master=$2

trigger_file=$3

# Do nothing if standby goes down.

#if [ $failed_node = 1 ]; then

#        exit 0;

#fi

/usr/bin/ssh -T $new_master /bin/touch $trigger_file

exit 0;

 

主節點配置

cd /opt/pgpool/etc

cp pool_hba.conf.sample pool_hba.conf

vim pool_hba.conf

host    all             all           192.168.2.4/32        trust

host    replication     replica       192.168.2.4/32        trust

host    postgres        postgres      192.168.2.4/32        trust

host    all             all           192.168.100.3/32      trust

host    replication     replica       192.168.100.3/32      trust

host    postgres        postgres      192.168.100.3/32      trust

host    postgres        postgres      192.168.100.3/32      trust

host    all             all           192.168.100.4/32      trust

host    replication     replica       192.168.100.4/32      trust

host    postgres        postgres      192.168.100.4/32      trust

host    postgres        postgres      192.168.100.4/32      trust

備節點配置

cd /opt/pgpool/etc

cp pool_hba.conf.sample pool_hba.conf

vim pool_hba.conf

host    all             all           192.168.2.4/32        trust

host    replication     replica       192.168.2.4/32        trust

host    postgres        postgres      192.168.2.4/32        trust

host    all             all           192.168.100.3/32      trust

host    replication     replica       192.168.100.3/32      trust

host    postgres        postgres      192.168.100.3/32      trust

host    postgres        postgres      192.168.100.3/32      trust

host    all             all           192.168.100.4/32      trust

host    replication     replica       192.168.100.4/32      trust

host    postgres        postgres      192.168.100.4/32      trust

host    postgres        postgres      192.168.100.4/32      trust

配置密碼文件(兩節點操做)

su – postgres

psql

select rolname,rolpassword from pg_authid;

vim pool_passwd

postgres:postgres的md5密碼

rolname:rolpassword

pgpool啓動:pgpool –n &

pgpool關閉:pgpool –m fast stop

鏈接pgpool:psql –h node (or IP) –p 9999 –U postgres –d postgres

查看pgpool集羣中nodes的狀態

show pool_nodes;

 

 

 

十7、安裝keepalived-1.2.15(主備兩節點操做)

下載keepalived-1.2.15到/opt/sotf_bak

tar zxvf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure

make

make install

node1 keepalived.conf 文件內容

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script chk_pgpool {

        script "killall -0 pgpool"

        interval 3

        weight 2

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.100.4

    }

         track_script {

        chk_pgpool

}

 

}

node2 keepalived.conf 文件內容

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script chk_pgpool {

        script "killall -0 pgpool"

        interval 3

        weight 2

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.100.4

    }

        track_script {

        chk_pgpool

}

}

 

相關文章
相關標籤/搜索