Skip to content

Instantly share code, notes, and snippets.

@reduardo7
Last active March 6, 2019 18:18
Show Gist options
  • Save reduardo7/c8ce682f7cb02f90168c5bec23fed53e to your computer and use it in GitHub Desktop.
Save reduardo7/c8ce682f7cb02f90168c5bec23fed53e to your computer and use it in GitHub Desktop.
Kubernetes: Get Mongo master of replicaset.
#!/usr/bin/env bash
# https://gist.github.com/reduardo7/c8ce682f7cb02f90168c5bec23fed53e
################# Get Master Mongo POD #################
# Params:
# - Environment: uat/prod
#
# Examples:
# ./get-mongo-master.sh prod
# ./get-mongo-master.sh uat
#######################################################
SERVER_ENV=$1
if [ -z "${SERVER_ENV}" ]; then
echo 'Error! SERVER_ENV is required' >&2
exit 1
fi
for pod_name in $(kubectl get pods -n=${SERVER_ENV} --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep 'mongo-rs-mongodb-replicaset-')
do
if echo $(kubectl exec -n ${SERVER_ENV} ${pod_name} -- sh -c 'mongo --eval="rs.isMaster().ismaster ? 7111111 : 7222222"') | grep -q '7111111'
then
echo ${pod_name}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment