0

I am trying to create a mongodb cluster based on this resource. The service host name is not available inside the container. I followed the following steps:

Creating virtual machines:

docker-machine create -d virtualbox node1
docker-machine create -d virtualbox node2
docker-machine create -d virtualbox node3

Opening ports 2376, 7946, 4789 in all the nodes & restarted docker, using following commands.

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 2376 -j ACCEPT
iptables -A INPUT -p tcp --dport 2377 -j ACCEPT
iptables -A INPUT -p udp --dport 2377 -j ACCEPT
iptables -A INPUT -p tcp --dport 7946 -j ACCEPT
iptables -A INPUT -p udp --dport 7946 -j ACCEPT
iptables -A INPUT -p udp --dport 4789 -j ACCEPT

iptables-save > /etc/iptables.conf
/etc/init.d/docker restart

Initialization of swarm & adding nodes to it

Node 1:
docker swarm init --listen-addr 192.168.99.101 --advertise-addr 192.168.99.101
Node 2: 
docker swarm join --token <TOKEN> --listen-addr 192.168.99.102 --advertise-addr 192.168.99.102 192.168.99.101:2377
Node 3: 
docker swarm join --token <TOKEN> --listen-addr 192.168.99.103 --advertise-addr 192.168.99.103 192.168.99.101:2377

On Manager node, to label each node:

docker node update --label-add mongo.replica=1 $(docker node ls -q -f name=node1)
docker node update --label-add mongo.replica=2 $(docker node ls -q -f name=node2)
docker node update --label-add mongo.replica=3 $(docker node ls -q -f name=node3)

Creating network for Mongo Service Cluster:

docker network create --driver overlay --internal mongo

Creating Mongo Service:

docker service create --replicas 1 --network mongo --mount type=volume,source=mongodata1,target=/data/db --mount type=volume,source=mongoconfig1,target=/data/configdb --constraint 'node.labels.mongo.replica == 1' --name mongo1 mongo:3.2 mongod --replSet example
docker service create --replicas 1 --network mongo --mount type=volume,source=mongodata2,target=/data/db --mount type=volume,source=mongoconfig2,target=/data/configdb --constraint 'node.labels.mongo.replica == 2' --name mongo2 mongo:3.2 mongod --replSet example
docker service create --replicas 1 --network mongo --mount type=volume,source=mongodata3,target=/data/db --mount type=volume,source=mongoconfig3,target=/data/configdb --constraint 'node.labels.mongo.replica == 3' --name mongo3 mongo:3.2 mongod --replSet example

Initializing replica set:

docker exec -it $(docker ps -qf label=com.docker.swarm.service.name=mongo1) 
\ mongo --eval 'rs.initiate({ _id: "example", 
\ members: [{ _id: 1, host: "mongo1:27017" }, 
\ { _id: 2, host: "mongo2:27017" }, 
\ { _id: 3, host: "mongo3:27017" }], 
\ settings: { getLastErrorDefaults: { w: "majority", wtimeout: 30000 }}})'

I get following error for the last command:

MongoDB shell version: 3.2.21
connecting to: test
{
    "ok" : 0,
    "errmsg" : "No host described in new configuration 1 for replica set example maps to this node",
    "code" : 93
}

After some checks, I learnt that the host names mongo1, mongo2, mongo3 are not available in the container.

I am not sure where I am doing wrong. Any help would be appreciative to resolve this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.