20

I'm trying to setup an ingress controller(nginx) to forward some TCP traffic to a kubernetes service(GCP). There's this tutorial that shows how to route HTTP traffic to services (based on the paths) using nginx. I want to have a similar setup to forward TCP traffic.

In my cluster, I have a pod running a TCP echo server written in python using sockets. There is a service attached to the pod. If I set the service type of this service to LoadBalancer, I can run my client as follows and get the echo from the cluster.

python client.py --host <EXTERNAL-IP-OF-LOAD-BALANCER> --port <PORT>

Similar to the echo server, I have other TCP services in my cluster that serves other pods. Currently I have set all of them to LoadBalancers. So, they have external IPs and listen for traffic on different ports. However, I do not want to create LoadBalancers to all of these services. How would I use the nginx to route the TCP traffic to different services based on the port numbers. If nginx cannot do this, are there other options that I can use to achieve this?


UPDATE: Following the HangDu's answer I created the following files.

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: default
data:
  9000: "default/echo-service:50000"

and

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: default
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
    - name: proxied-tcp-9000
      port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

Then I used kubectl create -f <FILE_NAME> to create the config map and the service. So I was hoping I could use the external IP of the newly created service and the port 9000 and run python client.py --host <EXTERNAL-IP-OF-LOAD-BALANCER> --port 9000 to run my echo client. However, I get a connection refused error when I do that. Am I doing something wrong?

rasthiya
  • 650
  • 1
  • 6
  • 20
  • Is your `nginx-ingress-controller` pod in namespace `default`? – menya Aug 06 '19 at 02:45
  • Yes. I know I should've used multiple namespaces but currently I have the ingress in the default namespace. – rasthiya Aug 06 '19 at 12:22
  • 2
    I forgot to add the `"--tcp-services-configmap=default/tcp-services"` to the args in the nginx-ingress pod. It's working now – rasthiya Aug 06 '19 at 15:10
  • 1
    @rasthiya how do you add that to the nginx-ingress pod? I installed ingress nginx with the azure installer. It was all set up once it was on my system but that was not shown. – mjwrazor May 05 '20 at 16:05
  • @mjwrazor I've modified the deployment yaml file. Haven't done anything similar in Azure, but in google GKE you just go to workloads and edit the corresponding yaml file. – rasthiya May 05 '20 at 18:38
  • After creating the ingress controller I only see a service but no nginx-ingress pod in any namespace. Am I missing something? – Abinash Gupta Jul 23 '20 at 08:00
  • This configmap makes nginx acting like a LB or we need a Cloud LB anyway? – Arzhr Jul 23 '20 at 16:18

1 Answers1

6

I answered a similar question on another thread. How to use nginx ingress TCP service on different namespace

Basically, you can specify the port and backend for your service in configmap.

The following is the link to the document. https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md

Hang Du
  • 929
  • 6
  • 13
  • can you please assist me on my issue? I desrbibed it completely... Please if you can assist me on similar issue. Although I installed "ingress" as Service in completely different way using the Helm that is why I am confused... Thank you!!! https://stackoverflow.com/questions/66190275/kubernetes-ingress-controller-cannot-tcp-connect-from-outside-virtual-machine – vel Feb 14 '21 at 09:18