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:
- NAMESPACE is the namespace where your MQTT broker is installed.
- 1883 is the port number for the MQTT service.
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:
- We added a new port mqtt-no-tls with the port number 1883.
- The targetPorts section maps the new port to the correct internal service port.
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:
- Enhance Flexibility: Cater to a wider range of application requirements without being constrained to HTTP(S) traffic only.
- Improve Efficiency: Centralize the management of various types of traffic through a single ingress controller, simplifying operations and reducing overhead.
- Boost Reliability: Ensure that critical services that rely on TCP communication are accessible and performant, thus maintaining high standards of service availability.
Business Implications
From a business perspective, this capability translates to several key advantages:
- Streamlined Operations: Simplifying the exposure of diverse services through a unified ingress controller can lead to more streamlined and manageable operations.
- Cost Savings: Reducing the need for additional networking infrastructure to handle different types of traffic can lead to significant cost savings.
- Scalability: As businesses grow and their needs become more complex, having a flexible and scalable ingress solution ensures they can adapt quickly without major overhauls.
- Competitive Edge: Offering robust and versatile service exposure capabilities can be a unique selling point, especially for companies providing cloud-native applications and services.
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