<p>I deployed mongodb in a Kubernetes cluster with this helm chart : <a href="https://github.com/helm/charts/tree/master/stable/mongodb" rel="noreferrer">https://github.com/helm/charts/tree/master/stable/mongodb</a>. All is right. I can connect to mongo from within a replicatset container or from outside the cluster with a port-forward, or with a NodePort service. But I can't connect via an ingress.</p>

<p>When the ingress is deployed, I can curl mongodb and have this famous message : "It looks like you are trying to access MongoDB over HTTP on the native driver port.". But I can't connect with a mongo client, the connection stucks and I can see in mongodb logs that I never reach mongo.</p>

<p>Does someone have any information about accessing mongodb via an ingress object ? Maybe it's a protocol problem ?</p>

<p>The ingress manifests :</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ template "mongodb.fullname" . }}
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: {{ .Values.ingress.hostName }}
    http:
      paths:
        - path: /
          backend:
            serviceName: "{{ template "mongodb.fullname" $ }}"
            servicePort: mongodb
  tls:
  - hosts:
    - {{ .Values.ingress.hostName }}
    secretName: secret
</code></pre>

<p>Thank you very much !</p>

<p>Ingress controllers are designed for HTTP connections, as the error hinted, the ingress is not the way to access mongodb.</p>

<p>None of the information in an ingress definition makes much sense for a plain TCP connection, <code>host</code> names and http URL <code>paths</code> don't apply to plain TCP connections. </p>

<p>Some ingress controllers (like <a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md" rel="noreferrer">nginx-ingress</a>) can support plain TCP load balancers but not via an ingress definition. They use custom config maps. </p>

<p>Use a Service with <code>type: loadBalancer</code> if your hosting environment supports it or <code>type: nodePort</code> if not. There is an <a href="https://github.com/helm/charts/blob/ef50e8c7114b097dd1fdd497912e57274608aecc/stable/mongodb/templates/svc-standalone.yaml" rel="noreferrer">example in the stable mongodb helm chart</a> and it's associated <a href="https://github.com/helm/charts/blob/ef50e8c7114b097dd1fdd497912e57274608aecc/stable/mongodb/values.yaml#L118-L141" rel="noreferrer">values</a>. </p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: {{ template "mongodb.fullname" . }}
  labels:
    app: {{ template "mongodb.name" . }}
spec:
  type: loadBalancer
  ports:
  - name: mongodb
    port: 27017
    targetPort: mongodb
  - name: metrics
    port: 9216
    targetPort: metrics
</code></pre>
