一)環(huán)境描述
主節(jié)點(diǎn):172.31.0.2
從節(jié)點(diǎn):172.31.0.3
數(shù)據(jù)庫(kù)目錄:/data/apps_data/postgres_data
postgres用戶密碼:a123456!
數(shù)據(jù)同步賬號(hào):repl
數(shù)據(jù)同步密碼:a123456!
二)安裝步驟與主從復(fù)制搭建
安裝postgresql數(shù)據(jù)庫(kù)
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y
yum list | grep postgresql
yum install postgresql10-contrib.x86_64 postgresql10-server.x86_64 -y
創(chuàng)建數(shù)據(jù)目錄挖息,默認(rèn)是/var/lib/pgsql带斑,這里更改為/data/apps_data/postgres_data
mkdir /data/apps_data/postgres_data
chown postgres:postgres -R /data/apps_data/postgres_data/
chmod 750 /data/apps_data/postgres_data/
修改配置文件,更改第31行數(shù)據(jù)目錄
vim /usr/lib/systemd/system/postgresql-10.service
Environment=PGDATA=/data/apps_data/postgres_data
添加環(huán)境變量
vim /etc/profile
exportPATH=/usr/pgsql-10/bin:$PATH
exportLD_LIBRARY_PATH=/usr/pgsql-10/lib
exportPGDATA=/data/apps_data/postgres_data
source /etc/profile
初始化postgresql并配置重啟自動(dòng)啟動(dòng)
/usr/pgsql-10/bin/postgresql-10-setup initdb
systemctl start postgresql-10
systemctl enable postgresql-10.service
設(shè)置登錄密碼
su - postgres
-bash-4.2$ psql
psql (10.9)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD 'a123456!';
ALTER ROLE
postgres=# \q
設(shè)置遠(yuǎn)程訪問(wèn)及主從配置
創(chuàng)建復(fù)制賬號(hào):
su - postgres
createuser --replication -P repl
根據(jù)提示輸入密碼
vim /data/apps_data/postgres_data/pg_hba.conf #拉到最下面藕赞,更改如下
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, bya user with the
# replication privilege.
#local replication all md5
#host replication all 127.0.0.1/32 md5
#host replication all ::1/128 md5
host all all 0.0.0.0/0 md5
host replication repl 172.31.0.2/32 md5
host replication repl 172.31.0.3/32 md5
更改postgresql.conf配置文件
vim /data/apps_data/postgres_data/postgresql.conf
listen_addresses = '*'
port = 5432
max_connections = 1000
wal_level = hot_standby
synchronous_commit = local
archive_mode = on
archive_command = 'cp %p /data/apps_data/postgres_data/archive/%f'
max_wal_senders = 2
wal_keep_segments = 10
synchronous_standby_names = 'pgslave01'
創(chuàng)建archive目錄,并修改權(quán)限和用戶
mkdir -pv?/data/apps_data/postgres_data/archive/?
chmod 700 -R /data/apps_data/postgres_data/archive/?
chown -R postgres:postgres /data/apps_data/postgres_data/archive/?
啟動(dòng)主節(jié)點(diǎn)的postgres
systemctl start postgresql-10
從節(jié)點(diǎn)加載最新配置,并停止
systemctl start postgresql-10
systemctl stop postgresql-10
備份從節(jié)點(diǎn)數(shù)據(jù)目錄唁情,并創(chuàng)建新的數(shù)據(jù)目錄
mv /data/apps_data/postgres_data /data/apps_data/postgres_data-back_`date+%Y-%m-%d-%M`
mkdir /data/apps_data/postgres_data && chmod-R 700 /data/apps_data/postgres_data && chown -R postgres:postgres /data/apps_data/postgres_data
進(jìn)入postgres用戶登陸啊终,并將所有的數(shù)據(jù)目錄從主服務(wù)器同步回來(lái)
su - postgres
pg_basebackup -h 172.31.0.2 -U repl -D /data/apps_data/postgres_data/ -P
輸入密碼:
傳輸完畢后镜豹,在/data/apps_data/postgres_data/目錄下創(chuàng)建recovery.conf文件,并寫(xiě)入相關(guān)配置
touch /data/apps_data/postgres_data/recovery.conf
chown postgres:postgres?/data/apps_data/postgres_data/recovery.conf
vim /data/apps_data/postgres_data/recovery.conf
standby_mode = 'on'
primary_conninfo = 'host=172.31.0.3 port=5432 user=repl password=a123456! application_name=pgslave1'
啟動(dòng)從節(jié)點(diǎn)的postgres蓝牲,并且登陸主節(jié)點(diǎn)執(zhí)行
select * frompg_stat_replication; 查看是否有從節(jié)點(diǎn)信息逛艰,若有則說(shuō)明主從復(fù)制搭建成功
三)keepalived高可用搭建
安裝keepalived
yum install -y net-tools psmisc
yum install -y net-snmp
yum install -y ipvsadm? keepalived
cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf
主節(jié)點(diǎn)配置
?cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
script_user root
# enable_script_security
}
vrrp_script check_postgres {
script "/data0/scripts/keepalived_scripts/check-postgres.sh"
interval 10
weight +10
fall 5
rise 1
}
vrrp_instance VI_4 {
? state BACKUP
? notify_master "/data0/scripts/keepalived_scripts/start-postgres.sh"
? unicast_src_ip 172.31.0.2
? unicast_peer {
? ? 172.31.0.3
? }
? nopreempt? ? ? ?#非搶占模式,用于在故障恢復(fù)的時(shí)候不切換
? interface ens5
? virtual_router_id 54
? priority 140
? authentication {
? ? ? auth_type PASS
? ? ? auth_pass 555555
? }
? virtual_ipaddress {
? ? ? 172.31.0.66
? }
? track_script {
? ? ? check_postgres
? }
}
cat?/data0/scripts/keepalived_scripts/check-postgres.sh
#!/bin/bash
fix_pid=`ps -aux|grep postgres|grep -v check-postgres.sh|grep -v grep`
if [ -z "$fix_pid" ];
then
? ? exit 1
else
? ? exit 0
fi
cat?/data0/scripts/keepalived_scripts/start-postgres.sh
#!/bin/bash
su - postgres -c "pg_ctl promote -D /data/apps_data/postgres_data/"
從節(jié)點(diǎn)配置
?cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
script_user root
# enable_script_security
}
vrrp_script check_postgres {
script "/data0/scripts/keepalived_scripts/check-postgres.sh"
interval 10
weight 1
fall 3
rise 1
}
vrrp_instance VI_4 {
? state BACKUP
? notify_master "/data0/scripts/keepalived_scripts/start-postgres.sh"
? notify_backup "/data0/scripts/keepalived_scripts/kill-postgres.sh"
? unicast_src_ip 172.31.0.3
? unicast_peer {
? ? 172.31.0.2
? }
? interface ens5
? virtual_router_id 54
#? nopreempt
? priority 142
? authentication {
? ? ? auth_type PASS
? ? ? auth_pass 555555
? }
? virtual_ipaddress {
? ? ? 172.31.0.66
? }
? track_script {
? ? ? check_postgres
? }
}
cat?/data0/scripts/keepalived_scripts/start-postgres.sh
#!/bin/bash
su - postgres -c "pg_ctl promote -D /data/apps_data/postgres_data/"
touch /data/scripts/keepalived_scripts/postgres.text
cat /data0/scripts/keepalived_scripts/kill-postgres.sh
#!/bin/bash
rm -rf /data/scripts/keepalived_scripts/postgres.text? ?#作為標(biāo)識(shí)位搞旭,用于識(shí)別此時(shí)的節(jié)點(diǎn)信息是否為主
cat /data0/scripts/keepalived_scripts/check-postgres.sh
#!/bin/bash
fix_pid=`ps -aux|grep postgres|grep -v check-postgres.sh|grep -v grep`
if [ -z "$fix_pid" ];
then
? ? if [ -e "/data/scripts/keepalived_scripts/postgres.text" ];? #判斷是否為主,若是從節(jié)點(diǎn)postgres宕機(jī)則無(wú)操作菇绵,為主節(jié)點(diǎn)時(shí)宕機(jī)則需要重啟keepalived
? ? then
? ? ? ? systemctl restart keepalived
? ? fi
? ? exit 0
else
? ? exit 0
fi
先啟動(dòng)主節(jié)點(diǎn)的keepalived然后再啟動(dòng)從節(jié)點(diǎn)的keepalived
高可用描述:當(dāng)主從正常時(shí)肄渗,主節(jié)點(diǎn)的優(yōu)先級(jí)為150,從節(jié)點(diǎn)的優(yōu)先級(jí)為143咬最,主節(jié)點(diǎn)獲得vip:172.31.0.66翎嫡,當(dāng)主節(jié)點(diǎn)的postgres宕機(jī)時(shí),其優(yōu)先級(jí)下降為140永乌,從節(jié)點(diǎn)升級(jí)為主機(jī)點(diǎn)獲得vip提供服務(wù)惑申,當(dāng)舊的主節(jié)點(diǎn)恢復(fù)的時(shí)候由于設(shè)置了非搶占模式,所以哪怕優(yōu)先級(jí)回到了150也不會(huì)搶占vip翅雏,將其配置為新主節(jié)點(diǎn)的從即可繼續(xù)實(shí)現(xiàn)高可用圈驼,在此時(shí)如果新的主節(jié)點(diǎn)的postgres再發(fā)生宕機(jī),那么根據(jù)keepalived的check腳本會(huì)重啟keepalived望几,讓非搶占式的從節(jié)點(diǎn)成為主節(jié)點(diǎn)绩脆,vip又切換回去了
寫(xiě)的有些匆忙可能還會(huì)有一些不足,歡迎指正
聯(lián)系郵箱:958905365@qq.com
參考鏈接: