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
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’sStatefulSet/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 to27017
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 thenmongodb-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 wellYour 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 withmongosh
Notwithstanding this, the points that Ernani makes are also relevant.