基于keepalived postgres的高可用

一)環(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


參考鏈接:

https://www.seraphln.com/article/2019/4/3/17.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末橄抹,一起剝皮案震驚了整個(gè)濱河市靴迫,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌楼誓,老刑警劉巖玉锌,帶你破解...
    沈念sama閱讀 218,640評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異疟羹,居然都是意外死亡主守,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)阁猜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)丸逸,“玉大人,你說(shuō)我怎么就攤上這事剃袍』聘眨” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,011評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵民效,是天一觀的道長(zhǎng)憔维。 經(jīng)常有香客問(wèn)我涛救,道長(zhǎng),這世上最難降的妖魔是什么业扒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,755評(píng)論 1 294
  • 正文 為了忘掉前任检吆,我火速辦了婚禮,結(jié)果婚禮上程储,老公的妹妹穿的比我還像新娘蹭沛。我一直安慰自己,他們只是感情好章鲤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布摊灭。 她就那樣靜靜地躺著,像睡著了一般败徊。 火紅的嫁衣襯著肌膚如雪帚呼。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,610評(píng)論 1 305
  • 那天皱蹦,我揣著相機(jī)與錄音煤杀,去河邊找鬼。 笑死沪哺,一個(gè)胖子當(dāng)著我的面吹牛沈自,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播凤粗,決...
    沈念sama閱讀 40,352評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼酥泛,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了嫌拣?” 一聲冷哼從身側(cè)響起柔袁,我...
    開(kāi)封第一講書(shū)人閱讀 39,257評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎异逐,沒(méi)想到半個(gè)月后捶索,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡灰瞻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評(píng)論 3 336
  • 正文 我和宋清朗相戀三年腥例,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酝润。...
    茶點(diǎn)故事閱讀 40,021評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡燎竖,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出要销,到底是詐尸還是另有隱情构回,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站纤掸,受9級(jí)特大地震影響脐供,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜借跪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評(píng)論 3 330
  • 文/蒙蒙 一政己、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧掏愁,春花似錦歇由、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,936評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至京腥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間溅蛉,已是汗流浹背公浪。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,054評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留船侧,地道東北人欠气。 一個(gè)月前我還...
    沈念sama閱讀 48,224評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像镜撩,于是被迫代替她去往敵國(guó)和親预柒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容