<p>I'm running locally a statefulset replica of MongoDB on Minikube and I'm trying to connect to this one using Spring Mongo DB.</p>
<p>In the configuration properties I have:</p>
<p><code>spring.data.mongodb.uri=mongodb://mongod-0.mongo:27017/test</code></p>
<p>The problem is that, when I try to deploy the application locally, I receive this error:</p>
<p><code>com.mongodb.MongoSocketException: mongod-0.mongo: Name or service not known</code></p>
<p>Looks like I can't enrich the deployed replica but I don't know why.</p>
<p>The statefulset, the service and the pods are running correctly.</p>
<p>Here is the configuration of them:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongod
spec:
  serviceName: mongodb-service
  replicas: 1
  selector:
    matchLabels:
      role: mongo
  template:
    metadata:
      labels:
        role: mongo
        environment: test
        replicaset: MainRepSet
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongod-container
          image: mongo
          command:
            - &quot;mongod&quot;
            - &quot;--bind_ip&quot;
            - &quot;0.0.0.0&quot;
            - &quot;--replSet&quot;
            - &quot;MainRepSet&quot;
          resources:
            requests:
              cpu: 0.2
              memory: 200Mi
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongodb-persistent-storage-claim
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
        name: mongodb-persistent-storage-claim
        annotations:
          volume.beta.kubernetes.io/storage-class: &quot;standard&quot;
      spec:
        accessModes: [ &quot;ReadWriteOnce&quot; ]
        resources:
          requests:
            storage: 1Gi 
</code></pre>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
  labels:
    name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
</code></pre>
<p>Someone has any idea how I could connect my application to the replica?</p>

liviubiur
<p>it's due to your service name is : <strong>mongodb-service</strong></p>
<p>You have to use always <strong>service name</strong> in connection strings everywhere mostly.</p>
<p>Traffic flow goes like :</p>
<pre><code>Service -&gt; deployment or statefulsets -&gt; PODs
</code></pre>
<p>now you have exposed the service name and port(27017) and K8s will auto manage the service name into internal DNS, so you can use the name for the connection string.</p>
<p>Your application will be only able to connect on <strong>service name</strong> if running on the same K8s cluster.</p>
<p><strong>Example</strong> :</p>
<p><code>spring.data.mongodb.uri=mongodb://mongo-service:27017/test</code></p>
<p>You can follow this article also for refrence. : <a href="https://medium.com/geekculture/how-to-deploy-spring-boot-and-mongodb-to-kubernetes-minikube-71c92c273d5e" rel="nofollow noreferrer">https://medium.com/geekculture/how-to-deploy-spring-boot-and-mongodb-to-kubernetes-minikube-71c92c273d5e</a></p>
