I'm trying to run a MongoDB Deployment + Service on Kubernetes where other Pods/Jobs or Containers could access it. So far I have apparently been successful at deploying it, however whenever I try to access it from within a Job, Pod or Container, I get (note that I'm using 0.0.0.0 instead of localhost in order to access the host machine; and that my timeout is 30 seconds):
pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:30001: [Errno 111] Connection refusedIf I try to access it through a Python CLI, it does look like it works though:
>>> import pymongo
>>> client = pymongo.MongoClient(host='0.0.0.0', port=30001) # 'localhost' also works
>>> client.list_database_names()
['admin', 'config', 'local', 'test_db'] # 'test_db' is a db I had previously createdShould I use another host address when trying to access the MongoDB service? (If so, where does it show on kubectl describe svc <service_name>?)
Deployment and Service ConfigurationsMy MongoDB deployment (adapted from Nigel Poulton's Kubernetes Book) is:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mymongodb-dep
spec:
replicas: 1
selector:
matchLabels:
app: hello-mongo
minReadySeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: hello-mongo
spec:
containers:
- name: mongo
image: mongo
imagePullPolicy: IfNotPresent
ports:
- containerPort: 27017And its service is:
apiVersion: v1
kind: Service
metadata:
name: hello-svc
labels:
app: hello-mongo
spec:
type: NodePort
ports:
- port: 27017
nodePort: 30001
protocol: TCP
selector:
app: hello-mongoYour connectivity experience from within the Kubernetes cluster and from outside will be different.
From within the cluster you should reference the MongoDB Pod using <service-name>.<namespace-name>.svc.cluster.local rather than 0.0.0.0. So, in your case, the host would end up being hello-svc.default.svc.cluster.local.
Also note that the port should be referenced as the one seen within the cluster and not the NodePort, which is used to access the cluster from outside. In your case, that would be 27017.