谢谢您的订阅!
当新的内容发布后您将开始接收邮件。您也可以点击邮件内的链接随时取消订阅。关闭Close

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
google
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,请尽快提交反馈,提出建议,或者与我们联系,讨论您的想法和请求。



订阅博客文章

订阅您感兴趣的主题

在提交此表格的同时,我确认已阅读和同意的隐私声明隐私政策。

查看更多内容

83% 的企业肯定开源软件的应用价值,但也表示在安全与治理方面存在重大缺口

Linux Foundation 最新报告揭示了全球企业对开源软件的采用情况、使用现状及认知态度。 Linux Foundation 携手 Canonical 发布的最新报告《全球开源现状》,是继今年早些时候 Linux Foundation 欧洲地区报告之后的全球性报告。报告证实,欧洲地区报告中揭示的诸多趋势在全球范围内同样存在。这份全球报告尤其证实了开源软件在全球范围内作为关键业务系统基础所发挥的重要作用,并且表明采用率的持续攀升。不过,企业仍然缺乏战略性且安全管理开源软件所需的治理机制、安全测试体系及战略成熟度。  报告指出,多数企业期望开源软件能达到企业级性能标准,但在所需治理框架、安全实践及社区协作方面投入不足。  下载报告 83% 的企业承认开源对其未来发展具 […]

Canonical 获得 ISO 27001 认证

该认证是符合网络安全标准的有力证明,可在最严苛的企业环境中全面保障开源产品和服务的应用安全。 Canonical 自豪地宣布,经过知名认证提供商 A-LIGN 的广泛评估,其信息安全管理系统(ISMS)已获得 ISO/IEC 27001 认证。这一里程碑彰显了 Canonical 致力于遵守政策,完善流程和控制措施,这些措施旨在保障企业内部数据、客户数据及供应商信息的保密性、完整性及可用性,并推进公司的企业基础设施环境建设。 Canonical 首席信息安全官 Stephanie Domas 表示:“获得 ISO 27001 认证,有力地证明了我们团队对信息安全的坚定承诺。这证实了我们的流程符合全球最高标准,让我们的客户和合作伙伴确信他们的数据受到最周全、最严谨的保护。安 […]

如何借 Anbox Cloud 构建出色的云游戏平台

为何选择云游戏? 云游戏正在改变我们的游戏方式。玩家无需购买昂贵的硬件,而是从云端流式传输游戏,就像游戏界的 Netflix 一样。 这已不再是未来主义的构想,它就在眼前。NVIDIA GeForce Now、Sony PS Plus 以及 Xbox Cloud Gaming 等服务已展示出可能的图景:将所有心仪游戏(包括独立游戏和 AAA 游戏)从强大的云服务器流式传输至低端设备,从而畅玩高端游戏。 但云游戏的意义不仅在于通过云端运行游戏,更在于让玩家摆脱硬件的束缚。手机可以变身游戏主机,笔记本电脑甚至不需要专用 GPU 就可以成为游戏级电脑。为了实现这一目标,平台必须做到: 构建自有云游戏平台是一项艰巨的任务。游戏的资源需求量大,并且高度依赖 GPU 运算。但网络混乱 […]