PostgreSQL 的高可用性令人着迷
by Canonical on 5 September 2023
我们曾在过去的博文讨论了,以高可用性方式运行数据库的模式。在本文中,我们将介绍针对 PostgreSQL 高可用性的开源方案。
高可用性 PostgreSQL 的好处
一份好的食谱始终应该首先介绍相关食物的好处。因此,我们将首先简要介绍使用 PostgreSQL 的好处,以及我们为何通常要求具有高可用性。
使用大象数据库服务的好处
StackOverflow 的最新调查显示,PostgreSQL 是使用最广泛的数据库之一。如 TimeScale 公布的 《2022 年 PostgreSQL 现状报告》所述,PostgreSQL 已经成功应用于诸如在线事务处理、在线分析处理等各种工作负载 35 年以上,其具有广泛的功能,适用于金融和医疗保健行业等众多行业。
在对 PostgreSQL 有了大致的了解之后,下面将讨论为什么只运行一个 PostgreSQL 实例通常是个坏主意。
为何一个不够
据 ManageForce 或 Phenom 等公布的报告估计,停机成本平均约为每小时 50 万美元。数据库停机通常会影响到其使用的应用程序,并导致所依赖的服务降级。因此,数据库停机对任何公司而言都是最糟糕的情况之一。
不幸的是,受停机所影响的远不止收入。根据 IDC 公布的《2021 年全球数据保护和 DR 现状调查》,停机还可能导致生产力下降、数据丢失和声誉受损。
这就是运行高可用性的 PostgreSQL 部署对公司有益的原因所在。那么如何才能实现呢?下面我们将讨论在 Canonical 中,我们选择用来实现 H.A.PostgreSQL 集群自动化部署的组件。
开源组件
下表列出了我们用来提供高可用性 PostgreSQL 部署的组件:
组件 | 角色/功能 | (主要)版本 |
PostgreSQL | 数据库服务器 | 14 |
PgBouncer | 连接池/连接故障切换 | 1 |
Patroni | 可用性管理器/复制管理器 | 3 |
pgBackRest | 备份恢复 | 2 |
Charmed operator | 用于 PostgreSQL 的运算器 其实现了部署、扩展等诸多管理任务的自动化 | 最新/稳定版 |
Juju | 运算器框架,允许用户在多种云平台上运行运算器,包括 K8s、OpenStack、AWS、Azure 和 GCP。 | 3.1 |
下面介绍如何安装这些组件。
我们的 PostgreSQL 高可用性秘诀
准备环境
第一步是安装我们的运算器框架 Juju :
sudo snap install juju --channel=3.1/stable
接下来,我们安装 LXD。LXD 是一个系统容器管理器,可供我们在本地机器中模拟云环境。
sudo snap install lxd --channel=5.0/stable
下一步是通过执行以下命令来启动 Juju 配置:
juju bootstrap
Clouds
aws
aws-china
aws-gov
azure
azure-china
equinix
localhost
oracle
Select a cloud [localhost]: localhost
Enter a name for the Controller [localhost-localhost]: demo-postgres
部署第一个 PostgreSQL 实例
执行完以上命令后,我们可以开始部署一个 PostgreSQL 实例:
juju add-model mymodel
juju deploy postgresql --channel edge
请注意,第一次运行上述命令时,Juju 可能需要几分钟才能下载完所需的 charm(即基于 Juju 的运算器或应用程序)和依赖项。随后重试时速度应该会更快。
若想检查部署状态,可以使用以下命令:
juju status
# You can type ‘juju status --watch 1s’ to continuously monitor the output
若想了解关于后台进程的更多详情,您可以键入:
juju debug-log --tail --replay
几分钟后(具体时间可能有所不同),在输入 juju status 后,应该得到类似于以下的输出:
Model Controller Cloud/Region Version SLA Timestamp
mymodel demo-postgres localhost/localhost 3.1.0 unsupported 15:00:31+02:00
App Version Status Scale Charm Channel Rev Exposed Message
postgresql active 1 postgresql edge 281 no
Unit Workload Agent Machine Public address Ports Message
postgresql/0* active idle 0 …
Machine State Address Inst id Base AZ Message
0 started … juju-3d56ca-0 ubuntu@22.04 Running
我们的第一个大象数据库已就绪!
扩展实例数量
如前文所述,运行单个实例并不是一个好主意。因此,我们现在来探索 charm 的另一个简洁功能——按需扩展。向 PostgreSQL 添加副本很简单,运行以下命令即可:
juju add-unit -n 2 postgresql
几分钟后,运行 juju status 命令之后应该得到类似于以下的输出:
Model Controller Cloud/Region Version SLA Timestamp
mymodel demo-postgres localhost/localhost 3.1.0 unsupported 15:14:14+02:00
App Version Status Scale Charm Channel Rev Exposed Message
postgresql active 3 postgresql edge 281 no
Unit Workload Agent Machine Public address Ports Message
postgresql/0* active executing 0 …
postgresql/1 active executing 1 …
postgresql/2 active executing 2 …
Machine State Address Inst id Base AZ Message
0 started … juju-3d56ca-0 ubuntu@22.04 Running
1 started … juju-3d56ca-1 ubuntu@22.04 Running
2 started … juju-3d56ca-2 ubuntu@22.04 Running
部署数据库代理
接下来,我们使用以下命令部署 PgBouncer 组件:
juju deploy pgbouncer --channel edge
几分钟后,运行 juju status 命令之后应该显示一个新的应用程序:
Model Controller Cloud/Region Version SLA Timestamp
mymodel demo-postgres localhost/localhost 3.1.0 unsupported 15:16:36+02:00
App Version Status Scale Charm Channel Rev Exposed Message
pgbouncer unknown 0 pgbouncer edge 25 no
postgresql active 3 postgresql edge 281 no
Unit Workload Agent Machine Public address Ports Message
postgresql/0* active idle 0 …
postgresql/1 active idle 1 …
postgresql/2 active idle 2 …
Machine State Address Inst id Base AZ Message
0 started … juju-3d56ca-0 ubuntu@22.04 Running
1 started … juju-3d56ca-1 ubuntu@22.04 Running
2 started … juju-3d56ca-2 ubuntu@22.04 Running
您可能已经注意到,PgBouncer 的状态显示为 unknown。不必担心,这在预料之中。PgBouncer 实际上是部署在同一个系统容器内的从属 charm。因此,PgBouncer 只在使用时才会被调用。
部署测试应用程序
为了模拟使用 PostgreSQL 的应用程序,我们将使用数据集成器charm:
juju deploy data-integrator --channel edge --config database-name=test-database
几分钟(或者如果您像我一样去吃零食,则需几个小时🙂)后,运行 juju status 命令之后应该得到类似于以下的输出:
Model Controller Cloud/Region Version SLA Timestamp
mymodel demo-postgres localhost/localhost 3.1.0 unsupported 17:24:20+02:00
App Version Status Scale Charm Channel Rev Exposed Message
data-integrator blocked 1 data-integrator edge 10 no Please relate the data-integrator with the desired product
pgbouncer unknown 0 pgbouncer edge 25 no
postgresql active 3 postgresql edge 281 no
Unit Workload Agent Machine Public address Ports Message
data-integrator/0* blocked idle 5 … Please relate the data-integrator with the desired product
postgresql/0* active idle 0 … Primary
postgresql/1 active idle 1 …
postgresql/2 active idle 2 …
Machine State Address Inst id Base AZ Message
0 started … juju-3d56ca-0 ubuntu@22.04 Running
1 started … juju-3d56ca-1 ubuntu@22.04 Running
2 started … juju-3d56ca-2 ubuntu@22.04 Running
5 started … juju-3d56ca-5 ubuntu@22.04 Running
将一切连接起来
Juju 提供了一个强大的集成抽象层(也称关系),允许在两个工作负载(例如 PgBouncer 和 PostgreSQL 服务器)之间建立通信链路。
因此,将 PgBouncer 关联到 PostgreSQL 很简单,运行以下命令即可:
juju relate postgresql pgbouncer
将数据集成器关联到 PgBouncer,和前面的操作一样简单:
juju relate data-integrator pgbouncer
几分钟后,运行 juju status 命令之后应该得到类似于以下的输出:
Model Controller Cloud/Region Version SLA Timestamp
mymodel demo-postgres localhost/localhost 3.1.0 unsupported 17:31:43+02:00
App Version Status Scale Charm Channel Rev Exposed Message
data-integrator active 1 data-integrator edge 10 no
pgbouncer active 1 pgbouncer edge 25 no
postgresql active 3 postgresql edge 281 no
Unit Workload Agent Machine Public address Ports Message
data-integrator/0* active idle 5 …
pgbouncer/0* active idle …
postgresql/0* active idle 0 … Primary
postgresql/1 active idle 1 …
postgresql/2 active idle 2 …
… The rest was omitted to save some bytes (and the elephant's environment).
瞧!当前已经在以下二者之间建立了通信链路:
- PgBouncer 和 PostgreSQL
- 数据集成器和 PgBouncer
菜肴已做好,让我们来品鉴一番吧。
品鉴时间
与大象数据库对话
我们将继续“烹饪”,使用数据集成器 charm 与 PostgreSQL 进行通信。运行以下命令将创建一个用户并显示其登录凭证:
juju run data-integrator/leader get-credentials
后者只会以下面的格式显示一次用户名和密码:
ok: "True"
postgresql:
database: test-database
endpoints: localhost:<port>
password: <password>
username: <username>
version: "14.7"
现在,我们可以使用以下命令连接到已部署的 PostgreSQL:
juju ssh postgresql/0*
psql --host=<ip address of postgresql/0* that displayed in juju status output>
--username=<previously provided user name>
--password test-database
在输入前面提供的密码后,我们会收到一个提示,我们可以在其中运行针对 PostgreSQL 部署的查询。例如,我们可以发出以下查询:
test-database=> SELECT VERSION();
您可以输入 \q 退出提示,也可以输入 exit 退出系统容器。
稍微干扰一下大象数据库
高可用性不仅仅关乎部署多个 PostgreSQL 副本。其还涉及到在问题影响到其中一个副本时提供自动故障切换。我们一起来看看,当我们在一个 PostgreSQL 数据库中模拟故障时,charmed operator 会为我们做些什么。
我们建议,从现在开始,在新的终端/选项卡中执行后续的指令,以便您可以查看 charmed operator 的操作。
我们将通过以下操作在 PostgreSQL 主数据库中模拟第一个问题:
# Connect to the system container where the primary elephant is hosted
juju ssh postgresql/0*
# The following will display all running processes
ps -edf
# The following will terminate all postgresql related processes
sudo pkill postgres
ps -edf
在所有 postgreSQL 进程被终止后不久,Patroni 应该会重新启动这些进程。作为 PostgreSQL 用户的您不会注意到有任何问题。
现在,我们将尝试突然停止运行 PostgreSQL 主数据库的系统容器。这样可以模拟服务器崩溃或主数据库突然网络隔离。
exit
Juju status
# Note the ip address of the primary PostgreSQL
# Then note the Inst id corresponding to the primary (in the Machine section of juju status’ output)
lxc list
# The above will display all the system containers managed by lxd/lxc
# Identify the system container used by the primary by comparing the lxc’s NAME to the previously identified Inst id
lxc stop --force --timeout 0 <Inst id>
# The above will abruptly stop the Primary’s host
lxc list
通过查看 juju status,您可以看到发生了自动故障切换,并且我们的集群仅在几秒钟后就已自我修复!
juju status 应该显示类似于以下的输出:
Model Controller Cloud/Region Version SLA Timestamp
mymodel demo-postgres localhost/localhost 3.1.0 unsupported 19:07:11+02:00
App Version Status Scale Charm Channel Rev Exposed Message
data-integrator active 1 data-integrator edge 10 no
pgbouncer active 1 pgbouncer edge 25 no
postgresql active 2/3 postgresql edge 281 no
Unit Workload Agent Machine Public address Ports Message
data-integrator/0* active idle 5 …
pgbouncer/0* active idle …
postgresql/0 unknown lost 0 … agent lost, see 'juju show-status-log postgresql/0'
postgresql/1 active idle 1 … Primary
postgresql/2* active idle 2 …
Machine State Address Inst id Base AZ Message
0 down … juju-3d56ca-0 ubuntu@22.04 Running
1 started … juju-3d56ca-1 ubuntu@22.04 Running
2 started … juju-3d56ca-2 ubuntu@22.04 Running
5 started … juju-3d56ca-5 ubuntu@22.04 Running
反馈时间
在 Canonical,我们致力于开源软件。因此,我们所有的 charm 都是开源的,可以在以下链接中获取:
- 运算器资源库:
- postgresql-operator,在基于虚拟机的部署环境中运行 PostgreSQL
- postgresql-k8s-operator,在基于 K8s 的部署环境中运行 PostgreSQL
- Charms:
- https://charmhub.io/postgresql,包含我们在数据库服务器端所需要的一切
- https://charmhub.io/pgbouncer
如果您和我们一样喜欢 PostgreSQL,请尽快提交反馈,提出建议,或者与我们联系,讨论您的想法和请求。
订阅博客文章
查看更多内容
Charmed MongoDB 全面上市
Canonical 发布 MongoDB® 企业版解决方案 Charmed MongoDB,其提供先进的自动化功能、多云功能和全面的支持 MongoDB® 是全球使用最广泛的数据库之一。它提供强大的扩展、协调和容错功能,成为了各种规模和各个行业企业组织的热门之选。Charmed MongoDB 是 MongoDB® 社区版的企业直接替代版,具有企业组织在其生产环境中需要用到的高级功能。 Canonical 产品副总裁 Cedric Gegout 称:“Charmed MongoDB 是我们开源数据解决方案产品组合中的一员,旨在满足现代部署的需求。”“企业组织可以放心地部署 Charmed MongoDB,他们知道 Canonical 会提供在任何云环境中的性能承诺以及长达 […]
Edge Computing Examples
您应当知道的 5 个边缘计算示例 在日新月异的科技世界中,创新是保持领先地位的关键。在企业追求效率、速度和实时数据处理能力的趋势下,人们越来越关注边缘计算。 边缘计算代表了数据处理和分析方式的范式转变。与将数据处理集中在远程数据中心的传统云计算不同,边缘计算是将数据处理带到了更接近数据源的地方。这样不仅减少了延迟,而且为各行各业开辟了一个充满可能性的世界。 我很高兴通过本篇博客跟大家一起来探讨这项尖端技术的示例及各种应用和用例,文中将特别着重于探讨 Canonical 的 MicroCloud 如何无缝地适应这一转变格局。 各行各业边缘计算示例 智慧城市和城市规划 边缘计算在智慧城市的发展中起着至关重要的作用。通过在整个城市环境中部署传感器、摄像头等边缘设备,数据可以就 […]
Canonical 发布 Ubuntu 24.04 LTS Noble Numbat
Canonical 的第 10 个长期支持版本树立了在性能工程、企业安全和开发人员体验方面的新标准。 伦敦,2024 年 4 月 25 日 Canonical 今日正式发布 Ubuntu 24.04 LTS,代号“Noble Numbat”(尊贵的袋食蚁兽),用户可前往 https://ubuntu.com/download 下载并安装。 Ubuntu 24.04 LTS 建立在前三个中期版本的进步以及世界各地开源开发人员的贡献之上,只为确保一个安全、优化和具有前瞻性的平台。 Canonical 首席执行官 Mark Shuttleworth 称:“Ubuntu 24.04 LTS 在性能工程和机密计算方面迈出了大胆的一步,实现了一个企业级创新平台,支持期限至少为 12 […]