<p>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
<a href="https://levelup.gitconnected.com/deploy-your-first-flask-mongodb-app-on-kubernetes-8f5a33fa43b4" rel="nofollow noreferrer">link</a></p>
<p>Below is my application yaml file</p>
<pre><code>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
</code></pre>
<p>The python code section which connects to the database ---&gt;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.</p>
<pre><code>from bson.objectid import ObjectId
import socket
app = Flask(__name__)
app.config[&quot;MONGO_URI&quot;] = &quot;mongodb://mongo:27017/dev&quot;
mongo = PyMongo(app)
db = mongo.db
@app.route(&quot;/&quot;)
def index():
    hostname = socket.gethostname()
    return jsonify(
        message=&quot;Welcome to Tasks app! I am running inside {} pod!&quot;.format(hostname)
    )
</code></pre>
<p>the mongo db deployment yaml</p>
<pre><code>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
</code></pre>
<p>the issue is that when i log into the pod for mongo, mongo command is not recognized as shown below?</p>
<pre><code>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
</code></pre>

bionics parv
<p>First things first, it is not advisable to run MongoDB as a <code>Deployment</code>, 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.</p>
<p>Considering this environment is for <strong>testing purposes</strong> or <strong>self managed</strong>, I'd run Mongo using either a helm chart <strong>(when in dev mode)</strong> such as <a href="https://github.com/bitnami/charts/tree/main/bitnami/mongodb/#installing-the-chart" rel="nofollow noreferrer">Bitnami's</a> or a more elegant and advanced Operator such as <a href="https://github.com/mongodb/mongodb-kubernetes-operator" rel="nofollow noreferrer">MongoDB Community Operator</a> <strong>(while in Production and if I really know what I am doing and care for retain mode of PVCs, constant backups, etc.)</strong></p>
<p>Aside from this, to your actual use case, it depends on the <code>namespace</code> you are deploying your app and Mongo's <code>StatefulSet/Deployment/Pod</code>.</p>
<p>If you are just deploying it in the <code>default</code> namespace, make sure to point the hostname of your Mongodb in your Python app to  <code>&lt;mongodb-svcname&gt;.default.svc.cluster.local</code> on MongoDB's port, normally defaults to <code>27017</code></p>
<p>So, again, if your Service is called <code>mongodb-svc</code>, in order for your Python app to be able to connect to it you'd use then <code>mongodb-svc.default.svc.cluster.local:27017</code> as a valid hostname for it.</p>
<p>Make sure to match both <code>&lt;service-name&gt;</code> and <code>&lt;namespace&gt;</code> and don't forget the <strong>Port</strong> as well</p>
