Trying to expose plain TCP with nginx-ingress-controller on Kubernetes Bare-Metal

2.3k views Asked by At

I'm currently learning kubernetes bases and i would like to expose a mongodb outside of my cluser. I've setting up my nginx ingress controller and followig this doc to expose plain TCP connexion.

This is my Ingress Service configuration :

apiVersion: v1
kind: Service
metadata:
  annotations:
  labels:
    helm.sh/chart: ingress-nginx-4.0.15
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/version: 1.1.1
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: controller
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  type: NodePort
  ipFamilyPolicy: SingleStack
  externalIPs:
  - 172.30.63.51
  ipFamilies:
    - IPv4
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: http
      appProtocol: http
    - name: https
      port: 443
      protocol: TCP
      targetPort: https
      appProtocol: https
    - name: proxied-tcp-27017
      port: 27017
      protocol: TCP
      targetPort: 27017
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/component: controller

The configmap to proxy TCP connexions :

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: ingress-nginx
data:
  27017: "global-stack/global-stack-mongo-svc:27017"

My ingress conroller works well on ports 80 and 443 to expose my services but impossible for me to access to port 27017

Result of kubectl get svc -n ingress-nginx :

NAME                                 TYPE        CLUSTER-IP      EXTERNAL-IP    PORT(S)                                
ingress-nginx-controller             NodePort    10.97.149.93    172.30.63.51   80:30159/TCP,443:32585/TCP,27017:30098/TCP
ingress-nginx-controller-admission   ClusterIP   10.107.33.165   <none>         443/TCP                                  

External IP is well responding to curl 172.30.63.51:80

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

But can't respond to port 27017 :

curl: (7) Failed to connect to 172.30.63.51 port 27017: Connection refused

My mongo service :

apiVersion: v1
kind: Service
metadata:
  name: global-stack-mongo-svc
  namespace: global-stack
  labels:
    app: global-stack-mongo-app
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 27017
    protocol: TCP
    targetPort: 27017
  selector:    
    app: global-stack-mongo-app

The services's cluster IP is 10.244.1.57 and well responding

>> curl 10.244.1.57:27017

It looks like you are trying to access MongoDB over HTTP on the native driver port.

If anyone could help me I would be very grateful. Thanks

Guieme.

1

There are 1 answers

0
Guieme On BEST ANSWER

After some research I solved my issue.

in the nginx-ingress documentation, it's not described but you need to mapp the TCP config map with the ingress-controller container with this lines into the deployment file :

  args:
    - /nginx-ingress-controller
    - --election-id=ingress-controller-leader
    - --controller-class=k8s.io/ingress-nginx
    - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
    - --validating-webhook=:8443
    - --validating-webhook-certificate=/usr/local/certificates/cert
    - --validating-webhook-key=/usr/local/certificates/key
    - --tcp-services-configmap=ingress-nginx/tcp-services