Can I Set Custom Ports for a Kubernetes Ingress to Listen on Besides 80 / 443?

Can I Set Custom Ports for a Kubernetes Ingress to Listen on Besides 80 / 443?
Kubernetes, the open-source platform for automating deployment, scaling, and management of containerized applications, is a powerful tool in the hands of data scientists. One of the common questions that arise when working with Kubernetes is whether it’s possible to set custom ports for an Ingress to listen on, besides the default ports 80 and 443. In this blog post, we’ll explore this question in detail.
What is Kubernetes Ingress?
Before diving into the main topic, let’s briefly discuss what Kubernetes Ingress is. Ingress in Kubernetes is an API object that manages external access to the services in a cluster, typically HTTP. Ingress can provide load balancing, SSL termination, and name-based virtual hosting.
By default, Kubernetes Ingress listens on ports 80 (for HTTP) and 443 (for HTTPS). But what if you want to use a different port? Can you set a custom port for your Ingress to listen on?
Setting Custom Ports in Kubernetes Ingress
The short answer is no, you can’t directly set custom ports in Kubernetes Ingress. The Ingress resource in Kubernetes does not support other ports than 80 and 443. This is by design, as the Ingress resource is meant to handle HTTP and HTTPS traffic, which traditionally operate on these ports.
However, this doesn’t mean you’re out of options. There are workarounds to this limitation, and we’ll discuss two of them: using a Service of type NodePort or LoadBalancer, or using a custom Ingress controller.
Using a Service of Type NodePort or LoadBalancer
One way to expose your service on a custom port is by using a Service of type NodePort or LoadBalancer. A NodePort service listens on a static port on each Node’s IP. A LoadBalancer service, on the other hand, provisions a load balancer for your application in supported cloud providers.
Here’s an example of a NodePort service:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
selector:
app: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376
nodePort: 30080
In this example, the service my-service
is exposed on each Node’s IP at port 30080
, and routes traffic to the MyApp
application on port 9376
.
Using a Custom Ingress Controller
Another workaround is to use a custom Ingress controller that supports listening on custom ports. An Ingress controller is responsible for fulfilling the Ingress, usually with a load balancer.
One such Ingress controller is Traefik. Traefik is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. It supports custom ports, among other features.
Here’s an example of how you can configure Traefik to listen on a custom port:
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: my-ingress
spec:
entryPoints:
- websecure
routes:
- match: Host(`myapp.mydomain.com`)
kind: Rule
services:
- name: my-service
port: 8443
In this example, the IngressRoute my-ingress
listens on the websecure
entry point, which can be configured to use a custom port.
Conclusion
While Kubernetes Ingress does not natively support custom ports, there are workarounds available. You can use a Service of type NodePort or LoadBalancer, or a custom Ingress controller like Traefik. These options provide flexibility in managing external access to your services, allowing you to tailor your Kubernetes setup to your specific needs.
Remember, Kubernetes is a powerful tool, but with great power comes great responsibility. Always ensure that your configurations follow best practices for security and scalability.
Stay tuned for more Kubernetes tips and tricks!
Keywords: Kubernetes, Ingress, Custom Ports, NodePort, LoadBalancer, Traefik, Data Science, Microservices, Load Balancer, SSL Termination, Virtual Hosting, HTTP, HTTPS
Meta Description: Learn how to set custom ports for a Kubernetes Ingress to listen on besides the default ports 80 and 443. Explore workarounds using a Service of type NodePort or LoadBalancer, or a custom Ingress controller like Traefik.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.