1

There are multiple same pods running in one cluster but different namespaces. This is the web application running in Kubernetes. I have the URL <HOSTNAME>:<PORT>/context/abc/def/...... I want to redirect to particular service based on the context. Is there a way i can achieve it using ingress controller ? Or Is there any way i can achieve it using different ports through ingress ?

My web application works fine if the URL is <HOSTNAME>:<PORT>/abc/def/...... Since i have to access the different pods using the same URL, I am adding context to it. Do we have any other way to achieve this use case ?

2 Answers2

2

You can do that with rewrite-target. In example below i used <HOSTNAME> value of rewrite.bar.com and <PORT> with value 80.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: context-service
          servicePort: 80
        path: /context1(/|$)(.*)
      - backend:
          serviceName: context-service2
          servicePort: 80
        path: /context2(/|$)(.*)       

For example, the ingress definition above will result in the following rewrites:

rewrite.bar.com/context1 rewrites to rewrite.bar.com/ for context 1 service.

rewrite.bar.com/context2 rewrites to rewrite.bar.com/ for context 2 service.

rewrite.bar.com/context1/new rewrites to rewrite.bar.com/new for context 1 service.

rewrite.bar.com/context2/new rewrites to rewrite.bar.com/new for context 2 service.

Piotr Malec
  • 3,429
  • 11
  • 16
  • I have edited my answer to address multiple backend services and fixed code block formatting. – Piotr Malec Nov 22 '19 at 16:56
  • If my url contains host:port/context1/new/abc/def/, will this work – Sunil Gudivada Nov 30 '19 at 18:26
  • Yes, but You have to remember to have correct `servicePort` and `host` matching with the host and port You used. So for example `http://example.com:80/context1/new/abc/def/` will redirect to `http://example.com:80/new/abc/def/` on service that matches selector for context1 criteria on `host` example.com and `targetPort` 80. – Piotr Malec Dec 02 '19 at 16:39
  • This solution is not working for me. It is not redirecting to the rewrite.bar.com/new – Sunil Gudivada Dec 16 '19 at 08:42
0

You can manage how the traffic is pointed to the correct service by using the rules configuration of the ingress, here is a simple example from the documentation:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
spec:
  rules:
  - http:
      paths:
      - path: /abc
        backend:
          serviceName: abc-service
          servicePort: 80
      - path: /context
        backend:
          serviceName: context-service
          servicePort: 80
wolmi
  • 1,659
  • 12
  • 25
  • I can do that. But my url is working without context. If i am adding the context my api is not working. Because my rest end point got changed. Is there any way we can without adding any context. I just want to achieve multitenancy using namespace separation. – Sunil Gudivada Nov 21 '19 at 16:44