Public Repository

Last pushed: 2 years ago
Short Description
Inspired by https://sebastianvoss.com/docker-mongodb-sharded-cluster.html
Full Description

This repo is inspired by https://sebastianvoss.com/docker-mongodb-sharded-cluster.html

The Dockerfile below is a shameless clone from that post.

Dockerfile

FROM ubuntu:latest

# Add 10gen official apt source to the sources list
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list

# Install MongoDB
RUN apt-get update
RUN apt-get install mongodb-10gen

# Create the MongoDB data directory
RUN mkdir -p /data/db

EXPOSE 27017
ENTRYPOINT ["usr/bin/mongod"]

The other image used in the script below is https://registry.hub.docker.com/u/midnightcoder/mongos/

I love automation hence I created the following script to do exactly what the above blog post intended to do, minus the manual parts. (Disclaimer: I haven't fully tested the script below so there might be parts that might not work. To start over use the clean.sh script), create a project folder, cd to it, create the following script and run it.

setup.sh

#!/usr/bin/env zsh
docker-ip() {
    sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"
}
mnt_root=$(exec 2>/dev/null;cd $(dirname $0);unset PWD;/usr/bin/pwd||/bin/pwd||pwd)
if [ ! -d mnt/rs1_srv1 ]; then
    mkdir -p mnt/rs1_srv{1,2,3}
    mkdir mnt/rs2_srv{1,2,3}
    mkdir mnt/cfg{1,2,3}
    mkdir mnt/router
fi

sudo docker run -P -v $mnt_root/mnt/rs1_srv1:/data/db --name rs1_srv1 -d midnightcoder/mongodb --replSet rs1 --noprealloc --smallfiles 
r1s1_ip=`docker-ip rs1_srv1`
sleep 2

sudo docker run -P -v $mnt_root/mnt/rs1_srv2:/data/db --name rs1_srv2 -d midnightcoder/mongodb --replSet rs1 --noprealloc --smallfiles 
r1s2_ip=`docker-ip rs1_srv2`
sleep 2

sudo docker run -P -v $mnt_root/mnt/rs1_srv3:/data/db --name rs1_srv3 -d midnightcoder/mongodb --replSet rs1 --noprealloc --smallfiles 
r1s3_ip=`docker-ip rs1_srv3`
sleep 2

sudo docker run -P -v $mnt_root/mnt/rs2_srv1:/data/db --name rs2_srv1 -d midnightcoder/mongodb --replSet rs2 --noprealloc --smallfiles 
r2s1_ip=`docker-ip rs2_srv1`
sleep 4

sudo docker run -P -v $mnt_root/mnt/rs2_srv2:/data/db --name rs2_srv2 -d midnightcoder/mongodb --replSet rs2 --noprealloc --smallfiles 
r2s2_ip=`docker-ip rs2_srv2`
sleep 4

sudo docker run -P -v $mnt_root/mnt/rs2_srv3:/data/db --name rs2_srv3 -d midnightcoder/mongodb --replSet rs2 --noprealloc --smallfiles 
r2s3_ip=`docker-ip rs2_srv3`

echo "INitializing replica set 1"
sleep 10
mongo $r1s1_ip<<EOF11
rs.initiate()
EOF11
sleep 40

mongo $r1s1_ip<<EOF12
rs.add("$r1s2_ip:27017");
rs.add("$r1s3_ip:27017");
EOF12
sleep 15
mongo $r1s1_ip<<EOF13
cfg=rs.conf()
cfg.members[0].host="$r1s1_ip:27017"
rs.reconfig(cfg)
rs.status()
EOF13

echo "Initializing replica set 2"
sleep 5 
mongo $r2s1_ip <<EOF21
rs.initiate()
EOF21
sleep 45

mongo $r2s1_ip <<EOF22
rs.add("$r2s2_ip:27017")
rs.add("$r2s3_ip:27017")
EOF22
sleep 30
mongo $r2s1_ip <<EOF23
cfg=rs.conf()
cfg.members[0].host="$r2s1_ip:27017"
rs.reconfig(cfg)
rs.status()
EOF23
sleep 30

echo "creating config servers"
sudo docker run -P -v $mnt_root/mnt/cfg1:/data/db --name cfg1 -d midnightcoder/mongodb --smallfiles --dbpath /data/db --port 27017
cfg1_ip=`docker-ip cfg1`
sleep 5
sudo docker run -P -v $mnt_root/mnt/cfg2:/data/db --name cfg2 -d midnightcoder/mongodb --smallfiles --dbpath /data/db --port 27017
cfg2_ip=`docker-ip cfg2`

sleep 5
sudo docker run -P -v $mnt_root/mnt/cfg3:/data/db --name cfg3 -d midnightcoder/mongodb --smallfiles --dbpath /data/db --port 27017
cfg3_ip=`docker-ip cfg3`

sleep 5
echo "creating router"
sudo docker run -P -v $mnt_root/mnt/router:/data/db --name router -d midnightcoder/mongos --port 27017 --configdb $cfg1_ip:27017,$cfg2_ip:27017,$cfg3_ip:27017
router_ip=`docker-ip router`

echo "init the shard"
sleep 10
mongo $router_ip <<EOF31
sh.addShard("rs1/$r1s1_ip:27017")
EOF31
sleep 10
mongo $router_ip <<EOF32
sh.addShard("rs2/$r2s1_ip:27017")
sh.status()
EOF32

clean.sh (in case you want to start over after adjusting setup.sh)

alias sdocker='sudo docker'
sdocker stop $(sdocker ps -q)
sdocker stop cfg1
sdocker stop cfg2
sdocker stop cfg3
sdocker stop router
sdocker rm rs1_srv1
sdocker rm rs1_srv2
sdocker rm rs1_srv3
sdocker rm rs2_srv1
sdocker rm rs2_srv2
sdocker rm rs2_srv3
sdocker rm cfg1
sdocker rm cfg2
sdocker rm cfg3
sdocker rm router
# note make sure you run the following NOT under your / directory
sudo find mnt -type f -delete
Docker Pull Command
Owner
midnightcoder

Comments (0)