postgresql 高可用 repmgr 的使用之一

os:ubunbu 16.04
postgresql:9.6.8
repmgr:4.1.1

repmgr是2ndquadrant公司维护的针对postgresql replication的工具。
工具虽小,却比较精悍。

需要注意的是repmgr必须安装在每个节点上。

目前在github可以找到该项目 https://github.com/2ndQuadrant/repmgr

有三种架构:
1 Primary + 1 Standby
1 Primary + 2 Standbys
1 Primary + 3 Standbys + 1 Witness

#下载、安装

# cd ~
# apt-get install apt-transport-https curl ca-certificates

# curl https://dl.2ndquadrant.com/default/release/get/deb | bash
# ls -l /etc/apt/sources.list.d
-rw-r--r-- 1 root root  88 Sep 21 14:09 2ndquadrant-dl-default-release.list
-rw-r--r-- 1 root root  62 Sep  2 15:26 pgdg.list

# apt list |grep -i repmgr

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

postgresql-10-repmgr/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-10-repmgr-dbg/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-11-repmgr/xenial-pgdg 4.0.6-1.pgdg16.04+1 amd64
postgresql-11-repmgr-dbg/xenial-pgdg 4.0.6-1.pgdg16.04+1 amd64
postgresql-9.1-repmgr/xenial-pgdg 2.0.3-2.pgdg16.04+1 amd64
postgresql-9.1-repmgr-dbg/xenial-pgdg 2.0.3-2.pgdg16.04+1 amd64
postgresql-9.2-repmgr/xenial-pgdg 2.0.3-2.pgdg16.04+1 amd64
postgresql-9.2-repmgr-dbg/xenial-pgdg 2.0.3-2.pgdg16.04+1 amd64
postgresql-9.3-repmgr/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.3-repmgr-dbg/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.4-repmgr/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.4-repmgr-dbg/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.5-repmgr/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.5-repmgr-dbg/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.6-repmgr/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64
postgresql-9.6-repmgr-dbg/xenial-2ndquadrant 4.1.1-1.xenial+1 amd64

repmgr/xenial-2ndquadrant 4.1.1-1.xenial+1 all
repmgr-common/xenial-2ndquadrant 4.1.1-1.xenial+1 all
repmgr-dbg/xenial-2ndquadrant 4.1.1-1.xenial+1 all

# apt-get install postgresql-9.6-repmgr

The following additional packages will be installed:
  repmgr-common
The following NEW packages will be installed:
  postgresql-9.6-repmgr repmgr-common

注意,安装了 postgresql-9.6-repmgr、repmgr-common 两个包

#配置
先看 postgresql-9.6-repmgr、repmgr-common 这两个包都设计到了哪些文件。

# dpkg -L postgresql-9.6-repmgr 
/.
/usr
/usr/lib
/usr/lib/postgresql
/usr/lib/postgresql/9.6
/usr/lib/postgresql/9.6/bin
/usr/lib/postgresql/9.6/bin/repmgrd
/usr/lib/postgresql/9.6/bin/repmgr
/usr/lib/postgresql/9.6/lib
/usr/lib/postgresql/9.6/lib/repmgr.so
/usr/share
/usr/share/postgresql
/usr/share/postgresql/9.6
/usr/share/postgresql/9.6/extension
/usr/share/postgresql/9.6/extension/repmgr--4.0--4.1.sql
/usr/share/postgresql/9.6/extension/repmgr--unpackaged--4.0.sql
/usr/share/postgresql/9.6/extension/repmgr--4.1.sql
/usr/share/postgresql/9.6/extension/repmgr--4.0.sql
/usr/share/postgresql/9.6/extension/repmgr.control
/usr/share/doc
/usr/share/doc/postgresql-9.6-repmgr
/usr/share/doc/postgresql-9.6-repmgr/changelog.Debian.gz
/usr/share/doc/postgresql-9.6-repmgr/copyright
/usr/share/doc/postgresql-9.6-repmgr/NEWS.Debian.gz

# dpkg -L repmgr-common
/.
/usr
/usr/bin
/usr/share
/usr/share/doc
/usr/share/doc/repmgr-common
/usr/share/doc/repmgr-common/filelist.sgml.gz
/usr/share/doc/repmgr-common/stylesheet.css
/usr/share/doc/repmgr-common/changelog.Debian.gz
/usr/share/doc/repmgr-common/repmgrd-cascading-replication.sgml
/usr/share/doc/repmgr-common/repmgr-standby-promote.sgml
/usr/share/doc/repmgr-common/configuration-file.sgml
/usr/share/doc/repmgr-common/repmgrd-witness-server.sgml
/usr/share/doc/repmgr-common/repmgrd-automatic-failover.sgml
/usr/share/doc/repmgr-common/copyright
/usr/share/doc/repmgr-common/repmgr-standby-switchover.sgml.gz
/usr/share/doc/repmgr-common/repmgr.sgml
/usr/share/doc/repmgr-common/install-requirements.sgml
/usr/share/doc/repmgr-common/repmgr-cluster-cleanup.sgml
/usr/share/doc/repmgr-common/repmgr-cluster-event.sgml
/usr/share/doc/repmgr-common/repmgr-node-rejoin.sgml.gz
/usr/share/doc/repmgr-common/promoting-standby.sgml
/usr/share/doc/repmgr-common/changes-in-repmgr4.md
/usr/share/doc/repmgr-common/configuration-file-service-commands.sgml.gz
/usr/share/doc/repmgr-common/appendix-release-notes.sgml.gz
/usr/share/doc/repmgr-common/overview.sgml.gz
/usr/share/doc/repmgr-common/repmgr-standby-register.sgml.gz
/usr/share/doc/repmgr-common/configuration-file-required-settings.sgml.gz
/usr/share/doc/repmgr-common/NEWS.Debian.gz
/usr/share/doc/repmgr-common/README.md
/usr/share/doc/repmgr-common/repmgr-cluster-crosscheck.sgml
/usr/share/doc/repmgr-common/repmgrd-configuration.sgml.gz
/usr/share/doc/repmgr-common/repmgr-standby-unregister.sgml
/usr/share/doc/repmgr-common/repmgr-witness-register.sgml
/usr/share/doc/repmgr-common/bdr-failover.md
/usr/share/doc/repmgr-common/appendix-packages.sgml.gz
/usr/share/doc/repmgr-common/repmgrd-bdr.sgml.gz
/usr/share/doc/repmgr-common/follow-new-primary.sgml
/usr/share/doc/repmgr-common/quickstart.sgml.gz
/usr/share/doc/repmgr-common/repmgr-node-check.sgml.gz
/usr/share/doc/repmgr-common/repmgr-node-status.sgml
/usr/share/doc/repmgr-common/repmgr-cluster-show.sgml.gz
/usr/share/doc/repmgr-common/configuring-witness-server.sgml
/usr/share/doc/repmgr-common/appendix-faq.sgml.gz
/usr/share/doc/repmgr-common/install-source.sgml.gz
/usr/share/doc/repmgr-common/repmgrd-node-fencing.md.gz
/usr/share/doc/repmgr-common/repmgrd-network-split.sgml
/usr/share/doc/repmgr-common/version.sgml
/usr/share/doc/repmgr-common/repmgrd-demonstration.sgml.gz
/usr/share/doc/repmgr-common/repmgr-cluster-matrix.sgml.gz
/usr/share/doc/repmgr-common/cloning-standbys.sgml.gz
/usr/share/doc/repmgr-common/appendix-signatures.sgml
/usr/share/doc/repmgr-common/legal.sgml
/usr/share/doc/repmgr-common/stylesheet.dsl.gz
/usr/share/doc/repmgr-common/install-packages.sgml.gz
/usr/share/doc/repmgr-common/repmgr-standby-clone.sgml.gz
/usr/share/doc/repmgr-common/Makefile.in
/usr/share/doc/repmgr-common/event-notifications.sgml.gz
/usr/share/doc/repmgr-common/repmgr-primary-register.sgml
/usr/share/doc/repmgr-common/repmgr-primary-unregister.sgml
/usr/share/doc/repmgr-common/repmgr-standby-follow.sgml
/usr/share/doc/repmgr-common/configuration.sgml
/usr/share/doc/repmgr-common/install.sgml
/usr/share/doc/repmgr-common/upgrading-repmgr.sgml.gz
/usr/share/doc/repmgr-common/repmgrd-degraded-monitoring.sgml
/usr/share/doc/repmgr-common/configuration-file-log-settings.sgml
/usr/share/doc/repmgr-common/upgrading-from-repmgr3.md
/usr/share/doc/repmgr-common/website-docs.css
/usr/share/doc/repmgr-common/switchover.sgml.gz
/usr/share/doc/repmgr-common/repmgr-witness-unregister.sgml
/usr/share/doc/repmgr-common/repmgrd-monitoring.sgml
/etc
/etc/default
/etc/default/repmgrd
/etc/init.d
/etc/init.d/repmgrd
/usr/bin/repmgrd
/usr/bin/repmgr

关于 repmgr 的描述如下

repmgr
A command-line tool used to perform administrative tasks such as:

setting up standby servers

promoting a standby server to primary

switching over primary and standby servers

displaying the status of servers in the replication cluster

关于 repmgrd 的描述如下

repmgrd
A daemon which actively monitors servers in a replication cluster and performs the following tasks:

monitoring and recording replication performance

performing failover by detecting failure of the primary and promoting the most suitable standby server

provide notifications about events in the cluster to a user-defined script which can perform tasks such as sending alerts by email

repmgrd service command: systemctl [start|stop|restart|reload] repmgrd
repmgrd service file location: /etc/init.d/repmgrd (defaults in: /etc/defaults/repmgrd)
repmgrd log file location: (not specified by package; set in repmgr.conf)

参考:
https://github.com/2ndQuadrant/repmgr

https://www.2ndquadrant.com/en/resources/repmgr/
https://repmgr.org/docs/4.1/index.html

https://repmgr.org/docs/4.1/installation.html
https://repmgr.org/docs/4.1/installation-packages.html#INSTALLATION-PACKAGES-DEBIAN
https://repmgr.org/docs/4.1/packages-debian-ubuntu.html

https://repmgr.org/docs/4.1/release-4.1.0.html

下面是 /etc/default/repmgrd 的内容

# more /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=no

# configuration file (required)
#REPMGRD_CONF="/path/to/repmgr.conf"

# additional options
#REPMGRD_OPTS=""

# user to run repmgrd as
#REPMGRD_USER=postgres

# repmgrd binary
#REPMGRD_BIN=/usr/bin/repmgrd

# pid file
#REPMGRD_PIDFILE=/var/run/repmgrd.pid

下面是 /etc/default/repmgrd 的内容

# more /etc/init.d/repmgrd 

#!/bin/sh
### BEGIN INIT INFO
# Provides:	     repmgrd
# Required-Start:    $local_fs $remote_fs $network $syslog postgresql
# Required-Stop:     $local_fs $remote_fs $network $syslog postgresql
# Should-Start:	     $syslog postgresql
# Default-Start:     2 3 4 5
# Default-Stop:	     0 1 6
# Short-Description: Start/stop repmgrd
# Description:       Enable repmgrd replication management and monitoring daemon for PostgreSQL
### END INIT INFO

DESC="PostgreSQL replication management and monitoring daemon"
NAME=repmgrd

REPMGRD_ENABLED=no
REPMGRD_CONF=
REPMGRD_OPTS=
REPMGRD_USER=postgres
REPMGRD_BIN=/usr/bin/repmgrd
REPMGRD_PIDFILE=/var/run/repmgrd.pid

# Read configuration variable file if it is present
[ -r "/etc/default/$NAME" ] && . "/etc/default/$NAME"

test -x "$REPMGRD_BIN" || exit 0

case "$REPMGRD_ENABLED" in
    [Yy]*)
	break
	;;
    *)
	exit 0
	;;
esac

# Define LSB log_* functions.
. /lib/lsb/init-functions

if [ -z "$REPMGRD_CONF" ]
then
    log_warning_msg "Not starting $NAME, REPMGRD_CONF not set in /etc/default/$NAME"
    exit 0
fi

do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   other if daemon could not be started or a failure occured
	start-stop-daemon --start --quiet --background --chuid "$REPMGRD_USER" --make-pidfile --pidfile "$REPMGRD_PIDFILE" --exec "$REPMGRD_BIN" --
 --config-file "$REPMGRD_CONF" $REPMGRD_OPTS
}

do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   other if daemon could not be stopped or a failure occurred
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile "$REPMGRD_PIDFILE" --name "$(basename $REPMGRD_BIN)"
}

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0) log_end_msg 0 ;;
		1) log_progress_msg "already started"
		   log_end_msg 0 ;;
		*) log_end_msg 1 ;;
	esac

	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0) log_end_msg 0 ;;
		1) log_progress_msg "already stopped"
		   log_end_msg 0 ;;
		*) log_end_msg 1 ;;
	esac

	;;
  restart|force-reload)
	$0 stop
	$0 start
	;;
  status)
	status_of_proc -p "$REPMGRD_PIDFILE" "$REPMGRD_BIN" "$NAME" && exit 0 || exit $?
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
	exit 3
	;;
esac

exit 0
posted @ 2018-09-21 15:33  peiybpeiyb  阅读(2092)  评论(0编辑  收藏  举报