本文介绍了使用MongoDb罗盘GUI连接到MongoDB副本集(在kubernetes内部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用无头服务创建了mongodb的两个副本(副本集名称= rs0).

I have created two replicas (replica set name = rs0) of mongodb with a headless service.

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和服务正常运行

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

我已运行以下命令来公开无头服务,以便可以使用mongodb罗盘GUI连接到数据库

I have run the below command to expose the headless service so that i can connect to my database using mongodb compass GUI

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

副本集已经初始化.

{
        "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)
}

如上图所示,GUI尝试连接时没有运气.

As you can see in the last image the GUI is trying connect without luck.

你们中的任何人不知道这是为什么吗?

Does any of you have any idea why this is??

推荐答案

指南针的最新版本似乎已经解决了该问题,因为它现在对我有效.

the latest version of compass seems to have resolved this problem as it works for me now.

这篇关于使用MongoDb罗盘GUI连接到MongoDB副本集(在kubernetes内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:58