Skip to content
Kong Logo | Kong Docs Logo
search
  • Docs
    • Explore the API Specs
      View all API Specs View all API Specs View all API Specs arrow image
    • Documentation
      API Specs
      Kong Gateway
      Lightweight, fast, and flexible cloud-native API gateway
      Kong Konnect
      Single platform for SaaS end-to-end connectivity
      Kong Mesh
      Enterprise service mesh based on Kuma and Envoy
      decK
      Helps manage Kong’s configuration in a declarative fashion
      Kong Ingress Controller
      Works inside a Kubernetes cluster and configures Kong to proxy traffic
      Kong Gateway Operator
      Manage your Kong deployments on Kubernetes using YAML Manifests
      Insomnia
      Collaborative API development platform
      Kuma
      Open-source distributed control plane with a bundled Envoy Proxy integration
  • Plugin Hub
    • Explore the Plugin Hub
      View all plugins View all plugins View all plugins arrow image
    • Functionality View all View all arrow image
      View all plugins
      Authentication's icon
      Authentication
      Protect your services with an authentication layer
      Security's icon
      Security
      Protect your services with additional security layer
      Traffic Control's icon
      Traffic Control
      Manage, throttle and restrict inbound and outbound API traffic
      Serverless's icon
      Serverless
      Invoke serverless functions in combination with other plugins
      Analytics & Monitoring's icon
      Analytics & Monitoring
      Visualize, inspect and monitor APIs and microservices traffic
      Transformations's icon
      Transformations
      Transform request and responses on the fly on Kong
      Logging's icon
      Logging
      Log request and response data using the best transport for your infrastructure
  • Support
  • Community
  • Kong Academy
Get a Demo Start Free Trial
Kong Ingress Controller
2.0.x
  • Home icon
  • Kong Ingress Controller
  • Guides
  • TCPIngress with Kong
github-edit-pageEdit this page
report-issueReport an issue
  • Kong Gateway
  • Kong Konnect
  • Kong Mesh
  • Plugin Hub
  • decK
  • Kong Ingress Controller
  • Kong Gateway Operator
  • Insomnia
  • Kuma

  • Docs contribution guidelines
  • 3.0.x (latest)
  • 2.12.x
  • 2.11.x
  • 2.10.x
  • 2.9.x
  • 2.8.x
  • 2.7.x
  • 2.6.x
  • 2.5.x
  • 2.4.x
  • 2.3.x
  • 2.2.x
  • 2.1.x
  • 2.0.x
  • 1.3.x
  • 1.2.x
  • 1.1.x
  • 1.0.x
enterprise-switcher-icon Switch to OSS
On this pageOn this page
  • Overview
  • Installation
  • Testing Connectivity to Kong
  • Configure Kong for new ports
  • Install TCP echo service
  • TCP port based routing
  • TLS SNI based routing
  • Bonus
  • Conclusion
You are browsing documentation for an outdated version. See the latest documentation here.

TCPIngress with Kong

This guide walks through using the TCPIngress Custom Resource to expose TCP-based services running in Kubernetes to the outside world.

Overview

TCP-based Ingress means that Kong simply forwards the TCP stream to a Pod of a Service that’s running inside Kubernetes. Kong will not perform any sort of transformations.

There are two modes available:

  • Port based routing: In this mode, Kong simply proxies all traffic it receives on a specific port to the Kubernetes Service. TCP connections are load balanced across all the available pods of the Service.
  • SNI based routing: In this mode, Kong accepts a TLS-encrypted stream at the specified port and can route traffic to different services based on the SNI present in the TLS handshake. Kong will also terminate the TLS handshake and forward the TCP stream to the Kubernetes Service.

Installation

Please follow the deployment documentation to install the Kong Ingress Controller on your Kubernetes cluster.

Note: This feature works with Kong versions 2.0.4 and above.

Note: This feature is available in Controller versions 0.8 and above.

Testing Connectivity to Kong

This guide assumes that the PROXY_IP environment variable is set to contain the IP address or URL pointing to Kong. Please follow one of the deployment guides to configure this environment variable.

If everything is setup correctly, making a request to Kong should return HTTP 404 Not Found.

Note: If you are running the example using Minikube on MacOS, you may need to run minikube tunnel in a separate terminal window. This exposes LoadBalancer services externally, which is not enabled by default.

$ curl -i $PROXY_IP
HTTP/1.1 404 Not Found
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 48
Server: kong/1.2.1

{"message":"no Route matched with those values"}

This is expected as Kong does not yet know how to proxy the request.

Configure Kong for new ports

First, we will configure Kong’s Deployment and Service to expose two new ports 9000 and 9443. Port 9443 expects a TLS connection from the client.

$ kubectl patch deploy -n kong ingress-kong --patch '{
  "spec": {
    "template": {
      "spec": {
        "containers": [
          {
            "name": "proxy",
            "env": [
              {
                "name": "KONG_STREAM_LISTEN",
                "value": "0.0.0.0:9000, 0.0.0.0:9443 ssl"
              }
            ],
            "ports": [
              {
                "containerPort": 9000,
                "name": "stream9000",
                "protocol": "TCP"
              },
              {
                "containerPort": 9443,
                "name": "stream9443",
                "protocol": "TCP"
              }
            ]
          }
        ]
      }
    }
  }
}'
deployment.extensions/ingress-kong patched
$ kubectl patch service -n kong kong-proxy --patch '{
  "spec": {
    "ports": [
      {
        "name": "stream9000",
        "port": 9000,
        "protocol": "TCP",
        "targetPort": 9000
      },
      {
        "name": "stream9443",
        "port": 9443,
        "protocol": "TCP",
        "targetPort": 9443
      }
    ]
  }
}'
service/kong-proxy patched

Install TCP echo service

Next, we will install an example TCP service.

$ kubectl apply -f https://bit.ly/tcp-echo
deployment.apps/tcp-echo created
service/tcp-echo created

Now, we have a TCP echo service running in Kubernetes. We will now expose this on plain-text and a TLS based port.

TCP port based routing

To expose our service to the outside world, create the following TCPIngress resource:

$ echo "apiVersion: configuration.konghq.com/v1beta1
kind: TCPIngress
metadata:
  name: echo-plaintext
  annotations:
    kubernetes.io/ingress.class: kong
spec:
  rules:
  - port: 9000
    backend:
      serviceName: tcp-echo
      servicePort: 2701
" | kubectl apply -f -
tcpingress.configuration.konghq.com/echo-plaintext created

Here we are instructing Kong to forward all traffic it receives on port 9000 to tcp-echo service on port 2701.

Once created, we can see the IP address at which this is available:

$ kubectl get tcpingress
NAME             ADDRESS        AGE
echo-plaintext   <PROXY_IP>   3m18s

Lets connect to this service using telnet:

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

We can see here that the tcp-echo service is now available outside the Kubernetes cluster via Kong.

TLS SNI based routing

Next, we will demonstrate how Kong can help expose the tcp-echo service in a secure manner to the outside world.

Create the following TCPIngress resource:

$ echo "apiVersion: configuration.konghq.com/v1beta1
kind: TCPIngress
metadata:
  name: echo-tls
  annotations:
    kubernetes.io/ingress.class: kong
spec:
  rules:
  - host: example.com
    port: 9443
    backend:
      serviceName: tcp-echo
      servicePort: 2701
" | kubectl apply -f -
tcpingress.configuration.konghq.com/echo-tls created

Now, we can access the tcp-echo service on port 9443, on SNI example.com.

You should setup a DNS record for a Domain that you control to point to PROXY_IP and then access the service via that for production usage.

In our contrived demo example, we can connect to the service via TLS using openssl’s s_client command:

$ openssl s_client -connect $PROXY_IP:9443 -servername example.com -quiet
openssl s_client -connect 35.247.39.83:9443 -servername foo.com -quiet
depth=0 C = US, ST = California, L = San Francisco, O = Kong, OU = IT Department, CN = localhost
verify error:num=18:self signed certificate
verify return:1
depth=0 C = US, ST = California, L = San Francisco, O = Kong, OU = IT Department, CN = localhost
verify return:1
Welcome, you are connected to node gke-harry-k8s-dev-pool-1-e9ebab5e-c4gw.
Running on Pod tcp-echo-844545646c-gvmkd.
In namespace default.
With IP address 10.60.1.17.
This text will be echoed back.
This text will be echoed back.
^C

Since Kong is not configured with a TLS cert-key pair for example.com, Kong is returning a self-signed default certificate, which is not trusted. You can also see that the echo service is running as expected.

Bonus

Scale the tcp-echo Deployment to have multiple replicas and observe how Kong load-balances the TCP-connections between pods.

Conclusion

In this guide, we see how to use Kong’s TCP routing capabilities using TCPIngress Custom Resource. This can be very useful if you have services running inside Kubernetes that have custom protocols instead of the more popular HTTP or gRPC protocols.

Thank you for your feedback.
Was this page useful?
Too much on your plate? close cta icon
More features, less infrastructure with Kong Konnect. 1M requests per month for free.
Try it for Free
  • Kong
    Powering the API world

    Increase developer productivity, security, and performance at scale with the unified platform for API management, service mesh, and ingress controller.

    • Products
      • Kong Konnect
      • Kong Gateway Enterprise
      • Kong Gateway
      • Kong Mesh
      • Kong Ingress Controller
      • Kong Insomnia
      • Product Updates
      • Get Started
    • Documentation
      • Kong Konnect Docs
      • Kong Gateway Docs
      • Kong Gateway Enterprise Docs
      • Kong Mesh Docs
      • Kong Insomnia Docs
      • Kong Konnect Plugin Hub
    • Open Source
      • Kong Gateway
      • Kuma
      • Insomnia
      • Kong Community
    • Company
      • About Kong
      • Customers
      • Careers
      • Press
      • Events
      • Contact
  • Terms• Privacy• Trust and Compliance
© Kong Inc. 2023