Skip to content

[BUG] cannot connect to replicaset mod mongodb outside k8s cluster #8373

Closed
@Pieerot

Description

@Pieerot

Describe the bug
when i try to connect to a deployed replicaset mode mongodb by golang mongo driver, the driver will monitor cluster topology and update topology to headless domain. But at outside of k8s cluster i can't resolve the headless domain, so it will occur error which "no such host".

I have expose the service by NodePort service, And if I directly connect a pod, it can work properly.

I noticed that Lorry uses the headless domain component Replicset topology. Is there any configuration to use custom external domains for component topology, or is there any other way for me to connect to MongoDB outside the k8s cluster?

$ kubectl get svc -n mongo-jy-test09
NAME                                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                 AGE
mongo-jy-test09-mongodb                      ClusterIP   10.97.194.156    <none>        27017/TCP                               9d
mongo-jy-test09-mongodb-headless             ClusterIP   None             <none>        27017/TCP,3601/TCP,3501/TCP,50001/TCP   9d
mongo-jy-test09-mongodb-mongodb              ClusterIP   10.100.55.69     <none>        27017/TCP                               9d
mongo-jy-test09-mongodb-mongodb-nodeport-0   NodePort    10.101.236.126   <none>        27017:32413/TCP                         2d
mongo-jy-test09-mongodb-mongodb-nodeport-1   NodePort    10.108.123.16    <none>        27017:31196/TCP                         2d
mongo-jy-test09-mongodb-mongodb-nodeport-2   NodePort    10.108.173.85    <none>        27017:31721/TCP                         2d
mongo-jy-test09-mongodb-mongodb-ro           ClusterIP   10.101.35.109    <none>        27017/TCP                               9d

$ kubectl get pods -n mongo-jy-test09 -o wide
NAME                        READY   STATUS    RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
mongo-jy-test09-mongodb-0   2/2     Running   0          6d1h   10.244.0.40   node1   <none>           <none>
mongo-jy-test09-mongodb-1   2/2     Running   0          6d1h   10.244.0.41   node1   <none>           <none>
mongo-jy-test09-mongodb-2   2/2     Running   0          6d1h   10.244.0.42   node1   <none>           <none>

$ kubectl get endpoints -n mongo-jy-test09
NAME                                         ENDPOINTS                                                           AGE
mongo-jy-test09-mongodb                      10.244.0.40:27017,10.244.0.41:27017,10.244.0.42:27017               9d
mongo-jy-test09-mongodb-headless             10.244.0.40:50001,10.244.0.41:50001,10.244.0.42:50001 + 9 more...   9d
mongo-jy-test09-mongodb-mongodb              10.244.0.42:27017                                                   9d
mongo-jy-test09-mongodb-mongodb-nodeport-0   10.244.0.40:27017                                                   2d
mongo-jy-test09-mongodb-mongodb-nodeport-1   10.244.0.41:27017                                                   2d
mongo-jy-test09-mongodb-mongodb-nodeport-2   10.244.0.42:27017                                                   2d
mongo-jy-test09-mongodb-mongodb-ro           10.244.0.40:27017,10.244.0.41:27017                                 9d
panic: server selection error: server selection timeout, current topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: mongo-jy-test09-mongodb-0.mongo-jy-test09-mongodb-headless.mongo-jy-test09.svc.cluster.local:27017, Type: U
nknown, Last error: dial tcp: lookup mongo-jy-test09-mongodb-0.mongo-jy-test09-mongodb-headless.mongo-jy-test09.svc.cluster.local: no such host }, { Addr: mongo-jy-test09-mongodb-1.mongo-jy-test09-mongodb-headless.mongo-jy-test09
.svc.cluster.local:27017, Type: Unknown, Last error: dial tcp: lookup mongo-jy-test09-mongodb-1.mongo-jy-test09-mongodb-headless.mongo-jy-test09.svc.cluster.local: no such host }, { Addr: mongo-jy-test09-mongodb-2.mongo-jy-test09
-mongodb-headless.mongo-jy-test09.svc.cluster.local:27017, Type: Unknown, Last error: dial tcp: lookup mongo-jy-test09-mongodb-2.mongo-jy-test09-mongodb-headless.mongo-jy-test09.svc.cluster.local: no such host }, ] }

To Reproduce
Steps to reproduce the behavior:

  1. deploy a replicaset mongodb, and expose each pod by NodePort service.
  2. use golang mongodb driver to connect mongodb cluster by multiple hosts.
const uri = "mongodb://[email protected]:32413,10.52.140.27:31196,10.52.140.27:31721/?authSource=admin"

func main() {

	serverAPI := options.ServerAPI(options.ServerAPIVersion1)
	opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI).SetAuth(options.Credential{
		Username: "root",
		Password: "0RV6j6P098BZ0F8w",
	})

	client, err := mongo.Connect(context.TODO(), opts)
	if err != nil {
		panic(err)
	}
	defer func() {
		if err = client.Disconnect(context.TODO()); err != nil {
			panic(err)
		}
	}()

	var result bson.M
	if err := client.Database("admin").RunCommand(context.TODO(), bson.D{{"ping", 1}}).Decode(&result); err != nil {
		panic(err)
	}
	fmt.Println("Pinged your deployment. You successfully connected to MongoDB!")
}

Expected behavior
could connect mongodb outside k8s cluster.

Screenshots

Desktop (please complete the following information):

  • go.mongodb.org/mongo-driver: v1.16.1
  • mongodb: 5.0.14
  • kubeblock: v0.9.0

Additional context

Metadata

Metadata

Labels

kind/bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions