GKE not able to reach MongoDB Atlas

5/30/2020

I have an issue with trying to deploy my containerized app to GKE. It is not able to reach my MongoDB Atlas cluster. Running the Docker container locally creates no issues and works perfectly. I am by no means an expert in Docker or Kubernetes, but I am assuming it is something to do with the DNS name resolution.

I have followed this tutorial - Deploying a containerized web application, with an addition of adding an EXTERNAL-IP of the LoadBalancer to my 'Network Access' IP Whitelist in the MongoDB Atlas console and using port mapping 443 -> 8443 since I am using HTTPS.

Only logs that my app is able to produce before failing:

(mongodb): 2020/05/30 15:07:39 logger.go:96: 2020-05-30T15:07:39Z 
[error] Failed to connect to mongodb. Check if mongo is running...
(mongodb): 2020/05/30 15:07:39 logger.go:132: 2020-05-30T15:07:39Z 
[fatal] server selection error: server selection timeout, current 
topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr: biomas- 
cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, 
State: Connected, Average RTT: 0, Last error: connection() : 
connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-180]) incomplete read of message 
header: EOF }, { Addr: biomas-cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-181]) incomplete read of message header: EOF }, { Addr: biomas-cluster-shard-<removed>.azure.mongodb.net:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection() : connection(biomas-cluster-shard-<removed>.azure.mongodb.net:27017[-179]) incomplete read of message header: EOF }, ] }

If there is a simple workaround with to this, that would be preferred since the app is in the development stage still, so I just need a basically working application using the said technologies.

The full workflow:

Android App -> Golang API running on Docker -> MongoDB Atlas

Thanks

-- Roland Stojkoski
docker
go
google-kubernetes-engine
kubernetes
mongodb-atlas

1 Answer

5/30/2020

Exactly as @Marc point, your traffic got out with EXTERNAL-IP of your worker nodes, not your loadblacner.

To find nodes EXTERNAL-IP IPs use:

kubectl get nodes -owide

To be more precise and output only IPs use (taken from kubectl Cheat Sheet):

kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

Next whitelist those IPs and you should be good, but keep in mind that after Kubernetes upgrade or cluster scaling the IPs might change, so I recommend using Cloud NAT to always have the same IP for your outgoing traffic.

-- FL3SH
Source: StackOverflow