Migrating Spring Cloud Gateway Routing to Kubernetes Ingress Routing

11/29/2019

I'm currently using Spring Cloud Gateway to handle my routing and I'm struggling to figure out how to create the rewrite rules in Kubernetes Ingress. My Spring Gateway logic looks like this.

Current Spring Gateway Routing

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: clover-service
          uri: http://domain:9500
          predicates:
            - Path=/portal/api/clover/**
          filters:
              //Rewrite to http://domain:9500/api/clover
            - RewritePath=/portal(?<segment>/?.*), $\{segment} 
        - id: auth-svc
          uri: http://domain:8900
          predicates:
            - Path=/portal/auth/**
          filters:
              //Rewrite to http://domain:8900/auth
            - RewritePath=/portal/auth(?<segment>/?.*), $\{segment}

        ######################################################
        ##                                                  ##
        ##   The default goes to the nuxt frontend app      ##
        ##                                                  ##
        ######################################################

        - id: frontend
          uri: http://domain:3000
          predicates:
            - Path=/**

My goal is to eliminate the Spring Cloud Gateway and move my routing logic into Ingress. Here's what I've tried in ingress and failed.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: routing-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
    - http:
        paths:
          - path: /portal/api/clover(/|$)(.*)
            backend:
              serviceName: clover-service
              servicePort: 9500
          - path: /portal/auth(/|$)(.*)
            backend:
              serviceName: auth-service
              servicePort: 8900
          - path: /portal/**
            backend:
              serviceName: frontend-service
              servicePort: 3000

It appears as if the rewrite-target is rewriting all my routes rather than individual routes. Anybody know how to rewrite individual routes?

-- George
kubernetes
kubernetes-ingress
spring-cloud-gateway

1 Answer

12/2/2019

Take a look on Ingress resources definition in Kubernetes.

Such option is not supported by offical Ingress configuration in Kubernetes. But you can use other implemented ingress-gateway which supports spring annotations. Here you can find more information: Spring Cloud Gateway Ingress.

Your Ingress configuration file should include annotations like kubernetes.io/ingress.class: spring.cloud.gateway and spring.cloud.gateway/routes.

Here is example Ingress configuration file:

apiVersion: networking.k8s.io/v1beta1
 kind: Ingress
 metadata:
   name: httpbin
   namespace: ingress
   annotations:
     kubernetes.io/ingress.class: spring.cloud.gateway
     spring.cloud.gateway/routes: |
       predicates:
       - Host=httpbin.nlighten.nl
       - Path=/{segment}
       filters:
       - SetPath=/anything/{segment}
       - PreserveHostHeader
       - SecureHeaders
       - name: Retry
         args:
           retries: 3
           statuses: BAD_GATEWAY
       uri: lb://httpbin
 spec:
   backend:
     serviceName: httpbin
     servicePort: 80

Remember to create Ingress in proper namespace. Both gateway and backends must be run in same namespace.

-- MaggieO
Source: StackOverflow