How to connect mongodb service by MONGO_URL when the db service is clusterIP?

8/29/2019

I separate a meteor app to be 2 containers: app and node. In the world of docker, the app can connect to the node successfully, but in kubernetes, I have difficulties.

My idea is first launching mongodb and creating the mongodb service, and then creating the app to connect the mongodb service, but I am not sure how to let app use MONGO_URL to connect to service's clusterIP.

So, I have a app-deployement showing bellow,

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: app
  name: mycloud
spec:
  replicas: 1
  selector:
    matchLabels:
     app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: app
        image: yufang/cloud_docker_app
        ports:
        - containerPort: 3000
        env:
        - name: MONGO_URL
          value: mongodb://localhost:27017/meteor # here comes the key point. How to specify the service's ip? or use the selector to specify the service's label?
        - name: PORT
          value: "3000"
       - name: ROOT_URL
          value: http://localhost

The service is described as bellow,

apiVersion: v1
kind: Service
metadata:
 name: mongo
 labels:
  name: mongo
spec:
 ports:
  - port: 27017
    targetPort: 27017
 clusterIP: None
 selector:
  app: mongo

Any ideas are appreciated.

-- Yu Fang
kubernetes
yaml

1 Answer

8/30/2019

If the mongodb and mycloud are in the same namespace, then you can use http://mongodb:27017

If they are in different namespaces then you can use FQDN http://mongodb.namespace.svc.cluster.local:27017

Refer: https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/

-- Tummala Dhanvi
Source: StackOverflow