Yes, we can set up custom ports for Kubernetes Ingress. This is possible beyond the default ports 80 and 443. By changing some settings in our Ingress resources, we can show our applications on any port we want. This gives us more flexibility for different services and helps us meet different networking needs.
In this article, we will look at how to set up custom ports for Kubernetes Ingress. We will learn about Kubernetes Ingress controllers and their limits. We will explain how to define custom ports in Ingress resources. Also, we will see how to set up NodePort and LoadBalancer services for custom ports. We will also use the NGINX Ingress Controller to manage custom ports better. Lastly, we will answer some common questions about this topic.
- Understanding Kubernetes Ingress Controllers and Their Limits
- How to Define Custom Ports in Ingress Resources
- Configuring NodePort Services for Custom Ports in Kubernetes Ingress
- Using LoadBalancer Services for Custom Ports with Ingress
- Using NGINX Ingress Controller for Custom Ports
- Frequently Asked Questions
Understanding Kubernetes Ingress Controllers and Their Limitations
Kubernetes Ingress Controllers help us manage how outside users access services in a Kubernetes cluster. They take care of HTTP and HTTPS traffic routing. Think of them as a bridge between users on the internet and our services inside the cluster. Ingress Controllers let us set rules for routing. They also help with SSL termination and routing based on host or path.
Key Types of Ingress Controllers
- NGINX Ingress Controller: This is the most popular controller. It gives many features and supports custom settings.
- Traefik: This is a dynamic reverse proxy. It works well with Kubernetes and updates its settings automatically when services change.
- HAProxy Ingress: This one offers great performance and advanced load balancing options.
Limitations of Kubernetes Ingress Controllers
- Default Ports: Ingress Controllers usually listen on ports 80 (HTTP) and 443 (HTTPS). If we want to use other ports, we need to do extra work.
- Protocol Support: Most Ingress Controllers mainly support HTTP and HTTPS. They may not work well with other protocols.
- Load Balancing: The default load balancing settings might not fit all applications. We may need to change them to fit our needs.
- Complexity in Configuration: Setting advanced routing rules can make things complicated. This can make it harder to manage.
Example: NGINX Ingress Configuration
To set up an NGINX Ingress Controller with a different port, we need to change the service definition:
apiVersion: v1
kind: Service
metadata:
name: nginx-ingress-controller
spec:
type: LoadBalancer
ports:
- name: http
port: 8080
targetPort: 80
- name: https
port: 8443
targetPort: 443
selector:
app: nginx-ingressThis setup shows the NGINX Ingress Controller on ports 8080 and 8443. We must make sure our Ingress resources are set up correctly to use these ports.
Considerations
- Network Policies: We should set up network policies. This helps control traffic between services since Ingress Controllers expose services to the outside.
- SSL/TLS Configuration: For safe connections, we need to make sure SSL certificates are set up and managed correctly.
- Monitoring & Logging: We can use tools like Prometheus and Grafana. They help us watch ingress traffic and logs for performance and problems.
For more information on how to set up Ingress Controllers and learn about their role in Kubernetes networking, we can check out how to configure ingress for external access to applications.
How to Define Custom Ports in Ingress Resources
To set custom ports in Kubernetes Ingress resources, we need to tell
the backend service what port we want in our Ingress YAML
file. Ingress helps us create rules to send traffic to different
services based on the request’s host and path. Here is how we can define
custom ports in our Ingress resource:
Example of Custom Port Configuration
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: custom-port-ingress
annotations:
nginx.ingress.kubernetes.io/backend-protocol: "HTTP" # or "HTTPS"
spec:
rules:
- host: custom.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 3000 # Custom portImportant Considerations
- Ingress Controller Compatibility: We must check if our Ingress controller can handle custom ports. Some controllers may limit which ports can be used.
- Firewall Rules: We need to ensure that any firewall or network rules let traffic go through the specified custom ports.
- Custom Annotations: Ingress controllers like NGINX may need special annotations for custom settings or behaviors.
- TLS Configuration: If we use HTTPS, we must set up TLS settings correctly for ports that are not standard.
Using Multiple Backends
If we want to send traffic to different services on various ports, we can set up multiple rules or paths in the same Ingress resource:
spec:
rules:
- host: custom.example.com
http:
paths:
- path: /service1
pathType: Prefix
backend:
service:
name: service1
port:
number: 3000
- path: /service2
pathType: Prefix
backend:
service:
name: service2
port:
number: 4000 # Another custom portThis setup lets us send traffic based on the URL path while using custom ports for each service.
Configuring NodePort Services for Custom Ports in Kubernetes Ingress
We can expose services on custom ports in Kubernetes by using
NodePort. To do this, we need to define a service of type
NodePort in our Kubernetes manifest. This helps us access
our application through a specific port on the cluster nodes.
Here is a simple YAML configuration for a NodePort service:
apiVersion: v1
kind: Service
metadata:
name: my-nodeport-service
spec:
type: NodePort
ports:
- port: 8080 # Port that the service listens on
targetPort: 80 # Port on the container
nodePort: 30008 # Custom port exposed on each node
selector:
app: my-appIn this configuration: - port is the port that the
service will use inside. - targetPort is the port on the
container where traffic goes. - nodePort is the port that
will open on each node in the cluster. If we do not set a port,
Kubernetes will choose one for us. But we can pick our own custom
port.
To deploy this service, we run the command:
kubectl apply -f my-nodeport-service.yamlNow we can access our application using
http://<node-ip>:30008. Here,
<node-ip> is the IP address of any node in our
cluster.
To connect this NodePort service with an Ingress controller, we might need more settings to route traffic the right way. We should check that our Ingress resources point to the service correctly. This way, external traffic can go through the chosen NodePort. For more help on setting up Ingress resources, see how to configure ingress for external access to applications.
Using LoadBalancer Services for Custom Ports with Ingress
In Kubernetes, we can set up LoadBalancer services to show our apps on custom ports with Ingress. This lets us send traffic to our apps on ports other than the usual HTTP (80) and HTTPS (443) ports.
To set up a LoadBalancer service with custom ports, we can follow these steps:
- Define a Service: We create a Kubernetes Service of
type
LoadBalancerthat tells the custom port.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: LoadBalancer
ports:
- port: 8080 # Port exposed by the LoadBalancer
targetPort: 80 # Port on the Pod that gets traffic
protocol: TCP
selector:
app: my-app- Deploy an Ingress Resource: We need to create an Ingress resource to send traffic from the LoadBalancer to our application. We make sure the Ingress controller can handle the custom port.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 8080 # The custom port we set in the ServiceDeploy the Ingress Controller: We check that our cluster has an Ingress controller running, like NGINX or Traefik. This controller will listen on the LoadBalancer’s IP for traffic that comes to the host and path we defined.
Access the Application: After the LoadBalancer service is running, we can reach our application by using the LoadBalancer’s external IP and the custom port we chose.
If we are using a domain name, we should set up our DNS to point to the LoadBalancer’s IP.
For more details on Kubernetes Ingress and how to show our applications well, we can check this guide on configuring Ingress for external access.
Leveraging NGINX Ingress Controller for Custom Ports
We can use the NGINX Ingress Controller to set up custom ports for our applications. This is helpful when our apps need ports that are not the standard HTTP (80) and HTTPS (443).
To set up custom ports with the NGINX Ingress Controller, we follow these steps:
Install the NGINX Ingress Controller if we have not done it yet:
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx helm repo update helm install nginx-ingress ingress-nginx/ingress-nginxChange Ingress Resource to include custom ports. We can define a custom backend service in our Ingress YAML file. Here is an example of an Ingress resource that listens on a custom port (like 8080):
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: custom-port-ingress spec: rules: - host: custom.example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 8080Set up NGINX to listen on more ports. We might need to change the NGINX config to add our custom ports. We can do this by updating the ConfigMap for the NGINX Ingress Controller:
kubectl edit configmap nginx-configuration -n ingress-nginxWe add this config to specify the extra ports:
data: server-snippet: | listen 8080;Check the configuration. After we apply our Ingress resource and update the ConfigMap, we can confirm if the NGINX Ingress Controller is listening on our custom ports:
kubectl get services -n ingress-nginxTesting. We can access our application through the custom port using the hostname we set up. For example, if we set our Ingress like above, we can use:
http://custom.example.com:8080
This setup helps us use the NGINX Ingress Controller well for routing traffic to custom ports instead of just the default HTTP and HTTPS ports. For more details on configuring Ingress resources, we can check the Kubernetes Ingress documentation.
Frequently Asked Questions
1. Can we use ports other than 80 and 443 for Kubernetes Ingress?
Yes, we can set custom ports for Kubernetes Ingress. But it depends on the Ingress controller we are using. Many controllers, like NGINX, let us define extra ports in our Ingress resource. To do this, we need to make sure that the Ingress controller listens on those ports for our services.
2. What are the limits of Kubernetes Ingress controllers?
Kubernetes Ingress controllers have some limits. They cannot handle TCP/UDP traffic directly. Also, they may have rules about protocol-specific features. Some controllers might not support custom ports unless we set them up correctly. Knowing these limits is important for good Ingress management. It helps us make sure our applications are reachable.
3. How do we define custom ports in Kubernetes Ingress resources?
To define custom ports in Kubernetes Ingress resources, we need to mention the ports in the backend service. We also have to make sure our Ingress controller is set up to direct traffic on these ports. For example, when using NGINX, we can define the service port in the Ingress YAML file. This lets external traffic come to our application through the chosen ports.
4. What is the difference between NodePort and LoadBalancer services in Kubernetes?
NodePort and LoadBalancer services have different uses in Kubernetes. A NodePort service makes our application available on a fixed port on each node’s IP address. This allows outside access. On the other hand, a LoadBalancer service uses a cloud provider’s load balancer. It spreads traffic to our application across many pods. We should choose the right service type based on our application’s setup and how we want to manage traffic.
5. How can we use the NGINX Ingress Controller for custom ports?
The NGINX Ingress Controller is easy to set up. We can configure it to use custom ports. We can change the settings to add new ports in the NGINX config file or mention these ports in our Ingress resource. This flexibility lets us direct traffic well through non-standard ports while keeping our applications working. For more help on setting up NGINX Ingress, check out our guide on how to configure ingress for external access to my applications.
By answering these common questions about setting custom ports for Kubernetes Ingress, we hope to give clear help for users who want to improve their Kubernetes networking setup.