Mongo shell doesn't work through kubectl cli

6/15/2020

Actually Im trying to automate mongo pod replication whenever pod autoscale up/down. Instance, I'm writing shell script as below

mongo_id=`kubectl exec -it mongo-0 -- bash -c "mongo --eval 'rs.status()'" | grep -i -c _id`
pods=`kubectl get pods |grep -i "mongo*" | awk '{print $1}'|wc -l`
# continue until mongo_ids reaches to mongo pods
while [ $mongo_id -le $pods ]
do
        kubectl exec -it mongo-0 -- bash -c "mongo --eval 'rs.add("mongo-$a.mongo:27017")'"
        echo "mongo-$mongo_id node as added"
        mongo_id=$(( mongo_id+1 ))     # increments $mongo_id
done

Note: i already did mongo replication steps inside the pod. Just i need to run this script whenever new pods creates. Im able to run mongo --eval 'rs.add("mongo-$a.mongo:27017") inside the pod. but getting issue when i run through kubectl cli.

-- Sreekanth Chityala
kubernetes
mongodb
shell

0 Answers