一、问题描述
我正在尝试运行MongoDB
Deployment
+ Service
on Kubernetes
其他地方Pods
/Jobs
or Containers
可以访问它。到目前为止,我显然已经成功地部署了它,但是每当我尝试从内部访问它时Job
, Pod
or Container
,我得到(请注意,我正在使用0.0.0.0
代替localhost
为了访问主机;我的超时时间是 30 秒):
pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:30001: [Errno 111] Connection refused
2.在本地,它似乎有效......
如果我尝试通过Python
CLI
, it does看起来它确实有效:
>>> 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 created
尝试访问时是否应该使用另一个主机地址MongoDB
service
? (如果是的话,它显示在哪里kubectl describe svc <service_name>
?)
3. Deployment
and Service
配置
My MongoDB
deployment
(改编自Nigel Poulton 的 Kubernetes 书 https://rads.stackoverflow.com/amzn/click/com/B072TS9ZQZ) 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: 27017
And 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-mongo