skip to Main Content

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

2

Answers


  1. First things first, it is not advisable to run MongoDB as a Deployment, also some folks do refrain using Stateful apps in Kubernetes in general such as Databases because it can lead into many problems and it is a hard thing to manage. Some managed services really helps such as Atlas for MongoDB.

    Considering this environment is for testing purposes or self managed, I’d run Mongo using either a helm chart (when in dev mode) such as Bitnami’s or a more elegant and advanced Operator such as MongoDB Community Operator (while in Production and if I really know what I am doing and care for retain mode of PVCs, constant backups, etc.)

    Aside from this, to your actual use case, it depends on the namespace you are deploying your app and Mongo’s StatefulSet/Deployment/Pod.

    If you are just deploying it in the default namespace, make sure to point the hostname of your Mongodb in your Python app to <mongodb-svcname>.default.svc.cluster.local on MongoDB’s port, normally defaults to 27017

    So, again, if your Service is called mongodb-svc, in order for your Python app to be able to connect to it you’d use then mongodb-svc.default.svc.cluster.local:27017 as a valid hostname for it.

    Make sure to match both <service-name> and <namespace> and don’t forget the Port as well

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search