gateway · 平台 API 网关

gateway 是 platform 类目下的 API 网关,镜像来自 ghcr.io/yldm-tech/gateway,以 SERVER_MODE=release 启动并监听 HTTP 8080,对外作为集群所有后端微服务的统一入口(域名 api.yldm.ai)。它按 URL 前缀把请求转发到 auth、user、commerce、engagement、notification、aidict、myetc、management、creativestore、magicbox、kubepocket 等后端服务,对每条路由施加 none / optional / required 三档鉴权策略,并对登录、注册、支付、短信/邮件发送等高危端点做更细粒度的限流。它通过 Consul 做服务发现,依赖独立的数据库与 Redis,并自行签发与校验 JWT。

部署形态

  • Name
    命名空间
    Description
    platform(kustomization 设 namespace: platformnamePrefix: platform-,故活动对象名为 platform-gateway-*)
  • Name
    工作负载
    Description
    Deployment,replicas: 2(kustomization replicas: 同样设为 2),revisionHistoryLimit: 2,滚动更新策略 maxUnavailable: 0 / maxSurge: 1,挂 priorityClassName: production-high
  • Name
    镜像与版本
    Description
    ghcr.io/yldm-tech/gateway:2.3.1(以 kustomization images: 块的 newTag 为准,由 argocd-image-updater 按 semver 写回;deployment.yaml 内联的 2.2.39 会被该 tag 覆盖,不要手动钉版本)
  • Name
    调度
    Description
    nodeSelector: workload=app;另配 podAntiAffinitypreferred 规则,按 kubernetes.io/hostname 把同 app=gateway 的 Pod 分散到不同节点
  • Name
    端口
    Description
    容器 containerPort: 8080(name http)
  • Name
    资源
    Description
    requests cpu: 50m / memory: 64Mi,limits cpu: 200m / memory: 256Mi
  • Name
    存储
    Description
    无 PVC;仅以 ConfigMap 形式挂载只读配置(base-config 挂到 /root/configs/base,gateway-configgateway.yamlsubPath 挂到 /root/configs/services/gateway.yaml)
  • Name
    健康探针
    Description
    livenessProbe GET /health/live、readinessProbe GET /health/ready,均走 8080

镜像拉取使用 ghcr-secret;Deployment 通过 reloader.stakater.com/auto: "true" 注解开启了 ConfigMap/Secret 变更后的自动滚动重载。

配置与依赖

主配置来自 ConfigMap gateway-configgateway.yaml,它继承 base/production.yaml 并覆盖网关特有字段,关键内容如下。

后端服务清单(services:)登记了各微服务的集群内地址(形如 http://prod-<svc>:80)与启停开关。当前 enabled: true 的有 auth、user、engagement、commerce、notification、aidict、myetc、admin(target 指向 prod-management)、creativestore、magicbox、kubepocket;enabled: false 的有 analytics、media、social、scheduler。

路由表(routes:)按前缀映射到后端,并标注鉴权等级:

前缀后端 servicetarget_base鉴权
/authauth/api/v1/authnone
/useruser/api/v1required
/commercecommerce/api/v1optional
/engagementengagement/api/v1optional
/linearengagement/api/v1/linearoptional
/notificationnotification/api/v1required
/aidictaidict/api/v1/aidictnone
/myetcmyetc/api/v1/myetcrequired
/managementadmin/api/v1required
/creativestorecreativestore/api/v1/creativestoreoptional
/analyticsanalytics/api/v1required
/mediamedia/api/v1/mediaoptional
/magicboxmagicbox/api/v1/magicboxrequired
/schedulerscheduler/api/v1/schedulerrequired
/socialsocial/api/v1/socialoptional
/kubepocketkubepocket/api/v1/kubepocketoptional

高危端点限流(rate_limit.high_risk_endpoints)对若干敏感路径配了独立的 rate / burst:/api/v1/auth/login(5/10)、/api/v1/auth/register(2/5)、/api/v1/auth/reset-password(2/5)、/api/v1/commerce/orders/pay(1/2)、/api/v1/notification/sms/send(1/2)、/api/v1/notification/email/send(1/3)。

服务发现走 Consul:service_discovery.consul.enabled: true,地址 consul.consul.svc.cluster.local:8500;k8s 原生发现关闭(k8s.enabled: false)。容器环境变量也开启了 CONSUL_ENABLED=true,CONSUL_ADDR=consul.consul.svc.cluster.local:8500,CONSUL_DATACENTER=dc1,CONSUL_WAIT_TIME=30s。链路追踪以 service_name: yldm-gateway 上报。

外部凭证由 ExternalSecret gateway-db-secret 从 ClusterSecretStore vault-backend 拉取(refreshInterval: 1h),合成同名 Secret 后注入容器。绝大多数键取自 yldm/production/gateway(数据库 db_host/db_port/db_name/db_user/db_password,Redis redis_host/redis_port/redis_password/redis_db/redis_pubsub_db,以及 JWT 的 jwt_secret/jwt_expires_in/jwt_refresh_expires_in);管理员账号 DB_ADMIN_USER/DB_ADMIN_PASSWORD 单独取自共享路径 yldm/database。由此可见网关依赖一套独立的数据库与 Redis,且自行签发/刷新 JWT。

访问与监控

Service gateway(ClusterIP)以 port: 80 暴露,targetPort: 8080,名为 http

入口由 Ingress yldm-api-gateway 提供,ingressClassName: traefik,host api.yldm.ai,path: /(Prefix)全部转发到 platform-gateway:80。TLS 证书 yldm-ai-api-tls-cert 由 cert-manager 的 letsencrypt-prod ClusterIssuer 签发。DNS 经 external-dns 写为指向 Cloudflare Tunnel 的 CNAME(*.cfargotunnel.com,cloudflare-proxied: "true")。Ingress 层另配了 proxy-body-size: 10m、连接/收发/读取超时各 600s、WebSocket 透传(websocket-services: platform-gateway)以及基础限流 limit-rps: 100(更细的限流由网关内部完成)。

监控方面有 ServiceMonitor gateway,抓 http 端口的 /metrics(interval: 30s),并把 pod/namespace/service 标签重写到指标上。PrometheusRule gateway(group gateway.rules)覆盖以下告警:GatewayPodDown(up==0 持续 1m,critical)、GatewayInsufficientPods(运行 Pod 数 < 1,critical)、GatewayHighErrorRate(5xx 占比 > 3% warning / > 5% critical,基于 http_requests_total)、GatewayHighLatency(P95 > 0.5s warning / > 1s critical,基于 http_request_duration_seconds_bucket)、GatewayPodRestarting(15m 重启率 > 0.05,warning)、GatewayHighCPUGatewayHighMemory(用量超 limit 80% 持续 10m,warning)。

弹性与可用性:HorizontalPodAutoscaler gateway 以 CPU 70% / 内存 80% 为目标,minReplicas: 2maxReplicas: 5,扩容激进(稳定窗口 0s,每 30s 最多翻倍或加 2 个 Pod 取 Max)、缩容保守(稳定窗口 300s,每 60s 最多减 50%)。VerticalPodAutoscaler gateway-vpaupdateMode: "Off"(仅给建议,不自动改),建议区间 cpu 25m–500m / memory 32Mi–512Mi。PodDisruptionBudget gatewaymaxUnavailable: 1

返回 platform 总览

评论