Bind application pod and database pod in kubernetes

10/18/2017

We are trying to implement two pods one with mondodba dn another one with java application. And java application requires to be bind with mongodb. How we can bind db and app when they are running on two different pods and with different subnets.

-- krishna bh
docker
java
kubernetes
mongodb

1 Answer

10/18/2017

You may want to use service for your mongo pod. You need to add label e.g. name: mongo to pod and create a service:

apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  ports:
  - port: 27017
  selector:
    name: mongo

Then mondgo will be accessible from java application pod with mongo:27017 address.

For a quick experiment you may use kubectl expose pod _MONGO_POD_NAME_ --port=27017 --name=mongo

This tutorial may be handy as well.

-- Alex
Source: StackOverflow