Teach you step-by-step to build a Docker-based MongoDB replication cluster environment

Source: Internet
Author: User
Tags documentation install mongodb mkdir mongodb docker ps docker run
teach you step-by-step to build a Docker-based MongoDB replication cluster environment 1. Installing Docker 2. Create a MongoDB image 3. Building a MongoDB cluster


This article ——-chain--Connect
http://blog.csdn.net/pddddd/article/details/46893593

Docker is an open-source application container engine that allows developers to package their applications and dependencies into a portable container.

 

1.ubuntu14.04 Installing Docker

Reference documentation http://docs.docker.com/installation/ubuntulinux/
Reference documentation 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

If it is another (earlier) version of Linux reference
Install Docker using apt-get command:
    $ apt-get install docker.io
Start services and daemons
    $ service docker.io status
    $ service docker.io start
Create a soft link: ln -sf /usr/bin/docker.io / usr / local / bin / docker
    If there is no error, it means that you have successfully installed Docker on Ubuntu 14.04.

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. Create an Image of MongoDB
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: ~ $
Write dockerfile
pc @ pc-ThinkPad: ~ $ vim Dockerfile
Dockerfile content is as follows

#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"]
After writing the Dockerfile, you can generate an image with mongodb: followed by the flag. The Dockerfile representing the current path
pc @ pc-ThinkPad: ~ $ sudo docker build -t pc / mongos: master.

Look at the docker image again
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. Build a MongoDB cluster
Below we start 3 container based on mongod: master
pc @ pc-ThinkPad: ~ $ sudo docker run --name rs_server1 -p 21117: 27017 -d pc / mongod: master --noprealloc --smallfiles --replSet rs1
After successful operation, the id of the container is returned
6306bf3057f8ebca79d7bd130ba35e260da7177925a85a1c9a3d5d21c551e0aa

The latter two containers are the same
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

Select a container and execute ifconfig to check the ip. Generally, it is incremented in the same network segment
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)

It can be imagined that the ip of the three containers should be 172.17.0.1 172.17.0.2 172.17.0.3

View running containers
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

From the above information, it can be seen that the local 21117 22117 23117 is mapped to the port of the docker container

Connect to 21117 using the native mongo client
pc @ pc-ThinkPad: ~ $ mongo --port 21117
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:21117/test

Start to configure the member information of the replication set
> 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"
        }
    ]
}

Initialize the synchronous replication cluster
> rs.initiate (myconf)
{"ok": 1}

View the node information of the current replication set
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
}
It can be seen that the container ip connected to is 172.17.0.1
You can also see who is the master node
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
        }
    }
}


Open another terminal and connect to the second container
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
}
You can see that the information of the master node is consistent with the information of the previous container
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.