gpt4 book ai didi

mongodb - 无法在 Kubernetes 中创建 Mongo 复制集

转载 作者:可可西里 更新时间:2023-11-01 10:00:03 28 4
gpt4 key购买 nike

我想在 Kubernetes 中创建一个 3 节点的 Mongo 副本集。我创建了一个 headless 服务,如下所示

apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongo

我还创建了一个 3 节点状态集,如下所示 -

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 3
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: mongo
command:
- mongod
- "--replSet"
- rs0
- "--smallfiles"
- "--noprealloc"
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 400m
memory: 256Mi
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
persistentVolumeClaim:
claimName: fast
spec:
accessModes: [ "ReadWriteMany" ]
resources:
requests:
storage: 10Gi

我已经创建了有状态集,并且 pod 已启动并正在运行。现在,如果我登录到其中一个容器并在 mongo shell 中设置复制集的配置,我会收到错误消息。我输入的命令是 -

> config = {
... "_id" : "rs0",
... "members" : [
... {
... _id: 1,
... host: 'mongo-0.mongo.demo.svc.cluster.local:27017'
... },
... {
... _id: 2,
... host: 'mongo-1.mongo.demo.svc.cluster.local:27017',
... },
... {
... _id: 3,
... host: 'mongo-2.mongo.demo.svc.cluster.local:27017'
... }
... ]
... }

> rs.initiate(config)

当我执行以下操作时,出现以下错误 -

"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: mongo-1.mongo.demo.svc.cluster.local:27017 failed with Connection refused, mongo-2.mongo.demo.svc.cluster.local:27017 failed with Connection refused"
"code" : 74,
"codeName" : "NodeNotFound",

我不知道如何调试它,因为容器已启动并正在运行。有人可以帮我吗?谢谢

最佳答案

如果您查看任何 Pod 的日志,您将看到一条警告:

[initandlisten] ** WARNING: This server is bound to localhost.
[initandlisten] ** Remote systems will be unable to connect to this server.

因此,您必须提供 "--bind_ip" 标志以确保 MongoDB 监听来自已配置地址上的应用程序的连接。在 official mongodb doc. 中查看有关 ip 绑定(bind)的更多信息

修正后,改进后的yaml如下:

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
spec:
---
template:
---
spec:
---
containers:
- ---
command:
- mongod
- "--replSet"
- rs0
- "--bind_ip" # Add these two
- 0.0.0.0 # lines...!!
- "--smallfiles"
- "--noprealloc"
---
---

而且,Host DNS 也需要稍作修改。该服务部署在 default 命名空间中,而不是在 demo 中。因此,正确的配置将是:

> config = {
"_id" : "rs0",
"members" : [
{
_id: 1,
host: 'mongo-0.mongo.default.svc.cluster.local:27017'
},
{
_id: 2,
host: 'mongo-1.mongo.default.svc.cluster.local:27017',
},
{
_id: 3,
host: 'mongo-2.mongo.default.svc.cluster.local:27017'
}
]
}

关于mongodb - 无法在 Kubernetes 中创建 Mongo 复制集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49190425/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com