MongoDB connects but timeouts after some time

Hello, I have mongodb deployed in Kubernetes as statefulset with 2 nodes (primary and reader - mongo-0 and mongo-1).
I am using ec2 as a jump host to connect to that mongodb database.
My mongodb service external ip points to classic load balancer in aws:
‘External IPs
<name>.us-east-1.elb.amazonaws.com’.
I am using this connection string with command:
‘mongosh mongodb://<user>:<password>@<ec2-jumphost-IP>:57057/?directConnection=true’.

I run it from my local machine. I have ssh key and security rule whitelisted so that jump host ec2 accepts my connection.

It works fine and I get connected to mongodb. However, after some time (from hour to few hours). I am starting to get error: ‘MongoServerSelectionError: Server selection timed out after 30000 ms’.

If I get such error, I restart my mongodb statefulset and try again with same command. This case repeats - I can access my mongodb. However, some time passes and I get same timeouts again.

Please help me identify the reason why could mongodb connection work and let me connect but without changes it stops working after some time. I can only revive connection by restarting mongodb statefulset.

Classic load balancer does not have sticky sessions enabled.

@michael_hoeller I saw your post here MongoDB Unofficial Discord
Would you please be able to help me out on this one? Feeling lost as it looks I have checked everything that was on my mind

Hello @karolizi ,
welcome back to the forum :wave:
There is a lot of infrastructure in your setup which could lead to a disconnect. Could you please check the mongod.log for suspicious errors and warnings and provide this information?
Also, when you can not connect to the database via the above shown URI, do you still have access to the server? If you can connect via localhost but not via the URI you provided then most likely need to go further down the road and check the jumphost, ssh, …

I am currently travelling the next weeks and have limited access. Due to this I have highlighted this post, asking someone to pick up.

Good luck finding the issue
Michael

Thank you a lot for your response!

Interestingly, I can’t see mongod.log file. I launch mongodb pod terminal, then go to /var/log/mongodb/ and that is an empty directory.
I can see that mongo operator has its own logs but particulalry DB does not.

Yes, service itself does not lose internal connection. It looks like external connection to jumphost is timeouting. However, routes, port forwarding, firewall rules look fine. It also works after restart but after some time timeouts randomly (then I need to restart both statefulset of mongodb and jumphost to access DB via url I mentioned.

@michael_hoeller There was also a memory hit in replica when it did not launch. However, it looks like it was only the coincidence