PostgreSQL码农集散地

进阶课 12 集群变单机

PolarDB 进阶课程12 集群变单机

穷鬼玩PolarDB RAC一写多读集群系列已经写了几篇:

  • 《在Docker容器中用loop设备模拟共享存储》
  • 《如何搭建PolarDB容灾(Standby)节点》
  • 《共享存储在线扩容》
  • 《计算节点 Switchover》
  • 《在线备份》
  • 《在线归档》
  • 《实时归档》
  • 《时间点恢复(PITR)》
  • 《读写分离》
  • 《主机全毁, 只剩共享存储的PolarDB还有救吗?》
  • 《激活容灾(Standby)节点》

本篇文章介绍一下如何把“共享存储实例”转换为“本地存储实例”? 实验环境依赖 《在Docker容器中用loop设备模拟共享存储》 , 如果没有环境, 请自行参考以上文章搭建环境.

本文还依赖如下文章:

  • 《如何搭建PolarDB容灾(Standby)节点》
  • 《激活容灾(Standby)节点》

DEMO

b站视频链接

Youtube视频链接

在pb3容器进行演示. 假设在pb3 容器中的standby节点已经被成功激活. 接下来将介绍把这个“共享存储实例”转换为“本地存储实例”.

预览当前目录结构:

# 数据放在PFS内  
$ sudo pfs -C disk ls /nvme2n1/shared_data/  
   Dir  1     256               Tue Dec 24 09:52:52 2024  pg_multixact  
   Dir  1     768               Tue Dec 24 14:29:01 2024  pg_wal  
   Dir  1     0                 Tue Dec 24 09:52:54 2024  pg_commit_ts  
   Dir  1     512               Tue Dec 24 14:46:02 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 14:45:55 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)  

# 本地实例放在本地目录中  
$ ls ~/standby  
backup_label.old  current_logfiles  pg_dynshmem    pg_log       pg_multixact  pg_serial     pg_stat_tmp  PG_VERSION         polar_fullpage        polar_shmem_stat_file  postmaster.opts  
backup_manifest   global            pg_hba.conf    pg_logical   pg_notify     pg_snapshots  pg_subtrans  pg_xact            polar_rel_size_cache  postgresql.auto.conf   postmaster.pid  
base              pg_commit_ts      pg_ident.conf  pg_logindex  pg_replslot   pg_stat       pg_tblspc    polar_cache_trash  polar_settings.conf   postgresql.conf  

设计目标目录结构

# 数据放在本地 /data/standby/shared_data 目录中  

# 本地实例还是放在本地 ~/standby 目录中  

1、准备存放PFS 共享盘数据库目录的本地目录

mkdir -p /data/standby/shared_data  

2、停库

pg_ctl stop -m fast -D ~/standby  

3、把PFS的内容拷贝到本地目录, 并修改owner和权限

pfs cp -r /nvme2n1/shared_data /data/standby/  

chmod 700 /data/standby/shared_data  
chown -R postgres:postgres /data/standby/shared_data  

预览新的本地共享数据目录结构

$ ll /data/standby/shared_data  
total 8  
drwx------ 15 postgres postgres  480 Dec 24 15:55 ./  
drwxr-xr-x  3 postgres postgres   96 Dec 24 15:48 ../  
drwxr-xr-x  6 postgres postgres  192 Dec 24 15:55 base/  
drwxr-xr-x 63 postgres postgres 2016 Dec 24 15:55 global/  
drwxr-xr-x  2 postgres postgres   64 Dec 24 15:55 pg_commit_ts/  
drwxr-xr-x  3 postgres postgres   96 Dec 24 15:55 pg_logindex/  
drwxr-xr-x  4 postgres postgres  128 Dec 24 15:55 pg_multixact/  
drwxr-xr-x  2 postgres postgres   64 Dec 24 15:55 pg_replslot/  
drwxr-xr-x  2 postgres postgres   64 Dec 24 15:55 pg_tblspc/  
drwxr-xr-x  2 postgres postgres   64 Dec 24 15:55 pg_twophase/  
drwxr-xr-x  8 postgres postgres  256 Dec 24 15:55 pg_wal/  
drwxr-xr-x  3 postgres postgres   96 Dec 24 15:55 pg_xact/  
drwxr-xr-x  6 postgres postgres  192 Dec 24 15:55 polar_fullpage/  
-rw-r--r--  1 postgres postgres  227 Dec 24 15:55 polar_non_exclusive_backup_label  
-rw-r--r--  1 postgres postgres  257 Dec 24 15:55 postgresql.auto.conf  

共享数据目录中这两个文件没什么用可以删掉

rm /data/standby/shared_data/polar_non_exclusive_backup_label  
rm -f /data/standby/shared_data/postgresql.auto.conf  

4、修改 ~/standby 内配置文件

postgresql.conf

echo"  
huge_pages=off  
port=5432  
polar_hostid=3  
polar_enable_shared_storage_mode=on  
polar_disk_name='home'  # 修改
# polar_storage_cluster_name='disk'   # 修改  
polar_datadir='file-dio:///data/standby/shared_data/'    # 修改  
polar_vfs.localfs_mode=true    # 修改   
shared_preload_libraries='\$libdir/polar_vfs,\$libdir/polar_worker'  
logging_collector=on  
log_line_prefix='%p\t%r\t%u\t%m\t'  
log_directory='pg_log'  
listen_addresses='0.0.0.0'  
max_connections=200  
# 下面几个参数解决replica不能promote的问题, 因为RO依赖logindex.  
polar_logindex_mem_size=64MB  
polar_xlog_queue_buffers=64MB  
polar_xlog_page_buffers=64MB  
# 使用pfs管理块设备时可以关掉 full page write 和 polar_has_partial_write  . 或者使用zfs这类cow的文件系统时也可以关掉以下2个配置.  
full_page_writes = off  
polar_has_partial_write = on  # 修改

polar_resource_manager.enable_resource_manager=off
"

 >> ~/standby/postgresql.conf    

5、启动实例

pg_ctl start -D ~/standby  

将“共享存储实例”转换为“本地存储实例”完成

$ psql  
psql (PostgreSQL 15.10 (PolarDB 15.10.2.0 build d4f5477d debug) on aarch64-linux-gnu)  
Type "help"forhelp.  

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)  

postgres=# show polar_datadir;  
             polar_datadir               
---------------------------------------  
 file-dio:///data/standby/shared_data/  
(1 row)  

postgres=# create table t3(id int);  
CREATE TABLE  
postgres=# insert into t3 select generate_series(1,10);  
INSERT 0 10  

验证完转换成功后, 可以把本地的pfsd进行退出.

PS: 如果你想比较平滑的完成转换, 可用考虑采用先建立本地盘的standby然后激活的方式. 参考 《如何搭建PolarDB容灾(Standby)节点》
这还不是完全的单机版, 完全的单机版参考: 《穷鬼玩PolarDB RAC一写多读集群系列 | 原来PolarDB还有单机版?》

参考

《穷鬼玩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还有救吗?》

《穷鬼玩PolarDB RAC一写多读集群系列 | 激活容灾(Standby)节点》