PostgreSQL+pgpool-II復制方案


title: PostgreSQL+pgpool-II復制方案
tags: PostgreSQL,pgpool-II
author: Chinge Yang
date: 2017-02-15

PostgreSQL+pgpool-II復制方案

@(學習)[PostgreSQL, pgpool-II, 高可用]

1. Pgpool-II介紹

pgpool-II是PostgreSQL服務器之間一種有效的中間件和PostgreSQL數據庫客戶端。它提供了以下功能。

連接池
pgpool-II保存到PostgreSQL服務器的連接,當一個相同新連接(如用戶名、數據庫、協議版本)進來時,重用他們。它減少了連接開銷,提高了系統的整體吞吐量。
復制
pgpool-II可以管理多個PostgreSQL服務器。使用復制功能可以使2個或更多的物理磁盤上創建一個實時備份,這樣服務不會因服務器的磁盤故障而中斷。
負載平衡
如果數據庫是復制的,在任何服務器上執行一個SELECT查詢會返回相同的結果。pgpool-II復制特性的優勢在於減少每個PostgreSQL服務器上的負載,因為它可以使用分布在多個服務器之間進行SELECT查詢,從而提高系統的整體吞吐量。最好是查詢和PostgreSQL服務器數量成一定比例,多用戶同時執行多查詢達到負載均衡最好的效果。
限制連接數
PostgreSQL的最大並發連接數有一定限制的,當超過限制的連接數后,連接會被拒絕。然而,設置增加最大連接數又會增加資源消耗,影響系統性能。pgpool-II也有最大連接數限制,但超過的連接進來時是進行立即排隊,而不是返回一個錯誤。

pgpool-II交互PostgreSQL的后端和前端協議時,起着繼電器的作用。因此,數據庫應用程序(前端)認為pgpool-II是真實的PostgreSQL服務器,服務器(后端)認為pgpool-II是它的客戶端之一。因為pgpool-II在服務器和客戶端是透明的,所以pgpool-II可以使用現有的數據庫應用程序而做到幾乎不修改它們。

環境:
CentOS6.8
PostgreSQL9.5
pgpool-II-pg95-3.6.1

2. pgpool-II安裝

2.1 安裝pgpool-II yum源,並安裝pgpool-II

從官網找到相關yum源rpm包,使用rpm安裝后,並使用yum安裝pgpool-II

rpm -ivh http://www.pgpool.net/yum/rpms/3.6/redhat/rhel-6-x86_64/pgpool-II-release-3.6-1.noarch.rpm
yum -y install pgpool-II-pg95
yum -y install pgpool-II-pg95-debuginfo
yum -y install pgpool-II-pg95-devel
yum -y install pgpool-II-pg95-extensions
chkconfig pgpool on    # 添加開機啟動
service start/stop pgpool   # 服務啟/停

2.2 添加Pgpool-II運行用戶

添加Pgpool-II運行用戶,並給相關目錄的讀寫權限

[root@im110 pgpool-II]# useradd pgpool                                                      
[root@im110 pgpool-II]# passwd pgpool
Changing password for user pgpool.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@im110 pgpool-II]# chown -R pgpool.pgpool /etc/pgpool-II
[root@im110 pgpool-II]# mkdir -p /var/run/pgpool/
[root@im110 pgpool-II]# chown pgpool.pgpool /var/run/pgpool/

修改啟動腳本的pgpool運行用戶:
圖1

2.3 設置pcp.conf

cp /etc/pcp.conf.sample /etc/pcp.conf

內容格式為如下,一行一個,#號為注釋標識

username:[md5 encrypted password]

[md5 encrypted password] 可以使用如下命令生成

$ pg_md5 pgpool
ba777e4c2f15c11ea8ac3be7e0440aa0

使用pg_md5 -p會隱藏輸入的密碼

$ pg_md5 -p
password: your_password

配置文件pcp.conf必須允許pgpool執行用戶可讀。

2.4 設置Pgpool-II配置文件

pgpool.conf是Pgpool-II的主配置文件。啟動Pgpool-II時可以使用 -f 參數指定 pgpool.conf路徑, 默認是使用/etc/pgpool.conf.

Pgpool-II每個模式對應的配置文件模板

圖2

復制一份作為你的配置文件:

# cd /etc
# cp pgpool.conf.sample-replication pgpool.conf

2.5 配置backend信息

在pgpool.conf中加入如下格式的配置,其中0為backend主機號,不能重復。

backend_socket_dir = '/tmp'
backend_hostname0 = '10.1.0.110'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/var/lib/pgsql/9.5/data/'
backend_flag0 = 'ALLOW_TO_FAILOVER'

2.6 修改認證方式

2.6.1. 修改pgpool-II的認證方式為md5

vim /etc/pgpool-II/pool_hba.conf

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
host    all             all             10.1.0.0/24             md5
# "local" is for Unix domain socket connections only
local   all             all                                    md5
# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
host    all         all         ::1/128               md5

2.6.2. 修改PostgreSQL的認證方式為md5

vim pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             10.1.0.0/24             md5
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   all     postgres                                md5
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident
[root@im110 pgpool-II]# 

2.7 測試pgpool-II同步

這里作簡單測試,在pgpool-II入口創建數據庫,看各節點是否自動創建,刪除后,看各節點是否自動刪除。

[root@im109 ~]# psql -U postgres -h 10.1.0.115 -p 9999
Password for user postgres: 
psql (9.5.6)
Type "help" for help.

postgres=# show pool_nodes;
 node_id |  hostname  | port  | status | lb_weight |  role  | select_cnt | load_balance_node | replication_delay 
---------+------------+-------+--------+-----------+--------+------------+-------------------+-------------------
 0       | 10.1.0.110 | 54321 | up     | 0.500000  | master | 0          | true              | 0
 1       | 10.1.0.109 | 54321 | up     | 0.500000  | slave  | 0          | false             | 0
(2 rows)

postgres=# 
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
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

postgres=# create database test01;
CREATE DATABASE
postgres=# \q
[root@im109 ~]# psql -U postgres -h 10.1.0.110 -p 54321
Password for user postgres: 
psql (9.5.6)
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
 test01    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

postgres=# \q
[root@im109 ~]# psql -U postgres -h 10.1.0.109 -p 54321  
Password for user postgres: 
psql (9.5.6)
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
 test01    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

postgres=# \q
[root@im109 ~]# psql -U postgres -h 10.1.0.115 -p 9999 
Password for user postgres: 
psql (9.5.6)
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
 test01    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

postgres=# drop database test01;
DROP 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
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

postgres=# \q
[root@im109 ~]# psql -U postgres -h 10.1.0.110 -p 54321
Password for user postgres: 
psql (9.5.6)
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
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

postgres=# \q
[root@im109 ~]# psql -U postgres -h 10.1.0.109 -p 54321
Password for user postgres: 
psql (9.5.6)
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
 wiseucmsg | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

postgres=# \q
[root@im109 ~]# 

測試成功,說明從pgpool-II入口操作,各節點會同步數據。

3. 安裝pgpoolAdmin(不推薦)

雖然官網提供此工具,用於頁面管理pgpool,但是我覺得不安全,且可能bug較多。以下文中會有一些bug列出來。
安裝方法:

3.1 解壓pgpoolAdmin至web目錄,使用戶能訪問其php

wget http://www.pgpool.net/download.php?f=pgpoolAdmin-3.5.3.tar.gz -O pgpoolAdmin-3.5.3.tar.gz
cd /var/www
tar -zxvf pgpoolAdmin-3.5.3.tar.gz
ln -s pgpoolAdmin-3.5.3 pgpooladmin

mkdir templates_c
chmod 777 templates_c
chown www conf/pgmgt.conf.php
chmod 644 conf/pgmgt.conf.php

3.2 php-fpm運行用戶和pgpool用戶統一

pgpoolAdmin使用pcp命令控制pgpool啟停,因此需要統一php和pgpool運行用戶,以便pgpoolAdmin從頁面控制pgpool啟停。可參考上文中2.2步驟操作。

3.3 根據向導完成安裝

訪問安裝頁面,完成檢測,完成安裝。
http://yourweb/pgpooladmin/install/
圖3

  1. 從上圖看到,3.5下面的勾處未有文字提示,我此處為通過。因為我裝了php-psql擴展。從源碼中也可以看出,是檢測pgsql擴展。
    圖4

  2. .pcppass文件格式如下(pgpool3.5以下用到):
    它的作用是用於指定連接postgresql的連接串,包括主機、端口、用戶、密碼。

host:port:user:password

3.4 一些細節說明

完全設置好的完整功能如下:

圖5

1). 顯示pgpoolAdmin運行機器的hostname
若php報warning,可在hosts中添加IP對應主機名

[root@im110 pgpoolAdmin-3.5.3]# vim /etc/hosts
10.1.0.110 im110

2). 此處為pgpoolAdmin的登錄用戶,superuser: yes表示在pgpool數據庫中,用戶為管理員用戶。此數據庫可以pgpool下,也可分離到其它postgresql中。若此處非yes,則節點相關操作為不可操作的灰色。

圖6

3). 頁面中顯示節點是否連接,是在pgpool中show pool_nodes命令下的節點status.

[root@im109 ~]# psql -U postgres -h 10.1.0.115 -p 9999
Password for user postgres: 
psql (9.5.6)
Type "help" for help.

postgres=# show pool_nodes;
 node_id |  hostname  | port  | status | lb_weight |  role  | select_cnt | load_balance_node | replication_delay 
---------+------------+-------+--------+-----------+--------+------------+-------------------+-------------------
 0       | 10.1.0.110 | 54321 | up     | 0.500000  | master | 0          | true              | 0
 1       | 10.1.0.109 | 54321 | up     | 0.500000  | slave  | 0          | false             | 0
(2 rows)

其它命令參考官方文檔:
http://www.pgpool.net/docs/latest/en/html/reference.html

4). pgpool.conf 設置中的健康檢查,檢查postgressql是否啟動
此處是通過連接各節點是否成功來判斷的,因此需要在各節點創建用於連接的角色。
圖7

[root@im110 pgpool-II]# psql -U pgpool -p 54321 -h 10.1.0.110 template1
Password for user pgpool: 
psql (9.5.6)
Type "help" for help.

template1=> 

3.5 php.ini的disable_functions設置

源代碼中php調用的exec執行pcp相關命令,
圖8
因此需要將php.ini配置文件中的disable_functions中的exec去掉,以允許php使用該函數。
圖9

3.6 pgpoolAdmin中pcp_stop_pgpool參數

在pgpool-II3.6中,pcp_stop_pgpool用法如下:

root@im110 pgpooladmin]# pcp_stop_pgpool --help
pcp_stop_pgpool - terminate pgpool-II
Usage:
pcp_stop_pgpool [OPTION...] 
Options:
  -U, --username=NAME    username for PCP authentication
  -h, --host=HOSTNAME    pgpool-II host
  -p, --port=PORT        PCP port number
  -w, --no-password      never prompt for password
  -W, --password         force password prompt (should happen automatically)
  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"
  -d, --debug            enable debug message (optional)
  -v, --verbose          output verbose messages
  -?, --help             print this help

pgpoolAdmin中需要做相應修改。
圖10

4. 總結

  1. 此PostgreSQL高可用方案搭建簡單方便,且可以在不影響原來環境的基礎上搭建;
  2. 生產環境中的pgpoool-II是怎樣的我不好評判。網上此博客寫的不錯,可以看看,PG的兩種集群技術:Pgpool-II與Postgres-XL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM