GKE ingress; apply rewrite to specific service

Viewed 138

Given this yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress-2
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: test.com
    http:
      paths:
      - backend:
          serviceName: admin
          servicePort: 8080
        path: /admin/*
        pathType: ImplementationSpecific
      - backend:
          serviceName: keycloak
          servicePort: 8080
        path: /auth/*
        pathType: ImplementationSpecific

I would like the rewrite-target to only apply to the service admin. Requests to keycloak should not be affected. How can I achieve that?

2 Answers

You can separate out ingress file or config, just make sure you keep the different name

So you can you create TWO ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress-1
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: test.com
    http:
      paths:
      - backend:
          serviceName: admin
          servicePort: 8080
        path: /admin/*
        pathType: ImplementationSpecific

Ingress: 2 with different config, you can edit anything as per need now remove annotation.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress-2
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: test.com
    http:
      paths:
      - backend:
          serviceName: keycloak
          servicePort: 8080
        path: /auth/*
        pathType: ImplementationSpecific

If you want to store everything in a single YAML file you can also do it by merging them with ---.

User Harsh Manvar's answer is good and shows the correct solution. But I am going to expand it a bit and tell you why. Always create deployments that are as little interdependent as possible. For example, if you need to change something in the keycloak, it shouldn't have any effect on admin. Additionally, if the ingress breaks down, you will have 2 broken services instead of one. You should practically always create one ingress to one service.

Additionally, you may want to create completely different rewrite rules. Then, creating separate ingress will be a very good idea. Look at this question.

See also documentation about rewrite rules.

Related