From 1d9799a21e48939d3a335c8ae78eab0cc8cdcf95 Mon Sep 17 00:00:00 2001 From: xiaojin Date: Thu, 26 Aug 2021 10:05:33 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3chore(=E5=B7=A5=E5=85=B7):=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20mongo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/README.md | 3 + docker/mongo/README.md | 87 +++++++++++++++++++++++++++ docker/mongo/docker-compose.1.yml | 50 +++++++++++++++ docker/mongo/docker-compose.2.yml | 50 +++++++++++++++ docker/mongo/docker-compose.cnf.yml | 49 +++++++++++++++ docker/mongo/docker-compose.shard.yml | 49 +++++++++++++++ docker/mongo/queries/init.js | 54 +++++++++++++++++ docker/mongo/queries/shard-status.js | 1 + docker/mongo/scripts/.gitattributes | 1 + docker/mongo/scripts/init-shard.sh | 32 ++++++++++ docker/mongo/scripts/setup-cnf.sh | 40 ++++++++++++ docker/mongo/scripts/setup.sh | 39 ++++++++++++ 12 files changed, 455 insertions(+) create mode 100644 docker/mongo/README.md create mode 100644 docker/mongo/docker-compose.1.yml create mode 100644 docker/mongo/docker-compose.2.yml create mode 100644 docker/mongo/docker-compose.cnf.yml create mode 100644 docker/mongo/docker-compose.shard.yml create mode 100644 docker/mongo/queries/init.js create mode 100644 docker/mongo/queries/shard-status.js create mode 100644 docker/mongo/scripts/.gitattributes create mode 100755 docker/mongo/scripts/init-shard.sh create mode 100755 docker/mongo/scripts/setup-cnf.sh create mode 100755 docker/mongo/scripts/setup.sh diff --git a/docker/README.md b/docker/README.md index 4694f1b..93e15df 100644 --- a/docker/README.md +++ b/docker/README.md @@ -8,6 +8,9 @@ https://www.apolloconfig.com/#/zh/deployment/quick-start redis 集群 https://github.com/Grokzen/docker-redis-cluster +mongo 集群 +https://github.com/senssei/mongo-cluster-docker + 本地 dns 系统: https://github.com/mafintosh/dns-discovery diff --git a/docker/mongo/README.md b/docker/mongo/README.md new file mode 100644 index 0000000..ff9e012 --- /dev/null +++ b/docker/mongo/README.md @@ -0,0 +1,87 @@ +# mongo-cluster-docker + +This is a simple 3 node replica mongodb setup based on official `mongo` docker image using `docker-compose` described in my blogpost at https://warzycha.pl/mongo-db-sharding-docker-example/. + +For details description, steps and discussion go to: + +1. https://warzycha.pl/mongo-db-sharding-docker-example/ +2. https://warzycha.pl/mongo-db-shards-by-location/ + +# Run + +``` +docker-compose -f docker-compose.1.yml -f docker-compose.2.yml -f docker-compose.cnf.yml -f docker-compose.shard.yml up +``` + +# Tests +> Manually for the time being + +0. Core tests + +Basic *replica* test on *rs1* replica set (data nodes), `mongo-1-1` +```js +rs.status(); +``` + +this should return in `members` 3 nodes. + +Basic *sharding* test on *router* (mongos), `mongo-router` +```js +sh.status(); +``` + +this should return something similar to: + +``` +--- Sharding Status --- + sharding version: { + "_id" : 1, + "minCompatibleVersion" : 5, + "currentVersion" : 6, + "clusterId" : ObjectId("587d306454828b89adaca524") +} + shards: + active mongoses: + "3.4.1" : 1 + balancer: + Currently enabled: yes + Currently running: yes + Balancer lock taken at Mon Jan 16 2017 22:18:53 GMT+0100 by ConfigServer:Balancer + Failed balancer rounds in last 5 attempts: 0 + Migration Results for the last 24 hours: + No recent migrations + databases: + +``` + +# Sharding configuration + +Connect to 'mongos' router and run `queries/shard-status.js` for shard status. + +To establish location based partitioning on it just run `queries/init.js`. + +# Issues and limitations + +It's sometimes stuck on 'mongo-router | 2017-01-16T21:29:48.573+0000 W NETWORK [replSetDistLockPinger] No primary detected for +set cnf-serv'. It's because quite random order in `docker-compose`. + +My workaround was just to kill all containers related. + +``` +docker-compose -f docker-compose.1.yml -f docker-compose.2.yml -f docker-compose.cnf.yml -f docker-compose.shard.yml rm -f +``` + +Please pull request. :) + +Basically `mongosetup` service is now splitted to multiple `yml` files. :) + +# Reference + +* http://www.sohamkamani.com/blog/2016/06/30/docker-mongo-replica-set/ +* https://github.com/singram/mongo-docker-compose +* http://stackoverflow.com/questions/31138631/configuring-mongodb-replica-set-from-docker-compose +* https://gist.github.com/garycrawford/0a45f820e146917d231d +* http://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y +* https://docs.docker.com/compose/startup-order/ +* http://stackoverflow.com/questions/31138631/configuring-mongodb-replica-set-from-docker-compose +* https://github.com/soldotno/elastic-mongo/blob/master/docker-compose.yml diff --git a/docker/mongo/docker-compose.1.yml b/docker/mongo/docker-compose.1.yml new file mode 100644 index 0000000..c853df9 --- /dev/null +++ b/docker/mongo/docker-compose.1.yml @@ -0,0 +1,50 @@ +version: '3' +services: + mongo-1-2: + container_name: "mongo-1-2" + image: bitnami/mongodb:latest + ports: + - "30012:27017" + command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + restart: always + + mongo-1-3: + container_name: "mongo-1-3" + image: bitnami/mongodb:latest + ports: + - "30013:27017" + command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + restart: always + + mongo-1-1: + container_name: "mongo-1-1" + image: bitnami/mongodb:latest + ports: + - "30011:27017" + command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + links: + - mongo-1-2:mongo-1-2 + - mongo-1-3:mongo-1-3 + restart: always + + mongo-rs1-setup: + container_name: "mongo-rs1-setup" + image: bitnami/mongodb:latest + depends_on: + - "mongo-1-1" + - "mongo-1-2" + - "mongo-1-3" + links: + - mongo-1-1:mongo-1-1 + - mongo-1-2:mongo-1-2 + - mongo-1-3:mongo-1-3 + volumes: + - ./scripts:/scripts + environment: + - MONGO1=mongo-1-1 + - MONGO2=mongo-1-2 + - MONGO3=mongo-1-3 + - RS=rs1 + entrypoint: [ "/scripts/setup.sh" ] + + diff --git a/docker/mongo/docker-compose.2.yml b/docker/mongo/docker-compose.2.yml new file mode 100644 index 0000000..4c0265e --- /dev/null +++ b/docker/mongo/docker-compose.2.yml @@ -0,0 +1,50 @@ +version: '3' +services: + mongo-2-2: + container_name: "mongo-2-2" + image: bitnami/mongodb:latest + ports: + - "30022:27017" + command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + restart: always + + mongo-2-3: + container_name: "mongo-2-3" + image: bitnami/mongodb:latest + ports: + - "30023:27017" + command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + restart: always + + mongo-2-1: + container_name: "mongo-2-1" + image: bitnami/mongodb:latest + ports: + - "30021:27017" + command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + links: + - mongo-2-2:mongo-2-2 + - mongo-2-3:mongo-2-3 + restart: always + + mongo-rs2-setup: + container_name: "mongo-rs2-setup" + image: bitnami/mongodb:latest + depends_on: + - "mongo-2-1" + - "mongo-2-2" + - "mongo-2-3" + links: + - mongo-2-1:mongo-2-1 + - mongo-2-2:mongo-2-2 + - mongo-2-3:mongo-2-3 + volumes: + - ./scripts:/scripts + environment: + - MONGO1=mongo-2-1 + - MONGO2=mongo-2-2 + - MONGO3=mongo-2-3 + - RS=rs2 + entrypoint: [ "/scripts/setup.sh" ] + + diff --git a/docker/mongo/docker-compose.cnf.yml b/docker/mongo/docker-compose.cnf.yml new file mode 100644 index 0000000..0a07785 --- /dev/null +++ b/docker/mongo/docker-compose.cnf.yml @@ -0,0 +1,49 @@ +version: '3' +services: + mongo-cnf-2: + container_name: "mongo-cnf-2" + image: bitnami/mongodb:latest + ports: + - "30102:27017" + command: mongod --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + restart: always + + mongo-cnf-3: + container_name: "mongo-cnf-3" + image: bitnami/mongodb:latest + ports: + - "30103:27017" + command: mongod --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + restart: always + + mongo-cnf-1: + container_name: "mongo-cnf-1" + image: bitnami/mongodb:latest + ports: + - "30101:27017" + command: mongod --replSet cnf-serv --configsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles + links: + - mongo-cnf-2:mongo-cnf-2 + - mongo-cnf-3:mongo-cnf-3 + restart: always + + mongo-cnf-setup: + container_name: "mongo-cnf-setup" + image: bitnami/mongodb:latest + depends_on: + - "mongo-cnf-1" + - "mongo-cnf-2" + - "mongo-cnf-3" + links: + - mongo-cnf-1:mongo-cnf-1 + - mongo-cnf-2:mongo-cnf-2 + - mongo-cnf-3:mongo-cnf-3 + volumes: + - ./scripts:/scripts + environment: + - MONGO1=mongo-cnf-1 + - MONGO2=mongo-cnf-2 + - MONGO3=mongo-cnf-3 + - RS=cnf-serv + - PORT=27017 + entrypoint: [ "/scripts/setup-cnf.sh" ] diff --git a/docker/mongo/docker-compose.shard.yml b/docker/mongo/docker-compose.shard.yml new file mode 100644 index 0000000..596b176 --- /dev/null +++ b/docker/mongo/docker-compose.shard.yml @@ -0,0 +1,49 @@ +version: '3' +services: + mongo-router: + container_name: "mongo-router" + image: bitnami/mongodb:latest + ports: + - "30001:27017" + depends_on: + - "mongo-rs1-setup" + - "mongo-rs2-setup" + - "mongo-cnf-setup" + external_links: + - mongo-cnf-1:mongo-cnf-1 + - mongo-cnf-2:mongo-cnf-2 + - mongo-cnf-3:mongo-cnf-3 + - mongo-1-1:mongo-1-1 + - mongo-1-2:mongo-1-2 + - mongo-1-3:mongo-1-3 + - mongo-2-1:mongo-2-1 + - mongo-2-2:mongo-2-2 + - mongo-2-3:mongo-2-3 + command: mongos --configdb cnf-serv/mongo-cnf-1:27017,mongo-cnf-2:27017,mongo-cnf-3:27017 --port 27017 --bind_ip 0.0.0.0 + restart: always + + mongo-shard-setup: + container_name: "mongo-shard-setup" + image: bitnami/mongodb:latest + depends_on: + - "mongo-router" + links: + - mongo-router:mongo-router + volumes: + - ./scripts:/scripts + environment: + - MONGOS=mongo-router + - MONGO11=mongo-1-1 + - MONGO12=mongo-1-2 + - MONGO13=mongo-1-3 + - MONGO21=mongo-2-1 + - MONGO22=mongo-2-2 + - MONGO23=mongo-2-3 + - RS1=rs1 + - RS2=rs2 + - PORT=27017 + - PORT1=27017 + - PORT2=27017 + - PORT3=27017 + entrypoint: [ "/scripts/init-shard.sh" ] + restart: on-failure:20 diff --git a/docker/mongo/queries/init.js b/docker/mongo/queries/init.js new file mode 100644 index 0000000..46cbcdb --- /dev/null +++ b/docker/mongo/queries/init.js @@ -0,0 +1,54 @@ +sh.removeShardTag("rs1", "US"); + +sh.removeShardTag("rs2", "EU"); + +sh.addShardTag("rs1", "US"); + +sh.addShardTag("rs2", "EU"); + +sh.disableBalancing("test.sample"); + +db.sample.drop(); + +db.createCollection("sample"); + +db.sample.createIndex( { factoryId: 1 } ); + +sh.enableSharding("test"); + +sh.shardCollection("test.sample",{ location: 1, factoryId: 1}); + +sh.addTagRange( + "test.sample", + { "location" : "US", "factoryId" : MinKey }, + { "location" : "US", "factoryId" : MaxKey }, + "US" +); + +sh.addTagRange( + "test.sample", + { "location" : "EU", "factoryId" : MinKey }, + { "location" : "EU", "factoryId" : MaxKey }, + "EU" +); + + + + +sh.enableBalancing("test.sample"); + +for(var i=0; i<100; i++){ + db.sample.insert({ + "location": "US", + "factoryId": NumberInt(i) + }); + + db.sample.insert({ + "location": "EU", + "factoryId": NumberInt(100+i) + }); +} + +sh.startBalancer(); + +db.sample.find(); \ No newline at end of file diff --git a/docker/mongo/queries/shard-status.js b/docker/mongo/queries/shard-status.js new file mode 100644 index 0000000..3a2a4af --- /dev/null +++ b/docker/mongo/queries/shard-status.js @@ -0,0 +1 @@ +sh.status(true); \ No newline at end of file diff --git a/docker/mongo/scripts/.gitattributes b/docker/mongo/scripts/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/docker/mongo/scripts/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/docker/mongo/scripts/init-shard.sh b/docker/mongo/scripts/init-shard.sh new file mode 100755 index 0000000..8467b74 --- /dev/null +++ b/docker/mongo/scripts/init-shard.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +mongodb1=`getent hosts ${MONGOS} | awk '{ print $1 }'` + +mongodb11=`getent hosts ${MONGO11} | awk '{ print $1 }'` +mongodb12=`getent hosts ${MONGO12} | awk '{ print $1 }'` +mongodb13=`getent hosts ${MONGO13} | awk '{ print $1 }'` + +mongodb21=`getent hosts ${MONGO21} | awk '{ print $1 }'` +mongodb22=`getent hosts ${MONGO22} | awk '{ print $1 }'` +mongodb23=`getent hosts ${MONGO23} | awk '{ print $1 }'` + +mongodb31=`getent hosts ${MONGO31} | awk '{ print $1 }'` +mongodb32=`getent hosts ${MONGO32} | awk '{ print $1 }'` +mongodb33=`getent hosts ${MONGO33} | awk '{ print $1 }'` + +port=${PORT:-27017} + +echo "Waiting for startup.." +until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do + printf '.' + sleep 1 +done + +echo "Started.." + +echo init-shard.sh time now: `date +"%T" ` +mongo --host ${mongodb1}:${port} </dev/null; do + printf '.' + sleep 1 +done + +echo "Started.." + +echo setup-cnf.sh time now: `date +"%T" ` +mongo --host ${mongodb1}:${port} </dev/null; do + printf '.' + sleep 1 +done + +echo "Started.." + +echo setup.sh time now: `date +"%T" ` +mongo --host ${mongodb1}:${port} <