How to set-up Mongo replica set on Kubernetes?

12/24/2015

I'm trying set-up a three node Mongo replica set on Kubernetes. My thoughts is start three pods, with Mongo in each one, when the pod starts, it automatically sets up the replica set, so I need to modify the Mongo image Dockerfile to execute some command in Mongo shell.
But I am stuck at executing the command rs.initiate(). If I just use the command rs.initiate() without parameters it works, but when adding parameters then it occurs error like

rs.initiate({ "_id": "rs0", "members" : [ { "_id" : 0, "host" : "172.18.248.87:27017" } ]})

But when executing the pod and using the same command with parameters, it works. The same situation with the command rs.add() . How to resolve this?

Plus: in my Dockerfile I use mongo admin --port 27017 --eval "rs.initiate()" to execute the command .

-- henry
docker
kubernetes
mongodb

1 Answer

12/25/2015

You should check hosts file (hostname) in pod and add :

ip_address mongoslave

ip_address mongomaster

For example :

172.18.248.87 mongomaster

Then you add parametters:

rs.initiate()

rsconf = { _id: "rs0", members: [ { _id: 0, host: "mongomaster:27017" } ] }

rs.reconfig(rsconf, {force: true})

-- Thanh Nguyen Van
Source: StackOverflow