[转帖]KeenTune调优实践之Nginx调优

keentune,实践,nginx · 浏览次数 : 0

小编点评

内容生成时需要带简单的排版,以便生成内容时能够清晰地排列。排版方法可以根据需要选择,例如以下排版方法可用于生成内容: * **空格**:空格可用于排列内容,例如将多个单词排列到一起。 * ****:****可用于排列内容,例如将多个字符排列到一起。 * ******:****可用于排列内容,例如将多个字符排列到一起。 * ******:****可用于排列内容,例如将多个字符排列到一起。 * ******:****可用于排列内容,例如将多个字符排列到一起。 * ******:****可用于排列内容,例如将多个字符排列到一起。 排版方法的具体选择取决于需要。例如,如果需要将多个单词排列到一起,可以使用空格或**空格**。如果需要将多个字符排列到一起,可以使用****或**。如果需要将多个字符排列到一起,可以使用****或**。

正文

https://openanolis.cn/sig/KeenTune/doc/423701433171625827

 

环境准备

准备两台虚拟机,VM A安装wrk,keentuned,keentune-brain,keentune-bench,作为测试机;VM B安装Nginx,作为Nginx服务端,同时安装KeenTune-target,为待测机器。
注:请确保两台虚拟机网络互通

Nginx和wrk安装

1.wrk

在VM A上安装wrk测试工具

2. Nginx

在VM B上安装和启动nginx服务

2.1. install Nginx

yum install -y epel-release
yum install -y nginx

2.2. Modify Nginx configuration

vi /etc/nginx/nginx.conf 

events {
  #worker_connections 1024;
  worker_connections 65535;
}

http {
	...
  
  #access_log  /var/log/nginx/access.log  main;
  access_log  off;
  
  ...
}

KeenTune安装

1. keentuned

设置环境变量

#go编译器安装
wget https://storage.googleapis.com/golang/go1.15.6.linux-amd64.tar.gz
tar -C /usr/local -xzvf ./go1.15.6.linux-amd64.tar.gz
ln -s /usr/local/go/bin/* /usr/bin/ 
export PATH=$PATH:/usr/local/go/bin

#验证go编译器
go env
go version

#配置go环境
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

下载源码

git clone https://codeup.openanolis.cn/codeup/keentune/keentuned.git

解决依赖

cd keentuned
go mod vendor

编译安装

cd keentuned
./keentuned_install.sh

启动

keentuned

2. KeenTune-target

安装依赖包

# 安装python36和python36开发包(提供头文件和静态链接库)
yum install python36 python36-devel rust openssl-devel git
# 安装应用所需的Python依赖包
pip3 install tornado==6.1

下载源码

git clone https://codeup.openanolis.cn/codeup/keentune/keentune_target.git

编译安装

cd keentune_target
sudo python3 setup.py install

启动

keentune-target

3. KeenTune-bench

安装依赖包

# 安装python36和python36开发包(提供头文件和静态链接库)
yum install python36 python36-devel rust openssl-devel git
# 安装应用所需的Python依赖包
pip3 install tornado==6.1

下载源码

git clone https://codeup.openanolis.cn/codeup/keentune/keentune_bench.git

编译安装

cd keentune_bench
python3 setup.py install

启动

keentune-bench

4. KeenTune-brain

安装依赖包

# 安装python36和python36开发包(提供头文件和静态链接库)
yum install python36 python36-devel rust openssl-devel git
# 安装应用所需的Python依赖包,最好先更新一下pip:pip3 install --upgrade pip
pip3 install numpy==1.19.5 POAP==0.1.26 tornado==6.1 hyperopt==0.2.5 ultraopt==0.1.1 bokeh==2.3.2 requests==2.25.1 pySOT==0.3.3 scikit_learn==0.24.2 paramiko==2.7.2 PyYAML==5.4.1

下载源码

git clone https://codeup.openanolis.cn/codeup/keentune/keentune_brain.git

编译安装

cd keentune_brain
sudo python3 setup.py install

启动

keentune-brain

参数调优

1. Nginx.conf参数调优

1.1 Nginx.conf参数介绍

Nginx.conf参数配置文件

nginx_conf参数域使用的是/etc/nginx/nginx.conf文件中定义的nginx配置参数 默认安装路径:/etc/keentune/parameter/nginx.json

更多参数配置文件写法参考《KeenTune自定义调优参数》

1.2 benchmark配置介绍

定义benchmark分为benchmark脚本和benchmark配置两部分

wrk-http长连接benchmark脚本

benchmark脚本中实现benchmark工具的封装,本例中执行wrk命令并解析输出。
执行的wrk命令为

wrk -t 10 -c 300 -d 1 --latency [ip]


选择Latency_90,Latency_99,Requests_sec,Transfer_sec四个benchmark指标作为输出

wrk-http长连接benchmark配置

配置文件中定义了执行benchmark脚本的命令,benchmark脚本的位置以及benchmark脚本输出指标的权重, 本例中为吞吐量调优配置,Requests_sec权重设置为100,其他为0

更多bench配置文件写法参考《KeenTune自定义benchmark》

1.3 执行调优

keentune param tune --param nginx.json --bench bench_wrk_nginx_long.json --job nginx_demo --iteration 10

2 敏感参数识别

使用 keentune sensitize collect 命令采集数据

keentune sensitize collect --param sysctl.json --bench bench_wrk_nginx_long.json --data demo --iteration 100

使用 keentune sensitize list 命令可以看到所有可以用于敏感参数识别的数据

>> keentune sensitize list

Get sensitive parameter identification results successfully, and the details displayed in the terminal.
+------------+----------------------+-----------+
|data name   |application scenario  |algorithm  |
+------------+----------------------+-----------+
|nginxtest1  |tuning                |TPE        |
|nginxtest   |tuning                |TPE        |
|demo        |tuning                |TPE        |
+------------+----------------------+-----------+
使用 keentune sensitize train 命令执行敏感参数识别
>> keentune sensitize train --data demo --output demo --trials 2

[ok] Running Sensitize Train Success.

        trials: 2
        data: demo
        output: demo

        see more detailsby log file:  "/var/log/keentuned-sensitize-train-1637228899.log"

日志文件中可以看到敏感参数识别结果

| parameter name | sensitivity ratio |
| vm.max_map_count      |  0.02636468   |
| vm.min_free_kbytes    |  0.024904983  |
| net.ipv4.tcp_fin_timeout      |  0.023090081  |
| net.ipv4.tcp_moderate_rcvbuf  |  0.023066513  |
| vm.page-cluster       |  0.020621613  |
| kernel.shmall         |  0.020550262  |
| net.ipv4.conf.all.accept_redirects    |  0.019789968  |
| vm.watermark_scale_factor     |  0.019286443  |
| kernel.msgmni         |  0.019159662  |
| net.ipv4.tcp_syn_retries      |  0.017952599  |
| kernel.shmmni         |  0.017676342  |
| net.core.netdev_budget        |  0.01709693   |
| kernel.dmesg_restrict         |  0.016918708  |
| net.ipv4.tcp_ecn      |  0.01632732   |
| net.ipv4.tcp_no_metrics_save  |  0.016036037  |
| net.ipv4.route.max_size       |  0.015378745  |
| net.ipv4.tcp_fastopen         |  0.014844485  |
| net.ipv4.tcp_window_scaling   |  0.014674037  |
| vm.overcommit_ratio   |  0.014470241  |
| net.core.wmem_default         |  0.014349915  |

我们可以根据敏感参数筛选的结果对参数进行选择

3. Nginx.conf + Sysctl参数混合调优

这一步我们将会把Nginx_conf参数和sysctl经过筛选的参数写到一个参数配置文件中进行参数混合调优(生成参数混合配置文件的操作目前只能手动进行,文件下文已经提供)。 将参数配置文件放到/etc/keentune/parameter目录下,运行参数调优即可。

Nginx参数+敏感内核参数混合参数配置文件:

{
    "nginx_conf": {
        "worker_rlimit_nofile": {
            "desc": "默认值:500",
            "dtype": "int",
            "range": [
                300,
                655350
            ]
        },
        "worker_connections": {
            "desc": "默认值:500",
            "dtype": "int",
            "range": [
                300,
                160000
            ]
        },
        "multi_accept": {
            "desc": "默认值:off",
            "dtype": "string",
            "options": [
                "on",
                "off"
            ]
        },
        "sendfile": {
            "desc": "默认值:on",
            "dtype": "string",
            "options": [
                "on",
                "off"
            ]
        },
        "tcp_nopush": {
            "desc": "默认值:on",
            "dtype": "string",
            "options": [
                "on",
                "off"
            ]
        },
        "tcp_nodelay": {
            "desc": "默认值:on",
            "dtype": "string",
            "options": [
                "on",
                "off"
            ]
        },
        "keepalive_timeout": {
            "desc": "默认值:30",
            "dtype": "int",
            "range": [
                10,
                100
            ]
        },
        "keepalive_requests": {
            "desc": "默认值:100",
            "dtype": "int",
            "range": [
                10,
                1000
            ]
        },
        "client_body_timeout": {
            "desc": "默认值:30",
            "dtype": "int",
            "range": [
                10,
                100
            ]
        },
        "send_timeout": {
            "desc": "默认值:30",
            "dtype": "int",
            "range": [
                10,
                100
            ]
        },
        "client_body_buffer_size": {
            "desc": "默认值:64k",
            "dtype": "string",
            "options": [
                "8k",
                "16k",
                "32k",
                "64k",
                "128k",
                "526k",
                "1024k"
            ]
        },
        "client_max_body_size": {
            "desc": "默认值:32m",
            "dtype": "string",
            "options": [
                "4m",
                "8m",
                "16m",
                "32m",
                "64m",
                "128m",
                "256m"
            ]
        },
        "client_header_buffer_size": {
            "desc": "默认值:4k",
            "dtype": "string",
            "options": [
                "2k",
                "4k",
                "8k",
                "16k",
                "32k",
                "64k"
            ]
        },
        "client_header_timeout": {
            "desc": "默认值:30",
            "dtype": "int",
            "range": [
                10,
                100
            ]
        },
        "open_file_cache": {
            "desc": "默认值:max=10240 inactive=10s",
            "dtype": "string",
            "options": [
                "max=1024 inactive=10s",
                "max=10240 inactive=10s"
            ]
        },
        "open_file_cache_valid": {
            "desc": "默认值:30s",
            "dtype": "string",
            "options": [
                "10s",
                "30s",
                "60s",
                "90s",
                "120s",
                "150s"
            ]
        },
        "open_file_cache_min_uses": {
            "desc": "默认值:2",
            "dtype": "int",
            "range": [
                2,
                10
            ]
        },
        "gzip": {
            "desc": "默认值:off",
            "dtype": "string",
            "options": [
                "on",
                "off"
            ]
        },
        "gzip_min_length": {
            "desc": "默认值:1024",
            "dtype": "int",
            "range": [
                1024,
                102400
            ]
        },
        "gzip_buffers": {
            "desc": "默认值:4 4k",
            "dtype": "string",
            "options": [
                "4 4k",
                "4 8k",
                "8 8k",
                "16 8k",
                "16 16k",
                "16 32k"
            ]
        },
        "gzip_comp_level": {
            "desc": "默认值:5",
            "dtype": "int",
            "range": [
                1,
                9
            ]
        },
        "ssl_session_cache": {
            "desc": "默认值:shared:SSL:1m",
            "dtype": "string",
            "options": [
                "off",
                "none",
                "builtin:1000",
                "shared:SSL:1m"
            ]
        },
        "ssl_session_timeout": {
            "desc": "默认值:5m",
            "dtype": "string",
            "options": [
                "2m",
                "5m",
                "10m",
                "20m"
            ]
        },
        "proxy_connect_timeout": {
            "desc": "默认值:5",
            "dtype": "int",
            "range": [
                1,
                50
            ]
        },
        "proxy_read_timeout": {
            "desc": "默认值:60",
            "dtype": "int",
            "range": [
                6,
                600
            ]
        },
        "proxy_send_timeout": {
            "desc": "默认值:60",
            "dtype": "int",
            "range": [
                6,
                600
            ]
        },
        "ssl_buffer_size": {
            "desc": "默认值:64",
            "dtype": "string",
            "options": [
                "64",
                "128",
                "526",
                "4k",
                "64k",
                "256k",
                "1m",
                "4m"
            ]
        }
    },
    "sysctl": {
        "vm.max_map_count":{
            "desc":"Defines the maximum memory area that a process can have.",
            "dtype":"int",
            "range":[
                100000,
                10000000
            ],
            "step":100000
        },
        "vm.min_free_kbytes":{
            "desc":"Size of memory reserved in each memory area, in KB.",
            "dtype":"int",
            "range":[
                10240,
                1024000
            ],
            "step":10240
        },
        "net.ipv4.tcp_fin_timeout":{
            "desc":"Maximum duration for a socket to remain in the FIN_WAIT_2 state.",
            "dtype":"int",
            "range":[
                1,
                120
            ],
            "step":1
        },
        "net.ipv4.tcp_moderate_rcvbuf":{
            "desc":"Whether to adjust the receive buffer when receiving data 0: no adjustment 1: yes",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "0",
                "1"
            ]
        },
        "vm.page-cluster":{
            "desc":"Number of pages written to the swap partition each time.",
            "dtype":"int",
            "range":[
                0,
                8
            ],
            "step":1
        },
        "kernel.shmall":{
            "desc":"The total amount of shared memory available on the system in bytes",
            "dtype":"int",
            "range":[
                1073741824,
                8589934592
            ],
            "step":1073741824
        },
        "net.ipv4.conf.all.accept_redirects":{
            "desc":"Receives and sends ICMP redirection messages. The default value is True for hosts and False for routers. 0: disabled 1: yes",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "0",
                "1"
            ]
        },
        "vm.watermark_scale_factor":{
            "desc":"Controls the radical degree of the kswapd process, that is, how much memory needs to be released for the system (NUMA node) from wakeup to hibernation.",
            "dtype":"int",
            "range":[
                10,
                1000
            ],
            "step":10
        },
        "kernel.msgmni":{
            "desc":"System Message Queue Length",
            "dtype":"int",
            "range":[
                8000,
                128000
            ],
            "step":8000
        },
        "net.ipv4.tcp_syn_retries":{
            "desc":"Number of times that the local device retransmits TCP SYN packets due to timeout. The value cannot be greater than 255. This parameter is valid only for outgoing connections. For incoming connections, this parameter is controlled by tcp_retries1.",
            "dtype":"int",
            "range":[
                3,
                64
            ],
            "step":1
        },
        "kernel.shmmni":{
            "desc":"Maximum number of shared memory segments in the system.",
            "dtype":"int",
            "range":[
                1024,
                16384
            ],
            "step":1024
        },
        "net.core.netdev_budget":{
            "desc":"Number of network packets processed in each software interrupt",
            "dtype":"int",
            "range":[
                100,
                1000
            ],
            "step":100
        },
        "kernel.dmesg_restrict":{
            "desc":"Which users are restricted from viewing syslogs? 0: no restriction 1: Only privileged users can view the information.",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "0",
                "1"
            ]
        },
        "net.ipv4.tcp_ecn":{
            "desc":"Indicates whether to enable the direct TCP congestion notification function. 0: disabled; 1: enabled",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "0",
                "1",
                "2"
            ]
        },
        "net.ipv4.tcp_no_metrics_save":{
            "desc":"After a TCP connection is closed, the saved parameters can be used to initialize the connection when the same connection is created next time as long as dst_entry is valid.",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "0",
                "1"
            ]
        },
        "net.ipv4.route.max_size":{
            "desc":"Maximum number of entries in the routing cache. If the number of entries in the routing cache exceeds the maximum, the old entries are cleared.",
            "dtype":"int",
            "range":[
                67108864,
                2080374784
            ],
            "step":67108864
        },
        "net.ipv4.tcp_fastopen":{
            "desc":"Whether to enable the TCP quick open mode is to avoid the three-way handshake of hot requests. This greatly improves the performance in the scenario where small objects are moved.",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "1",
                "2",
                "4"
            ]
        },
        "net.ipv4.tcp_window_scaling":{
            "desc":"The window scaling defined in RFC 1323 is enabled. To support a TCP window larger than 64 KB, this parameter must be set to 1. The maximum size of a TCP window is 1 GB. This parameter takes effect only when both parties of a TCP connection are enabled.",
            "dtype":"string",
            "needrestart":"false",
            "options":[
                "0",
                "1"
            ]
        },
        "vm.overcommit_ratio":{
            "desc":"When overcommit_memory is set to 2, the percentage of physical RAM that is considered is set.",
            "dtype":"int",
            "range":[
                0,
                100
            ],
            "step":10
        },
        "net.core.wmem_default":{
            "desc":"Default TCP send window size (bytes)",
            "dtype":"int",
            "range":[
                8192,
                1048576
            ],
            "step":8192
        }
    }
}

与[转帖]KeenTune调优实践之Nginx调优相似的内容:

[转帖]KeenTune调优实践之Nginx调优

https://openanolis.cn/sig/KeenTune/doc/423701433171625827 环境准备 准备两台虚拟机,VM A安装wrk,keentuned,keentune-brain,keentune-bench,作为测试机;VM B安装Nginx,作为Nginx服务端,

[转帖]AnolisOS 入门八:使用keentune(轻豚) 调优系统性能

Anolis 龙蜥社区https://openanolis.cn/轻豚主站http://keentune.io/home 注意:使用keentune 前您应当对服务器硬件、Linux核心参数、网络通讯等知识领域有所了解 警告: 错误的使用调试工具,可能会使服务中断,请谨慎使用! 简介: 性能调优,是

[转帖]龙蜥社区成立智能调优 SIG,物理机、虚拟机、容器的多个典型应用调优效果平均在 10% 以上

https://baijiahao.baidu.com/s?id=1715482603122115961&wfr=spider&for=pc 关注 KeenTune SIG 是一款 AI 算法与专家知识库双轮驱动的操作系统全栈式智能优化产品,为主流的操作系统提供轻量化、跨平台的一键式性能调优,让应用

[转帖]KeenTune(轻豚)

Dependencies 安装python3运行环境 yum install python36 python36-devel pip3 install --upgrade pip 安装python依赖包 pip3 install hyperopt==0.2.5 pip3 install numpy=

[转帖]

Linux ubuntu20.04 网络配置(图文教程) 因为我是刚装好的最小系统,所以很多东西都没有,在开始配置之前需要做下准备 环境准备 系统:ubuntu20.04网卡:双网卡 网卡一:供连接互联网使用网卡二:供连接内网使用(看情况,如果一张网卡足够,没必要做第二张网卡) 工具: net-to

[转帖]

https://cloud.tencent.com/developer/article/2168105?areaSource=104001.13&traceId=zcVNsKTUApF9rNJSkcCbB 前言 Redis作为高性能的内存数据库,在大数据量的情况下也会遇到性能瓶颈,日常开发中只有时刻

[转帖]ISV 、OSV、 SIG 概念

ISV 、OSV、 SIG 概念 2022-10-14 12:29530原创大杂烩 本文链接:https://www.cndba.cn/dave/article/108699 1. ISV: Independent Software Vendors “独立软件开发商”,特指专门从事软件的开发、生产、

[转帖]Redis 7 参数 修改 说明

2022-06-16 14:491800原创Redis 本文链接:https://www.cndba.cn/dave/article/108066 在之前的博客我们介绍了Redis 7 的安装和配置,如下: Linux 7.8 平台 Redis 7 安装并配置开机自启动 操作手册https://ww

[转帖]HTTPS中间人攻击原理

https://www.zhihu.com/people/bei-ji-85/posts 背景 前一段时间,公司北京地区上线了一个HTTPS防火墙,用来监听HTTPS流量。防火墙上线之前,邮件通知给管理层,我从我老大那里听说这个事情的时候,说这个有风险,然后意外地发现,很多人原来都不知道HTTPS防

[转帖]关于字节序(大小端)的一点想法

https://www.zhihu.com/people/bei-ji-85/posts 今天在一个技术群里有人问起来了,当时有一些讨论(不完全都是我个人的观点),整理一下: 为什么网络字节序(多数情况下)是大端? 早年设备的缓存很小,先接收高字节能快速的判断报文信息:包长度(需要准备多大缓存)、地