Hello,
It seems I have the same problem and I am not able to solve it with the methods you mentioned. Interestingly, when I create a pod in cluster and connect within it to mongodb, I connect to the primary node. When I port-forward locally, I get connected to the secondary node. Might this be related to the connection string and how mongodb resolves nodes?
How it goes locally:
$ kubectl get services -n perconamongodb
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
perconamongodbcluster-rs0 ClusterIP None <none> 27017/TCP 176m
$ kubectl port-forward service/perconamongodbcluster-rs0 27017:27017 -n perconamongodb
Forwarding from 127.0.0.1:27017 -> 27017
Forwarding from [::1]:27017 -> 27017
$ mongosh "mongodb://user:pass@localhost:27017/helloworld?ssl=false&replicaSet=rs0&directConnection=true"
Current Mongosh Log ID: 65cb6753f23d6c23d8e058c5
Connecting to: mongodb://<credentials>@localhost:27017/helloworld?ssl=false&replicaSet=rs0&directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.4
Using MongoDB: 6.0.9-7
Using Mongosh: 2.1.4
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
rs0 [direct: secondary] helloworld>
How it goes on the pod I have created in cluster (from the self: true you can see that the connection points to primary node):
[mongodb@percona-client /]$ mongo "mongodb://user:pass@perconamongodbcluster-rs0.perconamongodb.svc.cluster.local/helloworld?ssl=false&replicaSet=rs0&directConnection=true"
<min?ssl=false&replicaSet=rs0&directConnection=true"
Percona Server for MongoDB shell version v4.4.24-23
connecting to: mongodb://perconamongodbcluster-rs0.perconamongodb.svc.cluster.local:27017/helloworld?compressors=disabled&directConnection=true&gssapiServiceName=mongodb&replicaSet=rs0&ssl=false
Implicit session: session { "id" : UUID("05518a51-a1a5-47fd-933d-12d6804b1325") }
Percona Server for MongoDB server version: v6.0.9-7
WARNING: shell and server versions do not match
{
"_id" : 1,
"name" : "perconamongodbcluster-rs0-1.perconamongodbcluster-rs0.perconamongodb.svc.cluster.local:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 8898,
"optime" : {
"ts" : Timestamp(1707829462, 2),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2024-02-13T13:04:22Z"),
"lastAppliedWallTime" : ISODate("2024-02-13T13:04:22.021Z"),
"lastDurableWallTime" : ISODate("2024-02-13T13:04:22.021Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1707820581, 1),
"electionDate" : ISODate("2024-02-13T10:36:21Z"),
"configVersion" : 8,
"configTerm" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
}
What might be the reason to this? Otherwise, how to develop locally and test data insertion to the database?
I have deployed the Percona mongodb operator on Azure AKS, following this guide: Install on Microsoft Azure Kubernetes Service (AKS) - Percona Operator for MongoDB
Thanks.