当前位置:网站首页>我们被一个 kong 的性能 bug 折腾了一个通宵
我们被一个 kong 的性能 bug 折腾了一个通宵
2022-07-26 13:09:00 【Erda技术团队】
本文约 3500 字,预计阅读时间:9 分钟
在 Erda 的技术架构中,我们使用了 kong 作为 API 网关的技术选型。因其具备高并发低延时的特性,同时结合了 Kubernetes Ingress Controller,基于云原生的声明式配置方式,能够实现丰富的 API 策略。
参数调优NGINX_WORKER_PROCESSES、MEM_CACHE_SIZE、 DB_UPDATE_FREQUENCY、WORKER_STATE_UPDATE_FREQUENCY 参数以及 postgres 的 work_mem、 share_buffers 都进行了适当的调优。但是,没有任何效果 。
清理数据
kong 实例的读写分离确定了是 admin 接口的原因后,我们决定将 admin 跟业务的 kong 实例分开,希望 admin 的调用不会影响到业务的正常流量访问,期待达到 kong 的 admin 接口慢就慢吧,但是不要影响业务的访问性能。
然而,没有任何效果。
postgres 迁移 RDSkong 层面的努力无果之后,我们在测试过程中同时观察到了当调用 admin 接口试,postgres 的进程也增多了很多,CPU使用率也涨了起来,也是决定将 pg 迁移到 更为专业的RDS中。
还是,没有任何效果。
回滚最终我们还是回滚到了 0.14 版本,追求暂时“心灵的宁静”。
至此,线上的尝试基本搞一段落,也大致摸清了问题复现的条件,于是我们决定在线下构造一个环境来继续找出问题的原因。
导入数据我们将有问题的集群中的 postgre 的数据备份之后然后在一个新的集群中导入:
psql -h 127.0.0.1 -U kong < kong.sql
并且开启 kong 的 prometheus 插件, 方便使用 grafana 来查看性能图标:
curl -X POST http://10.97.4.116:8001/plugins --data "name=prometheus"
现象一curl http://10.97.4.116:8001
现象二然后我们来模拟在线上遇到的调用 admin 接口后业务访问性能变差的现象,先调用 admin 接口创建个业务的 api,以供测试,我们创建了一个 service 以及一个 routes:
curl -i -X POST http://10.97.4.116:8001/services/ -d 'name=baidu2' -d 'url=http://www.baidu.com'curl -i -X POST http://10.97.4.116:8001/services/baidu2/routes \-d "name=test2" \-d "paths[1]=/baidu2"
curl http://10.97.4.116:8000/baidu2 来模拟业务接口进行测试。#!/bin/bashcurl -i -X POST http://10.97.4.116:8001/services/ -d 'name=baidu' -d 'url=http://www.baidu.com'curl -i -X POST http://10.97.4.116:8001/services/baidu/routes \-d "name=test" \-d "paths[1]=/baidu"curl -s http://10.97.4.116:8001/servicescurl -i -X DELETE http://10.97.4.116:8001/services/baidu/routes/test
for i in `seq 1 100`; do sh 1.sh ; donecurl http://10.97.4.116:8000/baidu2
伴随现象kong 实例的 cpu 跟 mem 都持续上涨,且当 admin 接口调用结束后此现象依然没有结束。mem 上涨到一定程度会到时 nginx worker 进程 oom 掉,然后重启,这个也许是访问慢的原因;
我们设置了
KONG_NGINX_WORKER_PROCESSES为 4,并且为 pod 的内存为 4G 的时候,pod 的整体内存会稳定在 2.3G, 但是 调用 admin 接口试,pod 的内存就会一直上涨至超过 4G,触发 worker 的 OOM,于是我将 pod 的内存调整到了 8G。再次调用 admin 接口,发现 pod 的内存依然一直上涨,只是上涨到了 4.11 G 就结束了,这似乎意味着我们是要设置 pod 的内存为KONG_NGINX_WORKER_PROCESSES两倍,这个问题就解决了(但是还有个重要的问题是为什么调用一次 admin 接口,会导致内存涨了那么多);另外,当我持续调用 admin 接口的时候, 最终的内存会持续增长并且稳定到 6.9G。
这个时候我们将问题抽象一下:
继续调查内存是被什么占用了:
pmap -x [pid] 前后两次查看了 worker 进程的内存分布,变化的是第二张如中框起来来的部分,从地址上看整块内存都被换过了,但是将内存数据导出并且字符串化之后,没有什么有效的信息可供进一步排查。

该问题跟 kong 的升级(0.14 --> 2.2.0) 没有关系,直接使用 2.2.0 版本也会有这个问题; kong 每隔 worker_state_update_frequency时间后会在内存中重建 router,一旦开始重建就会导致 内存上涨,看了下代码问题出在了Router.new的方法这里,会申请 lrucache 但是使用后没有flush_all,根据最新的 2.8.1 版本的 lrucache 进行了释放问题依然存在;也就是 kong 的 Router.new方法里的其他逻辑到时的内存上涨;

这也就表明这个问题是 kong 存在的一个性能 bug,及时在最新的版本中依然存在,当 route 跟 service 达到某个量级的时候会出现调用 admin 接口,导致 kong 的 worker 内存迅速上涨,使得 oom 进而引发业务访问性能变差的现象,临时的解决办法可以是减少 NGINX_WORKER_PROCESSES的数量并且增加 kong pod 的内存量,保证调用 admin 接口后需要的内存足够使用不触发 oom,才能保证业务的正常使用。
———
Erda Github 地址: https://github.com/erda-project/erda Erda Cloud 官网: https://www.erda.cloud/

本文分享自微信公众号 - 尔达 Erda(gh_0f507c84dfb0)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
边栏推荐
- Can MySQL customize variable parameter storage functions?
- 关于自动重复调用接口的一种实现方式-反射
- 历史上的今天:IBM 获得了第一项专利;Verizon 收购雅虎;亚马逊发布 Fire Phone...
- RMII, smii, gmii, rgmii interfaces of Ethernet Driver
- Extra(5)—mysql执行计划(五十一)
- Detailed explanation of redis's single login
- Bitwise and how to write SQL
- 【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯
- LeetCode 263.丑数
- Dimension disaster dimension disaster suspense
猜你喜欢

Today in history: IBM obtained the first patent; Verizon acquires Yahoo; Amazon releases fire phone

牛客刷SQL---2

维度灾难 维数灾难 暂记

Food safety | can you eat any fruit?
![[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication](/img/d4/c677de31f73a0e0a4b8b10b91e984a.png)
[upper computer tutorial] Application of integrated stepping motor and Delta PLC (as228t) under CANopen communication

Emotion analysis model based on Bert

目标检测网络R-CNN 系列

HCIP第十二天笔记整理(BGP联邦、选路规则)

Hcip day 11 comparison (BGP configuration and release)

Food safety | is self-made food purchased online healthy food? Don't fall into these misunderstandings
随机推荐
Flutter prevents scientific counting and removes mantissa invalid 0
Mysql数据目录(3)---表数据结构myISAM(二十六)
Extra (5) - MySQL execution plan (51)
Create EOS account action
基于Bézier曲线的三维造型与渲染
火山引擎云上增长方案全景:30+方案齐出,兵发优势领域
How to face scientific and technological unemployment?
Today's sleep quality record 75 points
A college archives management system based on asp.net
Guys, please ask me, I have configured CDC to connect to Oracle according to the document, and I always run error reports and can't find the class validstione
Where is safe to open an account when buying stocks on mobile phones?
B+树挑选索引(2)---mysql从入门到精通(二十三)
Azure synapse analytics Performance Optimization Guide (2) -- optimize performance using materialized views (Part 1)
Food safety | is self-made food purchased online healthy food? Don't fall into these misunderstandings
panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)
jvm:类加载子系统干什么的?由什么组成?需要记住哪些八股文?
The best engineer was "forced" away by you like this!
B+树索引使用(7)匹配列前缀,匹配值范围(十九)
HCIP第十一天比较(BGP的配置、发布)
LCD notes (7) LCD driver framework_ Configure clock