<p>I'm trying to setup an ingress controller(nginx) to forward some TCP traffic to a kubernetes service(GCP). There's <a href="https://matthewpalmer.net/kubernetes-app-developer/articles/kubernetes-ingress-guide-nginx-example.html" rel="noreferrer">this tutorial</a> 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.</p>

<p>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. </p>

<pre class="lang-sh prettyprint-override"><code>python client.py --host &lt;EXTERNAL-IP-OF-LOAD-BALANCER&gt; --port &lt;PORT&gt;
</code></pre>

<p>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?</p>

<hr>

<p>UPDATE:
Following the <a href="https://stackoverflow.com/users/4666002/hang-du">HangDu</a>'s answer I created the following files.</p>

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

<p>and</p>

<pre><code>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
</code></pre>

<p>Then I used <code>kubectl create -f &lt;FILE_NAME&gt;</code> 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 <code>python client.py --host &lt;EXTERNAL-IP-OF-LOAD-BALANCER&gt; --port 9000</code> to run my echo client. However, I get a connection refused error when I do that. Am I doing something wrong?</p>

<p>I answered a similar question on another thread. <a href="https://stackoverflow.com/questions/57301394/how-to-use-nginx-ingress-tcp-service-on-different-namespace/57309255#57309255">How to use nginx ingress TCP service on different namespace</a></p>

<p>Basically, you can specify the port and backend for your service in configmap.</p>

<p>The following is the link to the document.
<a href="https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md" rel="noreferrer">https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md</a></p>
