python - 如何(正确)在 Kubernetes 上部署 MongoDB 并从另一个 Pod/Job 访问它?
问题描述
一、问题描述
我正在尝试在其他/或可以访问它的地方运行MongoDB
Deployment
+ 。到目前为止,我显然已经成功部署了它,但是每当我尝试从,或中访问它时,我都会得到(请注意,我正在使用而不是为了访问主机;并且我的超时时间为 30 秒):Service
Kubernetes
Pods
Jobs
Containers
Job
Pod
Container
0.0.0.0
localhost
pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:30001: [Errno 111] Connection refused
2.在本地,它似乎工作......
如果我尝试通过 a 访问它Python
CLI
,它看起来确实有效:
>>> 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
和Service
配置
我的MongoDB
deployment
(改编自Nigel Poulton 的 Kubernetes Book)是:
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
它service
是:
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
解决方案
您在集群内部和外部的连接体验Kubernetes
会有所不同。
在集群中,您应该引用MongoDB
Pod
using<service-name>.<namespace-name>.svc.cluster.local
而不是0.0.0.0
. 所以,在你的情况下,host
最终会是hello-svc.default.svc.cluster.local
.
另请注意,port
应该将 引用为在集群中看到的NodePort
,而不是用于从外部访问集群的 。在你的情况下,那将是27017
.
推荐阅读
- git - DevOps - Mac M1:Git 停止工作 - 致命:无法访问共享锁文件
- linux - 从 Windows 进行 ssh-remote 后无法在 linux 上打开 vscode 编辑器
- python - Django 'ListBlogPost' 对象没有属性 'get_object'
- powerbi - Power BI 前 x 天
- angular - 更新到 Angular 12 后未调用 CanActivate
- angular - 重定向htaccess角度
- node.js - Nodemon 不启动服务器
- vue.js - 如何在侧边栏中收听滚动事件 [Bootstrap Vue]
- amazon-web-services - 模板错误:每个 Fn::Sub 对象的上下文对象的每个值都必须是字符串或返回字符串的函数
- wordpress - Wordpress 重定向 api 重定向到错误的 url