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

大城市里の小女人 提交于 2020-12-15 09:05:31

问题


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 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

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.

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


回答1:


When connecting to a replica set, the hostnames and IP addresses in the connection string are the seed list. The driver will attempt to connect to each host in the seed list in turn, and once it gets a connection will run isMaster.

The isMaster command will return the list of member hostname:port in the replica set, as entered in the configuration document. 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
  • The connection is established, Compass runs the isMaster command
  • The node responds with the member list:
    o "tickets-mongo-depl-0.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

Take a look at the MongoDB documentation on deplying a replica set using Kubernetes



来源:https://stackoverflow.com/questions/62482913/connecting-to-a-mongodb-replicaset-inside-kubernetes-with-mongodb-compass-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!