🐳chore(工具):增加 mongo

develop
xiaojin 5 years ago
parent e42df0197b
commit 1d9799a21e

@ -8,6 +8,9 @@ https://www.apolloconfig.com/#/zh/deployment/quick-start
redis 集群 redis 集群
https://github.com/Grokzen/docker-redis-cluster https://github.com/Grokzen/docker-redis-cluster
mongo 集群
https://github.com/senssei/mongo-cluster-docker
本地 dns 系统: 本地 dns 系统:
https://github.com/mafintosh/dns-discovery https://github.com/mafintosh/dns-discovery

@ -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

@ -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" ]

@ -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" ]

@ -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" ]

@ -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

@ -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();

@ -0,0 +1 @@
* text=auto eol=lf

@ -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} <<EOF
sh.addShard( "${RS1}/${mongodb11}:${PORT1},${mongodb12}:${PORT2},${mongodb13}:${PORT3}" );
sh.addShard( "${RS2}/${mongodb21}:${PORT1},${mongodb22}:${PORT2},${mongodb23}:${PORT3}" );
sh.status();
EOF

@ -0,0 +1,40 @@
#!/bin/bash
mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'`
mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'`
mongodb3=`getent hosts ${MONGO3} | 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 setup-cnf.sh time now: `date +"%T" `
mongo --host ${mongodb1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"configsvr": true,
"protocolVersion": 1,
"members": [
{
"_id": 100,
"host": "${mongodb1}:${port}"
},
{
"_id": 101,
"host": "${mongodb2}:${port}"
},
{
"_id": 102,
"host": "${mongodb3}:${port}"
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
EOF

@ -0,0 +1,39 @@
#!/bin/bash
mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'`
mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'`
mongodb3=`getent hosts ${MONGO3} | 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 setup.sh time now: `date +"%T" `
mongo --host ${mongodb1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"protocolVersion": 1,
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}"
},
{
"_id": 1,
"host": "${mongodb2}:${port}"
},
{
"_id": 2,
"host": "${mongodb3}:${port}"
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
EOF
Loading…
Cancel
Save