Natjo
Natjo

Reputation: 2118

Pod with Mongo reachable via localhost but not service name

This problem only arises on a cluster created with default Minikube settings, but not on a remote cluster created via Kops.

I run this setup on a Minikube cluster in a virtual machine. I have a Pod running MongoDB in my namespace and a service pointing to it:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: mongo
  namespace: platform
  labels:
    app: mongo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo
          env:
          - name: MONGO_INITDB_ROOT_USERNAME
            value: root
          - name: MONGO_INITDB_ROOT_PASSWORD
            value: password

---
apiVersion: v1
kind: Service
metadata:
  name: mongo
  namespace: platform
spec:
  selector:
    app: mongo
  ports:
    - protocol: TCP
      port: 27017

When I run a shell inside a mongo container, I can connect to the database with

mongo mongodb://root:password@localhost:27017

but it does not work with

$ mongo mongodb://root:password@mongo:27017
MongoDB shell version v4.4.1
connecting to: mongodb://mongo:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server mongo:27017, connection attempt failed: SocketException: Error connecting to mongo:27017 (10.110.155.65:27017) :: caused by :: Connection timed out :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

I checked the service and it points to the correct address:

$ kubectl describe service mongo --namespace platform
Name:              mongo
Namespace:         platform
Labels:            <none>
Annotations:       <none>
Selector:          app=mongo
Type:              ClusterIP
IP:                10.110.155.65
Port:              <unset>  27017/TCP
TargetPort:        27017/TCP
Endpoints:         172.17.0.9:27017
Session Affinity:  None
Events:            <none>
$ kubectl describe pod mongo-6c79f887-5khnc | grep IP   
IP:           172.17.0.9

And using this endpoint directly works, too:

mongo mongodb://root:[email protected]:27017

As a sidenote, I have other pods+services running webservers in the same namespace, which work as expected through the service.

Addition

I run the mongo ... connection commands from within the MongoDB container/pod. I also tried all these connection commands from other pods inside the same namespace. Always with the same result: Using the IP works, using mongo for dns resolution does not work.

As a test, I replicated the exact same pod/service configuration but with an nginx server instead of mongodb:

...
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx
  namespace: debug-mongo
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx

---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: debug-mongo
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80

And curl nginx:80 returns the default nginx page successfully.

Upvotes: 0

Views: 683

Answers (1)

Tarum
Tarum

Reputation: 993

Okay forgot what I said before. You need to set your services clusterIP to none.

spec:
  clusterIP: None

K8s service with cluster ip acts like loadbalancer and mongo cant work like that i suppose.

Upvotes: 1

Related Questions