简体   繁体   English

如何使用Docker / Kubernetes为PostgreSQL故障转移群集建模?

[英]How do I model a PostgreSQL failover cluster with Docker/Kubernetes?

I'm still wrapping my head around Kubernetes and how that's supposed to work. 我仍在围绕Kubernetes进行工作,这应该如何工作。 Currently, I'm struggling to understand how to model something like a PostgreSQL cluster with streaming replication, scaling out and automatic failover/failback ( pgpool-II , repmgr , pick your poison). 目前,我正在努力了解如何使用流复制,扩展和自动故障转移/故障回复( pgpool-IIrepmgr ,选择毒药)对诸如PostgreSQL集群的模型进行建模。

My main problem with the approach is the dual nature of a PostgreSQL instance, configuration-wise -- it's either a master or a cold/warm/hot standby. 我的方法的主要问题是PostgreSQL实例的双重属性,即配置方面的问题-它既可以是主实例,也可以是冷/热/热备用实例。 If I increase the number of replicas, I'd expect them all to come up as standbys, so I'd imagine creating a postgresql-standby replication controller separately from a postgresql-master pod. 如果我增加副本的数量,我希望他们都来充当备用,所以我想像创造了postgresql-standby从单独复制控制器postgresql-master舱。 However I'd also expect one of those standbys to become a master in case current master is down, so it's a common postgresql replication controller after all. 但是我也希望那些备用数据库中的一个能够在当前主数据库发生故障的情况下成为主数据库,因此毕竟这是一个常见的postgresql复制控制器。

The only idea I've had so far is to put the replication configuration on an external volume and manage the state and state changes outside the containers. 到目前为止,我唯一的想法是将复制配置放在外部卷上,并在容器外部管理状态和状态更改。

(in case of PostgreSQL the configuration would probably already be on a volume inside its data directory, which itself is obviously something I'd want on a volume, but that's beside the point) (在PostgreSQL的情况下,配置可能已经在其data目录内的某个卷上,这显然是我想要在卷上执行的操作,但这不重要)

Is that the correct approaach, or is there any other cleaner way? 这是正确的方法,还是还有其他更清洁的方法?

OpenShift中有一个示例: https : //github.com/openshift/postgresql/tree/master/examples/replica该原理在纯Kube中是相同的(它没有使用任何真正的OpenShift特定的东西,您可以使用纯图像码头工人)

You can give PostDock a try, either with docker-compose or Kubernetes. 你可以给PostDock与码头工人,撰写或Kubernetes一试,无论是。 Currently I have tried it in our project with docker-compose, with the schema as shown below: 目前,我已经在我们的项目中使用docker-compose对其进行了尝试,其架构如下所示:

pgmaster (primary node1)  --|
|- pgslave1 (node2)       --|
|  |- pgslave2 (node3)    --|----pgpool (master_slave_mode stream)----client
|- pgslave3 (node4)       --|
   |- pgslave4 (node5)    --|

I have tested the following scenarios, and they all work very well: 我已经测试了以下方案,它们都运行良好:

  • Replication: changes made at the primary (ie, master) node will be replicated to all standby (ie, slave) nodes 复制:在主节点(即主节点)上所做的更改将被复制到所有备用节点(即从节点)
  • Failover: stops the primary node, and a standby node (eg, node4) will automatically take over the primary role. 故障转移:停止主节点,备用节点(例如,node4)将自动接管主要角色。
  • Prevention of two primary nodes: resurrect the previous primary node (node1), node4 will continue as the primary node, while node1 will be in sync but as a standby node. 防止两个主节点:复活先前的主节点(node1),node4将继续作为主节点,而node1将同步但作为备用节点。

As for the client application, these changes are all transparent. 对于客户端应用程序,这些更改都是透明的。 The client just points to the pgpool node, and keeps working fine in all the aforementioned scenarios. 客户端仅指向pgpool节点,并在上述所有情况下都能正常工作。

Note : In case you have problems to get PostDock up running, you could try my forked version of PostDock . 注意 :如果您在启动PostDock时遇到问题,可以尝试使用我的PostDock分支版本

Pgpool-II with Watchdog 带看门狗的Pgpool-II

A problem with the aforementioned architecture is that pgpool is the single point of failure. 前述架构的一个问题是pgpool是单点故障。 So I have also tried enabling Watchdog for pgpool-II with a delegated virtual IP, so as to avoid the single point of failure. 因此,我还尝试了使用委派的虚拟IP 为pgpool-II启用Watchdog,以避免出现单点故障。

master (primary node1)  --\
|- slave1 (node2)       ---\     / pgpool1 (active)  \
|  |- slave2 (node3)    ----|---|                     |----client
|- slave3 (node4)       ---/     \ pgpool2 (standby) /
   |- slave4 (node5)    --/

I have tested the following scenarios, and they all work very well: 我已经测试了以下方案,它们都运行良好:

  • Normal scenario: both pgpools start up, with the virtual IP automatically applied to one of them, in my case, pgpool1 正常情况:两个pgpool都启动,并且虚拟IP自动应用于其中一个(在我的情况下为pgpool1)
  • Failover: shutdown pgpool1. 故障转移:关闭pgpool1。 The virtual IP will be automatically applied to pgpool2, which hence becomes active. 虚拟IP将自动应用于pgpool2,因此将被激活。
  • Start failed pgpool: start again pgpool1. 启动失败的pgpool:再次启动pgpool1。 The virtual IP will be kept with pgpool2, and pgpool1 is now working as standby. 虚拟IP将与pgpool2一起保存,并且pgpool1现在作为备用数据库工作。

As for the client application, these changes are all transparent. 对于客户端应用程序,这些更改都是透明的。 The client just points to the virtual IP, and keeps working fine in all the aforementioned scenarios. 客户端仅指向虚拟IP,并在上述所有情况下都能正常工作。

You can find this project at my GitHub repository on the watchdog branch . 您可以在watchdog分支的GitHub存储库中找到此项目。

Kubernetes's statefulset is a good base for setting up the stateful service. Kubernetes的有状态集是设置有状态服务的良好基础。 You will still need some work to configure the correct membership among PostgreSQL replicas. 您仍然需要一些工作来配置PostgreSQL副本之间的正确成员资格。

Kubernetes has one example for it. Kubernetes就是一个例子。 http://blog.kubernetes.io/2017/02/postgresql-clusters-kubernetes-statefulsets.html http://blog.kubernetes.io/2017/02/postgresql-clusters-kubernetes-statefulsets.html

You can look at one of the below postgresql open-source tools 您可以查看以下Postgresql开源工具之一

1 Crunchy data postgresql 1松散的数据PostgreSQL

  1. Patroni postgresql . Patroni postgresql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kubernetes + Django / PostgreSQL-将PostgreSQL数据库部署到Kubernetes集群时如何指定HOST - Kubernetes + Django / PostgreSQL - How do I specify HOST of my PostgreSQL Database when I deploy it to Kubernetes Cluster 如何在使用docker的postgresql上进行故障转移后提升master - how to promote master, after failover on postgresql with docker 如何访问部署在 Kubernetes 集群上的 postgresql - How to access postgresql, deployed on Kubernetes cluster kubernetes 如何将 pod 暴露给集群机器之外的东西? - kubernetes how do I expose pods to things outside of cluster machine? 什么是 PostgreSQL “集群”,我该如何创建? - What's a PostgreSQL “Cluster” and how do I create one? 访问Kubernetes集群的Postgresql数据 - Accessing Postgresql data of Kubernetes cluster 如何使用 sqlalchemy 在 Docker 中访问 postgresql? - How do I access postgresql within Docker with sqlalchemy? 如何在 docker 中为 postgresql 设置入口点 initdb 脚本? - How do I setup entrypoint initdb scripts for postgresql in docker? 如何访问使用 docker-compose 设置的 postgresql 数据库? - How do I access a postgresql DB that was setup with docker-compose? 如何在 docker 中为我的 postgresql 数据库创建持久卷? - How do I create a persistent volume in docker for my postgresql database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM