forked from galal-hussein/rancher-mongo-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hitesh
committed
May 23, 2016
1 parent
2eda1a5
commit bce26b3
Showing
10 changed files
with
268 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
FROM mongo:latest | ||
FROM alpine:latest | ||
MAINTAINER Hussein Galal | ||
|
||
RUN apt-get update \ | ||
&& apt-get install -yqq python build-essential python-dev python-pip python-setuptools \ | ||
&& pip install pymongo \ | ||
&& pip install netifaces \ | ||
&& pip install pydns | ||
ENV MONGO_SERVICE_NAME mongo | ||
|
||
RUN mkdir -p /mongo | ||
WORKDIR /mongo | ||
ADD ./*.sh /opt/rancher/bin/ | ||
RUN chmod u+x /opt/rancher/bin/*.sh | ||
|
||
ENV MONGO_SERVICE_NAME mongo | ||
VOLUME /opt/rancher/bin | ||
|
||
COPY docker-entrypoint.sh /entrypoint.sh | ||
RUN chmod u+x /entrypoint.sh | ||
ENTRYPOINT ["/entrypoint.sh"] | ||
CMD ["mongod"] | ||
ENTRYPOINT ["/bin/true"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,61 @@ | ||
# rancher-mongo | ||
A Docker container that check for a new mongoDB service container and add it to the Replica set | ||
# rancher-mongo-config | ||
A base Docker image to be used as a sidekick for MongoDB container, the scripts in this container will create replica set when you create a service in Rancher environment (at least 3 containers). | ||
|
||
## Requirements | ||
- Docker engine. | ||
- Rancher server. | ||
- rancher-compose. | ||
|
||
## Usage | ||
|
||
After installing rancher-compose, you can create a service that contain the following: | ||
|
||
**docker-compose.yml** | ||
``` | ||
mongo-cluster: | ||
restart: always | ||
environment: | ||
MONGO_SERVICE_NAME: mongo-cluster | ||
tty: true | ||
entrypoint: /opt/rancher/bin/entrypoint.sh | ||
command: | ||
- --replSet | ||
- "rs0" | ||
image: mongo:3.0 | ||
labels: | ||
io.rancher.container.hostname_override: container_name | ||
io.rancher.sidekicks: mongo-base, mongo-datavolume | ||
volumes_from: | ||
- mongo-datavolume | ||
- mongo-base | ||
mongo-base: | ||
restart: always | ||
tty: true | ||
labels: | ||
io.rancher.container.hostname_override: container_name | ||
io.rancher.container.start_once: true | ||
build: ./ | ||
stdin_open: true | ||
entrypoint: /bin/true | ||
mongo-datavolume: | ||
net: none | ||
labels: | ||
io.rancher.container.hostname_override: container_name | ||
io.rancher.container.start_once: true | ||
volumes: | ||
- /data/db | ||
entrypoint: /bin/true | ||
image: busybox | ||
``` | ||
**rancher-compose.yml** | ||
``` | ||
mongo-cluster: | ||
scale: 3 | ||
``` | ||
|
||
And now to run the service run the following: | ||
|
||
``` | ||
# rancher-compose up | ||
``` | ||
As an added bonus the service can scale up as you go, just change the settings in rancher-compose.yml or use the Web UI to scale the serivce and the container will connect to the replicaset and be attached. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/bash | ||
DIG=/usr/bin/dig | ||
|
||
function cluster_init { | ||
sleep 10 | ||
MYIP=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1 | sed -n 2p) | ||
$DIG A $MONGO_SERVICE_NAME +short > ips.tmp | ||
mongo --eval "printjson(rs.initiate())" | ||
for member in $(cat ips.tmp); do | ||
if [ $member != $MYIP ]; then | ||
mongo --eval "printjson(rs.add('$member:27017'))" | ||
sleep 5 | ||
fi | ||
done | ||
|
||
} | ||
|
||
function find_master { | ||
$DIG A $MONGO_SERVICE_NAME +short > ips.tmp | ||
for IP in $(cat ips.tmp); do | ||
IS_MASTER=`mongo --host $IP --eval "printjson(db.isMaster())" | grep 'ismaster'` | ||
if echo $IS_MASTER | grep "true"; then | ||
return 0 | ||
fi | ||
done | ||
return 1 | ||
} | ||
# Script starts here | ||
# wait for mongo to start | ||
while [ `$DIG A $MONGO_SERVICE_NAME +short | wc -l` -lt 3 ]; do | ||
echo 'mongo instances are less than 3.. waiting!' | ||
sleep 5 | ||
done | ||
|
||
# Wait until all services are up | ||
sleep 10 | ||
find_master | ||
if [ $? -eq 0 ]; then | ||
echo 'Master is already initated.. nothing to do!' | ||
else | ||
echo 'Initiating the cluster!' | ||
cluster_init | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
mongo-cluster: | ||
restart: always | ||
environment: | ||
MONGO_SERVICE_NAME: mongo-cluster | ||
tty: true | ||
entrypoint: /opt/rancher/bin/entrypoint.sh | ||
command: | ||
- --replSet | ||
- "rs0" | ||
image: mongo:3.0 | ||
labels: | ||
io.rancher.container.hostname_override: container_name | ||
io.rancher.sidekicks: mongo-base, mongo-datavolume | ||
volumes_from: | ||
- mongo-datavolume | ||
- mongo-base | ||
mongo-base: | ||
restart: always | ||
tty: true | ||
labels: | ||
io.rancher.container.hostname_override: container_name | ||
io.rancher.container.start_once: true | ||
build: ./ | ||
stdin_open: true | ||
entrypoint: /bin/true | ||
mongo-datavolume: | ||
net: none | ||
labels: | ||
io.rancher.container.hostname_override: container_name | ||
io.rancher.container.start_once: true | ||
volumes: | ||
- /data/db | ||
entrypoint: /bin/true | ||
image: busybox |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/bin/bash | ||
|
||
# Installing dnsutils and jq | ||
echo "deb http://http.debian.net/debian wheezy-backports main" | tee /etc/apt/sources.list.d/wheezy-backports.list > /dev/null | ||
echo "deb http://security.debian.org/ wheezy/updates main contrib non-free " | tee /etc/apt/sources.list.d/wheezy-security.list > /dev/null | ||
echo "deb-src http://security.debian.org/ wheezy/updates main contrib non-free" | tee /etc/apt/sources.list.d/wheezy-security.list > /dev/null | ||
apt-get -q update > /dev/null | ||
apt-get install -qqy dnsutils jq > /dev/null 2>&1 | ||
|
||
# Check for lowest ID | ||
/opt/rancher/bin/lowest_idx.sh | ||
if [ "$?" -eq "0" ]; then | ||
echo "This is the lowest numbered contianer.. Handling the initiation." | ||
/opt/rancher/bin/initiate.sh $@ | ||
else | ||
|
||
# Run the scaling script | ||
/opt/rancher/bin/scaling.sh & | ||
|
||
# Start mongodb | ||
if [ $? -ne 0 ] | ||
then | ||
echo "Error Occurred.." | ||
fi | ||
|
||
set -e | ||
|
||
if [ "${1:0:1}" = '-' ]; then | ||
set -- mongod "$@" | ||
fi | ||
|
||
if [ "$1" = 'mongod' ]; then | ||
chown -R mongodb /data/db | ||
|
||
numa='numactl --interleave=all' | ||
if $numa true &> /dev/null; then | ||
set -- $numa "$@" | ||
fi | ||
|
||
exec gosu mongodb "$@" | ||
fi | ||
|
||
exec "$@" | ||
|
||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
|
||
|
||
# Run cluster init script | ||
/opt/rancher/bin/connect.sh & | ||
|
||
# Start mongodb | ||
if [ $? -ne 0 ] | ||
then | ||
echo "Error Occurred.." | ||
fi | ||
|
||
set -e | ||
|
||
if [ "${1:0:1}" = '-' ]; then | ||
set -- mongod "$@" | ||
fi | ||
|
||
if [ "$1" = 'mongod' ]; then | ||
chown -R mongodb /data/db | ||
|
||
numa='numactl --interleave=all' | ||
if $numa true &> /dev/null; then | ||
set -- $numa "$@" | ||
fi | ||
|
||
exec gosu mongodb "$@" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
### | ||
# Detect if this container has the lowest create ID | ||
### | ||
|
||
META_URL="http://rancher-metadata/2015-07-25" | ||
|
||
ALLMETA=$(curl -s -H 'Accept: application/json' ${META_URL}) | ||
MY_CREATE_INDEX="$(echo ${ALLMETA} | jq -r .self.container.create_index)" | ||
|
||
get_create_index() | ||
{ | ||
echo $(echo ${ALLMETA}| jq -r ".containers[]| select(.name==\"${1}\")| .create_index") | ||
} | ||
|
||
SMALLEST="${MY_CREATE_INDEX}" | ||
for container in $(echo ${ALLMETA}| jq -r .self.service.containers[]); do | ||
IDX=$(get_create_index "${container}") | ||
if [ "${IDX}" -lt "${SMALLEST}" ]; then | ||
SMALLEST=${IDX} | ||
fi | ||
done | ||
|
||
if [ "${MY_CREATE_INDEX}" -eq "${SMALLEST}" ]; then | ||
exit 0 | ||
fi | ||
|
||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mongo-cluster: | ||
scale: 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
sleep 5 | ||
DIG=/usr/bin/dig | ||
|
||
function scaleup { | ||
MYIP=$(ip -o -4 addr list eth0 | awk '{print $4}' | cut -d/ -f1 | sed -n 2p) | ||
$DIG A $MONGO_SERVICE_NAME +short > ips.tmp | ||
for IP in $(cat ips.tmp); do | ||
IS_MASTER=$(mongo --host $IP --eval "printjson(db.isMaster())" | grep 'ismaster') | ||
if echo $IS_MASTER | grep "true"; then | ||
mongo --host $IP --eval "printjson(rs.add('$MYIP:27017'))" | ||
return 0 | ||
fi | ||
done | ||
return 1 | ||
} | ||
|
||
# Script starts here | ||
if [ $($DIG A $MONGO_SERVICE_NAME +short | wc -l) -gt 3 ]; then | ||
scaleup | ||
fi |