如何为多个数据中心设置 MongoDB?

发布于 2024-12-12 10:48:20 字数 2406 浏览 0 评论 0原文

环境是这样的:

[ Data Center 1 ]
   [ load balancer, ip: 45.45.45.45] 
     [ Server 1-A, internal ip: 10.0.0.1, external ip: 200.0.0.1 ]
     [ Server 1-B, internal ip: 10.0.0.2, external ip: 200.0.0.2 ]
     [ Server 1-C, internal ip: 10.0.0.3, external ip: 200.0.0.3 ]

[ Data Center 2 ]
   [ load balancer, ip: 90.90.90.90] 
     [ Server 2-A, internal ip: 10.0.0.1, external ip: 201.0.0.1 ]
     [ Server 2-B, internal ip: 10.0.0.2, external ip: 201.0.0.2 ]
     [ Server 2-C, internal ip: 10.0.0.3, external ip: 201.0.0.3 ]

[ Data Center 3 ]
   [ load balancer, ip: 88.88.88.88] 
     [ Server 3-A, internal ip: 10.0.0.1, external ip: 221.0.0.1 ]
     [ Server 3-B, internal ip: 10.0.0.2, external ip: 221.0.0.2 ]
     [ Server 3-C, internal ip: 10.0.0.3, external ip: 221.0.0.3 ]

我想要实现的是每台服务器安装一台mongo服务器,并且只允许Data Center 1服务器(1-A、1-B和1-C)作为主服务器。数据中心 2 和数据中心 3 中的 MongoDB 服务器只是辅助服务器。应用程序可以专门从数据中心 2 读取数据,因为这些应用程序可能与数据中心 2 位于同一网络中,数据中心 2 的访问速度比连接到 Data Cetner 1 的速度更快。服务器正在使用复制集。没有分片。

我的问题如下:

  1. 设置复制集时,是否需要使用外部真实 IP 地址来指定数据中心 2 和数据中心 3 的主机?例如

    config = { _id: 'foo', 成员: [
                          // 数据中心1
                          {_id: 0, 主机: '10.0.0.1:27017'},
                          {_id: 1, 主机: '10.0.0.2:27017'},
                          {_id: 2, 主机: '10.0.0.3:27017'},
                          // 数据中心2
                          {_id: 3, 主机: '201.0.0.1:27017'},
                          {_id: 4, 主机: '201.0.0.2:27017'},
                          {_id: 5, 主机: '201.0.0.3:27017'},
                          // 数据中心3
                          {_id: 6, 主机: '221.0.0.1:27017'},
                          {_id: 7, 主机: '221.0.0.2:27017'},
                          {_id:8,主机:'221.0.0.3:27017'}
                         ]
           }
    
  2. 由于数据中心 2 中的服务器位于负载平衡器和防火墙后面,因此 IP 地址可能无法爆炸到外界。是否可以使用负载均衡器 IP 地址?如果是这样,该怎么办?

  3. 我应该使用priorty=0 在数据中心2 和数据中心2 中创建服务器。数据中心 3 永远不会成为主要服务器,对吧?

  4. 其他应用程序如何连接到数据中心2和数据中心3中的MongoDB(仅用于读取数据)?假设应用程序(服务器端应用程序)也在数据中心 2 网络中。应用程序应使用什么 IP 地址连接到 DC2 中的 MongoDB?我应该使用内部IP还是外部IP?如果应用程序位于数据库内部网络之外,并且只想连接到 DC2 中的 MongoDB 以读取数据,我应该使用负载均衡器 IP 还是外部 IP?

  5. 因为我希望DC2和DC3中的服务器能够允许读取数据,所以如何设置slaveOk?我应该在主“slaveOK”中设置它,还是应该在DC2和DC3中的每个服务器中设置它?

  6. 更进一步,假设 DC1、DC2 和 DC3 都是单独的复制集。例如,DC 1 有其唯一的初级和次级。 DC2有自己的初级和次级,到目前为止DC3。当 DC1 发生更改(新记录或更新)时,是否有一种简单且实时的方法来更新 DC2 和 DC3?

Here is the environment:

[ Data Center 1 ]
   [ load balancer, ip: 45.45.45.45] 
     [ Server 1-A, internal ip: 10.0.0.1, external ip: 200.0.0.1 ]
     [ Server 1-B, internal ip: 10.0.0.2, external ip: 200.0.0.2 ]
     [ Server 1-C, internal ip: 10.0.0.3, external ip: 200.0.0.3 ]

[ Data Center 2 ]
   [ load balancer, ip: 90.90.90.90] 
     [ Server 2-A, internal ip: 10.0.0.1, external ip: 201.0.0.1 ]
     [ Server 2-B, internal ip: 10.0.0.2, external ip: 201.0.0.2 ]
     [ Server 2-C, internal ip: 10.0.0.3, external ip: 201.0.0.3 ]

[ Data Center 3 ]
   [ load balancer, ip: 88.88.88.88] 
     [ Server 3-A, internal ip: 10.0.0.1, external ip: 221.0.0.1 ]
     [ Server 3-B, internal ip: 10.0.0.2, external ip: 221.0.0.2 ]
     [ Server 3-C, internal ip: 10.0.0.3, external ip: 221.0.0.3 ]

What I would like to achieve is that each server install one mongo server, and only Data Center 1 servers (1-A, 1-B, and 1-C) are allowed to be primary. MongoDB servers in Data Center 2 and Data Center 3 are only secondary. Applications can specifically read data from Data Center 2, because those applications may be in the same network as Data Center 2 which has a faster access than connecting to Data Cetner 1. Server is using replicate Sets. There is no sharding.

Here are my questions:

  1. When setting up the replicate set, do I need to use external real IP address to specify the host from Data Center 2 and 3? e.g.

    config = {  _id: 'foo', members: [
                          // data center 1
                          {_id: 0, host: '10.0.0.1:27017'},
                          {_id: 1, host: '10.0.0.2:27017'},
                          {_id: 2, host: '10.0.0.3:27017'},
                          // data center 2
                          {_id: 3, host: '201.0.0.1:27017'},
                          {_id: 4, host: '201.0.0.2:27017'},
                          {_id: 5, host: '201.0.0.3:27017'},
                          // data center 3
                          {_id: 6, host: '221.0.0.1:27017'},
                          {_id: 7, host: '221.0.0.2:27017'},
                          {_id: 8, host: '221.0.0.3:27017'}
                         ]
           }
    
  2. Because the servers in Data Center 2 will behind load balancer and firewall, the IP address may not be able to explode to outside world. Is it possible to use the load balancer IP address? if so, how to do it?

  3. I should use priorty=0 to make servers in Data Center 2 & Data Center 3 never be primary, right?

  4. How other application connect to the MongoDB in Data Center 2 and 3 (for read data only)? Let say, the application (a server side app) is also in Data Center 2 network. What IP address should the application use to connect to the MongoDB in DC2? Should I use the internal IP or external IP? If an application is outside of the DB internal network and would like to connect to MongoDB in DC2 to read data only, should I use the load balancer IP or external IP?

  5. Because I want Servers in DC2 and DC3 able to allow read data, how to set slaveOk? Should I set it in the primary "slaveOK", or should I set it in each server in DC2 and DC3?

  6. One step further, let say DC1, DC2 and DC3 are all individual replicate set. e.g. DC 1 has its only primary and secondary. DC2 has its own primary and secondary, so far DC3. Is there an easy and real-time way to update DC2 and DC3 when there is change (new record or update) in DC1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-12-19 10:48:20
  1. 视情况而定。它们需要是您的应用程序服务器可以使用的主机名/IP
    使用权。如果您的应用程序服务器在您的 mongod 进程运行的同一个 DC 上运行,它们可以而且应该是内部的。任何其他设置都存在重大安全问题。
  2. 从技术上来说,是的。将每个 mongod 进程分配给不同的端口。但是,我不明白为什么您需要负载均衡器来分发 mongo 查询。这就是驱动程序会为您做的事情,甚至可能会产生副作用,让驱动程序以外的其他东西决定查询发送给哪个 RS 成员。
  3. 正确的。
  4. 内部 IP 是最佳选择。应用程序服务器应使用本地成员的所有内部 IP 初始化其驱动程序(请参阅:同一 DC 上的成员)。
  5. 您不要将服务器设置为 SlaveOk。 SlaveOk 功能位于驱动程序端。通常为每个连接设置,但大多数驱动程序也有一个全局标志和每个查询标志。显然,如果不启用此标志,您的设置将无法工作。
  6. 不可以。MongoDB atm 中没有主主复制。
  1. Depends. They need to be hostnames/IPs that your application servers can
    access. If your app servers run on the same DC your mongod process are running they can and should be internal. Any other setup has major security issues.
  2. Technically, yes. Assign each mongod process to a different port. However, I don't understand why you'd ever want a load balancer to distribute mongo queries. That's what the driver would do for you and there might even be side effects to letting something other than the driver decide which RS member the query goes to.
  3. Correct.
  4. Internal IPs are the way to go. App servers should initialize their driver with all internal IPs of the local members (read: the members on the same DC)
  5. You don't set servers to slaveOk. SlaveOk functionality is on the driver end of things. Typically set per connection but most drivers have a global and per query flag for it as well. Obviously your setup will not work without this flag enabled.
  6. No. There is no master-master replication in MongoDB atm.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文