进阶课 11 激活Standby
PolarDB 进阶课程11 激活Standby
穷鬼玩PolarDB RAC一写多读集群系列已经写了几篇:
《在Docker容器中用loop设备模拟共享存储》 《如何搭建PolarDB容灾(Standby)节点》 《共享存储在线扩容》 《计算节点 Switchover》 《在线备份》 《在线归档》 《实时归档》 《时间点恢复(PITR)》 《读写分离》 《主机全毁, 只剩共享存储的PolarDB还有救吗?》
本篇文章介绍一下如何激活容灾(Standby)节点? 实验环境依赖 《在Docker容器中用loop设备模拟共享存储》 , 如果没有环境, 请自行参考以上文章搭建环境.
本文还依赖如下文档:
《如何搭建PolarDB容灾(Standby)节点》
DEMO
b站视频链接
Youtube视频链接
假设你的PolarDB Standby已按前面的文档在容器pb3中部署完成, Standby 节点正常运行中.
1、pb3, 查看一下Standby节点本地数据目录
$ cd ~/standby/
$ ll
total 344
drwx------ 22 postgres postgres 4096 Dec 24 14:07 ./
drwxr-x--- 1 postgres postgres 4096 Dec 24 11:06 ../
-rw------- 1 postgres postgres 227 Dec 24 09:52 backup_label.old
-rw------- 1 postgres postgres 183650 Dec 24 09:52 backup_manifest
drwx------ 6 postgres postgres 4096 Dec 24 09:52 base/
-rw------- 1 postgres postgres 163 Dec 24 14:07 current_logfiles
drwx------ 2 postgres postgres 4096 Dec 24 14:07 global/
drwx------ 2 postgres postgres 4096 Dec 24 14:07 pg_commit_ts/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_dynshmem/
-rw------- 1 postgres postgres 4870 Dec 24 09:52 pg_hba.conf
-rw------- 1 postgres postgres 1636 Dec 24 09:52 pg_ident.conf
drwx------ 2 postgres postgres 4096 Dec 24 14:07 pg_log/
drwx------ 4 postgres postgres 4096 Dec 24 14:22 pg_logical/
drwx------ 2 postgres postgres 4096 Dec 24 14:07 pg_logindex/
drwx------ 4 postgres postgres 4096 Dec 24 14:07 pg_multixact/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_notify/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_replslot/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_serial/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_snapshots/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_stat/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_stat_tmp/
drwx------ 2 postgres postgres 4096 Dec 24 09:58 pg_subtrans/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 pg_tblspc/
-rw------- 1 postgres postgres 3 Dec 24 09:52 PG_VERSION
drwx------ 2 postgres postgres 4096 Dec 24 14:07 pg_xact/
drwx------ 2 postgres postgres 4096 Dec 24 14:12 polar_cache_trash/
drwx------ 2 postgres postgres 4096 Dec 24 14:07 polar_fullpage/
drwx------ 2 postgres postgres 4096 Dec 24 09:52 polar_rel_size_cache/
-rw------- 1 postgres postgres 100 Dec 24 14:07 polar_settings.conf
-rw------- 1 postgres postgres 9 Dec 24 14:07 polar_shmem_stat_file
-rw------- 1 postgres postgres 505 Dec 24 09:53 postgresql.auto.conf
-rw------- 1 postgres postgres 31063 Dec 24 09:53 postgresql.conf
-rw------- 1 postgres postgres 81 Dec 24 14:07 postmaster.opts
-rw------- 1 postgres postgres 84 Dec 24 14:07 postmaster.pid
-rw------- 1 postgres postgres 0 Dec 24 09:52 standby.signal
共享盘nvme2n1中的数据目录
$ sudo pfs -C disk ls /nvme2n1/shared_data/
Dir 1 256 Tue Dec 24 09:52:52 2024 pg_multixact
Dir 1 256 Tue Dec 24 09:52:52 2024 pg_wal
Dir 1 0 Tue Dec 24 09:52:54 2024 pg_commit_ts
File 1 0 Tue Dec 24 09:52:54 2024 standby.signal
Dir 1 512 Tue Dec 24 09:52:57 2024 polar_fullpage
Dir 1 0 Tue Dec 24 09:52:57 2024 pg_replslot
Dir 1 0 Tue Dec 24 09:52:57 2024 pg_tblspc
File 1 227 Tue Dec 24 09:52:57 2024 polar_non_exclusive_backup_label
File 1 257 Tue Dec 24 09:52:57 2024 postgresql.auto.conf
Dir 1 0 Tue Dec 24 09:52:57 2024 pg_twophase
Dir 1 128 Tue Dec 24 09:52:57 2024 pg_xact
Dir 1 128 Tue Dec 24 09:53:37 2024 pg_logindex
Dir 1 7808 Tue Dec 24 09:52:57 2024 global
Dir 1 512 Tue Dec 24 09:52:58 2024 base
total 16384 (unit: 512Bytes)
2、pb3, 激活容灾(Standby)节点
PS: 激活之前, 可以先观察standby和主集群或实时流式归档(如有)的WAL差异, 有差异的话可以通过文件拷贝到standby节点, 通过restore_command配置进行恢复. 尽量减少standby与上游的数据差异.
$ pg_ctl promote -D ~/standby
日志如下, 有点多.
[PFSD_SDK INF Dec 24 14:27:57.278019][94]pfsd_umount_force 247: pbdname nvme2n1
[PFSD_SDK INF Dec 24 14:27:57.279200][94]chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/94.pid
[PFSD_SDK INF Dec 24 14:27:57.279212][94]pfsd_umount_force 261: umount success for nvme2n1
waiting for server to promote...[PFSD_SDK INF Dec 24 14:27:57.380783][94]pfsd_umount_force 247: pbdname nvme2n1
[PFSD_SDK INF Dec 24 14:27:57.382082][94]chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/94.pid
[PFSD_SDK INF Dec 24 14:27:57.382091][94]pfsd_umount_force 261: umount success for nvme2n1
.[PFSD_SDK INF Dec 24 14:27:57.583592][94]pfsd_umount_force 247: pbdname nvme2n1
...
[PFSD_SDK INF Dec 24 14:28:52.847152][94]chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/94.pid
[PFSD_SDK INF Dec 24 14:28:52.847161][94]pfsd_umount_force 261: umount success for nvme2n1
done
server promoted
激活后是一个正常实例, 可以接收读写请求了
$ psql
psql (PostgreSQL 15.10 (PolarDB 15.10.2.0 build d4f5477d debug) on aarch64-linux-gnu)
Type "help"forhelp.
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
postgres=# create table t_new (id int);
CREATE TABLE
postgres=# insert into t_new values (1),(2),(3);
INSERT 0 3
postgres=# select * from t_new;
id
----
1
2
3
(3 rows)
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | t | table | postgres
public | t1 | table | postgres
public | t2 | table | postgres
public | t_new | table | postgres
public | tbl | table | postgres
(5 rows)
注意: PolarDB standby激活之后, 不能再成为原来PolarDB集群的Standby. 如果是PostgreSQL社区版本的话激活之后通过pg_rewind可以回退到过去, PolarDB standby如果部署在ZFS上, 可以通过zfs快照克隆来进行回退, 省去重建Standby的动作.
观察本地数据文件目录和共享盘nvme2n1中的数据目录, 对比激活前后只有一处不同. 本地数据文件目录的standby.signal文件不存在了, 而共享盘中的还在, 说明signal文件起作用的是本地数据目录. 共享盘中的standby.signal可以删掉.
$ sudo pfs -C disk rm /nvme2n1/shared_data/standby.signal
因为这种标记文件决定的是计算节点的角色, 所以只和计算节点相关, 和共享存储无关. 不会放在共享存储里, 想想看如果放在共享存储里, 那有多个计算节点时, 到底谁时RO呢?
激活之后, 如果上游节点还活着, 建议到PolarDB primary节点删除对应standby的slot, 以免导致wal文件堆积.
postgres=# select pg_drop_replication_slot('standby1');
postgres=# select * from pg_replication_slots ;
slot_name | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn | wal_status | safe_wal_size | two_phase
------------------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------+------------+---------------+-----------
replica1 | | physical | | | f | t | 208 | | | 1/C7AFA000 | | reserved | | f
online_archwal_1 | | physical | | | f | t | 226 | | | 1/C7AFA100 | | reserved | | f
(2 rows)
参考
《穷鬼玩PolarDB RAC一写多读集群系列 | 在Docker容器中用loop设备模拟共享存储》
《穷鬼玩PolarDB RAC一写多读集群系列 | 如何搭建PolarDB容灾(Standby)节点》
《穷鬼玩PolarDB RAC一写多读集群系列 | 共享存储在线扩容》
《穷鬼玩PolarDB RAC一写多读集群系列 | 计算节点 Switchover》
《穷鬼玩PolarDB RAC一写多读集群系列 | 在线备份》
《穷鬼玩PolarDB RAC一写多读集群系列 | 在线归档》
《穷鬼玩PolarDB RAC一写多读集群系列 | 实时归档》
《穷鬼玩PolarDB RAC一写多读集群系列 | 时间点恢复(PITR)》
《穷鬼玩PolarDB RAC一写多读集群系列 | 读写分离》
《穷鬼玩PolarDB RAC一写多读集群系列 | 主机全毁, 只剩共享存储的PolarDB还有救吗?》