How to connect with headless Mongo Service in kubernetes

9/24/2019
apiVersion: v1
kind: Service
metadata:
 name: mongo
 labels:
  name: mongo
spec:
 ports:
  - port: 27017
    targetPort: 27017
 clusterIP: None
 selector:
  name: mongo
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
 name: mongo
spec:
 serviceName: "mongo"
 replicas: 1
 template:
  metadata:
   labels:
    name: mongo
#    environment: test
  spec:
   terminationGracePeriodSeconds: 10
   volumes:
    - name: mongo-pv-storage
      persistentVolumeClaim:
       claimName: mongo-pv-claim
   containers:
    - name: mongo
      image: mongo:4.0.12-xenial
      command:
       - mongod
       - "--bind_ip"
       - 0.0.0.0
       - "--smallfiles"
       - "--noprealloc"
      ports:
       - containerPort: 27017
         name: mongo
      volumeMounts:
       - name: mongo-pv-storage
         mountPath: /data/db

I have used the above yaml. Mongo Db is running fine checked using kubectl exec command. Below yaml used to deploy spring boot application.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: imageprocessor-app-backend
  labels:
    app: imageprocessor-app-backend
spec:
  # modify replicas according to your case
  selector:
    matchLabels:
      tier: imageprocessor-app-backend
  template:
    metadata:
      labels:
        tier: imageprocessor-app-backend
    spec:
      containers:
      - name: imageprocessor-app-backend
        image: imageprocessor-app-backend:v1
        ports:
        - containerPort: 8099
        env:
        - name: spring.data.mongodb.host
          value: mongo-0.mongo
        - name: spring.data.mongodb.port
          value: "27017"
        - name: spring.data.mongodb.database
          value: testdb

---
apiVersion: v1
kind: Service
metadata:
  name: imageprocessor-app-backend
spec:
  type: NodePort
  ports:
  - port: 8099
    nodePort: 31471
  selector:
    tier: imageprocessor-app-backend

The exception I am getting is

2019-09-24 12:27:04.902  INFO 1 --- [o-0.mongo:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server mongo-0.mongo:27017

com.mongodb.MongoSocketException: mongo-0.mongo: Try again
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:188) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.UnknownHostException: mongo-0.mongo: Try again
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
    at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
    at java.net.InetAddress.getByName(InetAddress.java:1077) ~[na:1.8.0_212]
    at com.mongodb.ServerAddress.getSocketAddress(ServerAddress.java:186) ~[mongodb-driver-core-3.8.2.jar:na]
    ... 5 common frames omitted

How to connect with the headless mongo service with my application. I tried using - name: spring.data.mongodb.host value: mongo-0.mongo // and value: mongo

-- JibinNajeeb
docker
kubernetes
spring
spring-boot

1 Answer

9/24/2019

You need to use the name of the service as hostname. In your example, it's mongo. I deployed mongo with your above YAML and I could successfully connect to it from another pod in the same namespace.

If you're running imageprocessor-app-backend in a different namespace then mongo, then you have to add the namespace where mongo is running to the hostname: mongo.<namespace>, e.g. mongo.mongo.

-- gnvk
Source: StackOverflow