Not able to access multiple services from ingress controller hosted on different ports

2/24/2020

I have two services hosted on different ports and I have created a ingress resource which looks like this

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: mynamespace
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - path: /svc1/
        backend:
          serviceName: app1-svc
          servicePort: 3000
       - path: /svc2/
         backend:
           serviceName: app2-svc
           servicePort: 8080

on top of this I have created a NodePort type ingress controller.

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-ingress
  name: controller-nginx-ingress-controller
spec:
  clusterIP: 10.88.18.191
  externalTrafficPolicy: Cluster
  ports:
  - name: http
    nodePort: 30080
    port: 80
    protocol: TCP
    targetPort: http
  - name: https
    nodePort: 31442
    port: 443
    protocol: TCP
    targetPort: https
  selector:
    app: nginx-ingress
    component: controller
    release: controller

And finally, setup a cloud load balancer to access application running on my K8S cluster.

Problem: I couldn't able to access any of my application using URL routing

Can anyone please let me know what incorrect I'm doing? And how to resolve this issue?

-- Geeky Ninja
kubernetes
kubernetes-ingress
nginx-ingress

1 Answer

2/25/2020

From what you mentioned in comments I am pretty sure the problem can be solved with path rewrites.

Now, when you are sending a request to /svc1/ with path: /svc1/ then the request is forwarded to app1-svc with path set to /svc1/ and you are receiving 404 errors as there is no such path in app1. From what you mentioned, you can most probably solve the issue using rewrite. You can achieve it using nginx.ingress.kubernetes.io/rewrite-target annotation, so your ingress would look like following:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  namespace: mynamespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - path: /svc1(/|$)(.*)
        backend:
          serviceName: app1-svc
          servicePort: 3000
       - path: /svc2(/|$)(.*)
         backend:
           serviceName: app2-svc
           servicePort: 8080

In this case when sending request with path set to /svc1/something the request will be forwarded to app1 with path rewritten to /something.

Also take a look in ingress docs for more explanation.

Let me know if it solved you issue.

-- HelloWorld
Source: StackOverflow