【问题标题】:Exposing and Connecting MongoDB ReplicaSet running as K8S Statefulset暴露和连接作为 K8S Statefulset 运行的 MongoDB ReplicaSet
【发布时间】:2019-07-05 09:19:57
【问题描述】:

我在 Kubernetes 中部署了一个 MongoDB ReplicaSet 作为 StatefulSet。我正在运行一个 Bare Metal K8S 集群,因此我使用 MetalLB 来公开 LoadBalancer 类型的服务。如果我的 MongoDB-RS 设置暴露服务如下所示:

NAME              TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)           AGE
mongo-0           LoadBalancer   10.43.199.127   172.16.24.151   27017:31118/TCP   55m
mongo-1           LoadBalancer   10.43.180.131   172.16.24.152   27017:31809/TCP   55m
mongo-2           LoadBalancer   10.43.156.124   172.16.24.153   27017:30312/TCP   55m

这按预期工作,但从外部客户端连接到 RS 时出现问题:

➜  ~ mongo "mongodb://172.16.24.151:27017,172.16.24.152:27017,172.16.24.153:27017/?replicaSet=rs0"
MongoDB shell version v4.0.10
connecting to: mongodb://172.16.24.151:27017,172.16.24.152:27017,172.16.24.153:27017/?gssapiServiceName=mongodb&replicaSet=rs0
2019-07-05T10:47:27.058+0200 I NETWORK  [js] Starting new replica set monitor for rs0/172.16.24.151:27017,172.16.24.152:27017,172.16.24.153:27017
2019-07-05T10:47:27.106+0200 I NETWORK  [js] Successfully connected to 172.16.24.153:27017 (1 connections now open to 172.16.24.153:27017 with a 5 second timeout)
2019-07-05T10:47:27.106+0200 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to 172.16.24.151:27017 (1 connections now open to 172.16.24.151:27017 with a 5 second timeout)
2019-07-05T10:47:27.136+0200 I NETWORK  [ReplicaSetMonitor-TaskExecutor] changing hosts to rs0/10.42.2.155:27017,10.42.3.147:27017,10.42.4.108:27017 from rs0/172.16.24.151:27017,172.16.24.152:27017,172.16.24.153:27017
2019-07-05T10:47:52.654+0200 W NETWORK  [js] Unable to reach primary for set rs0
2019-07-05T10:47:52.654+0200 I NETWORK  [js] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2019-07-05T10:47:52.654+0200 E QUERY    [js] Error: connect failed to replica set rs0/172.16.24.151:27017,172.16.24.152:27017,172.16.24.153:27017 :
connect@src/mongo/shell/mongo.js:344:17

有时它会说“将主机更改为 rs0/10.42.2.155:27017,10.42.3.147:27017,10.42.4.108:27017”。由于这些 IP 是集群内部的,因此在此之后连接将失败。

有什么建议吗?

【问题讨论】:

  • 您好,您的集群 IP 是 10.43.199.127... 并连接到 10.42.2.155。它甚至没有连接到集群 IP
  • 哦,哇,没有意识到这一点。我猜这更奇怪
  • 这些 IP 是什么?
  • 您能提供kubectl get pods -o wide 的结果吗?这些可能是 pod 的 IP 地址。

标签: mongodb kubernetes kubernetes-service kubernetes-statefulset


【解决方案1】:

您正在使用一个不是 stiky session 的 LoadBalancer,您可以使用 nginx 控制器提供基于 nginx 的负载均衡器,并能够根据客户端或 cookie 设置粘性会话亲和性。

https://github.com/kubernetes/ingress-nginx

另一种选择是使用不同的容器作为 mongo 代理,例如,您可以使用 HAPoxy 容器,在其中配置 HAProxy 以侦听 27017 端口上的 TCP 连接,并将 3 个不同的 mongo 服务作为后端,您需要设置HAProxy 中的健康检查,以允许知道有多少后端是活着的。

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多