Detailed steps for building a MongoDB cluster using Docker

Source: Internet
Author: User
Tags mongodb prepare mongo shell docker run

This article will introduce how to use Docker to deploy a MongoDB cluster as follows:

MongoDB 2.6.5
Replica set)
Authentication
Persistent data to the local file system

First, you need to prepare three running Docker servers, which means you need to prepare a local Vagrant Box virtual machine with Docker installed (the system can use CoreOS) or AWS or other methods you like.

Step 1


You need to get the IP addresses of the three Docker servers and configure the following IP addresses to all servers. Execute the following command on each server (remember to replace the IP address ):

Root @ node *:/# export node1 = 10.11.32.174
Root @ node *:/# export node2 = 10.11.33.37
Root @ node *:/# export node3 = 10.11.31.176

Ideally, you do not need to do this. These IP addresses can be automatically configured through DNS. However, this is simpler. After all, this is only an installation test.

Step 2

Create a key file for each node. Run the following command on a server and copy the key file to the same location on the other two servers.

In this tutorial, I will put everything in the "/home/core" folder.

Root @ node *:/# mkdir-p/home/core
Root @ node *:/# cd/home/core
Root @ node *:/# openssl rand-base64 741> mongodb-keyfile
Root @ node *:/# chmod 600 mongodb-keyfile
Root @ node *:/# sudo chown 999 mongodb-keyfile

The owner of this key file is set to "999" because in the Docker container of MongoDB, this user must have the permission to operate the key file.

Step 3

Start the MongoDB container of node1 (the first Docker server. It starts a container without authentication mechanism, so we need to set a user.

Root @ node1:/# docker run -- name mongo \
-V/home/core/mongo-files/data:/data/db \
-V/home/core/mongo-files:/opt/keyfile \
-- Hostname = "node1.example.com "\
-P 27017: 27017 \
-D mongo: 2.6.5 -- smallfiles

Create an admin user. We can connect to the mongoDB container just started and enter an interactive shell environment.

Root @ node1: // # docker exec-it mongo/bin/bash

At this time, we will enter the MongoDB Docker container, and then we will open a mongo shell environment:

Root @ node1:/# mongo

The above command can open the mongo shell environment. After execution, you will see the following output:

MongoDB shell version: 2.6.5
Connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help ".
For more comprehensive documentation, see
Http://docs.mongodb.org/
Questions? Try the support group
Http://groups.google.com/group/mongodb-user
>

Switch to admin User:

> Use admin
Switched to db admin

Create a new site admin user

> Db. createUser ({
User: "siteUserAdmin ",
Pwd: "password ",
Roles: [{role: "userAdminAnyDatabase", db: "admin"}]
});

If the creation is successful, you will see the following successful information:

Successfully added user :{
"User": "siteUserAdmin ",
"Roles ":[
     {
"Role": "userAdminAnyDatabase ",
"Db": "admin"
     }
  ]
}

Create a root user:

> Db. createUser ({
User: "siteRootAdmin ",
Pwd: "password ",
Roles: [{role: "root", db: "admin"}]
});

You will see the following successful information:

Successfully added user :{
"User": "siteRootAdmin ",
"Roles ":[
        {
"Role": "root ",
"Db": "admin"
        }
    ]
}

We have created several users that we will use later. Now we will not exit the interactive shell environment (the mongo and Docker container environments ).

> Exit
Bye
Root @ node1:/# exit

Step 4

Stop the first MongoDB instance:

Root @ node1:/# docker stop mongo

Step 5


This time, the key file is used to start the first MongoDB instance (still operating on node1 ).

Root @ node1:/# docker rm mongo
Root @ node1:/# docker run \
-- Name mongo \
-V/home/core/mongo-files/data:/data/db \
-V/home/core/mongo-files:/opt/keyfile \
-- Hostname = "node1.example.com "\
-- Add-host node1.example.com: ${node1 }\
-- Add-host node2.example.com: ${node2 }\
-- Add-host node3.example.com: ${node3 }\
-P 27017: 27017-d mongo: 2.6.5 \
-- Smallfiles \
-- KeyFile/opt/keyfile/mongodb-keyfile \
-- ReplSet "rs0"

Note:

-- The keyFile path is/opt/keyfile/mongodb-keyfile, which is correct. This is the address of the key file inside Docker. We use the-v option to map the key file to the path inside the container (that is,/opt/keyfile/mongodb-keyfile ).
-- Add-host: add the information to the/etc/hosts file of the Docker container, so we can use the domain name instead of the IP address. In the actual production environment, the information is DNS, and these parameters can be ignored.

Step 6

Connect to the replica set and install and configure it. This is still performed on node1. We need to start another interactive shell environment to enter the mongo container and start a mongo shell environment:

Root @ node1: // # docker exec-it mongo/bin/bash
Root @ node1:/# mongo
MongoDB shell version: 2.6.5
>

Switch to the admin User:

> Use admin
Switched to db admin

Because we have set a password, we have to perform authentication this time. Set the password to: password.

> Db. auth ("siteRootAdmin", "password ");
1

Now we can enable the replica set:

> Rs. initiate ()
{
"Info2": "no configuration explicitly specified -- making one ",
"Me": "node1.example.com: 27017 ",
"Info": "Config now saved locally. Shocould come online in about a minute .",
"OK": 1
}
>

Step 7

Verify the configuration of the initialized replica set:

>
Rs0: PRIMARY> rs. conf ()
{
"_ Id": "rs0 ",
"Version": 1, r
"Members ":[
          {
"_ Id": 0,
"Host": "node1.example.com: 27017"
          }
    ]
}

Step 8

Start MongoDB on the remaining two nodes.

Run the following command on node2:

Root @ node2:/# docker run \
-- Name mongo \
-V/home/core/mongo-files/data:/data/db \
-V/home/core/mongo-files:/opt/keyfile \
-- Hostname = "node2.example.com "\
-- Add-host node1.example.com: ${node1 }\
-- Add-host node2.example.com: ${node2 }\
-- Add-host node3.example.com: ${node3 }\
-P 27017: 27017-d mongo: 2.6.5 \
-- Smallfiles \
-- KeyFile/opt/keyfile/mongodb-keyfile \
-- ReplSet "rs0"

Run the following command on node3:

Root @ node3:/# docker run \
-- Name mongo \
-V/home/core/mongo-files/data:/data/db \
-V/home/core/mongo-files:/opt/keyfile \
-- Hostname = "node3.example.com "\
-- Add-host node1.example.com: ${node1 }\
-- Add-host node2.example.com: ${node2 }\
-- Add-host node3.example.com: ${node3 }\
-P 27017: 27017-d mongo: 2.6.5 \
-- Smallfiles \
-- KeyFile/opt/keyfile/mongodb-keyfile \
-- ReplSet "rs0"

Step 9


Add the two nodes to the replica set.

Return to node1. If you press enter several times, you will see the following prompt: "rs0: PRIMARY ". This is because this node is the master node of the replica set "rso.

Rs0: PRIMARY> rs. add ("node2.example.com ")
Rs0: PRIMARY> rs. add ("node3.example.com ")

We can verify whether the other two nodes are correctly added to the replica set by executing the following command:

Rs0: PRIMARY> rs. status ()

It may take several minutes to synchronize the data above node1 to the other two nodes. You can view the logs to observe what happens in each MongoDB Docker container. Execute the following command on any server:

Root @ node *:/# docker logs-ft mongo

Conclusion


Now you have a MongoDB cluster. You can add nodes to the cluster at any time. You can even close one of the nodes, including the master node, and then observe that another node becomes the master node again. Since the data is written in your local file system, restarting any node is not a big problem.

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.