Can I Set Custom Ports for a Kubernetes Ingress to Listen on Besides 80 / 443?
Table of Contents
- Introduction
- What is Kubernetes Ingress?
- Setting Custom Ports in Kubernetes Ingress
- Common Errors and Troubleshooting
- Conclusion
Introduction
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.
Common Errors and Troubleshooting
Error 1: Port Already in Use
If you encounter a “port already in use” error, it could be due to a conflict with another service. Check for port availability and adjust accordingly.
Error 2: Ingress Not Reaching the Service
Troubleshoot connectivity issues by ensuring that your Ingress rules match the service selectors and that there are no networking problems within the cluster.
Error 3: Incorrect Ingress Annotations
Misconfigured annotations can lead to unexpected behavior. Double-check your Ingress annotations and refer to documentation for accurate syntax and options.
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.
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. Request a demo today to learn more.
Saturn Cloud provides customizable, ready-to-use cloud environments for collaborative data teams.
Try Saturn Cloud and join thousands of users moving to the cloud without
having to switch tools.