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

<pre><code>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
</code></pre>

<p>pods and the service are working as expected</p>

<pre><code>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             &lt;none&gt;        27017/TCP           6d12h
</code></pre>

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

<pre><code>kubectl port-forward service/tickets-mongo-srv 7000:27017
Forwarding from 127.0.0.1:7000 -&gt; 27017
Forwarding from [::1]:7000 -&gt; 2701
</code></pre>

<p>replica set already initialised.</p>

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

<p><a href="https://i.stack.imgur.com/Lnxtq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Lnxtq.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/ouUMl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ouUMl.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/ZlR51.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZlR51.png" alt="enter image description here"></a></p>

<p>As you can see in the last image the GUI is trying connect without luck.</p>

<p>Does any of you have any idea why this is??</p>

<p>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 <code>isMaster</code>.  </p>

<p>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.</p>

<p>In your case,  the following happens:</p>

<ul>
<li>Compass connects to localhost:7000, which Kubernetes forwards to port 27017</li>
<li>The connection is established, Compass runs the <code>isMaster</code> command</li>
<li>The node responds with the member list:<br>
o "tickets-mongo-depl-0.tickets-mongo-srv:27017"<br>
o "tickets-mongo-depl-1.tickets-mongo-srv:27017"  </li>
<li>Compass attempts to connect to the hostnames provided, but DNS resolution of the name fails</li>
</ul>

<p>Take a look at the MongoDB documentation on <a href="https://docs.mongodb.com/kubernetes-operator/master/tutorial/deploy-replica-set/#deploy-a-replica-set" rel="nofollow noreferrer">deplying a replica set using Kubernetes</a></p>
