K8s HTTPS流量管理实战:GatewayAPI指南

1. Gateway API简介与核心概念

Gateway API是Kubernetes中用于管理服务网格流量的新一代标准API,旨在替代传统的Ingress资源。它提供了更丰富的流量管理功能,特别适合现代微服务架构的需求。

1.1 核心组件

  • Gateway:定义网络入口点的配置,相当于传统架构中的负载均衡器
  • HTTPRoute:管理HTTP/HTTPS流量的路由规则
  • TLSRoute:专门处理TLS终结和路由的配置
  • BackendPolicy:定义后端服务的访问策略

1.2 与传统Ingress的区别

特性IngressGateway API
协议支持HTTP/HTTPSHTTP/HTTPS/TCP/UDP
路由粒度主机/路径请求头/方法/路径等
跨命名空间不支持支持
TLS管理基础配置细粒度控制
实现标准化各厂商实现差异大标准规范

2. HTTPS配置实战

2.1 证书管理方案

方案1:使用Cert-Manager自动签发证书
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: example-com
  namespace: default
spec:
  secretName: example-com-tls
  duration: 2160h # 90d
  renewBefore: 360h # 15d
  issuerRef:
    name: letsencrypt-prod
    kind: ClusterIssuer
  dnsNames:
  - example.com
  - www.example.com

方案2:手动导入已有证书
kubectl create secret tls example-com-tls \
  --cert=path/to/cert.pem \
  --key=path/to/key.pem \
  -n default

2.2 配置HTTPS Gateway

apiVersion: gateway.networking.k8s.io/v1beta1
kind: Gateway
metadata:
  name: secure-gateway
spec:
  gatewayClassName: nginx
  listeners:
  - name: https
    protocol: HTTPS
    port: 443
    tls:
      mode: Terminate
      certificateRefs:
      - kind: Secret
        name: example-com-tls
    allowedRoutes:
      namespaces:
        from: All

3. 高级流量管理场景

3.1 基于路径的路由

apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
  name: path-based-route
spec:
  parentRefs:
  - name: secure-gateway
  hostnames:
  - "example.com"
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /api
    backendRefs:
    - name: api-service
      port: 8080
  - matches:
    - path:
        type: PathPrefix
        value: /static
    backendRefs:
    - name: static-service
      port: 80

3.2 金丝雀发布配置

apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
  name: canary-release
spec:
  parentRefs:
  - name: secure-gateway
  hostnames:
  - "example.com"
  rules:
  - matches:
    - headers:
      - type: Exact
        name: Canary-User
        value: "true"
    backendRefs:
    - name: new-version-service
      port: 8080
      weight: 20
  - backendRefs:
    - name: stable-version-service
      port: 8080
      weight: 80

4. 监控与排错

4.1 关键监控指标

  • Gateway指标

    • gateway_requests_total:总请求数
    • gateway_request_duration_seconds:请求延迟
    • gateway_tls_handshake_errors:TLS握手错误
  • 路由指标

    • http_route_requests_total:按路由统计的请求数
    • http_route_response_status:响应状态码分布

4.2 常见问题排查

  1. 证书问题

    kubectl describe certificate example-com
    kubectl logs -n cert-manager -l app=cert-manager
    

  2. 路由不生效

    kubectl describe httproute path-based-route
    kubectl get gateway secure-gateway -o yaml
    

  3. 连接问题

    kubectl get endpoints api-service
    telnet api-service 8080
    

5. 生产环境最佳实践

  1. 证书管理

    • 使用自动续期方案
    • 维护至少两个证书颁发源
    • 监控证书过期时间
  2. 安全配置

    • 强制HTTPS重定向
    • 启用HSTS
    • 配置现代TLS协议和加密套件
  3. 性能优化

    • 启用HTTP/2
    • 配置连接池
    • 合理设置超时参数
  4. 高可用设计

    • 多区域部署Gateway实例
    • 配置健康检查
    • 实现自动故障转移

通过本指南,您可以全面掌握在Kubernetes环境中使用Gateway API管理HTTPS流量的各项技术,从基础配置到高级场景,构建安全、可靠的服务入口。

更多推荐