一、需求架构图
主从数据一致,用vip进行故障切换,挂了任何一台对客户的访问没有影响,从机只提供读和备份服务。
分析:正常情况cs通过vip访问ms的postgresql服务,ss这时只备份
当mspostgresql服务停止,keepalived检测到后,也stop 自己的服务,让vip浮动到ss上
当ms 服务正常,vip自动浮动到ms上
二、服务器配置
cs客户端
[root@cs ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@cs ~]# ls
anaconda-ks.cfg postgresql-9.6.5.tar.gz
解压postgresql并切换到相应的目录,
检测:要联网,检测缺少依赖包yum一下就好了
编译:这个过程太长了,我通常编译一会就ctrl c终止了,后来使用也没发现有什么不同
安装:一定要看到complete才ok,数据目录在/usr/local/pgsql下
[root@cs postgresql-9.6.5]# ./configure
[root@cs postgresql-9.6.5]# make
[root@cs postgresql-9.6.5]# make install
[root@cs pgsql]# ls
bin include lib share
没有数据目录是因为数据库还没有初始化,我还是自己创建一个数据目录pgdata吧
再指定postgresql的命令路径
[root@cs pgsql]# mkdir pgdata
[root@cs pgsql]# vim /etc/bashrc
#添加
export PATH=$PATH:/usr/local/pgsql/bin
export PGDATA=/usr/local/pgsql/pgdata
[root@cs pgsql]# useradd postgres
[root@cs pgsql]# cd ..
[root@cs local]# chown postgres:postgres pgsql -R
[root@cs local]# which initdb
/usr/local/pgsql/bin/initdb
[root@cs local]# su postgres
[postgres@cs local]$ initdb -D $PGDATA
[postgres@cs local]$ cd pgsql/pgdata
[postgres@cs pgdata]$ pwd
/usr/local/pgsql/pgdata
[postgres@cs pgdata]$ pg_ctl start -D $PGDATA -l logfile
[postgres@cs pgdata]$ psql
psql (9.6.5)
Type "help" for help.
postgres=#
postgres=# \q
[postgres@cs pgdata]$
[postgres@ms pgdata]$ psql
postgres# CREATE ROLE replica login replication encrypted password 'replica'
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
replica | Replication
postgres=# \du
[postgres@ms pgdata]$ vim pg_hba.conf
添加
host all all 192.168.12.1/32 trust
host all all 192.168.12.7/32 trust
host replication replica 192.168.12.8/32 md5
[postgres@ms pgdata]$ vim postgresql.conf
修改
listen_addresses = '*' # 监听所有IP
port = 5432
max_connections = 100 #这个设置要注意下,从库的max_connections必须要大于主库的
wal_level = hot_standby
max_wal_senders = 32 # 这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
wal_keep_segments = 256 # 设置流复制保留的最多的xlog数目
wal_sender_timeout = 60s # 设置流复制主机发送数据的超时时间
[postgres@ms pgdata]$ pg_ctl stop -D $PGDATA
[postgres@ms pgdata]$ pg_ctl start -D $PGDATA -l logfile
客户端cs测试是否可以连接ms
[postgres@cs pgdata]$ psql -h 192.168.12.7
psql (9.6.5)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
replica | Replication | {}
postgres=#
[postgres@cs pgdata]$psql -h 192.168.12.7
psql (9.6.5)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
replica | Replication | {}
postgres=# \q
[postgres@ss pgdata]$rm -rf ./* #先将data目录下的数据都清空
[postgres@ss pgdata]$pg_basebackup -F p --progress -D $PGDATA -h 192.168.12.7 -p 5432 -U replica --password # 基础备份
输入密码:replica
[postgres@ss pgdata]$ls
[postgres@ss pgdata]$cp ../share/recovery.conf.sample recovery.conf
[postgres@ss pgdata]$vim recovery.conf
standby_mode = on
primary_conninfo = 'host=192.168.12.7 port=5432 user=replica password=replica'
recovery_target_timeline = 'latest'
修改postgres.conf
[postgres@ss pgdata]$vim postgres.conf
max_connections = 1000
hot_standby = on
max_standby_streaming_delay = 30
wal_receiver_status_interval = 1s
hot_standby_feedback = on
重启从库
[postgres@ss pgdata]$pg_ctl stop -D $PGDATA
[postgres@ms pgdata]$ pg_ctl start -D $PGDATA -l logfile
这里没有意外,数据库的主从就ok了
测试:
1.cs上:psql -h 192.168.12.8
2.在数据库上创建数据库到从上查看是否同步
[postgres@ms pgsql]psql
psql (9.6.5)
Type "help" for help.
postgres-# \l;
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# create database test;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
postgres=#
[root@ss ~]# yum install -y keepalived
[root@ss ~]# yum install -y keepalived
Complete!
[root@ms ~]# cd /etc/keepalived/
[root@ms keepalived]# ls
keepalived.conf
[root@ms keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.12.7(8)
}
vrrp_script chk_pg {
script "/etc/keepalived/pg_check.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER(BACKUP)
interface ens33
virtual_router_id 51
mcast_src_ip 192.168.12.7(8)
priority 100(90)
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_pg
}
virtual_ipaddress {
192.168.12.100
}
}
[root@ms keepalived]# vim pg_check.sh
#!/bin/bash
N=`ps -C postgres --no-header | wc -l`
if [ $N -eq 0 ];then
systemctl stop keepalived
fi
增加执行权限
[root@ms keepalived]# chmod 755 pg_check.sh
启动keepalived
[root@ms keepalived]# systemctl start keepalived
[root@ms keepalived]# ip a
ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:81:2b:99 brd ff:ff:ff:ff:ff:ff
inet 192.168.12.7/24 brd 192.168.221.255 scope global dynamic ens33
valid_lft 1629sec preferred_lft 1629sec
inet 192.168.12.100/32 scope global ens33 #在对应网卡下找到vip表示成功
valid_lft forever preferred_lft forever
从和主机同样的操作只是在从机的配置文件keepalived.conf将有括号的地方改成括号里的就可以了。
从机启动成功后,ip a看不到绑定的虚拟ip,只有等主机挂了,虚拟ip才会浮到从机去。
这里的ss上的postgresql是只读的,也就意味着主挂了切到从上面,虽然服务可以访问却不能写入数据。
解决这个弊端,请看云部署(2)
别人的经验只能用来参考