pgpool-II導入してみた・その3(負荷分散・レプリケーション)

※DBサーバ(primary:node1,standby:node2)のprimaryが独立、standbyのDBサーバとwebサーバ(pgpool)が同梱であるサンプル
①pgpoolとpostgresのインストールは同じであるため、割愛する
②webサーバ(pgpool)を設定する
[root@node2 pgpool-II]# egrep -i 'backend_|connection_cache|num_init_children|max_pool|enable_pool_hba|memory_cache_enabled|listen_addresses|client_min_messages|log_min_messages|sr_check_' pgpool.conf ★
listen_addresses = '*' ★触った
...
backend_hostname0 = '192.168.56.101' ★
backend_port0 = 5432 ★
backend_weight0 = 1 ★
backend_data_directory0 = '/var/lib/pgsql/11/data' ★
backend_flag0 = 'ALLOW_TO_FAILOVER' ★
backend_hostname1 = '192.168.56.102' ★
backend_port1 = 5432 ★
backend_weight1 = 1 ★
backend_data_directory1 = '/var/lib/pgsql/11/data' ★
backend_flag1 = 'ALLOW_TO_FAILOVER' ★
enable_pool_hba = on ★
num_init_children = 30 ★
max_pool = 2 ★
client_min_messages = log # values in order of decreasing detail: ★
log_min_messages = info # values in order of decreasing detail: ★
connection_cache = on ★
sr_check_period = 10 ★
sr_check_user = 'postgres' ★
...
sr_check_password = 'postgres' ★
sr_check_database = 'postgres' ★
[root@node2 pgpool-II]# cat pool_hba.conf
# pgpool Client Authentication Configuration File
# ===============================================
#
# The format rule in this file follows the rules in the PostgreSQL
# Administrator's Guide. Refer to chapter "Client Authentication" for a
...
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host postgres postgres 192.168.56.0/24 md5 ★追加したエントリはこれ
[root@node2 pgpool-II]# cat pool_passwd ★パスワードファイルも作成しておく


postgres:md53175bce1d3201d16594cebf9d7eb3f9d
[root@node2 pgpool-II]# pgpool -n -d > /var/log/pgpool/pgpool.log 2>&1 & ★pgpoolを起動させる
[1] 11414
[root@node2 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres'
root 3372 3240 0 14:49 pts/1 00:00:00 su - postgres
postgres 3373 3372 0 14:49 pts/1 00:00:00 -bash
postgres 6746 1 0 15:30 pts/1 00:00:00 /usr/pgsql-11/bin/postgres
postgres 6747 6746 0 15:30 ? 00:00:00 postgres: pg11: logger
postgres 6748 6746 0 15:30 ? 00:00:00 postgres: pg11: startup recovering 000000010000000000000018
postgres 6749 6746 0 15:30 ? 00:00:00 postgres: pg11: checkpointer
postgres 6750 6746 0 15:30 ? 00:00:00 postgres: pg11: background writer
postgres 6751 6746 0 15:30 ? 00:00:00 postgres: pg11: stats collector
postgres 6752 6746 0 15:30 ? 00:00:05 postgres: pg11: walreceiver streaming 0/18C81B68
root 11414 3178 0 16:26 pts/0 00:00:00 pgpool -n -d
root 11415 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
...
root 11444 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
root 11446 11414 0 16:26 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 11447 11414 0 16:26 pts/0 00:00:00 pgpool: worker process
root 11448 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(0)
root 11449 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(1)
root 11459 3178 0 16:26 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
③クライアント側からpgpool経由でDBサーバへ接続する

[root@node2 ~]# psql -d postgres -h 192.168.56.102 -p 9999 -U postgres ★rootユーザでも接続できるぞ!
ユーザ postgres のパスワード: ★ここでパスワードを入力する必要がありますが
psql (11.3)
"help" でヘルプを表示します。

postgres=#
postgres=# show pool_nodes; ★
node_id | hostname | port | status | lb_weight | role | select_cnt | load_balance_node | replication_delay | last_status_change
---------+----------------+------+--------+-----------+---------+------------+-------------------+-------------------+---------------------
0 | 192.168.56.101 | 5432 | up | 0.500000 | primary | 0 | true | 0 | 2019-06-12 15:41:23
1 | 192.168.56.102 | 5432 | up | 0.500000 | standby | 0 | false | 0 | 2019-06-12 15:41:23
(2 行)


[root@node2 ~]# psql -d postgres -h 192.168.56.102 -p 9999 -U postgres ★二番目の接続も立ち上げる
ユーザ postgres のパスワード:
psql (11.3)
"help" でヘルプを表示します。

postgres=#


④DBサーバで接続バックエンドプロセスを確認する
※プライマリ側
-bash-4.2$ date;ps -ef | grep postgres;date
2019年 6月 12日 水曜日 16:41:08 JST
root 3268 3213 0 14:49 pts/0 00:00:00 su - postgres
postgres 3269 3268 0 14:49 pts/0 00:00:00 -bash
postgres 6276 1 0 15:30 pts/0 00:00:01 /usr/pgsql-11/bin/postgres
postgres 6278 6276 0 15:30 ? 00:00:00 postgres: pg11: logger
postgres 6280 6276 0 15:30 ? 00:00:00 postgres: pg11: checkpointer
postgres 6281 6276 0 15:30 ? 00:00:00 postgres: pg11: background writer
postgres 6282 6276 0 15:30 ? 00:00:00 postgres: pg11: walwriter
postgres 6283 6276 0 15:30 ? 00:00:00 postgres: pg11: autovacuum launcher
postgres 6284 6276 0 15:30 ? 00:00:00 postgres: pg11: archiver
postgres 6285 6276 0 15:30 ? 00:00:00 postgres: pg11: stats collector
postgres 6286 6276 0 15:30 ? 00:00:00 postgres: pg11: logical replication launcher
postgres 6290 6276 0 15:30 ? 00:00:00 postgres: pg11: walsender postgres 192.168.56.102(55469) streaming 0/18C81B68
postgres 10521 6276 0 16:27 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(57067) idle ★接続プロセス1
postgres 11420 6276 0 16:39 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(57375) idle ★接続プロセス2
postgres 11526 3269 0 16:41 pts/0 00:00:00 ps -ef
postgres 11527 3269 0 16:41 pts/0 00:00:00 grep --color=auto postgres
2019年 6月 12日 水曜日 16:41:08 JST
※スタンバイ側
[root@node2 pgpool-II]# ps -ef | egrep -i 'pgpool|postgres'
root 3372 3240 0 14:49 pts/1 00:00:00 su - postgres
postgres 3373 3372 0 14:49 pts/1 00:00:00 -bash
postgres 6746 1 0 15:30 pts/1 00:00:00 /usr/pgsql-11/bin/postgres
postgres 6747 6746 0 15:30 ? 00:00:00 postgres: pg11: logger
postgres 6748 6746 0 15:30 ? 00:00:00 postgres: pg11: startup recovering 000000010000000000000018
postgres 6749 6746 0 15:30 ? 00:00:00 postgres: pg11: checkpointer
postgres 6750 6746 0 15:30 ? 00:00:00 postgres: pg11: background writer
postgres 6751 6746 0 15:30 ? 00:00:00 postgres: pg11: stats collector
postgres 6752 6746 0 15:30 ? 00:00:07 postgres: pg11: walreceiver streaming 0/18C81B68
root 11414 3178 0 16:26 pts/0 00:00:00 pgpool -n -d
root 11415 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
...
root 11442 11414 0 16:26 pts/0 00:00:00 pgpool: wait for connection request
root 11443 11414 0 16:26 pts/0 00:00:00 pgpool: postgres postgres 192.168.56.102(51261) idle ★poolプロセス1
root 11444 11414 0 16:26 pts/0 00:00:00 pgpool: postgres postgres 192.168.56.102(50953) idle ★poolプロセス2
root 11446 11414 0 16:26 pts/0 00:00:00 pgpool: PCP: wait for connection request
root 11447 11414 0 16:26 pts/0 00:00:00 pgpool: worker process
root 11448 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(0)
root 11449 11414 0 16:26 pts/0 00:00:00 pgpool: health check process(1)
postgres 11464 3373 0 16:27 pts/1 00:00:00 psql -d postgres -h 192.168.56.102 -p 9999 -U postgres
postgres 11468 6746 0 16:27 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(58671) idle ★接続プロセス1
root 12346 7924 0 16:39 pts/2 00:00:00 psql -d postgres -h 192.168.56.102 -p 9999 -U postgres
postgres 12357 6746 0 16:39 ? 00:00:00 postgres: pg11: postgres postgres 192.168.56.102(58979) idle ★接続プロセス2
root 12573 3178 0 16:42 pts/0 00:00:00 grep -E --color=auto -i pgpool|postgres
※結論:pgpool経由でprimary-standby構成のDBへ接続する際に、デフォルトは、両方接続させることになる。
④もちろん、この場合でも、localhostの方法で利用が可能です。
[root@node2 pgpool-II]# grep 'listen_addresses' pgpool.conf ★
listen_addresses = 'localhost' ★「*⇒localhost」へ変更
pcp_listen_addresses = '*'
[root@node2 pgpool-II]# cat pool_hba.conf
# pgpool Client Authentication Configuration File
# ===============================================
#

host all all ::1/128 trust
#host postgres postgres 192.168.56.0/24 md5 ★コメントアウトしたエントリ
host postgres postgres ::1/128 md5 ★追加したエントリ
<省略>
-bash-4.2$ psql -d postgres -h localhost -p 9999 -U postgres ★「-h localhost」指定でも可能になった。
psql (11.3) ★localhostの場合、パスワードも聞かれなくなった。
"help" でヘルプを表示します。

postgres=#
<省略>
プロセス状況は一緒でであるため、割愛する