Skip to content

Commit

Permalink
basic config questions implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
cws committed Jun 12, 2016
1 parent a801d44 commit 8533701
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 27 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
# Build tools.
#

all:
build:
src/clean.sh
src/generate.sh
.PHONY: build

run:
src/create.sh
sleep 60
src/automate.sh
.PHONY: run

build:
src/generate.sh
.PHONY: build
config:
src/config.sh
.PHONY: config

clean:
src/clean.sh
Expand Down
12 changes: 6 additions & 6 deletions src/automate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ MONGOSPORT="27017"
CFGPORT="27018"
PORT=$1 && [ -z "${1}" ] && PORT="27020" || true

kubectl get pods -l role=mongoshard -o name > ./temp/podfile
kubectl get pods -l role=mongoshard -o name > ./tmp/podfile

i=1
while read p; do
ii=$(printf %02d "${i}")
POD=$(echo $p | cut -d / -f2)
NODE=$(echo $POD | cut -d'-' -f3 | cut -b 5-6)
JSFILE=$(ls node${NODE}-rs*)
JSFILE=$(ls ./build/node${NODE}-rs*)
RSNUM=$(echo $JSFILE | cut -d'-' -f2 | cut -b 3-4)
CONTAINER="rsp${RSNUM}-node${NODE}"
echo "${ii}: Initialize replication set rs${RSNUM} on node ${NODE}"
echo "Execute command on pod ${POD} and container ${CONTAINER}"
kubectl exec -ti ${POD} -c ${CONTAINER} mongo 127.0.0.1:${PORT} <./build/${JSFILE}
if [ -e "cfg${ii}-init.js" ]; then
kubectl exec -ti ${POD} -c ${CONTAINER} mongo 127.0.0.1:${PORT} <${JSFILE}
if [ -e "./build/cfg${ii}-init.js" ]; then
echo "Initialize Config Server Replication Set"
kubectl exec -ti ${POD} -c ${CONTAINER} mongo 127.0.0.1:${CFGPORT} <./build/cfg${ii}-init.js
fi
i=$((i+1))
done < ./temp/podfile
done < ./tmp/podfile

sleep 15
echo "Initialize Shard..."
kubectl exec -ti ${POD} -c ${CONTAINER} mongo 127.0.0.1:${MONGOSPORT} <./build/shard-init.js

sleep 15
echo "Initialize database collections for sharding ..."
kubectl exec -ti ${POD} -c ${CONTAINER} mongo 127.0.0.1:${MONGOSPORT} <./build/js-templates/shardkeys.js
kubectl exec -ti ${POD} -c ${CONTAINER} mongo 127.0.0.1:${MONGOSPORT} <./js-templates/shardkeys.js
18 changes: 18 additions & 0 deletions src/configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -eo pipefail

NODES=$1 && [ -z "${1}" ] && NODES="3" || true

echo
echo "Please supply some important configuration parameters below:"
echo "============================================================"
read -p "SSH user for cluster access [root]: " SSHUSER
[ -z "$SSHUSER" ] && SSHUSER="root" || true
read -p "SSH port for cluster access [22]: " SSHPORT
[ -z "$SSHPORT" ] && SSHPORT="22" || true
read -p "Root directory for your mongodb data [/data]: " BASEDIR
[ -z "$BASEDIR" ] && BASEDIR="/data" || true
read -p "Number of nodes for your shard [${NODES}]: " CFGNODES
[ -z "$CFGNODES" ] && CFGNODES=$NODES || true
echo
2 changes: 1 addition & 1 deletion src/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ while read p; do
echo "Create Deployment on machine ${NAME} (node${NODE})..."
kubectl create -f ./build/node${NODE}-deployment.yaml
i=$((i+1))
done < ./temp/nodefile
done < ./tmp/nodefile
2 changes: 1 addition & 1 deletion src/delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ while read p; do
echo "Create Deployment on machine ${NAME} (node${NODE})..."
kubectl delete -f ./build/node${NODE}-deployment.yaml
i=$((i+1))
done < ./temp/nodefile
done < ./tmp/nodefile
21 changes: 14 additions & 7 deletions src/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function genYamlFromTemplates(){
> $OUTFILE
OUTFILE="./tmp/yaml/node${NODENUM}-db${RSNUM}-volumes.yaml"
cat "${TEMPLATE_PATH}/volumes-template.yaml" \
| sed "s|__BASEDIR__|${BASEDIR}|g" \
| sed "s|__RSNUM__|${RSNUM}|g" \
| sed "/##/d" \
> $OUTFILE
Expand Down Expand Up @@ -255,6 +256,7 @@ genFinalFromPartials(){
${YAML_PATH}/node${NODENUM}-mgs*.yaml \
${TEMPLATE_PATH}/volumes-head.yaml \
${YAML_PATH}/node${NODENUM}-db*-volumes.yaml \
| sed "s|__BASEDIR__|${BASEDIR}|g" \
| sed "s|__CONFIG_SERVERS_SERVICES__|${CONFIG_SERVERS_SERVICES}|g" \
> "./build/node${NODENUM}-deployment.yaml"
done
Expand All @@ -270,15 +272,19 @@ genFinalFromPartials(){

# Ensure clean startup
cleanUp

# Gather basic config parameters
echo "Please ensure that pods can be scheduled on all these nodes."
echo "------------------------------------------------------------"
kubectl get nodes -o=name > ./tmp/nodefile
getNodeNames "./tmp/nodefile" "./tmp"
NODES=$(cat ./tmp/nodefile |wc -l)
#NODES=$((NODES+2))
echo "CLUSTER NODES.....................: ${NODES}"

# Ask for some config parameters
source src/configure.sh ${NODES}

echo "Please ensure that pods can be scheduled on all these nodes."
echo "------------------------------------------------------------"
NODES=${CFGNODES}
echo "CLUSTER NODES.....................: ${NODES}"
SHARDS=${NODES}
validateConstraints $SHARDS 1 $NODES
echo "SHARD MEMBERS.....................: ${SHARDS}"
Expand Down Expand Up @@ -367,6 +373,7 @@ done
genFinalFromPartials

echo 'Generate needed directories on remote server ...'
sh remote.sh ${SHARDS}

echo "Successfully executed. Inspect the yaml folder for the generated files."
./src/remote.sh ${SHARDS} ${SSHUSER} ${SSHPORT} ${BASEDIR}
echo
echo "Successfully executed."
echo "Execute 'make run' to fire up the mongodb shard on your cluster."
14 changes: 8 additions & 6 deletions src/remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

set -eo pipefail

REMOTEPORT=$2 && [ -z "${2}" ] && REMOTEPORT="22"
SSHUSER=$2 && [ -z "${2}" ] && SSHUSER="core" || true
SSHPORT=$3 && [ -z "${3}" ] && SSHPORT="22" || true
BASEDIR=$4 && [ -z "${4}" ] && BASEDIR="/data" || true

function sshCall(){
ssh -p $REMOTEPORT core@$1 $2 </dev/null
ssh -p $SSHPORT core@$1 $2 </dev/null
}
function execRemote(){
FILE=${1}
Expand All @@ -15,14 +17,14 @@ function execRemote(){
ii=$(printf %02d "${i}")
HOSTNAME=$(echo $pp | cut -d / -f2)
echo "Executing commands on ${HOSTNAME}..."
sshCall $HOSTNAME "sudo mkdir -p /enc/mongodb/db-cfg"
sshCall $HOSTNAME "sudo mkdir -p ${BASEDIR}/mongodb/db-cfg"
echo "Ensure correct kernel settings for mongodb..."
sshCall $HOSTNAME 'sudo /bin/bash -c "echo never > /sys/kernel/mm/transparent_hugepage/enabled"'
sshCall $HOSTNAME 'sudo /bin/bash -c "echo never > /sys/kernel/mm/transparent_hugepage/defrag"'
for j in $(seq ${SHARDS}); do
RSNUM=$(printf %02d ${j})
echo "Create directories for replica set ${RSNUM}."
sshCall $HOSTNAME "sudo mkdir -p /enc/mongodb/db-rs${RSNUM}"
sshCall $HOSTNAME "sudo mkdir -p ${BASEDIR}/mongodb/db-rs${RSNUM}"
done
i=$((i+1))
done < "$FILE"
Expand All @@ -33,8 +35,8 @@ if [ -z "$1" ]; then
exit 1
fi
echo "Number of Shards / Replication Sets: ${1}"
if [ -e ./temp/nodefile ]; then
execRemote "./temp/nodefile" $1
if [ -e ./tmp/nodefile ]; then
execRemote "./tmp/nodefile" $1
else
echo "You need to run generate.sh first."
exit 1
Expand Down
2 changes: 1 addition & 1 deletion yaml-templates/volumes-head.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
volumes:
- name: db-cfg
hostPath:
path: /enc/mongodb/db-cfg
path: __BASEDIR__/mongodb/db-cfg
2 changes: 1 addition & 1 deletion yaml-templates/volumes-template.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- name: db-rs__RSNUM__
hostPath:
path: /enc/mongodb/db-rs__RSNUM__
path: __BASEDIR__/mongodb/db-rs__RSNUM__

0 comments on commit 8533701

Please sign in to comment.