任务:从 OCP Route 迁移到 GatewayAPI Route

简介

使用 Envoy Gateway 将 OpenShift Container Platform (OCP) Routes 迁移到 Kubernetes Gateway API HTTPRoutes。使用以下按功能划分的映射,将每个 OCP Route 功能转换为等效的 Gateway API 配置。

前提条件

  1. 配置 EnvoyGatewayCtl
  2. 配置 Gateway
  3. 基本了解 Gateway API Routes

基本 HTTP Route

OCP Route 配置

在 OCP 中,使用 Route 资源创建基本 HTTP route:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  namespace: demo
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  port:
    targetPort: 8080
  1. route 的主机名
  2. 后端服务引用
  3. 后端服务上的目标端口

Gateway API 配置

在 Gateway API 中,使用 HTTPRoute 配置等效功能:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
      namespace: demo
  rules:
    - backendRefs:
        - name: example-service
          port: 8080
  1. 此 route 接受的主机名(等效于 OCP Route 的 host)
  2. 对 Gateway listener 的引用
  3. 后端服务和端口

Route 超时

OCP Route 配置

在 OCP 中,使用注解配置超时:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/timeout: 30s
    haproxy.router.openshift.io/timeout-tunnel: 1h
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  1. HTTP 请求的常规超时
  2. 隧道连接的超时(WebSocket、HTTP/2 等)

Gateway API 配置

在 Gateway API 中,在 HTTPRoute 规则中配置超时:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
  rules:
    - backendRefs:
        - name: example-service
          port: 8080
      timeouts:
        request: 30s
        backendRequest: 25s
  1. 请求超时配置

有关更多详细信息,请参阅请求超时。

NOTE

Gateway API 没有专门针对隧道连接的独立超时。request 超时适用于所有连接类型。

HTTP 严格传输安全(HSTS)

OCP Route 配置

在 OCP 中,使用边缘终止或重新加密 route 上的注解配置 HSTS:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/hsts_header: max-age=31536000;includeSubDomains;preload
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  tls:
    termination: edge
  1. HSTS 标头配置

Gateway API 配置

在 Gateway API 中,使用响应标头修改配置 HSTS:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
  rules:
    - filters:
        - type: ResponseHeaderModifier
          responseHeaderModifier:
            add:
              - name: Strict-Transport-Security
                value: max-age=31536000;includeSubDomains;preload
      backendRefs:
        - name: example-service
          port: 8080
  1. 向响应添加 HSTS 标头

有关更多详细信息,请参阅 HTTP 标头修改。

NOTE

与 OCP 的 requiredHSTSPolicies 集群范围强制机制不同,Gateway API 要求针对每个 route 进行显式配置。Gateway API 没有全局 HSTS 策略强制机制。ACP 通过 Envoy Gateway 的 ClientTrafficPolicy 扩展提供 Gateway 范围的 HSTS;有关更多信息,请参阅如何配置 HSTS。

OCP Route 配置

在 OCP 中,使用 annotations 配置会话亲和性:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/balance: source
    haproxy.router.openshift.io/disable_cookies: "false"
    router.openshift.io/cookie_name: my-cookie
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  1. 负载均衡算法
  2. 启用基于 Cookie 的会话亲和性
  3. 用于会话持久性的 Cookie 名称

Gateway API 配置

在 Gateway API 中,在 HTTPRoute 规则中配置会话持久性:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
  rules:
    - backendRefs:
        - name: example-service
          port: 8080
      sessionPersistence:
        type: Cookie
        sessionName: my-cookie
        cookieConfig:
          lifetimeType: Permanent
  1. 会话持久性配置
  2. Cookie 生命周期类型:Permanent(在浏览器会话之间保持)或 Session(浏览器关闭时过期)

有关更多详细信息,请参阅会话亲和性/粘性会话。

基于路径的路由

OCP Route 配置

在 OCP 中,基于路径的路由使用 path 字段:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
spec:
  host: example.com
  path: /api
  to:
    kind: Service
    name: example-service
  1. 用于匹配请求的路径前缀

Gateway API 配置

在 Gateway API 中,在 HTTPRoute 规则中配置路径匹配:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api
      backendRefs:
        - name: example-service
          port: 8080
  1. 路径匹配配置

Gateway API 支持多种匹配类型:

  • PathPrefix:匹配路径前缀(等效于 OCP 的默认行为)
  • Exact:匹配确切路径
  • RegularExpression:使用正则表达式进行匹配

有关更多详细信息,请参阅 HTTPRoute 匹配。

标头修改

OCP Route 配置

在 OCP 中,使用 annotations 修改标头:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/response-set-header: X-Custom-Header:value
    haproxy.router.openshift.io/request-set-header: X-Request-Header:value
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  1. 设置响应标头
  2. 设置请求标头

Gateway API 配置

在 Gateway API 中,使用过滤器配置标头修改:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
  rules:
    - filters:
        - type: RequestHeaderModifier
          requestHeaderModifier:
            add:
              - name: X-Request-Header
                value: value
        - type: ResponseHeaderModifier
          responseHeaderModifier:
            add:
              - name: X-Custom-Header
                value: value
      backendRefs:
        - name: example-service
          port: 8080
  1. 请求标头修改
  2. 响应标头修改

有关更多详细信息,请参阅 HTTP 标头修改。

连接限制

OCP Route 配置

在 OCP 中,使用 annotations 配置连接限制:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/pod-concurrent-connections: "100"
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  1. 每个后端 pod 的最大并发连接数

Gateway API 配置

在 Gateway API 中,使用附加到 Gateway 的 ClientTrafficPolicy 配置连接限制:

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: ClientTrafficPolicy
metadata:
  name: connection-limit-policy
  namespace: demo
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: Gateway
      name: demo-gateway
  connection:
    connectionLimit:
      value: 100
  1. 将策略附加到 Gateway
  2. 连接限制配置

有关更多详细信息,请参阅连接限制。

NOTE

与 OCP 的每个后端 pod 限制不同,Gateway API 的连接限制应用于 Gateway 级别,并分布在各个 Envoy 代理实例之间。

限流

OCP Route 配置

在 OCP 中,限流使用注解进行配置:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/rate-limit-connections: "true"
    haproxy.router.openshift.io/rate-limit-connections.concurrent-tcp: "10"
    haproxy.router.openshift.io/rate-limit-connections.rate-http: "100"
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  1. 限流配置

Gateway API 配置

在 Gateway API 中,使用 BackendTrafficPolicy 配置限流:

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
  name: rate-limit-policy
  namespace: demo
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: example-route
  rateLimit:
    type: Local
    local:
      rules:
        - limit:
            requests: 100
            unit: Second
  1. 限流配置

有关更多详细信息,请参阅限流。

NOTE

Gateway API 限流比 OCP Route 注解更加灵活,同时支持本地限流和全局限流机制。

IP 允许列表/阻止列表

OCP Route 配置

在 OCP 中,IP 允许列表使用注解进行配置:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/ip_allowlist: 192.168.1.0/24 10.0.0.1
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  1. IP 允许列表配置

Gateway API 配置

在 Gateway API 中,使用带有授权规则的 SecurityPolicy 实现 IP 过滤:

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
  name: ip-filter-policy
  namespace: demo
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: example-route
  authorization:
    defaultAction: Deny
    rules:
      - action: Allow
        principal:
          clientCIDRs:
            - 192.168.1.0/24
            - 10.0.0.1/32
  1. 将策略附加到 HTTPRoute
  2. 配置默认拒绝的授权规则
  3. 使用 CIDR 表示法配置 IP 允许列表

对于 IP 阻止列表(拒绝列表),设置 defaultAction: Allow 并使用 action: Deny:

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
metadata:
  name: ip-blocklist-policy
  namespace: demo
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: example-route
  authorization:
    defaultAction: Allow
    rules:
      - action: Deny
        principal:
          clientCIDRs:
            - 192.168.100.0/24
  1. 默认操作允许所有流量
  2. 拒绝来自特定 IP 的流量

有关更多详细信息,请参阅IP 允许列表/拒绝列表。

NOTE

如果 Gateway 位于负载均衡器或代理之后,请确保使用 ClientTrafficPolicy 正确配置客户端 IP 检测。

URL 重写

OCP Route 配置

在 OCP 中,URL 重写使用注解进行配置:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
  annotations:
    haproxy.router.openshift.io/rewrite-target: /new-path
spec:
  host: example.com
  path: /old-path
  to:
    kind: Service
    name: example-service
  1. 重写目标路径

Gateway API 配置

在 Gateway API 中,使用过滤器配置 URL 重写:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  hostnames:
    - example.com
  parentRefs:
    - name: demo-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /old-path
      filters:
        - type: URLRewrite
          urlRewrite:
            path:
              type: ReplacePrefixMatch
              replacePrefixMatch: /new-path
      backendRefs:
        - name: example-service
          port: 8080
  1. URL 重写过滤器

有关更多详细信息,请参阅URL 重写。

跨命名空间 Route 接纳

OCP Route 配置

在 OCP 中,Route 接纳策略控制不同命名空间中的 Route 是否可以声明相同的主机名。此配置通过 Ingress Operator 在集群级别进行。

Gateway API 配置

在 Gateway API 中,跨命名空间访问在 Gateway 监听器级别进行控制:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: demo-gateway
  namespace: gateway-ns
spec:
  gatewayClassName: envoy-gateway-operator-cpaas-default
  listeners:
    - name: http
      protocol: HTTP
      port: 80
      allowedRoutes:
        namespaces:
          from: All  # Allow routes from all namespaces
  1. 配置哪些命名空间可以附加 Route

allowedRoutes.namespaces.from 的选项:

  • Same:仅允许与 Gateway 位于同一命名空间中的 Route
  • All:允许来自任意命名空间的 Route
  • Selector:允许来自与标签选择器匹配的命名空间的 Route

有关更多详细信息,请参阅跨命名空间路由。

NOTE

与 OCP 的集群范围接纳策略不同,Gateway API 在单个 Gateway 监听器级别进行控制,因此能够提供更精细的控制。

Ingress 的默认 TLS 证书

OCP Route 配置

在 OCP 中,没有 TLS 配置的 Route 可以使用在 Ingress Controller 级别配置的默认证书。

Gateway API 配置

在 Gateway API 中,在 Gateway 监听器上配置默认 TLS 证书:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: demo-gateway
  namespace: demo
spec:
  gatewayClassName: envoy-gateway-operator-cpaas-default
  listeners:
    - name: https
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          - name: default-tls-cert
  1. 监听器的默认 TLS 证书

附加到此监听器且未配置特定 TLS 的任何 HTTPRoute 都将使用此默认证书。

使用自定义 CA 的 TLS 重新加密

OCP Route 配置

在 OCP 中,重新加密 Route 会在路由器处终止 TLS,并在验证后对后端重新加密:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  tls:
    termination: reencrypt
    destinationCACertificate: |
      -----BEGIN CERTIFICATE-----
      ...
      -----END CERTIFICATE-----
  1. 重新加密终止模式
  2. 用于验证后端的 CA 证书

Gateway API 配置

在 Gateway API 中,使用 BackendTLSPolicy 配置后端 TLS 验证:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: demo-gateway
  namespace: demo
spec:
  gatewayClassName: envoy-gateway-operator-cpaas-default
  listeners:
    - name: https
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          - name: frontend-tls
---
apiVersion: gateway.networking.k8s.io/v1
kind: BackendTLSPolicy
metadata:
  name: backend-tls-policy
  namespace: demo
spec:
  targetRefs:
    - group: ""
      kind: Service
      name: example-service
  validation:
    caCertificateRefs:
      - name: backend-ca-cert
        kind: ConfigMap
    hostname: backend.example.com
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  parentRefs:
    - name: demo-gateway
  hostnames:
    - example.com
  rules:
    - backendRefs:
        - name: example-service
          port: 8443
  1. 在 Gateway 处终止 TLS
  2. 将策略应用于后端 Service
  3. 后端 TLS 验证配置

有关更多详细信息,请参阅后端 TLS。

NOTE

CA 证书必须存储在 ConfigMap 中,而不是 Secret 中。

使用自定义证书的边缘终止

OCP Route 配置

在 OCP 中,边缘终止使用 Route 的 TLS 配置:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  tls:
    termination: edge
    certificate: |
      -----BEGIN CERTIFICATE-----
      ...
      -----END CERTIFICATE-----
    key: |
      -----BEGIN PRIVATE KEY-----
      ...
      -----END PRIVATE KEY-----
  1. 边缘终止模式
  2. TLS 证书
  3. TLS 私钥

Gateway API 配置

在 Gateway API 中,在 Gateway 监听器上配置 TLS 终止:

apiVersion: v1
kind: Secret
metadata:
  name: example-tls
  namespace: demo
type: kubernetes.io/tls
data:
  tls.crt: <base64-encoded-cert>
  tls.key: <base64-encoded-key>
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: demo-gateway
  namespace: demo
spec:
  gatewayClassName: envoy-gateway-operator-cpaas-default
  listeners:
    - name: https
      protocol: HTTPS
      port: 443
      tls:
        mode: Terminate
        certificateRefs:
          - name: example-tls
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: example-route
  namespace: demo
spec:
  parentRefs:
    - name: demo-gateway
      sectionName: https
  hostnames:
    - example.com
  rules:
    - backendRefs:
        - name: example-service
          port: 8080
  1. TLS Secret 类型
  2. HTTPS 协议
  3. 终止 TLS 模式
  4. 对 TLS Secret 的引用

TLS 透传

OCP Route 配置

在 OCP 中,透传 Route 不会终止 TLS:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: example-route
spec:
  host: example.com
  to:
    kind: Service
    name: example-service
  tls:
    termination: passthrough
  1. 透传终止模式

Gateway API 配置

在 Gateway API 中,TLS 透传使用 TLSRoute:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: demo-gateway
  namespace: demo
spec:
  gatewayClassName: envoy-gateway-operator-cpaas-default
  listeners:
    - name: tls-passthrough
      protocol: TLS
      port: 443
      tls:
        mode: Passthrough
---
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TLSRoute
metadata:
  name: example-route
  namespace: demo
spec:
  parentRefs:
    - name: demo-gateway
      sectionName: tls-passthrough
  hostnames:
    - example.com
  rules:
    - backendRefs:
        - name: example-service
          port: 8443
  1. TLS 协议
  2. 透传模式
  3. 用于路由的 SNI 主机名

有关更多详细信息,请参阅TLS 透传和TLSRoute 规范。

NOTE

TLSRoute 使用 SNI(服务器名称指示)进行路由决策,而不会终止 TLS。这意味着:

  • 无法访问用于路由决策的 HTTP 标头或请求内容
  • 无法应用 L7 过滤器(标头修改、URL 重写等)
  • 路由完全基于 SNI 主机名

功能对比摘要

功能OCP RouteGateway API备注
基本 HTTP 路由Route 规范HTTPRoute直接映射
基于路径的路由.spec.path.spec.rules[].matches[].pathGateway API 提供更多匹配类型
超时注解HTTPRoute .spec.rules[].timeoutsGateway API 具有结构化的超时配置
HSTS注解ResponseHeaderModifier 过滤器Gateway API 不提供全局强制执行
会话亲和性注解HTTPRoute .spec.rules[].sessionPersistenceGateway API 提供原生支持
标头修改注解RequestHeaderModifier/ResponseHeaderModifier 过滤器Gateway API 具有结构化的过滤器配置
连接限制注解ClientTrafficPolicy在 Gateway 级别应用,并分布到各个代理
速率限制注解BackendTrafficPolicyGateway API 提供更大的灵活性
IP 允许列表/阻止列表注解带授权的 SecurityPolicy通过 clientCIDRs 提供原生支持
URL 重写注解URLRewrite 过滤器Gateway API 具有结构化的重写配置
跨命名空间集群级策略Gateway .spec.listeners[].allowedRoutesGateway API 提供按侦听器控制
默认 TLS 证书控制器级别Gateway 侦听器 TLS按侦听器配置
TLS 重新加密.spec.tls.termination: reencryptBackendTLSPolicy需要单独的策略资源
TLS 边缘终止.spec.tls.termination: edge使用 HTTPS 的 Gateway 侦听器 TLS证书存储在 Secret 中
TLS 透传.spec.tls.termination: passthrough采用透传模式的 TLSRoute使用 TLSRoute 而不是 HTTPRoute

迁移策略

从 OCP Route 迁移到 Gateway API 时:

  1. 从 Gateway 开始:根据您的使用场景,创建具有适当侦听器的 Gateway 资源

    • 将 Gateway 部署在同一命名空间中,或部署在专用的 gateway 命名空间中
    • 为所需的所有协议(HTTP、HTTPS、TLS、TCP、UDP)配置侦听器
    • 确保已创建 Gateway service,并且具有可访问的端点
  2. 将 Route 转换为 HTTPRoute:将每个 OCP Route 迁移为 HTTPRoute,从简单的路由开始

    • 从非生产或低流量路由开始,以最大程度地降低风险
    • 验证主机名和路径匹配配置
    • 在添加高级功能之前,测试基本连接
  3. 应用策略:对于需要策略(BackendTrafficPolicy、SecurityPolicy、ClientTrafficPolicy)的功能,请在基本路由正常工作后创建并附加这些策略

    • 一次添加一个策略,并验证其功能
    • 监控是否存在任何意外行为或性能影响
  4. 增量测试:在继续迁移下一个路由之前,验证每个迁移步骤

    • 监控:检查 Gateway 和 Route 的状态条件,确认是否存在错误
    • 功能测试:使用 curl 或自动化测试验证所有路由是否按预期工作
    • 性能测试:将响应时间和吞吐量与 OCP Route 进行比较
    • 验证检查:
      • 验证 TLS 证书是否已正确应用
      • 测试会话亲和性和负载均衡行为
      • 验证速率限制和安全策略
      • 检查标头修改和 URL 重写
  5. 更新 DNS:验证完成后,更新 DNS 条目,使其指向新的 Gateway service

    • 双重运行期间(推荐):将部分流量指向新的 Gateway,同时保持 OCP Route 处于活动状态,以便支持回滚
    • 使用加权 DNS 或金丝雀部署逐步转移流量
    • 在切换期间监控错误率、延迟和应用指标
  6. 回滚流程:如果迁移后发现问题

    • 将 DNS 恢复为指向 OCP Route
    • DNS 切换后至少保持 OCP Route 活动 24–48 小时
    • 记录导致问题的任何配置差异
    • 如果需要回滚,为利益相关者制定沟通计划

相关文档

商标声明

Envoy® 是 The Linux Foundation 的注册商标。

Alauda 是独立供应商。本产品与 The Linux Foundation 没有关联,也未获得其认可或赞助。所有商标均归其各自所有者所有,此处仅用于标识目的。