Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ingress controller to route TCP traffic

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?

like image 878
rasthiya Avatar asked Aug 06 '19 00:08

rasthiya


People also ask

What port does ingress listen to?

By default, a Kubernetes ingress will deploy 1 load balancer on only 1 host using http/https on default ports 80 / 443 . Rancher has added the ability to support multiple load balancers using the port of your choice.

Does Kubernetes use TCP or UDP?

Kubernetes Services support TCP (default), UDP, and SCTP protocols.

What is the role of ingress controller?

An Ingress controller abstracts away the complexity of Kubernetes application traffic routing and provides a bridge between Kubernetes services and external ones. Kubernetes Ingress controllers: Accept traffic from outside the Kubernetes platform, and load balance it to pods (containers) running inside the platform.

How do I add a TCP service to the nginx ingress controller?

To add a TCP service to the nginx ingress controller you can run the following command: 6379 : the port your service should listen to from outside the minikube virtual machine default : the namespace that your service is installed in We can verify that our resource was patched with the following command:

What is ingress controller sharding and how does it work?

Ingress Controller sharding by using route labels means that the Ingress Controller serves any route in any namespace that is selected by the route selector. Ingress Controller sharding is useful when balancing incoming traffic load among a set of Ingress Controllers and when isolating traffic to a specific Ingress Controller.

How does an ingress controller admit routes?

Each Ingress Controller admits routes from the set of routes. By default, all Ingress Controllers admit all routes. The Ingress Controller: Has two replicas by default, which means it should be running on two worker nodes. Can be scaled up to have more replicas on more nodes.

What ports does the ingress addon listen on?

The ingress addon uses the ingress nginx controller which by default is only configured to listen on ports 80 and 443. TCP and UDP services listening on other ports can be enabled.


1 Answers

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

like image 146
Hang Du Avatar answered Oct 20 '22 00:10

Hang Du