一步一步教你搭建基於docker的MongoDB複製叢集環境
1.安裝docker
2.建立MongoDB的Image
3.搭建MongoDB的叢集
本文——-鏈—–接
http://blog.csdn.net/pddddd/article/details/46893593
Docker 是一個開源的應用程式容器引擎,讓開發人員可以打包他們的應用以及依賴包到一個可移植的容器中。
1.ubuntu14.04安裝docker
參考文檔 http://docs.docker.com/installation/ubuntulinux/參考文檔 http://docs.docker.com/mac/started/pc@pc-ThinkPad:~$ wget -qO-https://get.docker.com/ | shpc@pc-ThinkPad:~$ sudo usermod -aG docker pcpc@pc-ThinkPad:~$ sudo reboot如果是其他(更早)版本的Linux參考安裝Docker使用apt-get命令: $ apt-get install docker.io啟動服務和守護進程 $ service docker.io status $ service docker.io start建立軟串連:ln -sf /usr/bin/docker.io /usr/local/bin/docker 如沒有提示錯誤則說明你已經在Ubuntu14.04上面快速安裝Docker成功了。卸載Uninstallation $ sudo apt-get purge lxc-dockerTo uninstall the Docker package and dependencies that are no longer needed: $ sudo apt-get autoremove --purge lxc-docker
2.建立MongoDB的Image
pc@pc-ThinkPad:~$ docker pull ubuntu:14.04pc@pc-ThinkPad:~$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEubuntu 14.04 6d4946999d4f 3 weeks ago 188.3 MBpc@pc-ThinkPad:~$
編寫dockerfilepc@pc-ThinkPad:~$ vim Dockerfile
Dockerfile內容如下
#VERSION 0.1.0FROM ubuntu:14.04#Install someRUN apt-get cleanRUN apt-get updateRUN apt-get install -y g++RUN apt-get install -y openssh-server RUN mkdir -p /var/run/sshd#open port 22EXPOSE 22#CMD ["/usr/sbin/sshd", "-D"]RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10ENV MONGO_MAJOR 3.0RUN echo "deb 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/db#open port 27017 EXPOSE 27017ENTRYPOINT ["usr/bin/mongod"]
寫好了Dockerfile 就可以產生帶有mongodb的image :後面是flag .表示當前路徑的Dockerfilepc@pc-ThinkPad:~$ sudo docker build -t pc/mongos:master .再次查看一下docker imagepc@pc-ThinkPad:~$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEpc/mongod master 6fcc1b62e091 54 minutes ago 542 MBubuntu 14.04 6d4946999d4f 4 weeks ago 188.3 MB
3.搭建MongoDB的叢集
下面我們就啟動3個基於mongod:master的container
pc@pc-ThinkPad:~$ sudo docker run --name rs_server1 -p 21117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1 操作成功之後返回的就是容器的id6306bf3057f8ebca79d7bd130ba35e260da7177925a85a1c9a3d5d21c551e0aa後面兩個容器同理pc@pc-ThinkPad:~$ sudo docker run --name rs_server2 -p 22117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1 b60c977313e1b6143c0346e1c6138c3ad4831818f6349fc98181fa76edbd2effpc@pc-ThinkPad:~$ sudo docker run --name rs_server3 -p 23117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1 cebebc142a1bfed7162a06441ad841f4173c3f71c97f3dded8d5c3833ea9fa78選擇一個容器執行一下ifconfig 查看ip 一般都是同網段中遞增的pc@pc-ThinkPad:~$ sudo docker exec 6306bf3057f8ebca79d7bd130ba35e260da7177925a85a1c9a3d5d21c551e0aa ifconfigeth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:01 inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe11:1/64 Scope:Link UP BROADCAST RUNNING MTU:1500 Metric:1 RX packets:26 errors:0 dropped:0 overruns:0 frame:0 TX packets:10 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2124 (2.1 KB) TX bytes:828 (828.0 B)lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)可以想象 3個容器的ip應該是172.17.0.1 172.17.0.2 172.17.0.3查看啟動並執行容器pc@pc-ThinkPad:~$ sudo docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMEScebebc142a1b pc/mongod:master "usr/bin/mongod --no About a minute ago Up About a minute 22/tcp, 0.0.0.0:23117->27017/tcp rs_server3 b60c977313e1 pc/mongod:master "usr/bin/mongod --no About a minute ago Up About a minute 22/tcp, 0.0.0.0:22117->27017/tcp rs_server2 6306bf3057f8 pc/mongod:master "usr/bin/mongod --no About a minute ago Up About a minute 22/tcp, 0.0.0.0:21117->27017/tcp rs_server1 從上述資訊可以看出原生21117 22117 23117 映射到docker的容器的連接埠上去了使用本機mongo用戶端串連到21117 pc@pc-ThinkPad:~$ mongo --port 21117MongoDB shell version: 3.0.4connecting to: 127.0.0.1:21117/test開始配置 複製集的成員資訊> myconf = {"_id":"rs1","members":[... {"_id":0,"host":"172.17.0.1:27017"},... {"_id":1,"host":"172.17.0.2:27017"},... {"_id":2,"host":"172.17.0.3:27017"}... ]... }{ "_id" : "rs1", "members" : [ { "_id" : 0, "host" : "172.17.0.1:27017" }, { "_id" : 1, "host" : "172.17.0.2:27017" }, { "_id" : 2, "host" : "172.17.0.3:27017" } ]}初始化同步複製叢集> rs.initiate(myconf){ "ok" : 1 }查看當前複製集的節點資訊rs1:OTHER> rs.isMaster(){ "setName" : "rs1", "setVersion" : 1, "ismaster" : true, "secondary" : false, "hosts" : [ "172.17.0.1:27017", "172.17.0.2:27017", "172.17.0.3:27017" ], "primary" : "172.17.0.1:27017", "me" : "172.17.0.1:27017", "electionId" : ObjectId("55a5cc505f7c25c88ad58e77"), "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-15T02:58:30.831Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1}可以看出串連到的容器ip是172.17.0.1 也可以看見誰是主節點rs1:PRIMARY> rs.status(){ "set" : "rs1", "date" : ISODate("2015-07-15T02:58:44.390Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "172.17.0.1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 540, "optime" : Timestamp(1436929097, 1), "optimeDate" : ISODate("2015-07-15T02:58:17Z"), "electionTime" : Timestamp(1436929104, 1), "electionDate" : ISODate("2015-07-15T02:58:24Z"), "configVersion" : 1, "self" : true }, { "_id" : 1, "name" : "172.17.0.2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 57, "optime" : Timestamp(1436929097, 1), "optimeDate" : ISODate("2015-07-15T02:58:17Z"), "lastHeartbeat" : ISODate("2015-07-15T02:58:44.133Z"), "lastHeartbeatRecv" : ISODate("2015-07-15T02:58:44.133Z"), "pingMs" : 0, "configVersion" : 1 }, { "_id" : 2, "name" : "172.17.0.3:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 57, "optime" : Timestamp(1436929097, 1), "optimeDate" : ISODate("2015-07-15T02:58:17Z"), "lastHeartbeat" : ISODate("2015-07-15T02:58:43.169Z"), "lastHeartbeatRecv" : ISODate("2015-07-15T02:58:44.134Z"), "pingMs" : 0, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 1 } ], "ok" : 1}rs1:PRIMARY> rs.conf(){ "_id" : "rs1", "version" : 1, "members" : [ { "_id" : 0, "host" : "172.17.0.1:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "172.17.0.2:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "172.17.0.3:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } }}另外開一個終端 串連到第二個容器pc@pc-ThinkPad:~$ mongo --port 22117MongoDB shell version: 3.0.4rs1:SECONDARY> rs.isMaster(){ "setName" : "rs1", "setVersion" : 1, "ismaster" : false, "secondary" : true, "hosts" : [ "172.17.0.1:27017", "172.17.0.2:27017", "172.17.0.3:27017" ], "primary" : "172.17.0.1:27017", "me" : "172.17.0.2:27017", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-15T02:59:27.121Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1}可以看到主節點的資訊 和上個容器的資訊一致rs1:SECONDARY> rs.status(){ "set" : "rs1", "date" : ISODate("2015-07-15T03:14:10.512Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "172.17.0.1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 981, "optime" : Timestamp(1436929097, 1), "optimeDate" : ISODate("2015-07-15T02:58:17Z"), "lastHeartbeat" : ISODate("2015-07-15T03:14:10.478Z"), "lastHeartbeatRecv" : ISODate("2015-07-15T03:14:10.478Z"), "pingMs" : 0, "electionTime" : Timestamp(1436929104, 1), "electionDate" : ISODate("2015-07-15T02:58:24Z"), "configVersion" : 1 }, { "_id" : 1, "name" : "172.17.0.2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1452, "optime" : Timestamp(1436929097, 1), "optimeDate" : ISODate("2015-07-15T02:58:17Z"), "infoMessage" : "could not find member to sync from", "configVersion" : 1, "self" : true }, { "_id" : 2, "name" : "172.17.0.3:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY