任务:从 OCP Route 迁移到 GatewayAPI Route
简介
使用 Envoy Gateway 将 OpenShift Container Platform (OCP) Routes 迁移到 Kubernetes Gateway API HTTPRoutes。使用以下按功能划分的映射,将每个 OCP Route 功能转换为等效的 Gateway API 配置。
前提条件
- 配置 EnvoyGatewayCtl
- 配置 Gateway
- 基本了解 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
- route 的主机名
- 后端服务引用
- 后端服务上的目标端口
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
- 此 route 接受的主机名(等效于 OCP Route 的
host)
- 对 Gateway listener 的引用
- 后端服务和端口
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
- HTTP 请求的常规超时
- 隧道连接的超时(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
有关更多详细信息,请参阅请求超时。
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
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
有关更多详细信息,请参阅 HTTP 标头修改。
NOTE
与 OCP 的 requiredHSTSPolicies 集群范围强制机制不同,Gateway API 要求针对每个 route 进行显式配置。Gateway API 没有全局 HSTS 策略强制机制。ACP 通过 Envoy Gateway 的 ClientTrafficPolicy 扩展提供 Gateway 范围的 HSTS;有关更多信息,请参阅如何配置 HSTS。
基于 Cookie 的会话亲和性
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
- 负载均衡算法
- 启用基于 Cookie 的会话亲和性
- 用于会话持久性的 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
- 会话持久性配置
- 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
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
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
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
有关更多详细信息,请参阅 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
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
有关更多详细信息,请参阅连接限制。
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
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
有关更多详细信息,请参阅限流。
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
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
- 将策略附加到 HTTPRoute
- 配置默认拒绝的授权规则
- 使用 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
有关更多详细信息,请参阅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
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
有关更多详细信息,请参阅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
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
附加到此监听器且未配置特定 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-----
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
- 在 Gateway 处终止 TLS
- 将策略应用于后端 Service
- 后端 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-----
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
- TLS Secret 类型
- HTTPS 协议
- 终止 TLS 模式
- 对 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
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
- TLS 协议
- 透传模式
- 用于路由的 SNI 主机名
有关更多详细信息,请参阅TLS 透传和TLSRoute 规范。
NOTE
TLSRoute 使用 SNI(服务器名称指示)进行路由决策,而不会终止 TLS。这意味着:
- 无法访问用于路由决策的 HTTP 标头或请求内容
- 无法应用 L7 过滤器(标头修改、URL 重写等)
- 路由完全基于 SNI 主机名
功能对比摘要
迁移策略
从 OCP Route 迁移到 Gateway API 时:
-
从 Gateway 开始:根据您的使用场景,创建具有适当侦听器的 Gateway 资源
- 将 Gateway 部署在同一命名空间中,或部署在专用的 gateway 命名空间中
- 为所需的所有协议(HTTP、HTTPS、TLS、TCP、UDP)配置侦听器
- 确保已创建 Gateway service,并且具有可访问的端点
-
将 Route 转换为 HTTPRoute:将每个 OCP Route 迁移为 HTTPRoute,从简单的路由开始
- 从非生产或低流量路由开始,以最大程度地降低风险
- 验证主机名和路径匹配配置
- 在添加高级功能之前,测试基本连接
-
应用策略:对于需要策略(BackendTrafficPolicy、SecurityPolicy、ClientTrafficPolicy)的功能,请在基本路由正常工作后创建并附加这些策略
- 一次添加一个策略,并验证其功能
- 监控是否存在任何意外行为或性能影响
-
增量测试:在继续迁移下一个路由之前,验证每个迁移步骤
- 监控:检查 Gateway 和 Route 的状态条件,确认是否存在错误
- 功能测试:使用 curl 或自动化测试验证所有路由是否按预期工作
- 性能测试:将响应时间和吞吐量与 OCP Route 进行比较
- 验证检查:
- 验证 TLS 证书是否已正确应用
- 测试会话亲和性和负载均衡行为
- 验证速率限制和安全策略
- 检查标头修改和 URL 重写
-
更新 DNS:验证完成后,更新 DNS 条目,使其指向新的 Gateway service
- 双重运行期间(推荐):将部分流量指向新的 Gateway,同时保持 OCP Route 处于活动状态,以便支持回滚
- 使用加权 DNS 或金丝雀部署逐步转移流量
- 在切换期间监控错误率、延迟和应用指标
-
回滚流程:如果迁移后发现问题
- 将 DNS 恢复为指向 OCP Route
- DNS 切换后至少保持 OCP Route 活动 24–48 小时
- 记录导致问题的任何配置差异
- 如果需要回滚,为利益相关者制定沟通计划
相关文档
商标声明
Envoy® 是 The Linux Foundation 的注册商标。
Alauda 是独立供应商。本产品与 The Linux Foundation 没有关联,也未获得其认可或赞助。所有商标均归其各自所有者所有,此处仅用于标识目的。