Teach you how to deploy a MongoDB cluster with Docker

Source: Internet
Author: User
Tags mongo shell docker run

MongoDB is a product between relational and non-relational databases, most like relational databases in non-relational databases. Supports an object-oriented query language that can achieve almost the most functionality of a relational database single-table query, and also supports indexing of data. This article describes how to use Docker to build a MongoDB cluster.

In this article I'll show you how to deploy a MongoDB cluster using Docker, as follows:

2.6.5 version of MongoDB

Replica set with 3 nodes (Replica set)

Identity verification

Persisting data to the local file system

Start by preparing three running Docker servers, which means you have to prepare a local vagrant box virtual machine with Docker installed (the system can use CoreOS) or use AWS or any other way you like.

Steps

STEP1: You need to get the IP address of 3 docker servers, and configure the IP address given below to all servers, each server must execute the following command (remember to replace the IP address):

[Email protected] *:/# export node1=10.11.32.174

[Email protected] *:/# export node2=10.11.33.37

[Email protected] *:/# export node3=10.11.31.176

Ideally you do not need to do this, these IPs can be automatically configured through DNS. But that would be easier, after all, it's just an installation test.

STEP2: Create a key file for each node. Execute the following command on one of the servers, and then copy the key file to the same location as the remaining two servers.

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

[Email protected] *:/# mkdir-p/home/core

[Email protected] *:/# Cd/home/core

[email protected] *:/# OpenSSL rand-base64 741 > Mongodb-keyfile

[Email protected] *:/# chmod mongodb-keyfile

[email protected] *:/# sudo chown 999 mongodb-keyfile

The owner of the key file is set to the user with the ID "999", because in MongoDB's Docker container, the user needs to have permission to manipulate the key file.

STEP3: Start the MongoDB container for the Node1 (that is, the first Docker server). It starts a container without an authentication mechanism, so we're going to set up a user.

[Email protected]:/# docker run--name MONGO \

-V/HOME/CORE/MONGO-FILES/DATA:/DATA/DB \

-v/home/core/mongo-files:/opt/keyfile \

--hostname= "node1.example.com" \

-P 27,017:27,017 \

-D mongo:2.6.5--smallfiles

Now create an Admin user. We can connect to the MongoDB container that just started and enter an interactive shell environment.

[Email protected]:/# Docker exec-it Mongo/bin/bash

That's when we go into MongoDB's Docker container and we're going to open a MONGO shell environment:

[Email protected]:/# MONGO

The above command can open the MONGO shell environment. After execution you will see an output like this:

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 you create a success, you will see the following success message:

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 success message:

Successfully added User: {

"User": "Siterootadmin",

"Roles": [

{

"Role": "Root",

"DB": "admin"

}

]

}

We've created several users we'll use later, and now we're not quitting the interactive shell environment (MONGO and Docker container environments).

> Exit

Bye

[Email protected]:/# exit

STEP4: Stop the first instance of MongoDB:

[Email protected]:/# Docker Stop MONGO

STEP5: This time use the key file to start the first MongoDB instance (or Node1).

[Email protected]:/# Docker RM MONGO

[email protected]:/# 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"

Attention:

The path to the--keyfile is/opt/keyfile/mongodb-keyfile, which is correct. This is the address of the key file inside Docker, and we use the-V option to map the key file to the path inside the container (that is:/opt/keyfile/mongodb-keyfile).

--add-host adds this information to the Docker container's/etc/hosts file, so we can use the domain name instead of the IP address. In the actual production environment, this information is DNS, and these parameters can be ignored.

STEP6: Connect to the replica set and install it properly. This is still done on the Node1. We're going to open another new interactive shell environment into the MONGO container, while opening a MONGO shell environment:

[Email protected]:/# Docker exec-it Mongo/bin/bash

[Email protected]:/# MONGO

MongoDB Shell version:2.6.5

>

Switch to Admin User:

> Use admin

Switched to DB admin

Because we have set a password, we have to do the authentication this time. We set the password to: password.

> Db.auth ("siterootadmin", "password");

1

Now we can turn on the replica set:

> Rs.initiate ()

{

"Info2": "No configuration explicitly specified-making one",

"Me": "node1.example.com:27017",

"Info": "Config now saved locally. Should come online in about a minute. ",

"OK": 1

}

>

STEP7: Verify the configuration of the replica set that has been initialized:

>

Rs0:primary> rs.conf ()

{

"_id": "Rs0",

"Version": 1,r

"Members": [

{

"_id": 0,

"Host": "node1.example.com:27017"

}

]

}

STEP8: Start MongoDB on the remaining two nodes.

Execute the command above NODE2:

[email protected]:/# 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"

Execute the command above NODE3:

[email protected]:/# 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.

Back to the Node1 node, if you press the ENTER key several times here, you will see the following prompt: "Rs0:primary". This is because this node is the primary node of the replica set "RSO".

Rs0:primary> Rs.add ("node2.example.com")

Rs0:primary> Rs.add ("node3.example.com")

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

Rs0:primary> Rs.status ()

It may take a few minutes to sync the data above the Node1 to the remaining two nodes. You can look at the logs to see what's going on in each of the MongoDB Docker containers. Execute the following command on any server:

[Email protected]*:/# Docker logs-ft MONGO

Conclusion

Now you have a MongoDB cluster. You are free to add nodes to this cluster at any time. You can even close one of these nodes, including the master node, and then observe that another node is re-converted to the master node. Since this data is written in your native file system, restarting any node is not a big problem.

Teach you how to deploy a MongoDB cluster with Docker

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.