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

5/21/2019

I don't mean being able to route to a specific port, I mean to actually change the port the ingress listens on.

Is this possible? How? Where is this documented?

-- Chris Stryczynski
kubernetes
kubernetes-ingress

3 Answers

5/21/2019

From the kubernetes documentation:

An Ingress does not expose arbitrary ports or protocols. Exposing services other than HTTP and HTTPS to the internet typically uses a service of type Service.Type=NodePort or Service.Type=LoadBalancer.

It may be possible to customize a LoadBalancer on a cloud provider like AWS to listen on other ports.

-- dlaidlaw
Source: StackOverflow

5/21/2019

I assume you are using NGINX Ingress Controller. In this case, during installation, instead of doing a kubectl apply in the official yaml like this is one, you can try downloading the yaml and changing the port. The file above, which is used for an L4 AWS ELB, would become like this:

kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: "*"
    service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: "60"
spec:
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  ports:
    - port: {custom port 1}
      targetPort: http
    - port: {custom port 2}
      targetPort: https

An alternative is to use a more powerful ingress controller. Here is a list of different controllers. My personal choice is Ambassador. If you follow the getting-started page, you just need to change the service definition for the port of your choice:

---
apiVersion: v1
kind: Service
metadata:
  name: ambassador
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  ports:
   - port: {custom port}
     targetPort: 8080
  selector:
    service: ambassador
-- victortv
Source: StackOverflow

5/22/2019

An Ingress definition is backed by an ingress controller. The ingress controller is deployed with normal Kubernetes objects so will have a Service associated with it's deployment that exposes the ingress controller. How that service is deployed is specific to each ingress controller type and also the deployment method you are using.

For example, a generic cloud deployment of kubernetes/ingress-nginx will deploy a service with type LoadBalancer:

kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  ports:
    - name: http
      port: 80
      targetPort: http
    - name: https
      port: 443
      targetPort: https

To modify the ports the load balancer is configured with, change the spec.ports[*].port values to what you need.

-- Matt
Source: StackOverflow