sxyzy 发表于 2019-1-7 13:43:32

debian8下配置postgresql9.5.2、pgpool3.5.2、heartbeat3.0.5的HA热备

  项目要求通过heartbeat控制pg和pgpool的主备过程,从而达到高可用,pg的自己的流复制主备方案参考我另外一篇主备方案文章,pgpool这里只用到连接池的功能。应用通过heartbeat虚拟出来的VIP访问pgpool池。
注:debian8下建议还是直接apt-get install heartbeat,编译各种依赖还会提示源码错误,配置文件路径一模一样,不过那个启动和关闭脚本得自己写,但是偶尔会报错openhpi相关的,老外说是BUG,但解决办法如下:
vim /etc/systemd/system/openhpid.service

Description=Daemon providing access to the SAF Hardware Platform Interface

Type=simple
ExecStart=/usr/sbin/openhpid -n -c /etc/openhpi/openhpi.conf

WantedBy=multi-user.target  一、准备工作
  1、两台debian8.4 xfs文件格式虚拟机,分别配置两个IP,留着一个VIP后面用
  2、按照另外一篇PG流复制主备方案,配置好PG的主备配置
  3、按照另外一篇pgpool编译安装用于连接池的配置安装pgpool
  4、参考我另外一篇关于linux下邮箱配置的文章,配置好邮箱
  二、配置heartbeat
  1、配置文件
cd /etc/ha.d
vim authkeys
auth 11 crc
#2 sha1 HI!
#3 md5 Hello!#---------------------heartbeat主配置文件----------------------
vim ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility   local0
#心跳间隔
keepalive 2
#死亡阀值
deadtime 30
#警告时间
warntime 10
#首次启动heartbeat,等待多久才启动主服务资源
initdead 30
#连接端口
udpport 694
#心跳线接口
#bcast   eth1
#主节点的网卡设备、备份机的心跳线接口IP
ucast eth0 192.168.180.223
#自动切换还是关掉好
auto_failback off
node elink-master
node elink-slave
#ping 192.168.180.1
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster#----------------------VIP资源控制文件----------------------
vim haresources
elink-master IPaddr::192.168.180.221/32/eth0 elinkresource.sh#---资源控制脚本,如果主机已经拿到资源,备机就算启动heartbeat也不会启动下面的脚本----
cd resource.d
vim elinkresource.sh
isPid()
{
      pid_result=`ps -ef | grep $1 | awk '{if($8!~/grep/) print $2}'`
      echo $1'的进程号: '$pid_result
      if [ -n "$pid_result" ];then
                return 1
      else
                return 0
      fi
}
start() {
      echo "***************************start PG HA******************************"
      isPid postmaster
      if [ $? -eq 0 ];then
                /etc/init.d/postgresql start
      else
                echo 'postgresql已经有进程,无需再次启动'
      fi
      isPid pgpool.conf
      if [ $? -eq 0 ];then
                pgpool -f /usr/local/etc/pgpool.conf -F /usr/local/etc/pcp.conf -D
      else
                echo 'pgpool已经有进程,无需再次启动'
      fi
      #pg_master_process_result=`ssh -l root 192.168.180.222 "ps -ef | grep postmaster | grep -v grep"`
      key_result=`su - postgres -c "pg_controldata | grep cluster | grep 'archive recovery'"`
      if [ -n "$key_result" ];then
                su - postgres -c "pg_ctl promote"
      else
                echo '此服务器不是备机无法promote为主机!!!'
      fi
      isPid sleep
      if [ $? -eq 0 ];then
                /opt/monitorpg.sh &
      else
                echo 'sleep监控进程已经存在无需再次启动'
      fi
}
stop() {
      echo "****************************stop PG HA*****************************"
      #sleep_result=`ps -ef | grep 'sleep 30' | awk '{if($8!~/grep/) print $3}'`
      #kill -9 $sleep_result
      pgpool -f /usr/local/etc/pgpool.conf -F /usr/local/etc/pcp.conf -D -m fast stop
      /etc/init.d/postgresql stop
      mv /usr/local/pgsql/data/recovery.done /usr/local/pgsql/data/recovery.conf
}
case $1 in
      start)
                start
      ;;
      stop)
                stop
      ;;
      restart)
                stop
                sleep 10
                start
      ;;
      *)
                echo "Usage: $0{start|stop|restart}"
esac  2、业务监控脚本
  heartbeat只能监控网络层的,例如关机、停止heartbeat后能切换过去,但如果sql都查询不了,那这个资源就是无效资源,当然需要切换
monitor(){
      result=`/usr/local/pgsql/bin/psql -h 192.168.180.222 -p5432 -U postgres -w -d postgres --command "select 1" | sed -n 3p | cut -c9-9`
      #echo $result
      if [ 1 -eq "$result" ];then
                echo `date`" 数据库正常,检测值:"$result >> /opt/moni.log
      else
                echo `date`" 数据库挂了,检测值:"$result >> /opt/moni.log
                service heartbeat stop
                break
      fi
}
while sleep 10
do
      monitor
done  报错:
业务监控脚本登陆pgpool提示需要输入密码,参考:
  .pgpass免密码登陆
通过在客户端/home/postgres,我们这里建议在/root和/home/postgres都放个隐藏文件.pgpass,从而避免链接数据库时弹出密码提示

vim /home/postgres/.pgpass
chmod 600 /home/postgres/.pgpass
cp /home/postgres/.pgpass /root
chown postgres.postgres /home/postgres/.pgpass
#格式:hostname:port:database:username:password
127.0.0.1:5432:postgres:postgres:postgres  3、测试
  目前主要有三类测试,
  a、主机heartbeat down机,备机能否正常接管资源
  主机:/etc/init.d/heartbeat stop,理论上业务监控脚本、pg、pgpool、heartbeat都会停止、recovery.done变为recovery.conf
  备机:pg_controldata | grep cluster显示为production,pg_ctl promote激活备机、pgpool、业务监控脚本启动、recovery.conf自动改为recovery.done
  主机恢复为standby:启动postgresql和heartbeat即可
  b、主机业务挂掉,备机能否接管
  主要是判断能否连上本地的数据库进行一个select查询,如果没有就会停止本地的heartbeat让备机接管资源
  c、主机硬关机,备机能否接管资源
  只要我心跳基本上都能很快接管,但是原主机恢复的时候,需要检查下recovery.done是否变为recover.conf,不然启动会报错
  

  

  




页: [1]
查看完整版本: debian8下配置postgresql9.5.2、pgpool3.5.2、heartbeat3.0.5的HA热备