[关闭]
@bergus 2015-12-30T17:10:13.000000Z 字数 10024 阅读 2187

搭建基于Docker的MongoDB复制集群环境

docker mongodb


ygeR
TEAM ZWT
03-73-233357
12-31-2015
ABJUK9 XZ5A9A Z95XYV Q6VEKJ ACKW69 WTBH95 8JMFV6 5K9BUW

一步一步教你搭建基于Docker的MongoDB复制集群环境

1.安装docker
2.创建MongoDB的Image
3.搭建MongoDB的集群

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中。

1.Ubuntu14.04安装docker
参考文档 http://docs.docker.com/installation/ubuntulinux/
参考文档 http://docs.docker.com/mac/started/

  1. linuxidc@linuxidc:~$ wget -qO-https://get.docker.com/ | sh
  2. linuxidc@linuxidc:~$ sudo usermod -aG docker pc
  3. linuxidc@linuxidc:~$ sudo reboot

如果是其他(更早)版本的Linux参考
安装Docker使用apt-get命令:

  1. $ apt-get install docker.

启动服务和守护进程

  1. $ service docker.io status
  2. $ service docker.io start

创建软连接:ln -sf /usr/bin/docker.io /usr/local/bin/docker
如没有提示错误则说明你已经在Ubuntu14.04上面快速安装Docker成功了。

卸载Uninstallation

  1. $ sudo apt-get purge lxc-docker

To uninstall the Docker package and dependencies that are no longer needed:

  1. $ sudo apt-get autoremove --purge lxc-docker

2.创建MongoDB的Image

  1. linuxidc@linuxidc:~$ docker pull ubuntu:14.04
  2. linuxidc@linuxidc:~$ sudo docker images
  3. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  4. ubuntu 14.04 6d4946999d4f 3 weeks ago 188.3 MB
  5. linuxidc@linuxidc:~$

编写dockerfile

  1. linuxidc@linuxidc:~$ vim Dockerfile

Dockerfile内容如下

  1. #VERSION 0.1.0
  2. FROM ubuntu:14.04
  3. #Install some
  4. RUN apt-get clean
  5. RUN apt-get update
  6. RUN apt-get install -y g++
  7. RUN apt-get install -y openssh-server
  8. RUN mkdir -p /var/run/sshd
  9. #open port 22
  10. EXPOSE 22
  11. #CMD ["/usr/sbin/sshd", "-D"]
  12. RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  13. ENV MONGO_MAJOR 3.0
  14. RUN echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/$MONGO_MAJOR main" > /etc/apt/sources.list.d/mongodb-org.list
  15. # Install MongoDB
  16. RUN apt-get update
  17. RUN 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
  18. # Create the MongoDB data directory
  19. RUN mkdir -p /data/db
  20. #open port 27017
  21. EXPOSE 27017
  22. ENTRYPOINT ["usr/bin/mongod"]

写好了Dockerfile 就可以生成带有mongodb的image :后面是flag .表示当前路径的Dockerfile

  1. linuxidc@linuxidc:~$ sudo docker build -t pc/mongos:master .

再次查看一下docker image

  1. linuxidc@linuxidc:~$ sudo docker images
  2. REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  3. pc/mongod master 6fcc1b62e091 54 minutes ago 542 MB
  4. ubuntu 14.04 6d4946999d4f 4 weeks ago 188.3 MB

3.搭建MongoDB的集群
下面我们就启动3个基于mongod:master的container

  1. linuxidc@linuxidc:~$ sudo docker run --name rs_server1 -p 21117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1
  2. 操作成功之后返回的就是容器的id
  3. 6306bf3057f8ebca79d7bd130ba35e260da7177925a85a1c9a3d5d21c551e0aa

后面两个容器同理

  1. linuxidc@linuxidc:~$ sudo docker run --name rs_server2 -p 22117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1
  2. b60c977313e1b6143c0346e1c6138c3ad4831818f6349fc98181fa76edbd2eff
  3. linuxidc@linuxidc:~$ sudo docker run --name rs_server3 -p 23117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1
  4. cebebc142a1bfed7162a06441ad841f4173c3f71c97f3dded8d5c3833ea9fa78

选择一个容器执行一下ifconfig 查看ip 一般都是同网段中递增的

  1. linuxidc@linuxidc:~$ sudo docker exec 6306bf3057f8ebca79d7bd130ba35e260da7177925a85a1c9a3d5d21c551e0aa ifconfig
  2. eth0 Link encap:Ethernet HWaddr 02:42:ac:11:00:01
  3. inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
  4. inet6 addr: fe80::42:acff:fe11:1/64 Scope:Link
  5. UP BROADCAST RUNNING MTU:1500 Metric:1
  6. RX packets:26 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:0
  9. RX bytes:2124 (2.1 KB) TX bytes:828 (828.0 B)
  10. lo Link encap:Local Loopback
  11. inet addr:127.0.0.1 Mask:255.0.0.0
  12. inet6 addr: ::1/128 Scope:Host
  13. UP LOOPBACK RUNNING MTU:65536 Metric:1
  14. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  15. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  16. collisions:0 txqueuelen:0
  17. 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

查看运行的容器

  1. linuxidc@linuxidc:~$ sudo docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. cebebc142a1b 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
  4. 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
  5. 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

  1. linuxidc@linuxidc:~$ mongo --port 21117
  2. MongoDB shell version: 3.0.4
  3. connecting to: 127.0.0.1:21117/test

开始配置 复制集的成员信息

  1. > myconf = {"_id":"rs1","members":[
  2. ... {"_id":0,"host":"172.17.0.1:27017"},
  3. ... {"_id":1,"host":"172.17.0.2:27017"},
  4. ... {"_id":2,"host":"172.17.0.3:27017"}
  5. ... ]
  6. ... }
  7. {
  8. "_id" : "rs1",
  9. "members" : [
  10. {
  11. "_id" : 0,
  12. "host" : "172.17.0.1:27017"
  13. },
  14. {
  15. "_id" : 1,
  16. "host" : "172.17.0.2:27017"
  17. },
  18. {
  19. "_id" : 2,
  20. "host" : "172.17.0.3:27017"
  21. }
  22. ]
  23. }

初始化同步复制集群

  1. > rs.initiate(myconf)
  2. { "ok" : 1 }

查看当前复制集的节点信息

  1. rs1:OTHER> rs.isMaster()
  2. {
  3. "setName" : "rs1",
  4. "setVersion" : 1,
  5. "ismaster" : true,
  6. "secondary" : false,
  7. "hosts" : [
  8. "172.17.0.1:27017",
  9. "172.17.0.2:27017",
  10. "172.17.0.3:27017"
  11. ],
  12. "primary" : "172.17.0.1:27017",
  13. "me" : "172.17.0.1:27017",
  14. "electionId" : ObjectId("55a5cc505f7c25c88ad58e77"),
  15. "maxBsonObjectSize" : 16777216,
  16. "maxMessageSizeBytes" : 48000000,
  17. "maxWriteBatchSize" : 1000,
  18. "localTime" : ISODate("2015-07-15T02:58:30.831Z"),
  19. "maxWireVersion" : 3,
  20. "minWireVersion" : 0,
  21. "ok" : 1
  22. }

可以看出连接到的容器ip是172.17.0.1
也可以看见谁是主节点

  1. rs1:PRIMARY> rs.status()
  2. {
  3. "set" : "rs1",
  4. "date" : ISODate("2015-07-15T02:58:44.390Z"),
  5. "myState" : 1,
  6. "members" : [
  7. {
  8. "_id" : 0,
  9. "name" : "172.17.0.1:27017",
  10. "health" : 1,
  11. "state" : 1,
  12. "stateStr" : "PRIMARY",
  13. "uptime" : 540,
  14. "optime" : Timestamp(1436929097, 1),
  15. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  16. "electionTime" : Timestamp(1436929104, 1),
  17. "electionDate" : ISODate("2015-07-15T02:58:24Z"),
  18. "configVersion" : 1,
  19. "self" : true
  20. },
  21. {
  22. "_id" : 1,
  23. "name" : "172.17.0.2:27017",
  24. "health" : 1,
  25. "state" : 2,
  26. "stateStr" : "SECONDARY",
  27. "uptime" : 57,
  28. "optime" : Timestamp(1436929097, 1),
  29. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  30. "lastHeartbeat" : ISODate("2015-07-15T02:58:44.133Z"),
  31. "lastHeartbeatRecv" : ISODate("2015-07-15T02:58:44.133Z"),
  32. "pingMs" : 0,
  33. "configVersion" : 1
  34. },
  35. {
  36. "_id" : 2,
  37. "name" : "172.17.0.3:27017",
  38. "health" : 1,
  39. "state" : 2,
  40. "stateStr" : "SECONDARY",
  41. "uptime" : 57,
  42. "optime" : Timestamp(1436929097, 1),
  43. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  44. "lastHeartbeat" : ISODate("2015-07-15T02:58:43.169Z"),
  45. "lastHeartbeatRecv" : ISODate("2015-07-15T02:58:44.134Z"),
  46. "pingMs" : 0,
  47. "lastHeartbeatMessage" : "could not find member to sync from",
  48. "configVersion" : 1
  49. }
  50. ],
  51. "ok" : 1
  52. }
  53. rs1:PRIMARY> rs.conf()
  54. {
  55. "_id" : "rs1",
  56. "version" : 1,
  57. "members" : [
  58. {
  59. "_id" : 0,
  60. "host" : "172.17.0.1:27017",
  61. "arbiterOnly" : false,
  62. "buildIndexes" : true,
  63. "hidden" : false,
  64. "priority" : 1,
  65. "tags" : {
  66. },
  67. "slaveDelay" : 0,
  68. "votes" : 1
  69. },
  70. {
  71. "_id" : 1,
  72. "host" : "172.17.0.2:27017",
  73. "arbiterOnly" : false,
  74. "buildIndexes" : true,
  75. "hidden" : false,
  76. "priority" : 1,
  77. "tags" : {
  78. },
  79. "slaveDelay" : 0,
  80. "votes" : 1
  81. },
  82. {
  83. "_id" : 2,
  84. "host" : "172.17.0.3:27017",
  85. "arbiterOnly" : false,
  86. "buildIndexes" : true,
  87. "hidden" : false,
  88. "priority" : 1,
  89. "tags" : {
  90. },
  91. "slaveDelay" : 0,
  92. "votes" : 1
  93. }
  94. ],
  95. "settings" : {
  96. "chainingAllowed" : true,
  97. "heartbeatTimeoutSecs" : 10,
  98. "getLastErrorModes" : {
  99. },
  100. "getLastErrorDefaults" : {
  101. "w" : 1,
  102. "wtimeout" : 0
  103. }
  104. }
  105. }

另外开一个终端 连接到第二个容器

  1. linuxidc@linuxidc:~$ mongo --port 22117
  2. MongoDB shell version: 3.0.4
  3. rs1:SECONDARY> rs.isMaster()
  4. {
  5. "setName" : "rs1",
  6. "setVersion" : 1,
  7. "ismaster" : false,
  8. "secondary" : true,
  9. "hosts" : [
  10. "172.17.0.1:27017",
  11. "172.17.0.2:27017",
  12. "172.17.0.3:27017"
  13. ],
  14. "primary" : "172.17.0.1:27017",
  15. "me" : "172.17.0.2:27017",
  16. "maxBsonObjectSize" : 16777216,
  17. "maxMessageSizeBytes" : 48000000,
  18. "maxWriteBatchSize" : 1000,
  19. "localTime" : ISODate("2015-07-15T02:59:27.121Z"),
  20. "maxWireVersion" : 3,
  21. "minWireVersion" : 0,
  22. "ok" : 1
  23. }

可以看到主节点的信息 和上个容器的信息一致

  1. rs1:SECONDARY> rs.status()
  2. {
  3. "set" : "rs1",
  4. "date" : ISODate("2015-07-15T03:14:10.512Z"),
  5. "myState" : 2,
  6. "members" : [
  7. {
  8. "_id" : 0,
  9. "name" : "172.17.0.1:27017",
  10. "health" : 1,
  11. "state" : 1,
  12. "stateStr" : "PRIMARY",
  13. "uptime" : 981,
  14. "optime" : Timestamp(1436929097, 1),
  15. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  16. "lastHeartbeat" : ISODate("2015-07-15T03:14:10.478Z"),
  17. "lastHeartbeatRecv" : ISODate("2015-07-15T03:14:10.478Z"),
  18. "pingMs" : 0,
  19. "electionTime" : Timestamp(1436929104, 1),
  20. "electionDate" : ISODate("2015-07-15T02:58:24Z"),
  21. "configVersion" : 1
  22. },
  23. {
  24. "_id" : 1,
  25. "name" : "172.17.0.2:27017",
  26. "health" : 1,
  27. "state" : 2,
  28. "stateStr" : "SECONDARY",
  29. "uptime" : 1452,
  30. "optime" : Timestamp(1436929097, 1),
  31. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  32. "infoMessage" : "could not find member to sync from",
  33. "configVersion" : 1,
  34. "self" : true
  35. },
  36. {
  37. "_id" : 2,
  38. "name" : "172.17.0.3:27017",
  39. "health" : 1,
  40. "state" : 2,
  41. "stateStr" : "SECONDARY",
  42. "uptime" : 981,
  43. "optime" : Timestamp(1436929097, 1),
  44. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  45. "lastHeartbeat" : ISODate("2015-07-15T03:14:09.523Z"),
  46. "lastHeartbeatRecv" : ISODate("2015-07-15T03:14:09.523Z"),
  47. "pingMs" : 0,
  48. "configVersion" : 1
  49. }
  50. ],
  51. "ok" : 1
  52. }
  53. linuxidc@linuxidc:~$ mongo --port 23117
  54. MongoDB shell version: 3.0.4
  55. connecting to: 127.0.0.1:23117/test
  56. rs1:SECONDARY> rs.status()
  57. {
  58. "set" : "rs1",
  59. "date" : ISODate("2015-07-15T03:13:25.167Z"),
  60. "myState" : 2,
  61. "members" : [
  62. {
  63. "_id" : 0,
  64. "name" : "172.17.0.1:27017",
  65. "health" : 1,
  66. "state" : 1,
  67. "stateStr" : "PRIMARY",
  68. "uptime" : 936,
  69. "optime" : Timestamp(1436929097, 1),
  70. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  71. "lastHeartbeat" : ISODate("2015-07-15T03:13:24.479Z"),
  72. "lastHeartbeatRecv" : ISODate("2015-07-15T03:13:23.531Z"),
  73. "pingMs" : 0,
  74. "electionTime" : Timestamp(1436929104, 1),
  75. "electionDate" : ISODate("2015-07-15T02:58:24Z"),
  76. "configVersion" : 1
  77. },
  78. {
  79. "_id" : 1,
  80. "name" : "172.17.0.2:27017",
  81. "health" : 1,
  82. "state" : 2,
  83. "stateStr" : "SECONDARY",
  84. "uptime" : 936,
  85. "optime" : Timestamp(1436929097, 1),
  86. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  87. "lastHeartbeat" : ISODate("2015-07-15T03:13:23.507Z"),
  88. "lastHeartbeatRecv" : ISODate("2015-07-15T03:13:23.506Z"),
  89. "pingMs" : 0,
  90. "configVersion" : 1
  91. },
  92. {
  93. "_id" : 2,
  94. "name" : "172.17.0.3:27017",
  95. "health" : 1,
  96. "state" : 2,
  97. "stateStr" : "SECONDARY",
  98. "uptime" : 1392,
  99. "optime" : Timestamp(1436929097, 1),
  100. "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
  101. "configVersion" : 1,
  102. "self" : true
  103. }
  104. ],
  105. "ok" : 1
  106. }

如果在从节点此时查看数据库会出错 ,信息显示不是主节点

  1. rs1:SECONDARY> show dbs;
  2. ××××××××××××××××××××××××××××××××××××

设置从节点可以读

  1. rs1:SECONDARY> db.getMongo().setSlaveOk();
  2. rs1:SECONDARY> show dbs;
  3. local 1.031GB

打开先前与主节点的终端 插入点数据

  1. rs1:PRIMARY> show dbs;
  2. local 1.031GB
  3. rs1:PRIMARY> use tt;
  4. switched to db tt
  5. rs1:PRIMARY> db.user.insert({"name":"mongodb is greate!!!!"})
  6. WriteResult({ "nInserted" : 1 })

打开与备份节点的终端

  1. rs1:SECONDARY> show dbs;
  2. local 1.031GB
  3. tt 0.031GB
  4. rs1:SECONDARY> use tt
  5. switched to db tt
  6. rs1:SECONDARY> show collections;
  7. system.indexes
  8. user

多了个数据库 ,还多了条文档

  1. rs1:SECONDARY> db.user.find()
  2. { "_id" : ObjectId("55a5d1390d49c20962937b52"), "name" : "mongodb is greate!!!!" }

MongoDB常用操作命令整理

MongoDB 3.0 正式版发布下载

CentOS编译安装MongoDB

CentOS 编译安装 MongoDB与mongoDB的php扩展

CentOS 6 使用 yum 安装MongoDB及服务器端配置

Ubuntu 13.04下安装MongoDB2.4.3

MongoDB入门必读(概念与实战并重)

Ubunu 14.04下MongoDB的安装指南

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios监控MongoDB分片集群服务实战

基于CentOS 6.5操作系统搭建MongoDB服务
本文永久更新链接地址

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注