码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-07-15 17:10:09      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:mongodb   docker   复制集   

一步一步教你搭建基于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/
pc@pc-ThinkPad:~$ wget -qO-https://get.docker.com/ | sh
pc@pc-ThinkPad:~$ sudo usermod -aG docker pc
pc@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-docker
To 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.04
pc@pc-ThinkPad:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              14.04               6d4946999d4f        3 weeks ago         188.3 MB
pc@pc-ThinkPad:~$ 
编写dockerfile
pc@pc-ThinkPad:~$ vim Dockerfile 

Dockerfile内容如下

#VERSION 0.1.0
FROM ubuntu:14.04

#Install some
RUN apt-get clean
RUN apt-get update
RUN apt-get install -y g++
RUN apt-get install -y openssh-server 
RUN mkdir -p /var/run/sshd

#open port 22
EXPOSE 22
#CMD ["/usr/sbin/sshd", "-D"]


RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
ENV MONGO_MAJOR 3.0
RUN echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/$MONGO_MAJOR main" > /etc/apt/sources.list.d/mongodb-org.list
# Install MongoDB
RUN apt-get update
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

# Create the MongoDB data directory
RUN mkdir -p /data/db

#open port 27017 
EXPOSE 27017
ENTRYPOINT ["usr/bin/mongod"]
写好了Dockerfile 就可以生成带有mongodb的image :后面是flag .表示当前路径的Dockerfile
pc@pc-ThinkPad:~$ sudo docker build -t pc/mongos:master .

再次查看一下docker image
pc@pc-ThinkPad:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
pc/mongod           master              6fcc1b62e091        54 minutes ago      542 MB
ubuntu              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 
操作成功之后返回的就是容器的id
6306bf3057f8ebca79d7bd130ba35e260da7177925a85a1c9a3d5d21c551e0aa

后面两个容器同理
pc@pc-ThinkPad:~$ sudo docker run --name rs_server2 -p 22117:27017 -d pc/mongod:master --noprealloc --smallfiles --replSet rs1 
b60c977313e1b6143c0346e1c6138c3ad4831818f6349fc98181fa76edbd2eff
pc@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 ifconfig
eth0      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 -a
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS              PORTS                              NAMES
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          
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 21117
MongoDB shell version: 3.0.4
connecting 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 22117
MongoDB shell version: 3.0.4

rs1: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",
            "uptime" : 981,
            "optime" : Timestamp(1436929097, 1),
            "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
            "lastHeartbeat" : ISODate("2015-07-15T03:14:09.523Z"),
            "lastHeartbeatRecv" : ISODate("2015-07-15T03:14:09.523Z"),
            "pingMs" : 0,
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

pc@pc-ThinkPad:~$ mongo --port 23117
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:23117/test
rs1:SECONDARY> rs.status()
{
    "set" : "rs1",
    "date" : ISODate("2015-07-15T03:13:25.167Z"),
    "myState" : 2,
    "members" : [
        {
            "_id" : 0,
            "name" : "172.17.0.1:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 936,
            "optime" : Timestamp(1436929097, 1),
            "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
            "lastHeartbeat" : ISODate("2015-07-15T03:13:24.479Z"),
            "lastHeartbeatRecv" : ISODate("2015-07-15T03:13:23.531Z"),
            "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" : 936,
            "optime" : Timestamp(1436929097, 1),
            "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
            "lastHeartbeat" : ISODate("2015-07-15T03:13:23.507Z"),
            "lastHeartbeatRecv" : ISODate("2015-07-15T03:13:23.506Z"),
            "pingMs" : 0,
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "172.17.0.3:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1392,
            "optime" : Timestamp(1436929097, 1),
            "optimeDate" : ISODate("2015-07-15T02:58:17Z"),
            "configVersion" : 1,
            "self" : true
        }
    ],
    "ok" : 1
}
如果在从节点此时查看数据库会出错 ,信息显示不是主节点
rs1:SECONDARY> show dbs;
××××××××××××××××××××××××××××××××××××

设置从节点可以读
rs1:SECONDARY> db.getMongo().setSlaveOk();
rs1:SECONDARY> show dbs;
local  1.031GB

打开先前与主节点的终端  插入点数据
rs1:PRIMARY> show dbs;
local  1.031GB

rs1:PRIMARY> use tt;
switched to db tt
rs1:PRIMARY> db.user.insert({"name":"mongodb is greate!!!!"})
WriteResult({ "nInserted" : 1 })

打开与备份节点的终端
rs1:SECONDARY> show dbs;
local  1.031GB
tt     0.031GB
rs1:SECONDARY> use tt
switched to db tt
rs1:SECONDARY> show collections;
system.indexes
user
多了个数据库 ,还多了条文档
rs1:SECONDARY> db.user.find()
{ "_id" : ObjectId("55a5d1390d49c20962937b52"), "name" : "mongodb is greate!!!!" }

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:mongodb   docker   复制集   

原文地址:http://blog.csdn.net/pddddd/article/details/46893593

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!