Skip to content

Commit

Permalink
Add example for replication with Docker. (sclorg#224)
Browse files Browse the repository at this point in the history
* Add example for replication with Docker.

* Do not use SRV DNS records.

* Update README + make test script more similar.

Report also logs after failure.

* Add waiting that container finished its scripts - implicit waiting times of wait_for_mongo_up is too low.

* Add hostname package into RHEL based image.

* Reenable other tests (disabled for testing).
  • Loading branch information
omron93 authored and StephenCoady committed Aug 2, 2017
1 parent 4ab8898 commit c64df5c
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 84 deletions.
2 changes: 1 addition & 1 deletion 2.6/Dockerfile.rhel7
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CMD ["run-mongod"]
RUN yum install -y yum-utils && \
yum-config-manager --enable rhel-server-rhscl-7-rpms && \
yum-config-manager --enable rhel-7-server-optional-rpms && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar v8314 rh-mongodb26-mongodb rh-mongodb26" && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar hostname v8314 rh-mongodb26-mongodb rh-mongodb26" && \
yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
yum clean all
Expand Down
3 changes: 2 additions & 1 deletion 2.6/root/usr/share/container-scripts/mongodb/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ function replset_addr() {
local current_endpoints
current_endpoints="$(endpoints)"
if [ -z "${current_endpoints}" ]; then
echo >&2 "Cannot get address of replica set: no nodes are listed in service"
info "Cannot get address of replica set: no nodes are listed in service!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi
echo "${MONGODB_REPLICA_NAME}/${current_endpoints//[[:space:]]/,}"
Expand Down
27 changes: 1 addition & 26 deletions 2.6/root/usr/share/container-scripts/mongodb/init-replset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ source "${CONTAINER_SCRIPTS_PATH}/common.sh"
# (for example, "replica-2.mongodb.myproject.svc.cluster.local")
readonly MEMBER_HOST="$(hostname -f)"

# Outputs available endpoints (hostnames) to stdout.
# This also includes hostname of the current pod.
#
# Uses the following global variables:
# - MONGODB_SERVICE_NAME (optional, defaults to 'mongodb')
function find_endpoints() {
local service_name="${MONGODB_SERVICE_NAME:-mongodb}"

# Extract host names from lines like this: "10 33 0 mongodb-2.mongodb.myproject.svc.cluster.local."
dig "${service_name}" SRV +search +short | cut -d' ' -f4 | rev | cut -c2- | rev
}

# Initializes the replica set configuration.
#
# Arguments:
Expand Down Expand Up @@ -60,20 +48,7 @@ function add_member() {
local host="$1"
info "Adding ${host} to replica set ..."

# TODO: replace this with a call to `replset_addr` from common.sh, once it returns host names.
local endpoints
endpoints="$(find_endpoints | paste -s -d,)"

if [ -z "${endpoints}" ]; then
info "ERROR: couldn't add host to replica set!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi

local replset_addr
replset_addr="${MONGODB_REPLICA_NAME}/${endpoints}"

if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "${replset_addr}" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "$(replset_addr)" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
info "ERROR: couldn't add host to replica set!"
return 1
fi
Expand Down
65 changes: 65 additions & 0 deletions 2.6/test/run
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function cleanup() {
echo "Done."
done
rmdir $CIDFILE_DIR

local network_name="mongodb-replset-$$"
docker network ls | grep -q ${network_name} && docker network rm ${network_name}
}
trap cleanup EXIT SIGINT

Expand Down Expand Up @@ -382,6 +385,67 @@ run_doc_test() {
echo
}

function run_local_replication_test() {
function print_logs() {
for file in $CIDFILE_DIR/replset*; do
echo "INFO: printing logs for CID file ${file}"
docker logs $(cat ${file})
done
}
trap print_logs ERR

echo "Testing replication on local docker"
#Initializing replicaset

cat > variables <<EOF
MONGODB_DATABASE=db
MONGODB_USER=user
MONGODB_PASSWORD=password
MONGODB_ADMIN_PASSWORD=adminPassword
MONGODB_REPLICA_NAME=rs0
MONGODB_KEYFILE_VALUE=xxxxxxxxxxxx
MONGODB_SMALLFILES=true
MONGODB_SERVICE_NAME=mongodb
EOF
source variables

local network_name="mongodb-replset-$$"
docker network create ${network_name}

docker run -d --cidfile $CIDFILE_DIR/replset0 --name=replset-0 --hostname=replset-0 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-0 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CIDFILE_DIR/replset1 --name=replset-1 --hostname=replset-1 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-1 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CIDFILE_DIR/replset2 --name=replset-2 --hostname=replset-2 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-2 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"

local host="$(docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c '. /usr/share/container-scripts/mongodb/common.sh && echo $(replset_addr)')"

# Storing document into replset and wait replication to finish ...
docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c "set -e
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 3
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"

# Adding new container
docker run -d --cidfile $CIDFILE_DIR/replset3 --name=replset-3 --hostname=replset-3 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-3 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"

# Storing document into replset and wait replication to finish ...
docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c "set -e
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 4
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"

rm variables
trap ERR

echo " Success!"
}

# Tests.
run_container_creation_tests
Expand All @@ -392,3 +456,4 @@ CONTAINER_ARGS="-u 12345" USER="user1" PASS="pass1" DB="test_db" ADMIN_PASS="r00
run_change_password_test
run_mount_config_test
run_doc_test
run_local_replication_test
2 changes: 1 addition & 1 deletion 3.0-upg/Dockerfile.rhel7
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CMD ["run-mongod"]
RUN yum install -y yum-utils && \
yum-config-manager --enable rhel-server-rhscl-7-rpms && \
yum-config-manager --enable rhel-7-server-optional-rpms && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar v8314 rh-mongodb30upg-mongodb rh-mongodb30upg" && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar hostname v8314 rh-mongodb30upg-mongodb rh-mongodb30upg" && \
yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
yum clean all
Expand Down
3 changes: 2 additions & 1 deletion 3.0-upg/root/usr/share/container-scripts/mongodb/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function replset_addr() {
local current_endpoints
current_endpoints="$(endpoints)"
if [ -z "${current_endpoints}" ]; then
echo >&2 "Cannot get address of replica set: no nodes are listed in service"
info "Cannot get address of replica set: no nodes are listed in service!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi
echo "${MONGODB_REPLICA_NAME}/${current_endpoints//[[:space:]]/,}"
Expand Down
27 changes: 1 addition & 26 deletions 3.0-upg/root/usr/share/container-scripts/mongodb/init-replset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ source "${CONTAINER_SCRIPTS_PATH}/common.sh"
# (for example, "replica-2.mongodb.myproject.svc.cluster.local")
readonly MEMBER_HOST="$(hostname -f)"

# Outputs available endpoints (hostnames) to stdout.
# This also includes hostname of the current pod.
#
# Uses the following global variables:
# - MONGODB_SERVICE_NAME (optional, defaults to 'mongodb')
function find_endpoints() {
local service_name="${MONGODB_SERVICE_NAME:-mongodb}"

# Extract host names from lines like this: "10 33 0 mongodb-2.mongodb.myproject.svc.cluster.local."
dig "${service_name}" SRV +search +short | cut -d' ' -f4 | rev | cut -c2- | rev
}

# Initializes the replica set configuration.
#
# Arguments:
Expand Down Expand Up @@ -60,20 +48,7 @@ function add_member() {
local host="$1"
info "Adding ${host} to replica set ..."

# TODO: replace this with a call to `replset_addr` from common.sh, once it returns host names.
local endpoints
endpoints="$(find_endpoints | paste -s -d,)"

if [ -z "${endpoints}" ]; then
info "ERROR: couldn't add host to replica set!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi

local replset_addr
replset_addr="${MONGODB_REPLICA_NAME}/${endpoints}"

if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "${replset_addr}" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "$(replset_addr)" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
info "ERROR: couldn't add host to replica set!"
return 1
fi
Expand Down
65 changes: 65 additions & 0 deletions 3.0-upg/test/run
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function cleanup() {
echo "Done."
done
rmdir $CIDFILE_DIR

local network_name="mongodb-replset-$$"
docker network ls | grep -q ${network_name} && docker network rm ${network_name}
}
trap cleanup EXIT SIGINT

Expand Down Expand Up @@ -382,6 +385,67 @@ run_doc_test() {
echo
}

function run_local_replication_test() {
function print_logs() {
for file in $CIDFILE_DIR/replset*; do
echo "INFO: printing logs for CID file ${file}"
docker logs $(cat ${file})
done
}
trap print_logs ERR

echo "Testing replication on local docker"
#Initializing replicaset

cat > variables <<EOF
MONGODB_DATABASE=db
MONGODB_USER=user
MONGODB_PASSWORD=password
MONGODB_ADMIN_PASSWORD=adminPassword
MONGODB_REPLICA_NAME=rs0
MONGODB_KEYFILE_VALUE=xxxxxxxxxxxx
MONGODB_SMALLFILES=true
MONGODB_SERVICE_NAME=mongodb
EOF
source variables

local network_name="mongodb-replset-$$"
docker network create ${network_name}

docker run -d --cidfile $CIDFILE_DIR/replset0 --name=replset-0 --hostname=replset-0 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-0 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CIDFILE_DIR/replset1 --name=replset-1 --hostname=replset-1 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-1 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"
docker run -d --cidfile $CIDFILE_DIR/replset2 --name=replset-2 --hostname=replset-2 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-2 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"

local host="$(docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c '. /usr/share/container-scripts/mongodb/common.sh && echo $(replset_addr)')"

# Storing document into replset and wait replication to finish ...
docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c "set -e
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 3
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"

# Adding new container
docker run -d --cidfile $CIDFILE_DIR/replset3 --name=replset-3 --hostname=replset-3 --network ${network_name} --network-alias mongodb --env-file=variables $IMAGE_NAME run-mongod-replication
docker exec replset-3 bash -c "while ! [ -f /tmp/initialized ]; do sleep 1; done"

# Storing document into replset and wait replication to finish ...
docker run --rm --env-file=variables --network ${network_name} ${IMAGE_NAME} bash -c "set -e
. /usr/share/container-scripts/mongodb/common.sh
. /usr/share/container-scripts/mongodb/test-functions.sh
wait_for_mongo_up '${host}'
wait_replicaset_members '${host}' 4
insert_and_wait_for_replication '${host}' '{a:5, b:10}'"

rm variables
trap ERR

echo " Success!"
}

# Tests.
run_container_creation_tests
Expand All @@ -392,3 +456,4 @@ CONTAINER_ARGS="-u 12345" USER="user1" PASS="pass1" DB="test_db" ADMIN_PASS="r00
run_change_password_test
run_mount_config_test
run_doc_test
run_local_replication_test
2 changes: 1 addition & 1 deletion 3.2/Dockerfile.rhel7
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CMD ["run-mongod"]
RUN yum install -y yum-utils && \
yum-config-manager --enable rhel-server-rhscl-7-rpms && \
yum-config-manager --enable rhel-7-server-optional-rpms && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar rh-mongodb32-mongodb rh-mongodb32 rh-mongodb32-mongo-tools" && \
INSTALL_PKGS="bind-utils gettext iproute rsync tar hostname rh-mongodb32-mongodb rh-mongodb32 rh-mongodb32-mongo-tools" && \
yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS && \
rpm -V $INSTALL_PKGS && \
yum clean all
Expand Down
3 changes: 2 additions & 1 deletion 3.2/root/usr/share/container-scripts/mongodb/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ function replset_addr() {
local current_endpoints
current_endpoints="$(endpoints)"
if [ -z "${current_endpoints}" ]; then
echo >&2 "Cannot get address of replica set: no nodes are listed in service"
info "Cannot get address of replica set: no nodes are listed in service!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi
echo "${MONGODB_REPLICA_NAME}/${current_endpoints//[[:space:]]/,}"
Expand Down
27 changes: 1 addition & 26 deletions 3.2/root/usr/share/container-scripts/mongodb/init-replset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ source "${CONTAINER_SCRIPTS_PATH}/common.sh"
# (for example, "replica-2.mongodb.myproject.svc.cluster.local")
readonly MEMBER_HOST="$(hostname -f)"

# Outputs available endpoints (hostnames) to stdout.
# This also includes hostname of the current pod.
#
# Uses the following global variables:
# - MONGODB_SERVICE_NAME (optional, defaults to 'mongodb')
function find_endpoints() {
local service_name="${MONGODB_SERVICE_NAME:-mongodb}"

# Extract host names from lines like this: "10 33 0 mongodb-2.mongodb.myproject.svc.cluster.local."
dig "${service_name}" SRV +search +short | cut -d' ' -f4 | rev | cut -c2- | rev
}

# Initializes the replica set configuration.
#
# Arguments:
Expand Down Expand Up @@ -60,20 +48,7 @@ function add_member() {
local host="$1"
info "Adding ${host} to replica set ..."

# TODO: replace this with a call to `replset_addr` from common.sh, once it returns host names.
local endpoints
endpoints="$(find_endpoints | paste -s -d,)"

if [ -z "${endpoints}" ]; then
info "ERROR: couldn't add host to replica set!"
info "CAUSE: DNS lookup for '${MONGODB_SERVICE_NAME:-mongodb}' returned no results."
return 1
fi

local replset_addr
replset_addr="${MONGODB_REPLICA_NAME}/${endpoints}"

if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "${replset_addr}" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
if ! mongo admin -u admin -p "${MONGODB_ADMIN_PASSWORD}" --host "$(replset_addr)" --eval "while (!rs.add('${host}').ok) { sleep(100); }" --quiet; then
info "ERROR: couldn't add host to replica set!"
return 1
fi
Expand Down
Loading

0 comments on commit c64df5c

Please sign in to comment.