Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGO_VERSION=4.0
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
/data
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.PHONY: initiate help

help: ## print this message
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-10s\033[0m %s\n", $$1, $$2}'

up: ## Start Mongo cluster (without init)
docker-compose up -d

init: ## Launch mongo cluster init
./initiate

clean: ## Stop docker containers and clean volumes
docker-compose down -v

start: up init ## Start containers and launch init

stop: ## Stop docker containers
docker-compose stop

watch: ## Watch logs
docker-compose logs -f

restart: clean start ## Clean and restart

59 changes: 35 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,63 @@ This repository provides a fully sharded mongo environment using docker-compose

The MongoDB environment consists of the following docker containers

- **mongosrs(1-3)n(1-3)**: Mongod data server with three replica sets containing 3 nodes each (9 containers)
- **mongocfg(1-3)**: Stores metadata for sharded data distribution (3 containers)
- **mongos(1-2)**: Mongo routing service to connect to the cluster through (1 container)
- **mongors(1-2)n(1-3)**: Mongod data server with two replica sets containing 3 nodes each (2 replica + 1 arbiter * 2 : 6 containers)
- **mongocfg(1-3)**: Stores metadata for sharded data distribution CSRS (3 containers)
- **mongos(1-2)**: Mongo routing service to connect to the cluster through (2 containers)

## Caveats

- This is designed to have a minimal disk footprint at the cost of durability.
- This is designed in no way for production but as a cheap learning and exploration vehicle.

## Installation (Debian base):

### Install Docker

sudo apt-get install -y apparmor lxc cgroup-lite curl
wget -qO- https://get.docker.com/ | sh
sudo usermod -aG docker YourUserNameHere
sudo service docker restart

### Install Docker-compose (1.4.2+)

sudo su
curl -L https://github.com/docker/compose/releases/download/1.4.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
exit
## Installation

### Check out the repository

git clone [email protected]:singram/mongo-docker-compose.git
cd mongo-docker-compose


### Setup Cluster
This will pull all the images from [Docker index](https://index.docker.io/u/jacksoncage/mongo/) and run all the containers.
This will pull all the images from [Docker index](https://hub.docker.com/_/mongo) and run all the containers.

docker-compose up
make up

Please note that you will need docker-compose 1.4.2 or better for this to work due to circular references between cluster members.
You will need to run the following *once* only to initialize all replica sets and shard data across them

./initiate
make init

Or both :

make start

Makefile documentation :

⇒ make help

----- BUILD ------------------------------------------------------------------------------
up Start docker-compose
init Launch mongo cluster init
clean Stop docker containers and clean volumes
start Start container and init
stop Stop docker containers
watch Watch logs
restart Clean and restart
----- OTHERS -----------------------------------------------------------------------------
help print this message


You should now be able connect to mongos1 and the new sharded cluster from the mongos container itself using the mongo shell to connect to the running mongos process

docker exec -it mongos1 mongo --port 21017

### Version

By default, it will start a MongoDb 4.0 cluster, but you can also start MongoDB in version 3.6 :

MONGO_VERSION=3.6 make start

## Persistent storage
Data is stored at `./data/` and is excluded from version control. Data will be persistent between container runs. To remove all data `./reset`
Data is stored in docker volumes. To remove all data : `make clean`.

## TODO

Expand All @@ -64,3 +74,4 @@ Data is stored at `./data/` and is excluded from version control. Data will be p
- [Mongo Docker ](https://github.com/jacksoncage/mongo-docker)
- [DnsDock](https://github.com/tonistiigi/dnsdock)
- [Docker](https://github.com/dotcloud/docker/)
- [WaitForIt](https://github.com/vishnubob/wait-for-it)
Empty file removed data/mongo-cfg-1/.gitkeep
Empty file.
Empty file removed data/mongo-cfg-2/.gitkeep
Empty file.
Empty file removed data/mongo-cfg-3/.gitkeep
Empty file.
Empty file removed data/mongors1n1/.gitkeep
Empty file.
Empty file removed data/mongors1n2/.gitkeep
Empty file.
Empty file removed data/mongors1n3/.gitkeep
Empty file.
Empty file removed data/mongors2n1/.gitkeep
Empty file.
Empty file removed data/mongors2n2/.gitkeep
Empty file.
Empty file removed data/mongors2n3/.gitkeep
Empty file.
110 changes: 41 additions & 69 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,125 +1,97 @@
version: "2"
version: "3"
services:

mongors1n1:
container_name: mongors1n1
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --replSet mongors1 --dbpath /data/db --nojournal --oplogSize 16 --noauth
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --replSet mongors1 --shardsvr --oplogSize 16 --noauth --bind_ip_all
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongors1n1:/data/db
expose:
- 27018

mongors1n2:
container_name: mongors1n2
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --replSet mongors1 --dbpath /data/db --nojournal --oplogSize 16 --noauth
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --replSet mongors1 --shardsvr --oplogSize 16 --noauth --bind_ip_all
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongors1n2:/data/db
expose:
- 27018

mongors1n3:
container_name: mongors1n3
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --replSet mongors1 --dbpath /data/db --nojournal --oplogSize 16 --noauth
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --replSet mongors1 --shardsvr --oplogSize 16 --noauth --bind_ip_all
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongors1n3:/data/db
expose:
- 27018

mongors2n1:
container_name: mongors2n1
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --replSet mongors2 --dbpath /data/db --nojournal --oplogSize 16 --noauth
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --replSet mongors2 --shardsvr --oplogSize 16 --noauth --bind_ip_all
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongors2n1:/data/db
expose:
- 27018

mongors2n2:
container_name: mongors2n2
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --replSet mongors2 --dbpath /data/db --nojournal --oplogSize 16 --noauth
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --replSet mongors2 --shardsvr --oplogSize 16 --noauth --bind_ip_all
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongors2n2:/data/db
expose:
- 27018

mongors2n3:
container_name: mongors2n3
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --replSet mongors2 --dbpath /data/db --nojournal --oplogSize 16 --noauth
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --replSet mongors2 --shardsvr --oplogSize 16 --noauth --bind_ip_all
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongors2n3:/data/db
expose:
- 27018

mongocfg1:
container_name: mongocfg1
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --dbpath /data/db --configsvr --noauth --port 27017
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --configsvr --replSet mongocfg1 --noauth --bind_ip_all
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongocfg1:/data/db
- 27019

mongocfg2:
container_name: mongocfg2
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --dbpath /data/db --configsvr --noauth --port 27017
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --configsvr --replSet mongocfg1 --noauth --bind_ip_all
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongocfg2:/data/db
- 27019

mongocfg3:
container_name: mongocfg3
image: mongo:3.0.14
command: mongod --noprealloc --smallfiles --dbpath /data/db --configsvr --noauth --port 27017
image: mongo:${MONGO_VERSION}
command: mongod --noprealloc --smallfiles --configsvr --replSet mongocfg1 --noauth --bind_ip_all
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- ${DATA_DIR}/mongocfg3:/data/db
- 27019

mongos1:
container_name: mongos1
image: mongo:3.0.14
image: mongo:${MONGO_VERSION}
depends_on:
- mongocfg1
- mongocfg2
- mongocfg3
command: mongos --configdb mongocfg1:27017,mongocfg2:27017,mongocfg3:27017 --port 27017
command: mongos --configdb mongocfg1/mongocfg1:27019,mongocfg2:27019,mongocfg3:27019 --bind_ip_all
ports:
- 27017:27017
volumes:
- /etc/localtime:/etc/localtime:ro
- ./wait-for-it.sh:/tmp/wait-for-it.sh


mongos2:
container_name: mongos2
image: mongo:3.0.14
image: mongo:${MONGO_VERSION}
depends_on:
- mongocfg1
- mongocfg2
- mongocfg3
command: mongos --configdb mongocfg1:27017,mongocfg2:27017,mongocfg3:27017 --port 27017
command: mongos --configdb mongocfg1/mongocfg1:27019,mongocfg2:27019,mongocfg3:27019 --bind_ip_all
ports:
- 27018:27017
volumes:
- /etc/localtime:/etc/localtime:ro

- 27018:27017
38 changes: 32 additions & 6 deletions initiate
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

for (( rs = 1; rs < 3; rs++ )); do
echo "Intializing replica ${rs} set"
replicate="rs.initiate(); sleep(1000); cfg = rs.conf(); cfg.members[0].host = \"mongors${rs}n1\"; rs.reconfig(cfg); rs.add(\"mongors${rs}n2\"); rs.add(\"mongors${rs}n3\"); rs.status();"
docker exec -it mongors${rs}n1 bash -c "echo '${replicate}' | mongo"
echo ""
echo "=> Intializing replicaset ${rs}"
echo ""
for (( node = 1; node < 3; node++ )); do
docker-compose exec mongos1 bash -c "/tmp/wait-for-it.sh -t 60 mongors${rs}n${node}:27018"
done
replicate="rs.initiate(); sleep(1000); cfg = rs.conf(); cfg.members[0].host = \"mongors${rs}n1:27018\"; sleep(1000);rs.reconfig(cfg); rs.add(\"mongors${rs}n2:27018\"); rs.addArb(\"mongors${rs}n3:27018\"); rs.status();"

echo "=> Replicaset${rs} configuration : "
docker-compose exec mongors${rs}n1 bash -c "echo '${replicate}' | mongo --port 27018"
done

sleep 2
echo ""
echo "=> Intializing replicaset for config"
echo ""
replicate="rs.initiate(); sleep(1000); cfg = rs.conf(); cfg.members[0].host = \"mongocfg1:27019\"; rs.reconfig(cfg); rs.add(\"mongocfg2:27019\"); rs.add(\"mongocfg3:27019\"); rs.status();"

echo ""
echo "=> Config replicaset configuration : "
echo ""
docker-compose exec mongocfg1 bash -c "echo '${replicate}' | mongo --port 27019"


docker-compose exec mongos1 bash -c "/tmp/wait-for-it.sh mongocfg1:27019;/tmp/wait-for-it.sh mongocfg2:27019;/tmp/wait-for-it.sh mongocfg1:27019;"

# Add better mechanisum to wait for mongos connectivity to be
# established by tailing docker log for connection readiness

docker exec -it mongos1 bash -c "echo \"sh.addShard('mongors1/mongors1n1:27017'); sh.addShard('mongors2/mongors2n1:27017');\" | mongo "
echo ""
echo "Intializing shards through router"
echo ""
docker-compose exec mongos1 bash -c "/tmp/wait-for-it.sh -t 60 mongos1:27017;/tmp/wait-for-it.sh mongos2:27017"
docker-compose exec mongos1 bash -c "echo \"sh.addShard('mongors1/mongors1n1:27018,mongors1n2:27018'); sh.addShard('mongors2/mongors2n1:27018,mongors2n2:27018');sh.status()\" | mongo "
9 changes: 0 additions & 9 deletions reset

This file was deleted.

Loading