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: platform、namePrefix: platform-,故活动对象名为platform-gateway-*)
- Name
- 工作负载
- Description
- Deployment,
replicas: 2(kustomizationreplicas:同样设为 2),revisionHistoryLimit: 2,滚动更新策略maxUnavailable: 0/maxSurge: 1,挂priorityClassName: production-high
- Name
- 镜像与版本
- Description
ghcr.io/yldm-tech/gateway:2.3.1(以 kustomizationimages:块的newTag为准,由 argocd-image-updater 按 semver 写回;deployment.yaml 内联的2.2.39会被该 tag 覆盖,不要手动钉版本)
- Name
- 调度
- Description
nodeSelector: workload=app;另配podAntiAffinity的preferred规则,按kubernetes.io/hostname把同app=gateway的 Pod 分散到不同节点
- Name
- 端口
- Description
- 容器
containerPort: 8080(namehttp)
- Name
- 资源
- Description
- requests
cpu: 50m/memory: 64Mi,limitscpu: 200m/memory: 256Mi
- Name
- 存储
- Description
- 无 PVC;仅以 ConfigMap 形式挂载只读配置(
base-config挂到/root/configs/base,gateway-config的gateway.yaml以subPath挂到/root/configs/services/gateway.yaml)
- Name
- 健康探针
- Description
- livenessProbe
GET /health/live、readinessProbeGET /health/ready,均走 8080
镜像拉取使用 ghcr-secret;Deployment 通过 reloader.stakater.com/auto: "true" 注解开启了 ConfigMap/Secret 变更后的自动滚动重载。
配置与依赖
主配置来自 ConfigMap gateway-config 的 gateway.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:)按前缀映射到后端,并标注鉴权等级:
| 前缀 | 后端 service | target_base | 鉴权 |
|---|---|---|---|
| /auth | auth | /api/v1/auth | none |
| /user | user | /api/v1 | required |
| /commerce | commerce | /api/v1 | optional |
| /engagement | engagement | /api/v1 | optional |
| /linear | engagement | /api/v1/linear | optional |
| /notification | notification | /api/v1 | required |
| /aidict | aidict | /api/v1/aidict | none |
| /myetc | myetc | /api/v1/myetc | required |
| /management | admin | /api/v1 | required |
| /creativestore | creativestore | /api/v1/creativestore | optional |
| /analytics | analytics | /api/v1 | required |
| /media | media | /api/v1/media | optional |
| /magicbox | magicbox | /api/v1/magicbox | required |
| /scheduler | scheduler | /api/v1/scheduler | required |
| /social | social | /api/v1/social | optional |
| /kubepocket | kubepocket | /api/v1/kubepocket | optional |
高危端点限流(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)、GatewayHighCPU 与 GatewayHighMemory(用量超 limit 80% 持续 10m,warning)。
弹性与可用性:HorizontalPodAutoscaler gateway 以 CPU 70% / 内存 80% 为目标,minReplicas: 2、maxReplicas: 5,扩容激进(稳定窗口 0s,每 30s 最多翻倍或加 2 个 Pod 取 Max)、缩容保守(稳定窗口 300s,每 60s 最多减 50%)。VerticalPodAutoscaler gateway-vpa 为 updateMode: "Off"(仅给建议,不自动改),建议区间 cpu 25m–500m / memory 32Mi–512Mi。PodDisruptionBudget gateway 设 maxUnavailable: 1。
返回 platform 总览