简体   繁体   English

使用 MongoDb 指南针 GUI 连接到 MongoDB 副本集(在 kubernetes 内)

[英]Connecting to a MongoDB replicaset (inside kubernetes) with MongoDb compass GUI

I have created two replicas (replica set name = rs0) of mongodb with a headless service.我使用无头服务创建了 mongodb 的两个副本(副本集名称 = rs0)。

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: tickets-mongo-depl
spec:
  replicas: 2
  serviceName: tickets-mongo-srv
  selector:
    matchLabels:
      app: tickets-mongo
  template:
    metadata:
      labels:
        app: tickets-mongo
    spec:
      containers:
        - name: tickets-mongo
          image: mongo
          command:
            - mongod
            - '--bind_ip_all'
            - '--replSet'
            - rs0
---
apiVersion: v1
kind: Service
metadata:
  name: tickets-mongo-srv
spec:
  clusterIP: None
  selector:
    app: tickets-mongo
  ports:
    - name: tickets-db
      protocol: TCP
      port: 27017
      targetPort: 27017

pods and the service are working as expected豆荚和服务按预期工作

pod/tickets-mongo-depl-0                 1/1     Running   0          50m
pod/tickets-mongo-depl-1                 1/1     Running   0          50m

service/tickets-mongo-srv   ClusterIP   None             <none>        27017/TCP           6d12h

I have run the below command to expose the headless service so that i can connect to my database using mongodb compass GUI我已经运行以下命令来公开无头服务,以便我可以使用 mongodb 指南针 GUI 连接到我的数据库

kubectl port-forward service/tickets-mongo-srv 7000:27017
Forwarding from 127.0.0.1:7000 -> 27017
Forwarding from [::1]:7000 -> 2701

replica set already initialised.副本集已经初始化。

{
        "set" : "rs0",
        "date" : ISODate("2020-06-20T08:03:02.019Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1592640178, 1),
                        "t" : NumberLong(1)
                },
                "lastCommittedWallTime" : ISODate("2020-06-20T08:02:58.983Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1592640178, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityWallTime" : ISODate("2020-06-20T08:02:58.983Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1592640178, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1592640178, 1),
                        "t" : NumberLong(1)
                },
                "lastAppliedWallTime" : ISODate("2020-06-20T08:02:58.983Z"),
                "lastDurableWallTime" : ISODate("2020-06-20T08:02:58.983Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1592640168, 1),
        "lastStableCheckpointTimestamp" : Timestamp(1592640168, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2020-06-20T06:29:48.607Z"),
                "electionTerm" : NumberLong(1),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1592634577, 1),
                        "t" : NumberLong(-1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2020-06-20T06:29:48.634Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2020-06-20T06:29:50.053Z")
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "tickets-mongo-depl-0.tickets-mongo-srv:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 5750,
                        "optime" : {
                                "ts" : Timestamp(1592640178, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-06-20T08:02:58Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1592634588, 1),
                        "electionDate" : ISODate("2020-06-20T06:29:48Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "tickets-mongo-depl-1.tickets-mongo-srv:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5604,
                        "optime" : {
                                "ts" : Timestamp(1592640178, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1592640178, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-06-20T08:02:58Z"),
                        "optimeDurableDate" : ISODate("2020-06-20T08:02:58Z"),
                        "lastHeartbeat" : ISODate("2020-06-20T08:03:00.524Z"),
                        "lastHeartbeatRecv" : ISODate("2020-06-20T08:03:00.028Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "tickets-mongo-depl-0.tickets-mongo-srv:27017",
                        "syncSourceHost" : "tickets-mongo-depl-0.tickets-mongo-srv:27017",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 1
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1592640178, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1592640178, 1)
}

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

As you can see in the last image the GUI is trying connect without luck.正如您在最后一张图片中看到的那样,GUI 正在尝试连接但没有运气。

Does any of you have any idea why this is??你们有谁知道这是为什么?

When connecting to a replica set, the hostnames and IP addresses in the connection string are the seed list.连接到副本集时,连接字符串中的主机名和 IP 地址是种子列表。 The driver will attempt to connect to each host in the seed list in turn, and once it gets a connection will run isMaster .驱动程序将依次尝试连接到种子列表中的每个主机,一旦获得连接就会运行isMaster

The isMaster command will return the list of member hostname:port in the replica set, as entered in the configuration document. isMaster 命令将返回副本集中成员 hostname:port 的列表,如配置文档中输入的那样。 The client then drops the original connection, and connects to the discovered nodes.客户端然后断开原始连接,并连接到发现的节点。

In your case, the following happens:在您的情况下,会发生以下情况:

  • Compass connects to localhost:7000, which Kubernetes forwards to port 27017 Compass 连接到 localhost:7000,Kubernetes 转发到端口 27017
  • The connection is established, Compass runs the isMaster command连接建立,Compass 运行isMaster命令
  • The node responds with the member list:节点以成员列表响应:
    o "tickets-mongo-depl-0.tickets-mongo-srv:27017" o “tickets-mongo-depl-0.tickets-mongo-srv:27017”
    o "tickets-mongo-depl-1.tickets-mongo-srv:27017" o “tickets-mongo-depl-1.tickets-mongo-srv:27017”
  • Compass attempts to connect to the hostnames provided, but DNS resolution of the name fails Compass 尝试连接到提供的主机名,但名称的 DNS 解析失败

Take a look at the MongoDB documentation on deplying a replica set using Kubernetes查看有关使用 Kubernetes 部署副本集的 MongoDB 文档

the latest version of compass seems to have resolved this problem as it works for me now.最新版本的指南针似乎已经解决了这个问题,因为它现在对我有用。

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

相关问题 使用 mongodb 罗盘 GUI 连接到 docker 内的 Mongodb - connecting to Mongodb inside a docker with mongodb compass GUI 连接到 Kubernetes 上的 MongoDB ReplicaSet - Connection to MongoDB ReplicaSet on Kubernetes MongoDB Atlas 连接到 Compass GUI 但不连接到“应用程序” - MongoDB Atlas connecting to Compass GUI but not to the “ application ” 如何连接到 Kube.netes 上的 MongoDB 复制集 - How to connect to MongoDB replicaset on Kubernetes Kubernetes MongoDB 副本集创建失败 - Kubernetes MongoDB replicaset creation failed 以 replicaset 模式在 kube.netes 上部署 Mongodb - Deploy Mongodb on kubernetes in replicaset Mode 使用 MongoDB Compass 连接 - Connecting using MongoDB Compass 将 MongoDB 罗盘客户端连接到 kubernetes 集群内的 mongodb 数据库 - Connect MongoDB compass client to the mongodb database inside kubernetes cluster 在启用外部访问的情况下连接到 kube.netes 中的 mongodb 副本集 - Connect to mongodb replicaset in kubernetes with external access enabled 从 Kubernetes 集群中的另一个服务连接到 MongoDB Ops Manager 上的 ReplicaSet,给出 MongooseServerSelectionError - Connecting to ReplicaSet on MongoDB Ops Manager from another service in the Kubernetes cluster giving MongooseServerSelectionError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM