Python 使用 Ingress 时 pod 无法连接到 MongoDB
Python pod can't connect to MongoDB when using Ingress
每当我尝试通过工作节点的地址访问它时,它都可以正常连接,但当我尝试通过入口网关访问时却无法连接。我收到以下错误:
pymongo.errors.ServerSelectionTimeoutError
pymongo.errors.ServerSelectionTimeoutError: mongo:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 60119598e7c0e0d52f58c52c, topology_type: Single, servers: [<ServerDescription ('mongo', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('mongo:27017: timed out',)>]>
这就是我通过 python 连接到 MongoDB 的方式,在不通过入口 url.
访问时工作正常
mongo = MongoClient("mongodb://mongo:27017/user_data")
这是我的 ingress.yaml 文件
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: weasel-ingress
spec:
rules:
- host: {host-address}
http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: weasel
servicePort: 5000
- path: /
pathType: Prefix
backend:
serviceName: mongo
servicePort: 27017
知道如何通过入口连接吗?我想我需要在入口处添加 mongo?
这两项服务都已通过外部 IP 公开。
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.21.0.1 <none> 443/TCP 19h
mongo LoadBalancer 172.21.218.91 {exposed-ip} 27017:31308/TCP 17h
weasel LoadBalancer 172.21.152.134 {exposed-ip} 5000:32246/TCP 17h
入口日志:
kubectl describe ingress weasel-ingress
Name: weasel-ingress
Namespace: default
Address:
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
{host-address}
/ weasel:5000 (172.30.27.69:5000)
/ mongo:27017 (<none>)
Annotations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CREATE 27s nginx-ingress-controller Ingress default/weasel-ingress
Normal CREATE 27s nginx-ingress-controller Ingress default/weasel-ingress
Normal CREATE 27s nginx-ingress-controller Ingress default/weasel-ingress
入口主要用于 HTTP 连接,入口不是访问选项 mongodb。
您可以使用服务类型LoadBalancer或服务类型节点端口。
入口控制器(例如 Nginx-ingress)可以支持普通 TCP 负载平衡器。
我的 deployment.yaml 有问题。它需要更改为以下内容:
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
app: mongo
spec:
type: LoadBalancer
ports:
- port: 27017
name: http
selector:
app: mongo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
version: v1
spec:
containers:
- name: mongo
image: mongo:latest
ports:
- containerPort: 27017
每当我尝试通过工作节点的地址访问它时,它都可以正常连接,但当我尝试通过入口网关访问时却无法连接。我收到以下错误:
pymongo.errors.ServerSelectionTimeoutError
pymongo.errors.ServerSelectionTimeoutError: mongo:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 60119598e7c0e0d52f58c52c, topology_type: Single, servers: [<ServerDescription ('mongo', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('mongo:27017: timed out',)>]>
这就是我通过 python 连接到 MongoDB 的方式,在不通过入口 url.
访问时工作正常mongo = MongoClient("mongodb://mongo:27017/user_data")
这是我的 ingress.yaml 文件
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: weasel-ingress
spec:
rules:
- host: {host-address}
http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: weasel
servicePort: 5000
- path: /
pathType: Prefix
backend:
serviceName: mongo
servicePort: 27017
知道如何通过入口连接吗?我想我需要在入口处添加 mongo?
这两项服务都已通过外部 IP 公开。
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.21.0.1 <none> 443/TCP 19h
mongo LoadBalancer 172.21.218.91 {exposed-ip} 27017:31308/TCP 17h
weasel LoadBalancer 172.21.152.134 {exposed-ip} 5000:32246/TCP 17h
入口日志:
kubectl describe ingress weasel-ingress
Name: weasel-ingress
Namespace: default
Address:
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
{host-address}
/ weasel:5000 (172.30.27.69:5000)
/ mongo:27017 (<none>)
Annotations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CREATE 27s nginx-ingress-controller Ingress default/weasel-ingress
Normal CREATE 27s nginx-ingress-controller Ingress default/weasel-ingress
Normal CREATE 27s nginx-ingress-controller Ingress default/weasel-ingress
入口主要用于 HTTP 连接,入口不是访问选项 mongodb。
您可以使用服务类型LoadBalancer或服务类型节点端口。
入口控制器(例如 Nginx-ingress)可以支持普通 TCP 负载平衡器。
我的 deployment.yaml 有问题。它需要更改为以下内容:
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
app: mongo
spec:
type: LoadBalancer
ports:
- port: 27017
name: http
selector:
app: mongo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
version: v1
spec:
containers:
- name: mongo
image: mongo:latest
ports:
- containerPort: 27017