1. 引言

在 Kubernetes 中,高效、灵活的服务网络能力对于管理复杂的云原生应用至关重要。Kubernetes Gateway API 正是为解决传统 Ingress 控制器的局限性而诞生的一项创新性方案。它提供了一种标准化、可扩展且面向角色的服务网络接口,是现代 Kubernetes 网络架构的重要演进。

本文将深入讲解 Kubernetes Gateway API 的核心概念、资源模型、安装配置方式,并与传统 Ingress 进行对比,帮助你理解其优势和迁移路径。


2. 背景与演进

在 Gateway API 出现之前,Kubernetes 依赖 Ingress 资源来管理服务的外部访问。Ingress 提供了 HTTP 负载均衡和 TLS 终止的基本功能,但随着集群规模扩大和业务复杂度提升,其局限性逐渐显现:

  • 协议支持有限:仅支持 HTTP/HTTPS,难以支持 TCP、UDP、gRPC 等协议。
  • 表达能力不足:复杂路由场景依赖非标准注解,导致厂商锁定。
  • ⚠️ 角色分离不清晰:集群管理员与应用开发者职责难以明确划分。

Kubernetes 最初通过 Service 提供稳定的 IP 映射机制,解决了 Pod IP 不稳定的问题。但 Service 本身无法满足复杂的路由需求,因此 Ingress 应运而生。然而,Ingress 的单体结构限制了其灵活性和扩展性。


3. Kubernetes Gateway API 概述

Kubernetes Gateway API 是由 SIG-NETWORK 社区维护的开源项目,旨在提供一种更灵活、标准化的服务网络接口。其核心设计思想是将网络功能解耦为多个资源对象,实现角色分离与职责明确。

✅ Gateway API 的优势:

  • 资源解耦:将路由、负载均衡等功能拆分为多个资源。
  • 多协议支持:支持 HTTP、TCP、UDP、TLS、gRPC 等多种协议。
  • 角色导向设计:支持集群管理员(ClusterOperator)和应用开发者(Application Developer)的职责分离。
  • 厂商中立:提供统一标准,便于不同实现之间的迁移。

4. Gateway API 的安装与配置

要使用 Gateway API,首先需要安装对应的 CRD(Custom Resource Definition),并选择一个支持 Gateway API 的控制器实现。

4.1 安装 Gateway API CRD

你可以根据需要选择安装标准版或实验版的 CRD:

✅ 安装标准版(适用于生产环境):

kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.1.0/standard-install.yaml

✅ 安装实验版(用于测试新特性):

kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.1.0/experimental-install.yaml

4.2 安装 Gateway 控制器

目前支持 Gateway API 的控制器包括:

  • Contour
  • Emissary-Ingress
  • Istio
  • Kong
  • Traefik
  • Apache APISIX ✅(本文示例中使用)

我们以 Apache APISIX 为例进行安装:

helm install apisix apisix/apisix \
  --namespace ingress-apisix \
  --create-namespace \
  --devel \
  --set gateway.type=NodePort \
  --set gateway.http.nodePort=30800 \
  --set ingress-controller.enabled=true \
  --set ingress-controller.config.kubernetes.enableApiGateway=true \
  --set ingressPublishService="ingress-apisix/apisix-gateway"

安装完成后,验证组件状态:

kubectl get all -n ingress-apisix

输出示例:

NAME                                           READY   STATUS    RESTARTS   AGE
pod/apisix-56c6c9b69b-x7k8f                    1/1     Running   0          1m
pod/apisix-etcd-0                              1/1     Running   0          1m
pod/apisix-ingress-controller-65d6c8f76c-jh8fw 1/1     Running   0          1m

NAME                    READY   AGE
statefulset.apps/apisix-etcd   1/1     1m

5. Gateway API 的核心资源

Gateway API 的资源模型由多个组件组成,各自承担不同的网络功能,实现高度模块化。

5.1 GatewayClass

GatewayClass 是 Gateway 的模板定义,是一个集群级别的资源,定义了多个 Gateway 的共享配置和行为。

  • ✅ 每个 GatewayClass 指定一个控制器(controllerName)。
  • ✅ 支持集群内多种类型的网关(如公网网关 vs 内网网关)。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GatewayClass
metadata:
  name: example-gateway-class
spec:
  controllerName: example.com/gateway-controller

5.2 Gateway

Gateway 是 GatewayClass 的具体实例,定义监听器配置(如端口、协议、TLS 等)。

  • ✅ 可以限定在某个命名空间或全局生效。
  • ✅ 是流量进入集群的入口点。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: Gateway
metadata:
  name: example-gateway
spec:
  gatewayClassName: example-gateway-class
  listeners:
    - name: http
      protocol: HTTP
      port: 80

5.3 Route 类型

Route 是命名空间级别的资源,定义流量路由规则,支持多种协议类型:

Route 类型 用途说明
HTTPRoute HTTP/HTTPS 请求路由
TCPRoute TCP 协议路由
UDPRoute UDP 协议路由
TLSRoute TLS 流量路由
GRPCRoute gRPC 协议路由

5.4 HTTPRoute

用于定义 HTTP 流量的路由规则,支持路径、方法、Header 等匹配条件。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: HTTPRoute
metadata:
  name: example-http-route
spec:
  parentRefs:
    - name: example-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /example
      backendRefs:
        - name: example-service
          port: 80

5.5 TCPRoute

用于非 HTTP 协议的 TCP 流量路由。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TCPRoute
metadata:
  name: example-tcp-route
spec:
  parentRefs:
    - name: example-gateway
  rules:
    - matches:
        - port: 8080
      backendRefs:
        - name: example-tcp-service
          port: 8080

5.6 UDPRoute

用于 UDP 协议流量路由,如 DNS 服务。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: UDPRoute
metadata:
  name: example-udp-route
spec:
  parentRefs:
    - name: example-gateway
  rules:
    - matches:
        - port: 53
      backendRefs:
        - name: example-udp-service
          port: 53

5.7 TLSRoute

用于基于 SNI 的 TLS 流量路由。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: TLSRoute
metadata:
  name: example-tls-route
spec:
  parentRefs:
    - name: example-gateway
  rules:
    - matches:
        - sniHosts:
            - example.com
      backendRefs:
        - name: example-tls-service
          port: 443

5.8 GRPCRoute

用于 gRPC 协议的流量路由,支持基于服务和方法的路由。

示例 YAML:

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GRPCRoute
metadata:
  name: example-grpc-route
spec:
  parentRefs:
    - name: example-gateway
  rules:
    - matches:
        - method:
            service: example.Greeter
            method: SayHello
      backendRefs:
        - name: example-grpc-service
          port: 50051

6. 从 Ingress 迁移到 Gateway API

如果你正在使用 Ingress 管理服务的外部访问,迁移至 Gateway API 是一个值得考虑的优化方向。

6.1 Ingress 示例

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /app1
        pathType: Prefix
        backend:
          service:
            name: app1-service
            port:
              number: 80
      - path: /app2
        pathType: Prefix
        backend:
          service:
            name: app2-service
            port:
              number: 80

6.2 创建 GatewayClass

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GatewayClass
metadata:
  name: nginx-gateway-class
spec:
  controllerName: nginx.org/gateway-controller

6.3 创建 Gateway 和 HTTPRoute

# Gateway
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: Gateway
metadata:
  name: example-gateway
spec:
  gatewayClassName: nginx-gateway-class
  listeners:
    - name: http
      protocol: HTTP
      port: 80

# HTTPRoute
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: HTTPRoute
metadata:
  name: example-http-route
spec:
  parentRefs:
    - name: example-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /app1
      backendRefs:
        - name: app1-service
          port: 80
    - matches:
        - path:
            type: PathPrefix
            value: /app2
      backendRefs:
        - name: app2-service
          port: 80

通过这种方式,你可以将原本集中于 Ingress 的路由逻辑拆解为多个资源,实现更灵活、可维护的网络架构。


7. 总结

Kubernetes Gateway API 是对传统 Ingress 的一次重大升级,它通过资源解耦、多协议支持和角色导向设计,提供了更灵活、可扩展的服务网络能力。

核心优势

  • 多协议支持(HTTP、TCP、UDP、gRPC 等)
  • 角色清晰,便于团队协作
  • 支持厂商中立,便于迁移

迁移建议

  • 从 Ingress 拆解为 GatewayClass + Gateway + Route
  • 逐步替换资源,确保兼容性和稳定性

随着越来越多厂商和社区对 Gateway API 的支持,它将成为 Kubernetes 服务网络的标准接口。掌握其核心概念和使用方式,将极大提升你在云原生环境下的网络管理能力。


原始标题:Kubernetes Gateway API