将Spring Cloud Gateway路由迁移到Kubernetes入口路由

时间:2019-11-29 14:28:00

标签: kubernetes kubernetes-ingress spring-cloud-gateway

我目前正在使用Spring Cloud Gateway来处理我的路由,并且正在努力寻找如何在Kubernetes Ingress中创建重写规则的方法。我的Spring Gateway逻辑看起来像这样。

当前的Spring Gateway路由

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=/**

我的目标是消除Spring Cloud Gateway,并将路由逻辑移至Ingress。这是我在入口尝试过但失败的方法。

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

似乎重写目标正在重写我的所有路由,而不是单个路由。有人知道如何重写单个路由吗?

1 个答案:

答案 0 :(得分:0)

看看Kubernetes中的Ingress resources定义。

Kubernetes中的正式Ingress配置不支持该选项。但是,您可以使用支持弹簧注释的其他已实现的入口网关。在这里您可以找到更多信息:Spring Cloud Gateway Ingress

您的Ingress配置文件应包含kubernetes.io/ingress.class: spring.cloud.gatewayspring.cloud.gateway/routes之类的注释。

这是示例Ingress配置文件:

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

请记住在适当的名称空间中创建Ingress。网关和后端都必须在相同的名称空间中运行。