Deploying MongoDB shards using Docker

Source: Internet
Author: User
Tags docker run

Create a Configuration service replica set
docker run --name configsvr0 -d mongo:3.6.2-jessie --configsvr --replSet "rs_configsvr"  --bind_ip_alldocker run --name configsvr1 -d mongo:3.6.2-jessie --configsvr --replSet "rs_configsvr"  --bind_ip_alldocker run --name configsvr2 -d mongo:3.6.2-jessie --configsvr --replSet "rs_configsvr"  --bind_ip_all
$ docker psIMAGE                COMMAND                  CREATED             STATUS              PORTS               NAMESmongo:3.6.2-jessie   "docker-entrypoint..."   8 seconds ago       Up 6 seconds        27019/tcp           configsvr2mongo:3.6.2-jessie   "docker-entrypoint..."   9 seconds ago       Up 7 seconds        27019/tcp           configsvr1mongo:3.6.2-jessie   "docker-entrypoint..."   19 seconds ago      Up 16 seconds       27019/tcp           configsvr0

By docker inspect locating the IP of three configuration service instances.
For example docker inspect configsvr0 | grep IPAddress ,
And because --configsvr the default port is 27019. So the address of the configuration service is

    • configsvr0:172.17.0.2:27019
    • configsvr1:172.17.0.3:27019
    • configsvr2:172.17.0.4:27019

Initialization

docker exec -it configsvr0 bashmongo --host 172.17.0.2 --port 27019rs.initiate(  {    _id: "rs_configsvr",    configsvr: true,    members: [      { _id : 0, host : "172.17.0.2:27019" },      { _id : 1, host : "172.17.0.3:27019" },      { _id : 2, host : "172.17.0.4:27019" }    ]  })

The connection string is

mongodb://172.17.0.2:27019,172.17.0.3:27019,172.17.0.4:27019/test?replicaSet=rs_configsvr

Create a shard copy set

The same as above, no longer repeat, directly on the command line.

docker run --name shardsvr00 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr0"  --bind_ip_alldocker run --name shardsvr01 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr0"  --bind_ip_alldocker run --name shardsvr02 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr0"  --bind_ip_alldocker run --name shardsvr10 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr1"  --bind_ip_alldocker run --name shardsvr11 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr1"  --bind_ip_alldocker run --name shardsvr12 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr1"  --bind_ip_all

By docker inspect locating the IP of the corresponding instance.
For example docker inspect shardsvr00 | grep IPAddress ,
And because --shardsvr the default port is 27018. So the address is

    • shardsvr00:172.17.0.5:27018
    • shardsvr01:172.17.0.6:27018
    • shardsvr02:172.17.0.7:27018

由于后来才增加的 shardsvr1,所以 IP 末尾是 9 而不是 8。

    • shardsvr10:172.17.0.9:27018
    • shardsvr11:172.17.0.10:27018
    • shardsvr12:172.17.0.11:27018
$ docker exec -it shardsvr00 bashmongo --host 172.17.0.5 --port 27018> rs.initiate(  {    _id : "rs_shardsvr",    members: [      { _id : 0, host : "172.17.0.5:27018" },      { _id : 1, host : "172.17.0.6:27018" },      { _id : 2, host : "172.17.0.7:27018" }    ]  })# 输出 { "ok" : 1 }
mongo --host 172.17.0.9 --port 27018> rs.initiate(  {    _id : "rs_shardsvr1",    members: [      { _id : 0, host : "172.17.0.9:27018" },      { _id : 1, host : "172.17.0.10:27018" },      { _id : 2, host : "172.17.0.11:27018" }    ]  })

The connection string is

mongodb://172.17.0.5:27018,172.17.0.6:27018,172.17.0.7:27018/test?replicaSet=rs_shardsvr0

mongodb://172.17.0.9:27018,172.17.0.10:27018,172.17.0.11:27018/test?replicaSet=rs_shardsvr1

Connecting MONGOs to a shard cluster

Because the default entry for the mirror is mongod , you want to --entrypoint "mongos" change it to mongos :

docker run --name mongos0 -d --entrypoint "mongos" mongo:3.6.2-jessie --configdb rs_configsvr/172.17.0.2:27019,172.17.0.3:27019,172.17.0.4:27019 --bind_ip_all

Address is172.17.0.8:27017

Adding shards to a cluster
$ docker exec -it mongos bash$ mongo --host 172.17.0.8 --port 27017> sh.addShard("rs_shardsvr0/172.17.0.5:27018,172.17.0.6:27018,172.17.0.7:27018"){    "shardAdded" : "rs_shardsvr",    "ok" : 1,    "$clusterTime" : {        "clusterTime" : Timestamp(1518405986, 8),        "signature" : {            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),            "keyId" : NumberLong(0)        }    },    "operationTime" : Timestamp(1518405986, 8)}> sh.addShard("rs_shardsvr1/172.17.0.9:27018,172.17.0.10:27018,172.17.0.11:27018")
Database Enable Sharding
> sh.enableSharding("test"){    "ok" : 1,    "$clusterTime" : {        "clusterTime" : Timestamp(1518406101, 8),        "signature" : {            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),            "keyId" : NumberLong(0)        }    },    "operationTime" : Timestamp(1518406101, 8)}
Shard Collection

test.order _id Hash shards for the field:

> sh.shardCollection("test.order", {"_id": "hashed" }){    "collectionsharded" : "test.order",    "collectionUUID" : UUID("12b22b5d-93d7-4ccf-b989-2a0e550e750f"),    "ok" : 1,    "$clusterTime" : {        "clusterTime" : Timestamp(1518406317, 12),        "signature" : {            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),            "keyId" : NumberLong(0)        }    },    "operationTime" : Timestamp(1518406317, 6)}

Inserting data

use testfor (i = 1; i <= 1000; i=i+1){    db.order.insert({'price': 1})}

View Data distribution

mongos> db.order.find().count()1000rs_shardsvr0:PRIMARY> db.order.find().count()526rs_shardsvr1:PRIMARY> db.order.find().count()474

Deploying MongoDB shards using 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.