68

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?

1
  • What ingress controller are you using? How are you deploying it?
    – Matt
    Commented May 22, 2019 at 0:19

4 Answers 4

53

No. 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.

1
  • 8
    It is possible to customise the ingress controller to listen on any port, but not possible to modify ports in a ingress definition
    – Matt
    Commented Jun 22, 2020 at 1:50
24

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
13

The ingress controller handling the ingress can have its ports changed via the ingress controllers deployment.

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 that exposes ports for the ingress controller.

The kubernetes/ingress-nginx static deploys have a deploy.yaml with a Service 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

Modify the ports the load balancer is configured with, in spec.ports[*].port in the external service, however that is deployed.

2
5

If you're using Helm to deploy the Kubernetes ingress-nginx controller, you can do this to change the ports from their defaults of 80 and 443 in a Helm values override file:

ingress-nginx:
  enabled: true
  ...
  controller:
    service:
      ports:
        http: 8123
        https: 9456

See here: https://artifacthub.io/packages/helm/ingress-nginx/ingress-nginx#values

In this example, any services exposed through the ingress are now available on ports 8123 (http) and 9456 (https).

1
  • Would love to see what your ingress file looks like, because I can't get this to work.
    – Edgaras
    Commented Jul 29 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.