Kubernetes
Ingress
Custom Ports
Networking
Configuration

Can I set custom ports for a Kubernetes ingress to listen on besides 80 / 443?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In Kubernetes, the ingress resource is utilized to manage external access to services within a cluster, typically supporting HTTP and HTTPS traffic. By default, ingress controllers operate on well-known ports: 80 for HTTP and 443 for HTTPS. However, there might be scenarios where you wish to use custom ports. Below is a detailed exploration of how you can achieve this, along with potential limitations and examples.

Understanding Kubernetes Ingress

Kubernetes ingress provides HTTP and HTTPS routing to services based on defined rules. Typically, it is a collection of rules that permit the host and path-based routing to the services.

The typical usage involves ingress controllers that encapsulate specific functionalities, such as:

  • Nginx Ingress Controller: Routes and manages external access to the services.
  • Traefik, Istio, among others, with specialized capabilities.

By default, Kubernetes ingress relies on ports 80 and 443. But what if you need to manage traffic through different ports, such as 8080 or another custom port?

Configuring Custom Ports

Limitations

  1. Ingress Controller Dependency: Not all ingress controllers support custom ports. The ability to set a custom port is directly tied to the ingress controller being used.
  2. Protocol Constraints: Ingress is designed for HTTP-based traffic, so non-HTTP traffic needs alternative methods like using NodePort or LoadBalancer.

Example with Nginx Ingress

By default, the Nginx Ingress Controller listens on two ports, 80 and 443. To override this behavior, consider the following configuration:

  1. Custom ConfigMap: Create a custom ConfigMap defining the port:
yaml
1   apiVersion: v1
2   kind: ConfigMap
3   metadata:
4     name: custom-nginx-configuration
5     namespace: ingress-nginx
6   data:
7     allow-http: "true" # Enable HTTP
8     http-port: "8080" # Define custom HTTP port
9     https-port: "8443" # Define custom HTTPS port
  1. Ingress Controller Deployment: Add the following environment variables to the ingress-nginx-controller deployment or modify the default configuration for Nginx:
yaml
1   - name: HTTP_PORT
2     value: "8080"
3   - name: HTTPS_PORT
4     value: "8443"
  1. Service Configuration: Ensure the Service exposing the Nginx controller is updated to accommodate custom ports. Here is an example for NodePort configuration.
yaml
1   apiVersion: v1
2   kind: Service
3   metadata:
4     name: ingress-nginx
5     namespace: ingress-nginx
6   spec:
7     ports:
8       - port: 8080
9         targetPort: 8080
10         protocol: TCP
11         name: http
12         nodePort: 30080 # Custom NodePort mapping
13       - port: 8443
14         targetPort: 8443
15         protocol: TCP
16         name: https
17         nodePort: 30443 # Custom NodePort mapping
18     selector:
19       app: ingress-nginx
20     type: NodePort

Using Alternative Approaches

If the ingress controller doesn't support custom ports, you can consider using a LoadBalancer or NodePort type service to expose the service on a different port:

  • NodePort: Exposes the service on each node's IP at a static port, defined by nodePort.
  • LoadBalancer: Creates an external load balancer in supported cloud environments and assigns it a fixed external IP.

Summary Table of Key Points

AspectDetails
Default Ingress Ports80 (HTTP), 443 (HTTPS)
Customization CapabilityController-dependent; not universally supported
Common Ingress ControllersNginx, Traefik, Istio
Custom Port ConfigurationModify ConfigMap and ingress controller deployment
Alternative ApproachesUse NodePort or LoadBalancer for custom ports

Conclusion

Setting custom ports beyond 80 and 443 for Kubernetes ingress is indeed feasible, though it is subject to the capabilities of the ingress controller in use. It's crucial to understand the IaaS being employed and adjust the network policies and configurations in tandem. Custom configurations often involve changes to ConfigMap, service definitions, and the ingress controller deployments themselves. Always verify the documentation of the ingress controller for specific support and configurations to avoid accidental misconfigurations.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design