快速搭建 PostgreSQL 集群
对于新手或开发人员,如何快速搭建一个PostgreSQL数据库集群。以往,设置数据库集群可能需要手动进行繁琐的配置步骤,但是最近发现一款非常好用的PostgreSQL高可用管理工具,与Patroni和Repmgr相比,最让人眼前一亮的是这个工具带有Web页面,不需要在命令行中操作,也不需要搞一堆配置文件,页面上点一点就可以拉起一个集群,对于开发者来说简直就是一大利器。而且这个工具还有开源版本,代码是用Python写的,对于懂Python的PostgreSQL新手来说也是一大利器,既可以快速搭建自己想要的集群,又可以参照源码学习一些PostgreSQL数据库集群的管理方法。
这个PostgreSQL 高可用管理软件叫CLup,参照其官方文档,可以快速部署服务。这里给点个赞,连Apache或Nginx服务都不需要安装,直接执行一个命令就可以了。对了还有一键安装PostgreSQL数据库的命令,好像是他们自己编译好的(操作系统只支持EL7或EL8的),这里再点个赞,连安装数据库的步骤都省了,当然对于新手来说,最好还是要自己动手安装一下。他们有两个版本,一个企业版本,一个开源版本,企业版的功能更全,默认会有20个CPU核心和集群数据库数量的限制,具体的大家可以看下其官方的文档[CLup简介](
https://www.csudata.com/clup/manual)。安装哪个版本,需要根据自己需求确定。我把两个版本都安装了试用了一下,都是一键安装,安装速度很快也不费事,都试试。
测试环境说明:
名称 | 说明 |
操作系统 | RockyLinux8 |
主机1 | 4核心虚拟机,4GB内存 |
主机2 | 4核心虚拟机,4GB内存 |
CLup企业版
参考文档:
https://www.csudata.com/clup/manual/5.x/10147
先附上部署后搭建的一个集群:
安装Server 端
使用一键安装命令(这里需要ssh连接到虚拟机上)
wget -qO /tmp/clup.sh --no-check-certificate https://get.csudata.com/csuinst/clup.sh && bash /tmp/clup.sh clup install
简单看了下安装步骤,里面好像安装了两个程序包,一个csumdb(官方说的是CLup服务所用到的数据库,是PostgreSQL12版本的),一个主服务程序CLup。
======== begin install csumdb1.1.2.el8.x86_64.bin ...
== Installation is starting ...
=== Installation is complete.
install csumdb1.1.2.el8.x86_64.bin success!
...
======== begin install clup5.1.5.el8.x86_64.bin ...
==== Installation is starting ...
...
==== register systemd service ...
register service clup sucessfully.
==== register systemd service ok.
==== Installation is complete.
install clup5.1.5.el8.x86_64.bin success!
盲猜数据库用户是放到/home下的,去看了下还真是
[root@Server01 home]# ll
total 4
drwx------ 4 csumdb csumdb 4096 Mar 12 03:14 csumdb
先看下服务是否正常(安装步骤里有注册systemctl的系统服务)
[root@Server01 ~]# systemctl status clup
● clup.service - clup
Loaded: loaded (/etc/systemd/system/clup.service; enabled; vendor preset: disabled)
Drop-In: /run/systemd/system/clup.service.d
└─zzz-lxc-service.conf
Active: active (running) since Mon 2024-08-26 10:05:24 CST; 5h 49min ago
Main PID: 2488 (clupserver)
Tasks: 20 (limit: 182297)
Memory: 51.7M
CGroup: /system.slice/clup.service
├─2488 /opt/clup/bin/clupserver start
└─2489 /opt/clup/bin/clupserver start
Aug 26 10:05:23 CLup-Server01 systemd[1]: Starting clup...
Aug 26 10:05:24 CLup-Server01 clupserver[2484]: clup not running
Aug 26 10:05:24 CLup-Server01 clupserver[2486]: 2024-08-26 02:05:24,282 INFO ========== CLup starting ==========
Aug 26 10:05:24 CLup-Server01 su[2490]: (to csumdb) root on none
Aug 26 10:05:24 CLup-Server01 su[2490]: pam_unix(su-l:session): session opened for user csumdb by (uid=0)
Aug 26 10:05:24 CLup-Server01 systemd[1]: Started clup.
Aug 26 10:05:24 CLup-Server01 su[2490]: pam_unix(su-l:session): session closed for user csumdb
看到了active,应该是起来了,登录Web页面(http://IP:8090 用户名admin,密码cstech)
安装Agent端
使用一键安装命令
wget -qO /tmp/clup.sh --no-check-certificate https://get.csudata.com/csuinst/clup.sh && bash /tmp/clup.sh clup-agent install
安装的步骤
======== begin install clup-agent5.1.5.el8.x86_64.bin ...
==== Installation is starting ...
What is your ip?
...
==== register systemd service ...
2024-08-26 02:21:03,312 INFO clup-agent v5.1.5 Copyright (c) 2018-2023 CSUDATA.COM All rights reserved.
Created symlink /etc/systemd/system/multi-user.target.wants/clup-agent.service → /etc/systemd/system/clup-agent.service.
register service clup-agent sucessfully.
==== register systemd service ok.
==== Installation is complete.
install clup-agent5.1.5.el8.x86_64.bin success!
======== begin clean PACKAGE ...
rm -rf /tmp/clup-agent5.1.5.el8.x86_64.bin
begin install database software ...
需要选择一个IP和输入Server端的IP。
安装步骤里也注册了systemctl的系统服务,查看下服务的状态
[root@Server01 ~]# systemctl status clup-agent
● clup-agent.service - clup-agent
Loaded: loaded (/etc/systemd/system/clup-agent.service; enabled; vendor preset: disabled)
Drop-In: /run/systemd/system/clup-agent.service.d
└─zzz-lxc-service.conf
Active: active (running) since Mon 2024-08-26 15:52:23 CST; 3min 22s ago
Process: 7258 ExecStop=/opt/clup-agent/bin/clup-agent stop (code=exited, status=0/SUCCESS)
Process: 7262 ExecStart=/opt/clup-agent/bin/clup-agent start (code=exited, status=0/SUCCESS)
Process: 7260 ExecStartPre=/opt/clup-agent/bin/clup-agent stop (code=exited, status=0/SUCCESS)
Main PID: 7264 (clup-agent)
Tasks: 13 (limit: 182297)
Memory: 16.9M
CGroup: /system.slice/clup-agent.service
└─7264 /opt/clup-agent/bin/clup-agent start
Aug 26 15:52:23 CLup-Server01 systemd[1]: Starting clup-agent...
Aug 26 15:52:23 CLup-Server01 clup-agent[7260]: clup-agent not running
Aug 26 15:52:23 CLup-Server01 clup-agent[7262]: 2024-08-26 07:52:23,651 INFO clup-agent v5.1.5 Copyright (c) 2018-2023 CSUDATA.COM All rights reserved.
Aug 26 15:52:23 CLup-Server01 systemd[1]: Started clup-agent.
看到了active,应该是可以了。
在另外一台机器上也执行下安装clup-agent,操作步骤如上。
安装PostgreSQL数据库
PostgreSQL官方是有安装文档的,不过如果需要快速安装多个版本的话,还是比较麻烦的,所以这里试下CLup提供的一键安装命令,安装PostgreSQL12和14的版本。
wget -qO /tmp/clup.sh --no-check-certificate https://get.csudata.com/csuinst/clup.sh && bash /tmp/clup.sh db install
安装过程中可以看到,先是安装了一堆的依赖,后面选择下数据库的版本,很快就安装好了
请选择要安装的数据库:
1) PostgreSQL 11
2) PostgreSQL 12
3) PostgreSQL 13
4) PostgreSQL 14
5) PolarDB
按 'q' 退出
请输入选择: 2
...
install csupg-12.16.el8.x86_64.bin success!
请选择要安装的数据库:
1) PostgreSQL 11
2) PostgreSQL 12
3) PostgreSQL 13
4) PostgreSQL 14
5) PolarDB
按 'q' 退出
请输入选择: 4
...
install csupg-14.9.el8.x86_64.bin success!
PostgreSQL官方默认的安装路径是在/usr下面,这里的一键安装脚本也是
[root@CLup-Server01 usr]# ll
total 88
dr-xr-xr-x 2 root root 20480 Aug 26 02:31 bin
drwxr-xr-x 6 root root 4096 Aug 21 2023 csupg-12.16
drwxr-xr-x 6 root root 4096 Aug 22 2023 csupg-14.9
...
搭建集群
官方文档说需要先创建一个VIP池,原因是PostgreSQL流复制集群需要使用VIP(虚拟IP),那就建一个(选一段没有使用的IP就行了):
现在就搭建一套集群试下,操作步骤HA集群->集群定义->创建集群->创建PostgreSQL流复制集群
点击下一步后,需要输入集群名称和选择一个vip池,然后分配一个vip,其他的就按默认值好了:
默认的数据库用户密码为postgres,这个看自己需求,想改就改一下。
点击下一步后,就出现了提交按钮,如果需要调整参数,则继续点击下一步,这里就跳过了。
点击提交后就是创建过程的日志,这里就不细看了。
看下创建的结果,HA管理->详情:
CLup 开源版本
开源版的安装提供有一键安装的命令,安装过程跟企业版基本上一样,就是多了Python的环境,有两个目录:
- /opt/csupy3.9.16:Python3的软件
- /opt/csu_pyenv: Python3的虚拟环境,里面安装了所需的工具包
安装Server端
wget -qO /tmp/clup.sh --no-check-certificate https://get.csudata.com/csuinst/clup.sh && bash /tmp/clup.sh openclup install
安装Agent端
wget -qO /tmp/clup.sh --no-check-certificate https://get.csudata.com/csuinst/clup.sh && bash /tmp/clup.sh openclup-agent install
安装成功后登录Web页面(http://IP:8090 用户名admin,密码openclup):
创建集群的页面跟企业版不太一样,不过也是先创建一个VIP池:
然后再创建集群,这里建一套PostgreSQL14版本的,需要添加两台机器:
这里的信息需要手工填入:
之后就是一路点击下一步就可以了,其中有修改参数的页面,有需要改的就改一下。
看下创建的结果,HA管理->详情:
OK,到此两个版本的安装都没问题,都可以很快的创建集群,然后如果需要启动高可用,需要集群上线,这样如果主库坏了,就会自动切换到备库上。高可用我们下次再探索。