云部署(2)keepalived+postgresql主从切换

在主从部署成功后,主库写入数据会同步到备库中。

keepalived检测pgsql的状态,当主库停止时,keepalived会将自己杀死,vip移到备库中,此时备库对外提供服务,但是不能写入。

下面配置主从切换后,从变成主,可读写。主变成从,只同步数据。

此时基于云部署(1),我的两个主从服务器状态同步正常

1.主从停掉postgres库,然后主从的keepalived都会停止,最好再检查一下让它们都停止。

2.修改主从的配置文件,如下:

[root@ss keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     ***@qq.com
   }
   notification_email_from ***@***.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ss
}

vrrp_script chk_pgsql {
   script "/etc/keepalived/pgsql_check.sh"
   interval 2
   weight -5
   fall 2
   rise 1
}
vrrp_instance VI_1 {
    state BACKUP  #主从都是BACKUP
    interface eth0
    virtual_router_id 51
    priority 100 #从改成96
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
       track_script {
       chk_pgsql
  }
   virtual_ipaddress {
        192.168.12.100
    }
}

3.修改主从的keepalived检测脚本,如下:

[root@ss keepalived]# cat pgsql_check.sh 
#!/bin/bash
A=`ps -C postgres --no-header | wc -l` #判断pg是否活着
B=`ip a | grep 192.168.12.100 | wc -l` #判断vip浮到哪里
C=`ps -ef | grep postgres | grep 'startup process' | wc -l` #判断是否是从库处于等待的状态
D=`ps -ef | grep postgres | grep 'receiver' | wc -l` #判断从库链接主库是否正常
E=`ps -ef | grep postgres | grep 'sender' | wc -l` #判断主库连接从库是否正常
if [ $A -eq 0 ];then #如果pg死了,将消息写入日记并且关闭keepalived
    echo "`date "+%Y-%m-%d--%H:%M:%S"` postgresql stop so vip stop " >> /etc/keepalived/check_pg.log
    systemctl stop keepalived
else
	if [ $B -eq 1 -a $C -eq 1 -a $D -eq 0 ];then #判断出主挂了,vip浮到了从,提升从的地位让他可读写
		su - postgres -c "pg_ctl promote -D /usr/local/pgsql/pgdata" 
		echo "`date "+%Y-%m-%d--%H:%M:%S"` standby promote " >> /etc/keepalived/check_pg.log
	fi
	if [ $B -eq 1 -a $C -eq 0 -a $D -eq 0 -a $E -eq 0 ];then #判断出自己是主并且和从失去联系
                sleep 10
       		echo "`date "+%Y-%m-%d--%H:%M:%S"` can't find standby " >> /etc/keepalived/check_pg.log
	fi
fi

依次启动主pg,从pg,主keepalived,从keepalived

将firewalld、selinux关掉

检测:

查看ms与ss的数据库是否一致,并在ms上写入数据,再到ss上查看是否同步过去了。若ms与ss主从同步没有问题

cs上,链接vip写入数据

[postgres@cs pgsql]$ psql -h 192.168.12.100
psql (9.6.5)
Type "help" for help.

postgres=# \c hedb;
You are now connected to database "hedb" as user "postgres".
hedb=# select * from hetb;
   name   | age 
----------+-----
 heyajing |  24
 heyaya   |  23
(2 rows)
hedb=# insert into hetb values('yayajing',25);
INSERT 0 1
hedb=# select * from hetb;
   name   | age 
----------+-----
 heyajing |  24
 heyaya   |  23
 yayajing |  25
(3 rows)

hedb=# 

再分别到主从上查看是否都新生成一行数据,目前现象和(1)没什么区别

下面我们停掉主的pg

[postgres@ms pgdata]$ pg_ctl stop -D $PGDATA
waiting for server to shut down.... done
server stopped

再向客户端插入数据

hedb=# insert into hetb values('noco',21);
INSERT 0 1
hedb=# 

成功,现在只有一台服务器运行(主ms已经停了),说明从库ss现在的身份是主,不信你可以去从库查看并插入数据,
======

那么ms故障恢复如何将它作为ss的从呢?

1.同步数据

这个地方最好弄read-wind但是我一直没成功,如果后来成功我会更新的

暂时就用pg_basebackup吧

[postgres@ss pgsql]$ cp pgdata pgdata.bbk -R
[postgres@ss pgsql]$ rm -rf pgdata/*
[postgres@ss pgsql]$ pg_basebackup -F p --progress -D $PGDATA -h 192.168.12.8 -p 5432 -U replica --password
Password: 
37503/37503 kB (100%), 1/1 tablespace
NOTICE:  WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup
[postgres@wt-07 pgsql]$ 

2.修改recovery.done表明自己降为从库

[postgres@ss pgdata]$ mv recovery.done recovery.conf
[postgres@ss pgdata]$ vim recovery.conf 
将host改为192.168.12.8
[postgres@ss pgdata]$ pg_ctl start -D $PGDATA -l $PGDATA/logfile
server starting
[postgres@ss pgdata]$ ps -ef | grep postgres
有从库进程标志
postgres 127141 127140  0 17:27 ?        00:00:00 postgres: startup process   recovering 000000020000000000000007
postgres 127142 127140  0 17:27 ?        00:00:00 postgres: wal receiver process   streaming 0/7000140
[postgres@ss pgdata]$ 

看ss上是否有主库进程标识:

[postgres@cs pgdata]$ ps -ef | grep postgres
postgres  25704  25597  0 17:27 ?        00:00:00 postgres: wal sender process replica 192.168.12.7(45848) streaming 0/7000140 #有

3.检测

cs 插入数据是ok的

ss与ms都可以查看到,ms作为从只读,但是此时ms的keepalived还没有开启,开启后vip却浮到了ms,这里说明我的keepalived的配置有问题。这里我只能手动降级。

还是觉得我的配置不严谨。











你可能感兴趣的:(云部署(2)keepalived+postgresql主从切换)