Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2016 18:17
Show Gist options
  • Save anonymous/9306d830fd97aabd0484b97bb1a50d3e to your computer and use it in GitHub Desktop.
Save anonymous/9306d830fd97aabd0484b97bb1a50d3e to your computer and use it in GitHub Desktop.
preStop script for mongo replica set in kubernetes
#!/usr/bin/env bash
PORT=27017
MONGO_LABEL=${TRACK}-mongo
LOGFILE="/postStart.log"
date > $LOGFILE
# Get the pods referenced by the Headless Service
function getPods() {
curl -s -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
https://${KUBERNETES_SERVICE_HOST}/api/v1/namespaces/${TRACK}/endpoints/${MONGO_LABEL} \
| jq '.subsets[]? .addresses[]? .ip' \
| awk -F\" '{print $2":'$PORT'"}'
}
# Check if the pod is part of a replica set, and output name if it is master
function replicaSetMaster() {
OUTPUT=$(mongo $1 --quiet --eval "db.isMaster()['primary']")
eval "$2=${OUTPUT}"
}
function reconfig() {
echo "RECONFIGURING" >> $LOGFILE
echo "cfg=rs.conf(); \
healthy=rs.status().members.filter(function(d){return d.health>0;}).map(function(d){ return d.name;}); \
cfg.members=cfg.members.filter(function(d){return healthy.indexOf(d.host) >=0;}); \
rs.reconfig(cfg,{force:true}); \
" | mongo $1 --quiet >> $LOGFILE
}
function removeMember() {
echo "REMOVING $2 node from replicaSet" >> $LOGFILE
mongo $1 --quiet --eval "rs.remove('${2}',{'force':true})" >> $LOGFILE
}
function findMaster() {
local PODS
PODS=$(getPods)
MASTER=""
for POD in $PODS;
do
replicaSetMaster $POD M
if [[ "$M" != "" ]];
then
MASTER=$M
fi
done
echo "FOUND MASTER: $MASTER" >> $LOGFILE
}
# wait a bit for Service to register
sleep 10
THISNODE="$(hostname -i):${PORT}"
echo "MY IP: $THISNODE" >> $LOGFILE
# Get IPs of Pods in the cluster
# when first node, there won't be any as the pod is only registered by the Service once this hook has run
PODS=$(getPods)
echo "KUBERNETES PODS found: $PODS" >> $LOGFILE
# Find the master if there is one
findMaster
while [[ "$MASTER" == "" ]]; do
sleep 2
reconfig ${PODS[0]}
findMaster;
done
removeMember $MASTER $THISNODE
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment