How can I expose a service on multiple ports over HTTP in ingress?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of Kubernetes, exposing services externally is a common requirement. One might occasionally need to expose a service on multiple ports over HTTP using an ingress. This scenario can be particularly useful when a service has multiple functionalities or APIs each running on different ports and needs to be accessed externally. In this article, we'll walk through how you can accomplish this using Kubernetes Ingress resources.
Understanding Kubernetes Ingress
Before diving into multiple port exposure, it's crucial to have a basic understanding of what Kubernetes Ingress is. Ingress is a Kubernetes API object that manages external access to services within a cluster, typically HTTP. Ingress can be configured to provide load balancing, SSL termination, and name-based virtual hosting.
Core Components of Ingress
- Ingress Controller: A Daemon running on a cluster that watches for Ingress resources and alters the routing rules.
- Ingress Resource: Provides HTTP and HTTPS routing to services based on URIs and hostnames.
Exposing Services on Multiple Ports
When a service in Kubernetes is serving different functionalities on distinct ports, exposing those ports via an ingress can pose a challenge. While an ingress is primarily designed for routing HTTP paths and hostnames, you can configure it to expose multiple ports using different services and paths.
Step-by-step Guide
- Define Your Services:Each port that needs exposure will be associated with a separate service. This means if your application listens on ports 8080 and 9090, you will define two services:
- port: 8080
- port: 9090
- host: example.com
- path: /service1
- path: /service2
- Non-HTTP Services: Ingress inherently supports only HTTP/S protocols. For services not using these protocols, NodePort, LoadBalancer, or ClusterIP with external tools like MetalLB is preferable.
- Complex Routing Logic: For routing requirements beyond simple path or hostname-based routing, consider using a service mesh such as Istio, which provides advanced routing capabilities.

