os: ubuntu 16.04
db: postgresql 9.6.8
repmgr: 4.2
os设置
目录
# mkdir -p /var/log/repmgr ;
chown -R postgres:postgres /var/log/repmgr ;
sudoer
# vi /etc/sudoers
postgres ALL=(ALL:ALL) NOPASSWD:ALL
或者
# vi /etc/sudoers
postgres ALL=(ALL:ALL) NOPASSWD:/usr/bin/pg_ctlcluster, \
/usr/bin/repmgr, \
/usr/bin/repmgrd
ssh 信任
# su - postgres
$ ssh-keygen -t rsa
$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@192.168.56.101;
$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@192.168.56.102;
/etc/logrotate.d/repmgrd
/var/log/repmgr/repmgrd.log {
missingok
compress
rotate 52
maxsize 100M
weekly
create 0600 postgres postgres
postrotate
/usr/bin/killall -HUP repmgrd
endscript
}
/etc/default/repmgrd
# default settings for repmgrd. This file is source by /bin/sh from
# /etc/init.d/repmgrd
# disable repmgrd by default so it won't get started upon installation
# valid values: yes/no
REPMGRD_ENABLED=yes
# configuration file (required)
REPMGRD_CONF="/etc/repmgr.conf"
# additional options
REPMGRD_OPTS="--daemonize=false"
# user to run repmgrd as
REPMGRD_USER=postgres
# repmgrd binary
REPMGRD_BIN=/usr/bin/repmgrd
# pid file
REPMGRD_PIDFILE=/var/run/repmgrd.pid
From repmgrd 4.1, ensure REPMGRD_OPTS includes --daemonize=false, as daemonization is handled by the service command.
If using systemd, you may need to execute systemctl daemon-reload. Also, if you attempted to start repmgrd using systemctl start repmgrd, you’ll need to execute systemctl stop repmgrd. Because that’s how systemd rolls.
/etc/repmgr.conf
# =============================================================================
# Required configuration items
# =============================================================================
node_id=1
node_name=node1
conninfo='host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/9.6/main'
# =============================================================================
# Optional configuration items
# =============================================================================
#------------------------------------------------------------------------------
# Server settings
#------------------------------------------------------------------------------
config_directory='/etc/postgresql/9.6/main/'
#------------------------------------------------------------------------------
# Replication settings
#------------------------------------------------------------------------------
#replication_user=''
#replication_type=physical
location='default'
use_replication_slots=true
#------------------------------------------------------------------------------
# Witness server settings
#------------------------------------------------------------------------------
#witness_sync_interval=15
#------------------------------------------------------------------------------
# Logging settings
#-------------------------------------------------------------------------------
log_level=INFO
#log_facility=STDERR
log_file=/var/log/repmgr/repmgrd.log
log_status_interval=10
#------------------------------------------------------------------------------
# Event notification settings
#------------------------------------------------------------------------------
#event_notification_command='/path/to/some/script %n %e %s "%t" "%d"'
#event_notifications=''
#------------------------------------------------------------------------------
# Environment/command settings
#------------------------------------------------------------------------------
pg_bindir='/usr/lib/postgresql/9.6/bin'
repmgr_bindir='/usr/bin'
#use_primary_conninfo_password=true
#passfile=''
#------------------------------------------------------------------------------
# external command options
#------------------------------------------------------------------------------
pg_ctl_options='-l /var/log/repmgr/startup.log'
#pg_basebackup_options=''
rsync_options=--exclude=postgresql.local.conf --archive
ssh_options='-q -o ConnectTimeout=10'
#------------------------------------------------------------------------------
# "node rejoin" settings
#------------------------------------------------------------------------------
#node_rejoin_timeout=60
#------------------------------------------------------------------------------
# Failover and monitoring settings (repmgrd)
#------------------------------------------------------------------------------
failover=automatic
promote_command='/usr/bin/repmgr standby promote -f /etc/repmgr.conf --log-to-file'
follow_command='/usr/bin/repmgr standby follow -f /etc/repmgr.conf --log-to-file --upstream-node-id=%n'
monitoring_history=yes
monitor_interval_secs=2
reconnect_attempts=10
reconnect_interval=6
async_query_timeout=600
#------------------------------------------------------------------------------
# service control commands
#------------------------------------------------------------------------------
service_start_command = 'sudo /usr/bin/pg_ctlcluster 9.6 main start'
service_stop_command = 'sudo /usr/bin/pg_ctlcluster 9.6 main stop'
service_restart_command = 'sudo /usr/bin/pg_ctlcluster 9.6 main restart'
service_reload_command = 'sudo /usr/bin/pg_ctlcluster 9.6 main reload'
service_promote_command = 'sudo /usr/bin/pg_ctlcluster 9.6 main promote'
参考:
https://repmgr.org/docs/4.2/index.html
https://raw.githubusercontent.com/2ndQuadrant/repmgr/master/repmgr.conf.sample