Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

Python pod can't connect to MongoDB when using Ingress

It can connect fine whenever I try to access it via the worker node's address, but not when I try access via the ingress gateway. I get the following error:

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',)>]>

This is how I connect to MongoDB via python which works fine when not accessing over the ingress url.

mongo = MongoClient("mongodb://mongo:27017/user_data")

This is my ingress.yaml file

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

Any idea's on how to get it to connect via ingress? I guess I need to add mongo to the ingress?

Both services are already exposed via external ip's.

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

ingress logs:

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
question from:https://stackoverflow.com/questions/65936076/python-pod-cant-connect-to-mongodb-when-using-ingress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ingress are mostly for HTTP connections, ingress is not option to access mongodb.

You can use the Service type LoadBalancer or service type Node port.

Ingress controllers(ex Nginx-ingress) can support plain TCP load balancers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...