242 Red Hat OpenShift and IBM Cloud Paks on IBM Power Systems: Volume 1
Network tunneling for MongoDB
A common solution that is present (even on the MongoDB Compass client) is the SSH 
tunneling as a means of accessing the server. The intention is that the script takes the SSH 
tunnel and points to the correct cloud. 
This example uses the CLI commands, although Kubernetes APIs also can be used. 
Example C-1 shows the script that was used to complete the seamless application 
connection to the MongoDB database.
Example C-1   Script that looks for running MongoDB Pod and tunneling to correct cloud
AWSMASTERIP=3.15.11.140
POWERMASTERIP=10.108.98.213
ssh -M -S mongoconnect -fNT -L 27017:$POWERMASTERIP:32767 localhost
while true
do
TESTVAR=$(ssh root@$POWERMASTERIP -i /home/danielsc/.ssh/id_aws kubectl get pod 
appmongo-ibm-mongodb-dev -o custom-columns=:{status.phase} -n default 
2>>/dev/null)
if [ $? -eq 0 ]
   then
        if [ $(cat /proc/"$(ssh -S /home/danielsc/mongo/mongoconnect -O check 
localhost 2>&1 |cut -d= -f2 | sed 's/)//')"/cmdline|cut -d: -f2) == 
"$POWERMASTERIP" ]
                then
                        TESTVAR=$(echo -n $TESTVAR)
                        echo "Pod status is $TESTVAR"
                        echo "Tunnel already connected to IBM correctly"
                else
                        ssh -S mongoconnect -O exit -p 443 localhost 2>>/dev/null
                        TESTVAR=$(echo -n $TESTVAR)
                        echo "Pod status is $TESTVAR"
                        ssh -M -S mongoconnect -fNT -L 27017:$POWERMASTERIP:32767 
localhost
                        echo "Tunneled to MongoDB running on IBM"
        fi
fi
TESTVAR=$(ssh root@$AWSMASTERIP -i /home/danielsc/.ssh/id_aws kubectl get pod 
appmongo-ibm-mongodb-dev -o custom-columns=:{.status.phase} -n default 
2>>/dev/null)
if [ $? -eq 0 ]
   then
        if [ $(cat /proc/"$(ssh -S /home/danielsc/mongo/mongoconnect -O check 
localhost 2>&1 |cut -d= -f2 | sed 's/)//')"/cmdline|cut -d: -f2) == 
"a1765d068fc8911e9b07106df2fba1c0-125393943.us-east-2.elb.amazonaws.com" ]
                then
                        TESTVAR=$(echo -n $TESTVAR)
                        echo "Pod status is $TESTVAR"
                        echo "Tunnel already connected to AWS correctly"
                else
                        ssh -S mongoconnect -O exit  localhost 2>>/dev/null