1.1配置postgresql复制hostnamectl set-hostname mypg01hostnamectl set-hostname mypg02Hosts文件配置# vim etc/hosts10.110.9.37 mypg0110.110.9.51 mypg0210.110.9.241 vip以下操作分别在主节点和备节点同时执行:root配置两台数据库服务器之间的信任关系su - rootssh-keygenssh-copy-id root@备节点IP 主节点执行ssh-copy-id root@主节点IP 备节点执行------------------------------------------------------------------------------------关闭防火墙systemctl status firewalld.servicesystemctl stop firewalld.servicesystemctl disable firewalld.serviceSELINUXvi etc/selinux/config将SELINUX=enforcing 改为SELINUX=disabled配置流复制主备,主节点配置编辑vi pg_hba.conf文件,添加如下内容:host all all 0.0.0.0/0 md5host replication replica 0.0.0.0/0 md5编辑postgresql.conf文件,添加如下内容:listen_addresses = '*'port = 5432wal_level = replicamax_wal_senders = 32max_replication_slots = 32hot_standby = onhot_standby_feedback = onmax_standby_streaming_delay = 30swal_receiver_status_interval = 10swal_sender_timeout = 60swal_log_hints = onunix_socket_directories = '.'unix_socket_permissions = 0700bgwriter_delay = '10ms'datestyle = 'iso, mdy'full_page_writes = 'True'huge_pages = 'False'lc_messages = 'C'lc_monetary = 'C'lc_numeric = 'C'lc_time = 'C'tcp_keepalives_idle = 0tcp_keepalives_interval = 0tcp_keepalives_count = 0wal_receiver_status_interval = '1s'wal_writer_delay = '10ms'autovacuum_max_workers=10maintenance_work_mem = 256MBvacuum_cost_limit=150max_worker_processes = 128checkpoint_timeout = 10minlog_disconnections = onwal_writer_delay = 10mslog_destination = 'csvlog'logging_collector = onlog_directory = '/opt/pg_root/pg_log'log_filename = 'postgresql-log.%a'log_file_mode = 0600log_truncate_on_rotation = onlog_rotation_age = 1440log_rotation_size = 1000000log_min_duration_statement = 500log_statement = ddlhuge_pages = off创建 .pgpass文件su - postgresvi ~/.pgpass1主机IP:5432:postgres:postgres:1234562主机IP:5432:postgres:postgres:1234561主机IP:5432:replication:replica:REPLICA3212主机IP:5432:replication:replica:REPLICA321chmod 0600 .pgpass流复制配置 在node1节点#创建replica用户create role replica login replication encrypted password 'REPLICA321';创建判断数据库时延的库、角色、表create role keepalived nosuperuser nocreatedb login encrypted password 'keepalived';create database keepalived with owner=keepalived encoding='UTF8';\c keepalived keepalivedcreate table sr_delay(id int4,sr_date timestamp(0) without time zone);insert into sr_delay values(1,now());配置主从复制备库配置从此处开始配置备库,首先通过pg_basebackup命令行工具在从库上生成基础备份命令如下:rm -rf opt/pg_root/*pg_basebackup -F p -P -R -D $PGDATA -h 10.110.9.37 -p 5432 -U replicaREPLICA321会生成:standby.signal 文件postgresql.auto.conf 文件会添加连接信息primary_conninfo = 'user=replica password=REPLICA321 host=10.110.8.97 port=5432 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any'
安装keeplivedkeepalived-2.2.7.tar.gztar –zxvf keepalived-2.2.7.tar.gzcd keepalived-2.2.7/./configure --prefix=/usr/local/keepalivedmake && make install将自动生成的keeplived文件复制目录usr/sbincp /usr/local/keepalived/sbin/keepalived usr/sbin/将自动生成的keeplived.conf文件复制到目录/etc/sysconfigcp /usr/local/keepalived/etc/sysconfig/keepalived etc/sysconfig/选择keeplived 文件上传,cd etc/init.d/设置文件当前用户可读可写chmod u+x etc/init.d/keepalived查看上传后的文件cat /etc/init.d/keepalived修改后 建立文件路径mkdir /etc/keepalived/mkdir /etc/keepalived/scriptsmkdir /etc/keepalived/logtouch /etc/keepalived/log/failover.logtouch /etc/keepalived/log/pg_status.log主备节点分别上传进入此目录下/etc/keepalived/上传修改后的keeplived.confchmod 644 keeplived.conf进入此目录下/etc/keepalived/scripts上传修check_pg.sh、failover.shchmod 755 /etc/keepalived/scripts/*修改keepalived.conf配置文件网卡名、VIP地址Keepalived启动及状态查看MASTER启动keepalived 服务器# service keepalived startMASTER查看keepalived 状态# service keepalived status
keeplived.conf! Configuration File for keepalivedglobal_defs {router_id mypg02}vrrp_script check_pg_alived {script "/etc/keepalived/scripts/check_pg.sh"interval 5fall 3 # require 3 failures for KO}vrrp_instance VI_1 {state BACKUPnopreemptinterface ens32virtual_router_id 10priority 90advert_int 1authentication {auth_type PASSauth_pass abcdefgh}track_script {check_pg_alived}virtual_ipaddress {10.110.9.241}smtp_alertnotify_master "/etc/keepalived/scripts/failover.sh"}------------------------------------------check_pg.sh#!/bin/bashexport PGDATABASE=keepalivedexport PGPORT=5432export PGUSER=keepalivedexport PGHOME=/opt/pgsqlexport PATH=$PGHOME/bin:$PATH:$HOME/binPGMIP=127.0.0.1LOGFILE=/etc/keepalived/log/pg_status.log#pg_port_status=`lsof -i :$PGPORT | grep LISTEN | wc -l`#pg_port_status=`ps -ef | grep LISTEN | wc -l`SQL1='SELECT pg_is_in_recovery from pg_is_in_recovery();'SQL2='update sr_delay set sr_date = now() where id =1;'SQL3='SELECT 1;'db_role=`echo $SQL1 | $PGHOME/bin/psql -h 127.0.0.1 -p $PGPORT -d $PGDATABASE -U $PGUSER -At -w`#if [ $pg_port_status -lt 1 ];then# echo -e `date +"%F %T"` "Error: The postgreSQL is not running,please check the postgreSQL server status!" >> $LOGFILE# exit 1#fiif [ "$db_role" == 't' ];thenecho -e `date +"%F %T"` "Attention1:the current database is standby DB!" >> $LOGFILEexit 0fiecho $SQL3 | psql -h 127.0.0.1 -p $PGPORT -d $PGDATABASE -U $PGUSER -At -wif [ $? -eq 0 ] ;thenecho $SQL2 | psql -h 127.0.0.1 -p $PGPORT -d $PGDATABASE -U $PGUSER -At -wecho -e `date +"%F %T"` "Success: update the master sr_delay successed!" >> $LOGFILEexit 0elseecho -e `date +"%F %T"` "Error:Is the server is running?" >> $LOGFILEexit 1fi------------------------------------------failover.sh#!/bin/bashexport PGPORT=5432export PGUSER=keepalivedexport PG_OS_USER=postgresexport PGDATA=/opt/pg_rootexport PGDBNAME=keepalivedexport LANG=zh_CN.UTF-8export PGPATH=/opt/pgsql/binexport PATH=$PATH:$PGPATHPGMIP=127.0.0.1LOGFILE=/etc/keepalived/log/failover.logSQL1='select pg_is_in_recovery from pg_is_in_recovery();'SQL2="select sr_date as delay_time from sr_delay where now()-sr_date < interval '100';"db_role=`echo $SQL1 | psql -At -h 127.0.0.1 -p $PGPORT -U $PGUSER -d $PGDBNAME -w`db_sr_delaytime=`echo $SQL2 | psql -h 127.0.0.1 -p $PGPORT -d $PGDBNAME -U $PGUSER -At -w`SWITCH_COMMAND='pg_ctl promote -D $PGDATA'if [ "$db_role" == 'f' ];thenecho -e `date +"%F %T"` "Attention:The current postgreSQL DB is master database,cannot switched!" >> $LOGFILEexit 0fiif [ "$db_role" == 't' ];thenecho -e `date +"%F %T"` "Attention:The current database is statndby,ready to switch master database!" >> $LOGFILEsu - $PG_OS_USER -c "$SWITCH_COMMAND"fi#if [ $db_sr_delaytime -gt 0 ];then# echo -e `date +"%F %T"` "Attention:The current master database is health,the standby DB cannot switched!" >> $LOGFILE# exit 0#fi#if [!"$db_sr_delaytime"];then# echo -e `date +"%F %T"` "Attention:The current database is statndby,ready to switch master database!" >> $LOGFILE# su - $PG_OS_USER -c "$SWITCH_COMMAND"#elif [ $? -eq 0 ];then# echo -e `date +"%F %T"` "success:The current standby database successed to switched the primary PG database !" >> $LOGFILE# exit 0#else# echo -e `date +"%F %T"` "Error: the standby database failed to switch the primary PG database !,pelease checked it!" >> $LOGFILE# exit 1#fi------------------------------------------
文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




