postgresql+keepalived高可用部署

前提准备

基于流复制部署,keepalived检测postgresql状态,如果pgsql死了,那么keepalivedVIP漂移到备库,同时提升备库为主库,可以进行读写操作。(默认流复制的主库为可读可写,备库为只读)

主库:10.211.55.8

从库:10.211.55.11

Vip: 10.211.55.15

安装keepalived

主备都需要安装keepalived

yum install -y keepalived

修改配置文件

$ vim /etc/keepalived/keepalived.conf

配置文件位置有可能不一样,可以通过whereis keepalived查找一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
! Configuration File for keepalived

#全局配置
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL # 这个主备配置一致
vrrp_skip_check_adv_addr
# vrrp_strict # 这行注释掉
vrrp_garp_interval 0
vrrp_gna_interval 0
}

#执行脚本配置
vrrp_script check_pg_alived {
script "/etc/keepalived/scripts/check_pg.sh" #检测脚本位置
interval 2
weight -5
fall 2
rise 1
}

vrrp_instance VI_1 {
state BACKUP # 主备一致,都是BACKUP
interface eth0 #网卡
virtual_router_id 51 # 主备需要一致
priority 101 # 优先级 ,主需要比备高 比如主是101 备可以是100
advert_int 1
authentication {
auth_type PASS # 主备需要一致
auth_pass 1111 # 主备需要一致
}
track_script {
check_pg_alived
}
virtual_ipaddress { # vip设置 主备一致
10.211.55.15
}
}

主备脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$ vim  /etc/keepalived/scripts/check_pg.sh 


#!/bin/bash
#判断pg是否活着
A=`ps -C postgres --no-header | wc -l`

#判断vip浮到哪里 这里的vip需要更改成自己设置的vip
B=`ip a | grep 10.211.55.15 | wc -l`

#判断是否是从库处于等待的状态
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`

#如果pg死了,将消息写入日记并且关闭keepalived
if [ $A -eq 0 ];then
echo "`date "+%Y-%m-%d--%H:%M:%S"` postgresql stop so vip stop " >> /etc/keepalived/log/check_pg.log
systemctl stop keepalived
else

#判断出主ku挂了,vip漂移到了从,提升从的地位让他可读写
if [ $B -eq 1 -a $C -eq 1 -a $D -eq 0 ];then
su - postgres -c "pg_ctl promote -D /var/lib/pgsql/9.5/data/" #重新加载pgsql使其可写
echo "`date "+%Y-%m-%d--%H:%M:%S"` standby promote " >> /etc/keepalived/log/check_pg.log
fi

#判断出自己是主并且和从失去联系
if [ $B -eq 1 -a $C -eq 0 -a $D -eq 0 -a $E -eq 0 ];then
echo "`date "+%Y-%m-%d--%H:%M:%S"` can't find standby " >> /etc/keepalived/log/check_pg.log
fi
fi

然后需要给与相应的可执行权限

chomd a+x /etc/keepalived/scripts/check_pg.sh

修改keepalived配置

vim /etc/sysconfig/keepalived

KEEPALIVED_OPTIONS="-D" 修改为KEEPALIVED_OPTIONS="-D -d -S 0"

vim /etc/rsyslog.conf

加入如下配置:

1
2
#keepalived -S 0  
local0.* /var/log/keepalived.log

关闭SELINUX和防火墙

1
2
$ systemctl stop firewalld  && systemctl disable firewalld 
$ setenforce 0 ##永久关闭修改vim /etc/sysconfig/selinux

启动keepalived服务检查VIP情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ systemctl start keepalived  && systemctl  enable keepalived 
$ ip addr show eth0
# 主库有vip
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:1c:42:fe:68:b6 brd ff:ff:ff:ff:ff:ff
inet 10.211.55.8/24 brd 10.211.55.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.211.55.15/32 scope global eth0
valid_lft forever preferred_lft forever

#从库没有vip
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:1c:42:da:8a:7a brd ff:ff:ff:ff:ff:ff
inet 10.211.55.11/24 brd 10.211.55.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever

检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1)查看主库与备库的数据库是否一致,并在主库上写入数据,再到备库上查看是否同步过去了。确定同步没有问题(流复制检查)  
2)使用vip连接数据库并写入数据
$ psql -h 10.211.55.15 -U postgres

create table test(id int); 创建test表

insert into test values (1),(2); 插入数据

3)主备查看数据

select * from test;

4)模拟主库宕机,检测备库情况
systemctl stop postgresql #停掉主库(10.211.55.8

检查vip在哪一台主机,连接vip再次插入数据,如果可以写入数据则表示备库切换主库成功;
insert into test values (3);

现在只有一台运行(74


postgresql+keepalived高可用部署
https://randzz.cn/18d840ef6529/postgresql-keepalived高可用部署/
作者
Ezreal Rao
发布于
2020年6月11日
许可协议