Proxy TCP traffic by port

Related Documentation
Tags
Related Resources
TL;DR

Create a TCPRoute or TCPIngress resource, which will then be converted in to a Kong Gateway Service and Route.

Prerequisites

If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

  1. The following Konnect items are required to complete this tutorial:
    • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Set the personal access token as an environment variable:

    export KONNECT_TOKEN='YOUR KONNECT TOKEN'
    
    Copied to clipboard!

Expose additional ports

Kong Gateway doesn’t include any TCP listen configuration by default. To expose TCP listens, update the Deployment’s environment variables and port configuration.

  1. Set the KONG_STREAM_LISTEN environment variable and expose port 9000 in the Deployment:

     kubectl patch deploy -n kong kong-gateway --patch '{
       "spec": {
         "template": {
           "spec": {
             "containers": [
               {
                 "name": "proxy",
                 "env": [
                   {
                     "name": "KONG_STREAM_LISTEN",
                     "value": "0.0.0.0:9000"
                   }
                 ],
                 "ports": [
                   {
                     "containerPort": 9000,
                     "name": "stream9000",
                     "protocol": "TCP"
                   }
                 ]
               }
             ]
           }
         }
       }
     }'
    
    Copied to clipboard!
  2. Update the proxy Service to indicate the new ports:

    kubectl patch service -n kong kong-gateway-proxy --patch '{
      "spec": {
        "ports": [
          {
            "name": "stream9000",
            "port": 9000,
            "protocol": "TCP",
            "targetPort": 9000
          }
        ]
      }
    }'
    
    Copied to clipboard!

Route TCP traffic

To publicly expose the Service, create a TCPRoute resource for Gateway APIs or a TCPIngress resource for Ingress.

This configuration instructs Kong Gateway to forward all traffic it receives on port 9000 to the echo Service on port 1025.

Validate your configuration

You can now test your Route using telnet:

telnet $PROXY_IP 9000
Copied to clipboard!

After you connect, type some text that you want as a response from the echo Service.

Trying 192.0.2.3...
Connected to 192.0.2.3.
Escape character is '^]'.
Welcome, you are connected to node gke-harry-k8s-dev-pool-1-e9ebab5e-c4gw.
Running on Pod echo-844545646c-gvmkd.
In namespace default.
With IP address 192.0.2.7.
This text will be echoed back.
This text will be echoed back.
^]
telnet> Connection closed.

To exit, press ctrl+] then ctrl+d.

Cleanup

kubectl delete -n kong -f https://developer.konghq.com/manifests/kic/echo-service.yaml
Copied to clipboard!

Did this doc help?

Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!