PostgreSQL码农集散地

奇葩领导的奇葩降本方案,集群变单机,准备跑路吗?

参考文档点击文末阅读原文打开; 推荐《最好的PostgreSQL学习镜像》;


领导说要降本,把“集群实例”转换为“单机实例”。研发懵了,出了故障抓合同工祭天?别怕,这是个实验!

系列请戳: 🏆《穷鬼玩PolarDB RAC系列》🏆

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

本文还依赖如下文章:

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

DEMO

在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   
full_page_writes = off  
polar_has_partial_write = on  
"
 >> ~/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  

参考

《穷鬼玩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)节点》

文末彩蛋:国产数据库周边生态

当然一款数据库要流行起来, 除了自己要强大, 还离不开生态. 用好周边生态工具, 管理水平战胜90%老司机!!! 下面简单介绍一下国产数据库周边生态.

1、管控软件

鸣嵩(前阿里云数据库总经理 / 研究员)等大佬们创业创办的云猿生, 核心产品是KubeBlocks. 他们的理念是让管理数据库和搭积木一样简单, 如果你要管理很多套并且种类(OLTP\OLAP\NoSQL\KV\TS\MQ等)很多的数据库产品, 推荐首选.

  • https://github.com/apecloud/kubeblocks

PG中文社区核心委员唐成老师的公司乘数开源的Clup, 专用管理PostgreSQL和PolarDB的集群管理软件, 如果你要管理很多套数据库, 推荐选择. 并且Clup还提供了企业版、自研的连接池、分布式存储、一体机、备份平台等, 是企业用户推荐之选.

  • https://www.csudata.com/

若航老司机开源的pigsty, 集成了300多个PG插件的PG集群和PolarDB集群管理软件, 如果你要管理很多套PG或PolarDB数据库, 且对插件有特别多的需求, 推荐选择.

  • https://pigsty.cc/zh/

2、审计监控诊断优化

翟总(曾经是我背后的男人)到海信聚好看后研发的 DBdoctor, 采用ebpf技术, 在对数据库几乎没有影响的情况下实时监控数据库和服务器的各项指标, 发现和诊断问题根因非常方便.

  • https://www.dbdoctor.cn/

天舟老哥的核心产品Bytebase 是位于您和数据库之间的中间件。它是数据库 DevOps 的 GitLab/GitHub,专为开发人员、DBA 和平台工程师打造。

  • https://bytebase.cc/docs/introduction/what-is-bytebase/

PawSQL, SQL优化和诊断产品.  

D-Smart, Oracle老前辈白老大出品, 专注企业级市场, 将业界顶级DBA经验的产品化作品, 产品功能包括数据库监控、诊断、优化等.

  • https://www.modb.pro/db/567140

3、国产数据库IDE

IDE是开发者的必备工具,例如社区有pgAdmin, 国产IDE则可以看看老程序猿达刚老师的DeskUI:

  • https://www.deskui.com

4、数据同步&迁移&备份恢复

NineData, 老领导出去创业做的产品, 产品涵盖了数据同步、迁移、备份、比对、devops、chatDBA等.

  • https://www.ninedata.cloud/home

DSG, 非常老牌的数据库同步迁移企业级产品, 支持各种数据库的异构和同构迁移, 用他们的话说, 没有dsg搞不定的迁移, 比goldengate还牛.

  • https://www.dsgdata.com/

公开课

如果你对PolarDB学习感兴趣可以阅读这个公开课系列:

除了PolarDB还非常值得关注的几款PG栈国产数据库:

  • HaloDB(基于PG兼容PostgreSQL、Oracle、MySQL. http://www.halodbtech.com/ )、
  • IvorySQL(基于开源PG兼容PG、Oracle. https://www.ivorysql.org/zh-cn/ )、
  • ProtonBase(云原生分布式数仓. https://protonbase.com/ )、
  • 成都文武数据库(https://ww-it.cn)

参考文档点击阅读原文获得


感谢关注我的github (https://github.com/digoal/blog) 及视频号:

Image