Python pod can't connect to MongoDB when using Ingress

1/28/2021

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
-- jor2
kubernetes
kubernetes-ingress
mongodb
python

2 Answers

1/28/2021

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.

-- Harsh Manvar
Source: StackOverflow

1/28/2021

It was a problem with my deployment.yaml. It needed to be changed to the following:

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
-- jor2
Source: StackOverflow