沪江 ApiGateway 实践
关于我 夏志培 沪江网运维架构师 / 基础运维团队负责人,10+ 年运维经验 负责沪江 Redis/Codis,DNS,ApiGateway,RabbitMQ,ELK,Ceph 等基础组件运维和架构工作 对系统底层以及开源的软件有浓厚的兴趣 微信 :summer_xia_027
目录 01 02 03 背景和目的 ApiGateway 架构 ApiGateway 的实现 CONTENTS 04 ApiGateway 部署 / 性能 05 QA
01 背景和目标
项目背景 域名调度 C P U TCP WAF/2fa 域名解析 F5 HTTS/irule TCP 业务节点 日志收集 ELK 灰度 限流 发布 soa/service 发布系统
目标 1. 流量调度 2. 协议适配 /SSL 卸载 / 路由 / 足够的扩展 限流 灰度发布 / 黑白名单 5. 统一日志收集 6. 贴近业务的 health_check 7. 轻松运维
02 ApiGateway 架构
ApiGateway 架构 Load balance 管理后台 saltstack APIGATEWAY SSL WAF APIGATEWAY ELK 灰度 路由 限流 irule 编排 后端服务 / 接口
03 ApiGateway 的实现
ApiGateway 实现 : 流量调度 - 调度域名 A 机房 SSL 的调度域名 group1.idca.cnc.ssl.cname.com A 机房 ApiGateway 的调度域名 group1.idca.cnc.gateway.cname.c om B 机房 SSL 的调度域名 group1.idcb.cnc.ssl.cname.com B 机房 ApiGateway 的调度域名 group1.idcb.cnc.gateway.cname.c om
ApiGateway 实现 : 流量调度 - 流量转发 ApiGateway SSL B 机房 ApiGateway A 机房 ApiGateway
ApiGateway 实现 : 流量调度 - 流量转发 通过专线将流量引到其他 IDC
ApiGateway 实现 : 协议适配 HTTPS HTTP ApiGateway SSL HTTP WAF/2fa HTTP ApiGateway irules HTTP HTTP HTTP 业务 1 业务 2 业务 2
ApiGateway 实现 : 协议适配 (SSL-offload) SSL:domain_A SSL:domain_B F5 DNAT DNAT DNAT domain_a:553 domain_a:554 domain_a:555
ApiGateway 实现 : 路由 (SSL 路由 ) site:domain_a/admin?xx site:domain_a/query?xx site:domain_a/static/xx ApiGate way SSL IDC_A WAF IDC_A 2fa IDC_A apigateway group1
ApiGateway 实现 : 路由 Loading-config Starting-work rewrite Rewrite tail Access tail content Output-header-filter Output-body-filter log route.lua upstream_xx_api upstream_xx_admin upstream_xx_auth xxx_irules.lua node1 node2 node3
ApiGateway 实现 : 路由 -irules 实例
ApiGateway 实现 : 路由 -irules 配置实例
ApiGateway 实现 : 限流 ( 客户端 / 服务器端 ) limit_req_zone $x_forwarded_for zone=domainx_c:50m rate=100r/s; limit_req_zone $remote_addr zone=domainx_s:50m rate=300r/s; limit_req zone=domainx_c_client burst=1000; limit_req zone=domainx_s burst=1000; cdn 业务节点 Burst=1000 漏桶算法 ApiGateway
ApiGateway 实现 : 灰度发布 灰度 pool upstream_xx_gray cdn/ssl/ WAF ApiGateway 上下线接口 LUA sharedict 正常 pool upstream_xx 发布系统 redis ( 配置策略 )
ApiGateway 实现 : 灰度发布 路由策略 : { api:gateway:strategy:login.hj.com : iproute,vesion_range } 策略处理 - 灰度处理 Local cache 格式 { api:gateway:process:iproute:login.hj.com:192.168.164.128 : gray_upstream } Redis 格式 { api:gateway:process:iproute:login.hj.com :[{"filterip":"192.168.164.128","upstream": gray1"},{"filterip":"192.168.1.0/24","upstream": gray2"}]}}1
ApiGateway 实现 : 日志收集 / 监控 A 机房 apigateway B 机房 apigateway A 机房的 ELK B 机房的 ELK 1. 单域名的流量 2. 单域名的 TPS 3. 请求 HTTP CODE 4. 设备 ua 5. 响应时间 监控系统
ApiGateway 实现 : health_check nginx_upstream_check_module-0.3.0 F5 TCP 5 秒 / 次, 16s 超时 ApiGateway SSL TCP 5 秒 / 次, 5 次异常剔除, 连续 2 次加入 WAF HTTP 3 秒 / 次, 5 次异常剔除, 正常 2 次加入 业务节点 ApiGateway
ApiGateway 实现 : health_check
04 ApiGateway 部署和性能
ApiGateway/SSL 的部署以及性能参数 安装 :yum install apigateway redis -y 服务器的内核参数 : net.core.somaxconn = 655360 系统设定的 backlog 值, 若 listen 时 backlog 大于此值, 则不会生效 net.core.netdev_max_backlog = 6553600 net.ipv4.tcp_max_tw_buckets = 50000 net.ipv4.tcp_tw_timeout = 5 net.ipv4.tcp_tw_reuse = 1 网卡设备的请求队列长度 ( 硬件 backlog) 端口回收, 以及限制 time_wait 状态的 tcp 连接 net.ipv4.ip_local_port_range = 1025 65535 net.unix.max_dgram_qlen = 655360 可用端口范围 unix domain socket 的数据包队列
ApiGateway 的部署以及性能参数 Nginx 的部署参数 : worker_processes` 8 nginx 进程数, 建议按照 cpu 数目来指定, 一般为它的 倍数 worker_cpu_affinity 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 01000000 10000000 为每个进程分配 cpu, 上例中将 8 个进程分配到 8 个 cpu, 当然可以写多个, 或者将一个进程分配到多个 cpu worker_rlimit_nofile 102400 这个指令是指当一个 nginx 进程打开的最多文件描述 符数目 events: worker_connections accept_mutex multi_accept use epoll; 前端开启 keepalive keepalive_timeout keepalive_requests 后端开启 keepalive upstream keepalive location proxy_http_version 65535 Off On 75 100 1000 1.1 每个进程允许的最多连接数, 理论上每台 nginx 服务器的最大连接数为 worker_processes*worker_connections 表示一条长连接可以保持的时间为 75s 表示一条长连接可以处理的请求数 100 个 其中 keepalive 参数表示 proxy 与 upstream 间每个 worker 维持的长连接数, 而 location 中需要将外部请求的 Connection 头部清空, 并设置请求的 http 版本为 1.1 版 这些配置才能使能 proxy 和 upstream 间的长连接
ApiGateway 的部署和性能 ApiGateway SSL 的压力测试数据 ( 不跨网络 ): 请求次数 请求内容大小 TPS 完成时间 服务器最高负载 带宽 ( 未跨网络 ) 43561497 4k 481628 90s 32 1.95GB/s 40028046 8k 442440 90s 32 3.48GB/s 32425127 16k 358258 90s 32 5.55GB/s 26482727 32k 293251 90s 32 7.98GB/s ApiGateway SSL 的压力测试数据 :( 跨网络 ) 请求次数 请求内容大小 TPS 完成时间 服务器最高负载 带宽 2487381 4k 27553 90s <0.5 114.3MB/s 1312779 8k 14531 90s <0.5 117.02MB/s 669161 16k 7406 90s <0.5 117.54MB/s 337922 32k 3740 90s <0.5 118.12MB/s
ApiGateway 的部署和性能 ApiGateway 的压力测试数据 ( 不跨网络 ): 请求次数请求内容大小 TPS 完成时间服务器最高负载带宽 ( 未跨网络 ) 1kw 8k 525974 86s 0.59, 0.19, 0.09 0 1kw 16k 99125.71 184s 0.68, 0.28, 0.13 0 1kw 32k 11700.03 1303s 0.22, 0.10, 0.08 0 1kw 64k 10206.13 7513s 0.18, 0.05, 0.01 0 ApiGateway 的压力测试数据 :( 跨网络 ) 请求次数请求内容大小 TPS 完成时间服务器最高负载带宽 1kw 8k 14720.81 73s 0.59, 0.19, 0.09 114.3MB/s 1kw 16k 7823.42 104s 0.68, 0.28, 0.13 112.7MB/s 1kw 32k 3416.15 1710s 0.22, 0.10, 0.08 116.5MB/s 1kw 64k 1715.43 8583s 0.18, 0.05, 0.01 118.8MB/s
ApiGateway 实施后的效果 1.SSL 的吞吐能力增加 1 个数量级 + 2.irules 处理能力增加一倍 +, 并且横向扩展 3. 更容易流量调度
ApiGateway 实施过程中的 TIP 1. 证书链 /SNI 2. Nginx 加载配置文件 3. Lua 的变量
QA