當(dāng)前位置:首頁(yè) > IT技術(shù) > 數(shù)據(jù)庫(kù) > 正文

基于pgpool搭建postgressql集群部署
2021-09-24 14:54:43

postgresql集群搭建

基于pgpool中間件實(shí)現(xiàn)postgresql一主多從集群部署,這里用兩臺(tái)服務(wù)器作一主一從示例

虛擬機(jī)名 IP 主從劃分
THApps 192.168.1.31 主節(jié)點(diǎn)
YY-Test-01 192.168.1.36 從節(jié)點(diǎn)
vip 192.168.1.100 虛擬ip

1.軟件版本

系統(tǒng)版本 CentOS Linux release 7.9.2009 (Core)
PostgreSQL 10
pgpool-II 4.0.1

2.整體架構(gòu)

Pgpool-II集群

3.安裝postgresql

3.0.安裝postgresql-10

# 1.更新源
網(wǎng)頁(yè)打開(kāi):https://yum.postgresql.org/repopackages.php  
找到版本 CentOS 7 - x86_64 
右鍵 復(fù)制鏈接
# 在centos系統(tǒng)中運(yùn)行:
	yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    
# 2.安裝postgresql
# 執(zhí)行命令:
	yum update -y
# 查看postgresql源:
	yum list | grep postgresql
# 我們需要安裝的是這兩個(gè): postgresql10-contrib postgresql10-server
yum install postgresql10 postgresql10-contrib postgresql10-server postgresql10-devel -y

# 3.初始化數(shù)據(jù)庫(kù) 執(zhí)行:
	/usr/pgsql-10/bin/postgresql-10-setup initdb

# 4.啟動(dòng)數(shù)據(jù)庫(kù)并設(shè)置開(kāi)機(jī)啟動(dòng)
    sudo systemctl start postgresql-10
    sudo systemctl enable postgresql-10.service

# 5.安裝完畢,配置pg環(huán)境變量
	vi ~/.bash_profile
# 在PATH后添加
	/usr/pgsql-10/bin
# 比如:
	PATH=$PATH:$HOME/bin:/usr/pgsql-10/bin
# 添加完后執(zhí)行:使它立即生效
	source ~/.bash_profile



3.1主節(jié)點(diǎn)配置

(1)創(chuàng)建用于主從訪問(wèn)的用戶(hù), 修改postgres用戶(hù)的密碼,用于遠(yuǎn)程登錄。(切換到postgres用戶(hù)操作)

su - postgres
psql
create role actorcloud login replication encrypted password 'public';
alter role postgres with password 'postgres';

(2)修改pg_hba.conf和postgresql.conf配置:
vim /var/lib/pgsql/10/data/pg_hba.conf在最后一行添加下面內(nèi)容:

host    replication    actorcloud    192.168.1.31/32    trust
host    replication    actorcloud    192.168.1.36/32    trust
host    all    all    192.168.1.0/24    md5
host    all    all    0.0.0.0/0    md5

修改postgresql.conf配置:
vim /var/lib/pgsql/10/data/postgresql.conf

listen_addresses = '*' 
port = 5432 
wal_level = replica
max_wal_senders= 10
wal_keep_segments = 10240
max_connections = 512

(3)重啟主節(jié)點(diǎn)
建議先停止,再啟動(dòng),而不是重啟。之后再驗(yàn)證一下是否啟動(dòng)成功

systemctl stop postgresql-10
systemctl start postgresql-10

3.2從節(jié)點(diǎn)配置

從節(jié)點(diǎn)的操作建議全部在postgres用戶(hù)下進(jìn)行。

(1)切換postgres用戶(hù)

su - postgres

(2)對(duì)主節(jié)點(diǎn)的數(shù)據(jù)進(jìn)行備份到從節(jié)點(diǎn),其中192.168.1.31為主節(jié)點(diǎn)IP,actorcloud是上一節(jié)主節(jié)點(diǎn)創(chuàng)建的用戶(hù)。

rm -rf /var/lib/pgsql/10/data/*
pg_basebackup -h 192.168.1.31 -U actorcloud -D /var/lib/pgsql/10/data -X stream -P

(3)拷貝recovery.conf,編輯recovery.conf內(nèi)容,其中192.168.1.31對(duì)應(yīng)主機(jī)IP,actorcloud是上一節(jié)主機(jī)創(chuàng)建的用戶(hù)。

cp /usr/pgsql-10/share/recovery.conf.sample /var/lib/pgsql/10/data/recovery.conf

vim /var/lib/pgsql/10/data/recovery.conf修改配置:

standby_mode = on
primary_conninfo = 'host=192.168.1.31 port=5432 user=actorcloud password=public' 
recovery_target_timeline = 'latest'
trigger_file = '/tmp/trigger_file0'

(4)修改從節(jié)點(diǎn)的postgresql.conf,用于開(kāi)啟standby模式。

hot_standby = on

(5)退出postgres用戶(hù),重啟PostgreSQL

4.驗(yàn)證主從

4.1查看從節(jié)點(diǎn)信息

在主節(jié)點(diǎn)切換至psql界面,輸入命令:

select client_addr,sync_state from pg_stat_replication;

若從節(jié)點(diǎn)顯示出來(lái),如下圖所示,則說(shuō)明PostgreSQL集群搭建成功。

4.2讀寫(xiě)測(cè)試

(1)在主節(jié)點(diǎn)寫(xiě)數(shù)據(jù),從節(jié)點(diǎn)讀數(shù)據(jù)。

create database test;
l

在從節(jié)點(diǎn)上查看創(chuàng)建之后的數(shù)據(jù)庫(kù)??梢钥匆?jiàn),數(shù)據(jù)庫(kù)同步了。

l

5.postgres用戶(hù)間免密登錄

(0)關(guān)閉防火墻和SETLINUX

# 查看防火墻狀態(tài)
firewall-cmd --state
# 停止firewall
systemctl stop firewalld.service
# 禁止firewall開(kāi)機(jī)啟動(dòng)
systemctl disable firewalld.service 

# 修改/etc/selinux/config 文件
# 將
SELINUX=enforcing
# 改為
SELINUX=disabled

# 重啟
reboot

以下操作兩臺(tái)機(jī)器同時(shí)進(jìn)行:

(1)給postgres用戶(hù)更改密碼:密碼修改為postgres

passwd postgres

(2)生成并同步密鑰

以192.168.1.31為例,想要免密登錄另外一臺(tái)機(jī)器。先切換到postgres用戶(hù)執(zhí)行,ssh-keygen時(shí)一路回車(chē)往下,提升輸入y時(shí)輸入y,ssh-copy-id時(shí)需要輸入密碼。

su postgres
ssh-keygen
ssh-copy-id 192.168.1.36

測(cè)試免密登錄:

ssh 192.168.1.36
ip addr

同理在192.168.1.36機(jī)器上也是相同操作

6.安裝pgpool

(1)安裝(兩臺(tái)機(jī)器都需要安裝)

# 添加源
yum install http://www.pgpool.net/yum/rpms/4.0/redhat/rhel-7-x86_64/pgpool-II-release-4.0-1.noarch.rpm
 
# 安裝
 yum install pgpool-II-pg10-debuginfo pgpool-II-pg10-devel  pgpool-II-pg10-extensions  pgpool-II-pg10

安裝完之后pgpool的配置文件在/etc/pgpool-II/下

(2)修改配置(兩臺(tái)機(jī)器都需要配置)

pool_hba.conf和之前配置的PostgreSQL中的配置時(shí)一樣的

vim /etc/pgpool-II/pool_hba.conf
host    replication    actorcloud    192.168.1.31/32    trust
host    replication    actorcloud    192.168.1.36/32    trust
host    all    all    192.168.1.0/24    md5
host    all    all    0.0.0.0/0    md5

(3)對(duì)postgres的密碼進(jìn)行加密。本文將postgres的密碼設(shè)置為和用戶(hù)名相同,將加密結(jié)果復(fù)制,并粘貼到pcp.conf中相應(yīng)的位置,取消掉該行的注釋。

pg_md5 postgres

vim /etc/pgpool-II/pcp.conf
postgres:81dc9bdb52d04dc20036dbd8313ed055

(4)執(zhí)行命令(先切換到postgres用戶(hù)再執(zhí)行然后輸入密碼):

pg_md5 -m -p -u postgres pool_passwd

7.修改集群配置

以192.168.1.31主節(jié)點(diǎn)為例

vim /etc/pgpool-II/pgpool.conf

(1)修改監(jiān)聽(tīng)地址,將localhost改為*,即監(jiān)聽(tīng)所有地址發(fā)來(lái)的請(qǐng)求。

修改前:listen_addresses = 'localhost'
修改后:listen_addresses = '*'

(2)修改backend相關(guān)參數(shù),對(duì)應(yīng)的是PostgreSQ兩個(gè)節(jié)點(diǎn)的相關(guān)信息。

修改前:
	backend_hostname0 = 'localhost'                           
	backend_port0 = 5432
	backend_weight0 = 1
	backend_data_directory0 = '/var/lib/pgsql/data'
	backend_flag0 = 'ALLOW_TO_FAILOVER'
修改后:
	backend_hostname0 = '192.168.1.31'
	backend_port0 = 5432
	backend_weight0 = 1
	backend_data_directory0 = '/var/lib/pgsql/10/data'
	backend_flag0 = 'ALLOW_TO_FAILOVER'
 	
    backend_hostname1 = '192.168.1.36'
    backend_port1 = 5432
    backend_weight1 = 1
    backend_data_directory1 = '/var/lib/pgsql/10/data'
    backend_flag1 = 'ALLOW_TO_FAILOVER'

(3)pg_hba.conf生效

修改前:enable_pool_hba = off
修改后:enable_pool_hba = on

(4)使負(fù)載均衡生效

修改前:load_balance_mode = off
修改后:load_balance_mode = on

(5)主從流復(fù)制生效,并配置用于檢查的用戶(hù),這個(gè)用戶(hù)就用上方創(chuàng)建的用于主從訪問(wèn)的用戶(hù)。

修改前:
	master_slave_mode = off
	sr_check_period = 0
	sr_check_user = 'nobody'
	sr_check_password = ''
	sr_check_database = 'postgres'
	delay_threshold = 0
修改后:
	master_slave_mode = on
	sr_check_period = 6
	sr_check_user = 'actorcloud'
	sr_check_password = 'public'
	sr_check_database = 'postgres'
	delay_threshold = 10000000

(6)健康檢查相關(guān)配置,并配置用于檢查的用戶(hù),這個(gè)用戶(hù)就用上方創(chuàng)建的用于主從訪問(wèn)的用戶(hù)。

修改前:
	health_check_period = 0
	health_check_user = 'nobody'
	health_check_password = ''
	health_check_database = ''
修改后:
	health_check_period = 10
	health_check_user = 'actorcloud'
	health_check_password = 'public'
	health_check_database = 'postgres'

(7)配置主機(jī)故障觸發(fā)執(zhí)行的腳本。

修改前:failover_command = ''
修改后:failover_command = '/var/lib/pgsql/10/failover_stream.sh %d %H'

(8)開(kāi)啟開(kāi)門(mén)狗,IP為本機(jī)IP

修改前:
	use_watchdog = off
	wd_hostname = ''
修改后:
	use_watchdog = on
	wd_hostname = '192.168.1.31'

(9)開(kāi)啟虛擬IP,并修改網(wǎng)卡信息。啟動(dòng)后直接使用虛擬IP進(jìn)行數(shù)據(jù)庫(kù)操作劇哦

修改前:
	delegate_IP = ''
	if_up_cmd = 'ip addr add $_IP_$/24 dev eth0 label eth0:0'
	if_down_cmd = 'ip addr del $_IP_$/24 dev eth0'
	arping_cmd = 'arping -U $_IP_$ -w 1 -I eth0'
修改后:
	delegate_IP = '192.168.1.100'
	if_up_cmd = 'ip addr add $_IP_$/24 dev ens192 label ens192:0'
	if_down_cmd = 'ip addr del $_IP_$/24 dev ens192'
	arping_cmd = 'arping -U $_IP_$ -w 1 -I ens192'

上方把所有eth0改為了ens192,ens192是該主機(jī)的網(wǎng)卡設(shè)備名稱(chēng),可通過(guò)命令ip addr查看

(10)心跳檢查的配置與看門(mén)狗配置。IP為從節(jié)點(diǎn)的IP

修改前:
	heartbeat_destination0 = 'host0_ip1'
	heartbeat_device0 = ''
	#other_pgpool_hostname0 = 'host0'
	#other_pgpool_port0 = 5432
	#other_wd_port0 = 9000
修改后:
	heartbeat_destination0 = '192.168.1.36'
	heartbeat_device0 = 'ens192'
	other_pgpool_hostname0 = '192.168.1.36'
	other_pgpool_port0 = 9999
	other_wd_port0 = 9000

以上,是主節(jié)點(diǎn)的配置。對(duì)于從節(jié)點(diǎn),只有(8)和(10)的IP需要更改,其他的一致。

8.編寫(xiě)故障切換腳本

vim /var/lib/pgsql/10/failover_stream.sh
內(nèi)容為:

#! /bin/sh
# Failover command for streaming replication.
# Arguments: $1: new master hostname.
failed_node=$1
new_master=$2
trigger_file=$3
# Do nothing if standby goes down.
if [ $failed_node = 1 ]; then
exit 0;
fi
# Create the trigger file.
# use commond 
/usr/bin/ssh -T $new_master /usr/pgsql-10/bin/pg_ctl promote -D /var/lib/pgsql/10/data/
# use file
# /usr/bin/ssh -T $new_master  /bin/touch /tmp/trigger_file0
exit 0

9.文件權(quán)限更改

chmod u+s /sbin/ifconfig &&chmod u+s /usr/sbin

chown postgres:postgres /var/lib/pgsql/10/failover_stream.sh && chmod 777 /var/lib/pgsql/10/failover_stream.sh

chown -R postgres.postgres /etc/pgpool-II

mkdir /var/log/pgpool

chown -R postgres.postgres /var/log/pgpool

mkdir /var/run/pgpool

chown -R postgres.postgres /var/run/pgpool

10.運(yùn)行pgpool

# 啟動(dòng)
systemctl start pgpool
# 開(kāi)機(jī)自啟
systemctl enable pgpool

登錄虛擬ip查看集群節(jié)點(diǎn)。以后所有的操作均可通過(guò)連接虛擬IP操作數(shù)據(jù)庫(kù)。

psql -p 9999 -h 192.168.1.100 -U postgres
show pool_nodes;

至此,基于 Pgpool-II 中間件的 PostgreSQL 集群搭建完成。

本文摘自 :https://www.cnblogs.com/

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >