Last active
March 6, 2019 18:18
-
-
Save reduardo7/00928a0a50bf51f7c9ba218e95eb37c3 to your computer and use it in GitHub Desktop.
Kubernetes: Connect to Mongo POD + port-forward
This file contains hidden or 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
#!/usr/bin/env bash | |
# https://gist.github.com/reduardo7/00928a0a50bf51f7c9ba218e95eb37c3 | |
################# Connect to Mongo DB ################# | |
# Params: | |
# - Environment: uat/prod | |
# - POD Name: Optional. Specific POD. | |
# | |
# Examples: | |
# ./connect-mongo.sh prod mongo-rs-mongodb-replicaset-1 | |
# ./connect-mongo.sh uat | |
# | |
# Full example: | |
# MONGO_FORWARD_PID=$(timeout 300 kubernetes/connect-mongo.sh uat) | |
# mongorestore \ | |
# -h localhost:27017 \ | |
# --drop -d conversifiprod \ | |
# /opt/speakapp/backup/prod_latest/conversifiprod | |
# kill ${MONGO_FORWARD_PID} | |
####################################################### | |
SERVER_ENV=$1 | |
MONGO_POD=$2 | |
if [ -z "${SERVER_ENV}" ]; then | |
echo 'Error! SERVER_ENV is required' >&2 | |
exit 1 | |
fi | |
if [ -z "${MONGO_POD}" ]; then | |
# get-mongo-master.sh => https://gist.github.com/reduardo7/c8ce682f7cb02f90168c5bec23fed53e | |
MONGO_POD="$(./get-mongo-master.sh ${SERVER_ENV})" | |
fi | |
if [ -z "${MONGO_POD}" ]; then | |
echo 'Error! MONGO_POD is required' >&2 | |
exit 1 | |
fi | |
kubectl port-forward -n ${SERVER_ENV} ${MONGO_POD} 27017:27017 >/dev/null 2>&1 & | |
PROC_PID=$! | |
cat <<EOF >&2 | |
# Kill with: | |
$ ps --pid ${PROC_PID} >/dev/null 2>&1 && kill ${PROC_PID} | |
EOF | |
echo ${PROC_PID} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment