Environment Docker version 1.6.2 MongoDB 3.0.4
The first step is to write the dockerfile and generate the mirror
Idea contains two dockerfile mirrors, one mongod, one mongos (responsible for routing in the cluster)
Write the Mongod dockerfile:
From Ubuntu:14.04run apt-key adv--keyserver hkp://keyserver.ubuntu.com:80--recv 7f0ceb10env MONGO_MAJOR 3.0RUN echo "de b Http://repo.mongodb.org/apt/debian wheezy/mongodb-org/$MONGO _major main ">/etc/apt/sources.list.d/ mongodb-org.list# Install mongodbrun apt-get updaterun sudo apt-get install-y mongodb-org=3.0.4 mongodb-org-server= 3.0.4 mongodb-org-shell=3.0.4 mongodb-org-mongos=3.0.4 mongodb-org-tools=3.0.4# Create the MongoDB data directoryrun Mkdir-p/data/dbexpose 27017ENTRYPOINT ["Usr/bin/mongod"]
Build the image above Dockerfile:
sudo docker build-t robin/mongod:master.
-t specifies the warehouse address to put, the colon after the master is the tag, and the next point represents the current directory (the directory containing the dockerfile that was just written)
Write the MONGOs dockerfile:
From Robin/mongod:masterexpose 27017ENTRYPOINT ["Usr/bin/mongos"]
To build the image:
sudo docker build-t robin/mongos:master.
The second step starts the Docker container required for the MongoDB cluster:
Create a replica set 1
Docker run--name rs1_srv1-p 21117:27017-d robin/mongod:master--noprealloc--smallfiles--replset rs1docker Run--name Rs1_srv2-p 21217:27017-d robin/mongod:master--noprealloc--smallfiles--replset rs1docker run--name rs1_srv3-p 21317: 27017-d robin/mongod:master--noprealloc--smallfiles--replset rs1
-P maps the ports inside the host machine and container. -D indicates Deamon mode runs.
--smallfile reduce the size of the initialization data file and limit the data file to 512M maximum (reduces the initial size for data files and limits the maximum size to megabytes ).
--noprealloc The main purpose is to save the hard disk, prohibit the pre-allocated hard disk. Production environments do not use this option, resulting in degraded performance and disk fragmentation.
Create a replica set 2
Docker run--name rs2_srv1-p 22117:27017-d robin/mongod:master--noprealloc--smallfiles--replset rs2docker Run--name Rs2_srv2-p 22217:27017-d robin/mongod:master--noprealloc--smallfiles--replset rs2docker run--name rs2_srv3-p 22317: 27017-d robin/mongod:master--noprealloc--smallfiles--replset rs2
Creating a Configuration Container
Docker run--name cfg1-p 20117:27017-d robin/mongod:master--noprealloc--smallfiles--configsvr--dbpath/data/db--po RT 27017docker Run--name cfg2-p 20217:27017-d robin/mongod:master--noprealloc--smallfiles--configsvr--dbpath/data /db--port 27017docker Run--name cfg3-p 20317:27017-d robin/mongod:master--noprealloc--smallfiles--configsvr--DBPA th/data/db--port 27017
Create MONGO Router
Locate the IP and port corresponding to the configuration container
Use Mac Boot2docker +virtualbox Be sure to note that the host's IP is VirtualBox and not native
Run the command below to get the container host machine IP
Boot2docker IP
Docker run--name mongos_router-p 27017:27017-d robin/mongos:master--configdb < host ip>:20117,< host ip>:20217, < host ip>:20317--port 27017
Only one routing service is created here, so there is a single point of failure and you can create three to solve the problem.
Step three configure replica sets and shards
Configuring replica Sets 1
Connect to Rs1_svr1mongo < host ip>:21117//configuration Replica set rs.initiate (); Rs.add ("< host ip>:21217"); Rs.add ("< host ip> : 21317 "); Rs.status (); Fix hostname of primary.cfg = rs.conf () Cfg.members[0].host = "< host ip>:21117"; Rs.reconfig (CFG); Rs.status ();// The above command executes one by one
Configuring Replica Sets 2
Connect to Rs2_svr1mongo < host ip>:22117//configuration Replica set rs.initiate (); Rs.add ("< host ip>:22217"); Rs.add ("< host ip> : 22317 "); Rs.status (); Fix hostname of primary.cfg = rs.conf () Cfg.members[0].host = "< host ip>:22117"; Rs.reconfig (CFG); Rs.status ();// The above command executes one by one
Configuring shards
Connect to the routing server MONGO < host Ip>:27017sh.addshard ("rs1/< host ip>:27017"); Sh.addshard ("rs2/< host ip>:27017"); Sh.status ();
Then test OK
If you are using a Mac VirtualBox to build Docker, you may encounter a lack of MONGO allocated file space, the size of the disk will be OK
Forwarding label Source: http://my.oschina.net/robinyao/blog/469431
Using Docker to deploy MongoDB clusters--shards and replica sets