我在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是群集内部的,因此此后连接将失败。
有什么建议我可以做什么?
答案 0 :(得分:0)
您正在使用的负载均衡器不是不稳定的会话,您可以使用nginx控制器为基于nginx的负载平衡器提供基于客户端或cookie设置粘性会话亲和力的功能。
https://github.com/kubernetes/ingress-nginx
另一种选择是使用其他容器作为mongo代理,例如,您可以使用HAPoxy容器,在其中配置HAProxy来侦听27017端口上的TCP连接,并作为后端3种不同的mongo服务,其余仍需要设置HAProxy中的一个healtheck,可以使您知道尚有多少个后台程序。