Back to Blog Uncategorized

How to Expose TCP Services with Ingress-NGINX Controller in K8s

July 22, 2024

In the dynamic world of Kubernetes, exposing services to external traffic is a common yet crucial task. Recently, we faced a challenge from a customer who needed to expose an MQTT Broker service within a Kubernetes cluster. MQTT, a standard messaging protocol for the Internet of Things (IoT), is widely used across various industries, from smart homes to petrochemical sectors.

Our challenge stemmed from the fact that while the Ingress-NGINX controller in Kubernetes is typically used to manage HTTP(S) traffic, the MQTT Broker service only deals with TCP traffic. This initially seemed like a hurdle because Ingress-NGINX by default handles only HTTP(S) traffic. However, after delving into the Ingress-NGINX specifications, we discovered that it could be configured to handle external TCP traffic and route it to internal services. Let’s walk through the process step by step.

 

Step 1: Create a ConfigMap

The first step is to create a ConfigMap that maps the external port to the internal service port. This ConfigMap acts as a bridge, defining which port on the ingress should forward traffic to which service within the cluster.

Here’s an example of what the ConfigMap looks like:

“`yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: tcp-services

  namespace: ingress-nginx

data:

  1883: “${NAMESPACE}/mqtt-broker:1883”

“`

 

In this ConfigMap:

 

 

Step 2: Update the Helm Chart

Next, we need to inform the Ingress-NGINX controller about this new ConfigMap. If you’re using a Helm chart to install the Ingress controller, you should make the following changes in the values.yaml file:

“`yaml

tcp:

  1883: ${NAMESPACE}/mqtt-broker:1883

controller:

  extraArgs:

    tcp-services-configmap: ingress-nginx/tcp-services

“`

 

These lines tell the controller to use the specified ConfigMap for TCP services, effectively linking the external port 1883 to the MQTT broker.

 

Step 3: Update the Service

Since we’re exposing a new TCP connection on a new port, we need to update the service configuration as well:

“`yaml

service:

  ports:

    http: 80

    https: 443

    mqtt-no-tls: 1883 # new port for TCP

  targetPorts:

    http: http

    https: https

    mqtt-no-tls: mqtt-no-tls # new port for TCP

“`

 

In this configuration:

 

 

Applying the Changes

Once you’ve updated these configurations, apply the changes to your Kubernetes cluster. The Ingress controller will start listening on port 1883 and route all TCP traffic to the MQTT broker on the specified port.

And that’s it! You’ve successfully configured the Ingress-NGINX controller to expose a TCP service within your Kubernetes cluster. This setup ensures that external TCP traffic can seamlessly reach your MQTT broker, allowing for efficient and reliable communication in your IoT applications.

 

Industry Relevance

This solution is not just relevant to those using MQTT. Any organization leveraging Kubernetes to manage their infrastructure may encounter scenarios where they need to expose non-HTTP services. By understanding how to configure Ingress-NGINX to handle TCP traffic, businesses can:

 

 

Business Implications

From a business perspective, this capability translates to several key advantages:

 

By leveraging the techniques outlined here, businesses can ensure they are well-equipped to handle a variety of traffic types, making their Kubernetes deployments more versatile and resilient.

 

Useful Links:

 

We hope this guide helps you manage your TCP services in Kubernetes more effectively. Feel free to reach out if you have any questions or need further assistance!

 

Author: Anna Chulets