Search code examples
pythonmongodbkubernetesflaskgoogle-kubernetes-engine

mongo command not working after logging into kubernetes pod


I wanted to check my mongo database, not sure which database my application is putting the data. Basically i have configured a very simple tasks application using a python flask api(deployed in gke) which in turn connects to a mongo database in the same gke cluster. The application works fine. I referred to this link link

Below is my application yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tasksapp
  labels:
    app: tasksapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tasksapp
  template:
    metadata:
      labels:
        app: tasksapp
    spec:
      containers:
        - name: tasksapp
          image: myimage/1.0
          ports:
            - containerPort: 5000
          imagePullPolicy: Always

The python code section which connects to the database --->This does not have username /password. Not sure which database it is connecting to(even though in the code it says as 'dev'. This is why I wanted to check the mongo db pod.

from bson.objectid import ObjectId
import socket
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://mongo:27017/dev"
mongo = PyMongo(app)
db = mongo.db
@app.route("/")
def index():
    hostname = socket.gethostname()
    return jsonify(
        message="Welcome to Tasks app! I am running inside {} pod!".format(hostname)
    )

the mongo db deployment yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
spec:
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: storage
              mountPath: /data/db
      volumes:
        - name: storage
          persistentVolumeClaim:
            claimName: mongo-pvc

the issue is that when i log into the pod for mongo, mongo command is not recognized as shown below?

kubectl exec -it mongo-869f6488c8-jnkgp -- /bin/bash
root@mongo-869f6488c8-jnkgp:/# cd home
root@mongo-869f6488c8-jnkgp:/home# ls
root@mongo-869f6488c8-jnkgp:/home# mongo
bash: mongo: command not found

Solution

  • Your deployment does not specify an image tag so will run the latest version of mongo (currently v6).

    v6 mongo does not include this legacy mongo shell; it has been replaced with mongosh

    docker run -it --entrypoint /bin/bash mongo
    root@e920eac138d7:/# mongo
    bash: mongo: command not found
    root@e920eac138d7:/# mongosh --nodb
    Current Mongosh Log ID: 648ce23ff5d64ce445218977
    Using Mongosh:          1.10.0
    
    For mongosh info see: https://docs.mongodb.com/mongodb-shell/
    
    
    To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
    You can opt-out by running the disableTelemetry() command.
    

    Notwithstanding this, the points that Ernani makes are also relevant.