PostgreSQL码农集散地

进阶课 13 单机变集群

PolarDB 进阶课程13 单机变集群

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

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

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

本文还依赖如下文章:

  • 《如何搭建PolarDB容灾(Standby)节点》
  • 《激活容灾(Standby)节点》
  • 《将“共享存储实例”转换为“本地存储实例”》

DEMO

b站视频链接

Youtube视频链接

在pb3容器进行演示. 假设你已完成 《将“共享存储实例”转换为“本地存储实例”》 的操作. 现在pb3上的实例为本地盘实例, 共享数据放在本地盘中.

预览当前目录结构:

# 共享数据放在本地盘中    
postgres@a8d5e5dc6b96:~/standby$ cd /data/standby/shared_data/  
postgres@a8d5e5dc6b96:/data/standby/shared_data$ ll  
total 0  
drwx------ 13 postgres postgres  416 Dec 24 15:58 ./  
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/  

# 本地实例放在本地目录中    
$ 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    

设计目标目录结构

# 共享数据放在pfs /nvme2n1/shared_data 目录中    

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

1、准备工作

  • 在pb3容器内准备PFS二进制. 已完成
  • 在宿主机挂载目录中dd共享盘 /data/VirtualDisk_standby.img . 已完成
  • 使用loop将该文件模拟为块设备 . 已完成
  • 软链到 /dev/nvme2n1 . 已完成

以上步骤不知道如何操作的话可以参考 《如何搭建PolarDB容灾(Standby)节点》

2、把/dev/nvme2n1格式化为pfs文件系统

sudo pfs -C disk mkfs -f nvme2n1      

3、启动pfsd

sudo /usr/local/polarstore/pfsd/bin/start_pfsd.sh -p nvme2n1 -w 1      

现在这个共享盘里什么都没了:

$ pfs ls /nvme2n1/  
  File  1     4194304           Tue Dec 24 16:27:16 2024  .pfs-paxos  
  File  1     1073741824        Tue Dec 24 16:27:18 2024  .pfs-journal  
total 2105344 (unit: 512Bytes)  

4、停库

pg_ctl stop -m fast -D ~/standby    

5、把本地共享数据目录的内容拷贝到pfs内

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

拷贝成功后, 查看一下pfs内的文件位置

postgres@a8d5e5dc6b96:/data$ pfs ls /nvme2n1/  
  File  1     4194304           Tue Dec 24 16:27:16 2024  .pfs-paxos  
  File  1     1073741824        Tue Dec 24 16:27:18 2024  .pfs-journal  
   Dir  1     1408              Tue Dec 24 16:29:41 2024  shared_data  
total 2105344 (unit: 512Bytes)  
postgres@a8d5e5dc6b96:/data$ pfs ls /nvme2n1/shared_data/  
   Dir  1     256               Tue Dec 24 16:29:25 2024  pg_multixact  
   Dir  1     768               Tue Dec 24 16:29:35 2024  pg_wal  
   Dir  1     0                 Tue Dec 24 16:29:38 2024  pg_commit_ts  
   Dir  1     512               Tue Dec 24 16:29:41 2024  polar_fullpage  
   Dir  1     0                 Tue Dec 24 16:29:41 2024  pg_replslot  
   Dir  1     0                 Tue Dec 24 16:29:41 2024  pg_tblspc  
   Dir  1     0                 Tue Dec 24 16:29:41 2024  pg_twophase  
   Dir  1     128               Tue Dec 24 16:29:41 2024  pg_xact  
   Dir  1     128               Tue Dec 24 16:29:41 2024  pg_logindex  
   Dir  1     7808              Tue Dec 24 16:29:41 2024  global  
   Dir  1     512               Tue Dec 24 16:29:42 2024  base  
total 0 (unit: 512Bytes)  

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

postgresql.conf

echo"    
huge_pages=off    
port=5432    
polar_hostid=3    
polar_enable_shared_storage_mode=on    
polar_disk_name='nvme2n1'  # 改了  
polar_storage_cluster_name='disk'  # 改了   
polar_datadir='/nvme2n1/shared_data/'  # 改了   
polar_vfs.localfs_mode=off  # 改了   
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     
full_page_writes = off    
polar_has_partial_write = off    # 改了

polar_resource_manager.enable_resource_manager=off
"

 >> ~/standby/postgresql.conf      

7、启动实例

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=# show polar_datadir;  
     polar_datadir       
-----------------------  
 /nvme2n1/shared_data/  
(1 row)  

postgres=# \dt  
         List of relations  
 Schema | Name  | Type  |  Owner     
--------+-------+-------+----------  
 public | t     | table | postgres  
 public | t1    | table | postgres  
 public | t2    | table | postgres  
 public | t3    | table | postgres  
 public | t_new | table | postgres  
 public | tbl   | table | postgres  
(6 rows)  

postgres=# create table test(id int);  
CREATE TABLE  
postgres=# insert into test values (1),(2);  
INSERT 0 2  
postgres=# select * from test;  
 id   
----  
  1  
  2  
(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还有救吗?》

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

《穷鬼玩PolarDB RAC一写多读集群系列 | 将“共享存储实例”转换为“本地存储实例”》