開啟reuse_port讓Tengine性能提升3倍

jopen 9年前發布 | 51K 次閱讀 Tengine Web服務器

為什么 reuse_port

  • 記得2008年做性能測試的時候,新進7臺lenovo4核4G 服務器用于性能測試。

    </li>

  • 當時資源緊張,這7臺服務器都裝了雙系統(Win2003/CentOS5)空閑時用于做肉雞(壓測的Agent)。

    </li>

  • 當時給Nginx做了一系列測試,印象很深的是:在這批機器上,Nginx狀態頁面的壓測。

    短連接的話最佳QPS約4萬,長連接的話最高QPS約13萬。

    </blockquote> </li>

  • 大概3年后,那批lenovo服務器已經沒人瞧得上了,只能做肉雞。

    </li>

  • 然而,一次不經意的測試,發現再牛的服務器,短連接最佳QPS也高不了多少。
  • 而且,肉雞的資源沒用完,被測試服務器的資源也用不完,網絡也沒瓶頸。

    服務器資源使用率很低,然而響應就是不夠快。

    </blockquote> </li>

  • 最后,我們發現了 SO_REUSEPORT。對,瓶頸在這(監聽的入口)!
  • </ul>

    引入 LVS fullnat

    • 當時用的 CentOS 5、6,內核版本都還在 2.6。
    • 嘗試過新內核,遇到一些奇怪的現象無力維護,最終放棄。
    • 也可以用Nginx監控多個IP來突破瓶頸(多虛擬幾個IP),維護成本略高。
    • 也可以用LVS的NAT方式來做轉發,然而NAT對網絡的要求你懂的。
    • 最終我們決定使用 淘寶LVS,依賴 淘寶內核
    • 然后使用 fullnat ,將對外端口映射到每個節點的多個端口(而非多IP)。
    • 曾在線上做過極端測試,效果非常顯著。然后我們就上線了!

      原本10多臺服務器的壓力,全壓到1臺服務器的10多個端口上。居然抗住了

      </blockquote> </li> </ul>

      壓測 reuse_port

      • 如果我用 Debian 最新 Jessie 發行版,默認內核 3.16.0-4。
      • 然而 Tengine 早已支持 reuse_port
      • 開啟reuse_port后,你會發現有很多進程同時監聽80端口:
        開啟reuse_port讓Tengine性能提升3倍
      • 加壓后你會發現,服務器性能可被你榨干:
        開啟reuse_port讓Tengine性能提升3倍
      • 對比一下測試 reuse_port的效果,小伙伴們驚呆了(短連接QPS過了24萬)!
        開啟reuse_port讓Tengine性能提升3倍
      • 真相大白后,你還等什么?
      • </ul>

        探個究竟

        • 測試過程中由于壓大TCP: Possible SYN flooding on port 80.,出大量錯誤 。
        • 于是將并發量降到了6萬net.core.somaxconn = 65535。
        • 再關閉 reuse_port 后,我們看下perf top的情況:
          開啟reuse_port讓Tengine性能提升3倍
        • 然后再打開 reuse_port ,對比perf top的情況:
          開啟reuse_port讓Tengine性能提升3倍
        • 此時再放大 Nginx 監聽的back_log,看下資源使用情況:
          開啟reuse_port讓Tengine性能提升3倍
        • 我們來看看些時的隊列情況(有入隊過萬了):
          開啟reuse_port讓Tengine性能提升3倍
        • 然后我們再來挑戰30萬并發(MTT是平均響應時間(ms)):
          開啟reuse_port讓Tengine性能提升3倍
        • 經過一系列調優,相同環境相同并發量,沒有再出現TCP: Possible SYN flooding on port 80.。但出現了少量連接超時的情況:
        • </ul>

          2015-11-29 01:31:54,779 ERROR Connection timed out
          java.net.ConnectException: Connection timed out
           at HTTPClient.HTTPConnection$EstablishConnection.run(HTTPConnection.java:4082) ~[grinder-httpclient-3.9.1.jar:na]

          測試環境

          Dell PowerEdge M620
          Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz
          Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux
          Ethernet controller: Broadcom Corporation NetXtreme II BCM57810 10 Gigabit Ethernet (rev 10)

          Tengine version: Tengine/2.1.1 (nginx/1.6.2) built by gcc 4.9.2 (Debian 4.9.2-10) TLS SNI support enabled configure arguments: --user=www-data --group=www-data --with-pcre-jit --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --dso-tool-path=/usr/sbin/dso_tool --dso-path=/usr/share/nginx/modules --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-syslog --with-jemalloc --with-mail --with-file-aio --with-mail_ssl_module --with-http_ssl_module --with-google_perftools_module --with-http_auth_request_module --with-http_dav_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_flv_module --with-http_slice_module --with-http_mp4_module --with-http_concat_module --with-http_random_index_module --with-http_secure_link_module --with-http_sysguard_module --with-http_split_clients_module=shared --with-http_referer_module=shared --with-http_uwsgi_module=shared --with-http_scgi_module=shared --with-http_memcached_module=shared --with-http_limit_conn_module=shared --with-http_limit_req_module=shared --with-http_empty_gif_module=shared --with-http_browser_module=shared --with-http_user_agent_module=shared --with-http_upstream_ip_hash_module=shared --with-http_upstream_least_conn_module=shared --with-http_upstream_session_sticky_module=shared --with-http_reqstat_module=shared --with-http_lua_module --with-luajit-inc=/usr/include/luajit-2.0 --with-luajit-lib=/usr/lib/x86_64-linux-gnu --with-cpu-opt=amd64 --with-ld-opt=-Wl,-z,relro --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --add-module=../echo-nginx-module-0.58 --add-module=../headers-more-nginx-module-0.261 --add-module=../redis2-nginx-module-0.12 --add-module=../ngx_cache_purge-2.3 --add-module=../ngx_cache_path_status-1.0 loaded modules: ngx_core_module (static) ngx_errlog_module (static) ngx_conf_module (static) ngx_dso_module (static) ngx_syslog_module (static) ngx_events_module (static) ngx_event_core_module (static) ngx_epoll_module (static) ngx_procs_module (static) ngx_proc_core_module (static) ngx_openssl_module (static) ngx_regex_module (static) ngx_http_module (static) ngx_http_core_module (static) ngx_http_log_module (static) ngx_http_upstream_module (static) ngx_http_spdy_module (static) ngx_http_static_module (static) ngx_http_gzip_static_module (static) ngx_http_dav_module (static) ngx_http_autoindex_module (static) ngx_http_index_module (static) ngx_http_random_index_module (static) ngx_http_concat_module (static) ngx_http_upstream_ip_hash_module (shared, 3.1) ngx_http_upstream_least_conn_module (shared, 3.1) ngx_http_auth_request_module (static) ngx_http_auth_basic_module (static) ngx_http_limit_conn_module (shared, 3.1) ngx_http_access_module (static) ngx_http_limit_req_module (shared, 3.1) ngx_http_realip_module (static) ngx_http_geo_module (static) ngx_http_geoip_module (static) ngx_http_split_clients_module (shared, 3.1) ngx_http_map_module (static) ngx_http_reqstat_module (shared, 3.1) ngx_http_user_agent_module (shared, 3.1) ngx_http_referer_module (shared, 3.1) ngx_http_rewrite_module (static) ngx_http_ssl_module (static) ngx_http_proxy_module (static) ngx_http_uwsgi_module (shared, 3.1) ngx_http_fastcgi_module (static) ngx_http_scgi_module (shared, 3.1) ngx_http_empty_gif_module (shared, 3.1) ngx_http_memcached_module (shared, 3.1) ngx_http_browser_module (shared, 3.1) ngx_http_secure_link_module (static) ngx_http_sysguard_module (static) ngx_http_flv_module (static) ngx_http_slice_module (static) ngx_http_mp4_module (static) ngx_http_upstream_consistent_hash_module (static) ngx_http_upstream_check_module (static) ngx_http_upstream_keepalive_module (static) ngx_http_upstream_dynamic_module (static) ngx_http_stub_status_module (static) ngx_http_redis2_module (static) ngx_http_cache_purge_module (static) ngx_http_slowfs_module (static) ngx_cache_path_status_module (static) ngx_http_write_filter_module (static) ngx_http_header_filter_module (static) ngx_http_chunked_filter_module (static) ngx_http_spdy_filter_module (static) ngx_http_range_header_filter_module (static) ngx_http_gzip_filter_module (static) ngx_http_postpone_filter_module (static) ngx_http_ssi_filter_module (static) ngx_http_charset_filter_module (static) ngx_http_xslt_filter_module (static) ngx_http_image_filter_module (static) ngx_http_sub_filter_module (static) ngx_http_addition_filter_module (static) ngx_http_gunzip_filter_module (static) ngx_http_userid_filter_module (static) ngx_http_footer_filter_module (static) ngx_http_upstream_session_sticky_module (shared, 3.1) ngx_http_trim_filter_module (static) ngx_http_headers_filter_module (static) ngx_http_echo_module (static) ngx_http_headers_more_filter_module (static) ngx_http_lua_module (static) ngx_http_copy_filter_module (static) ngx_http_range_body_filter_module (static) ngx_http_not_modified_filter_module (static) ngx_mail_module (static) ngx_mail_core_module (static) ngx_mail_ssl_module (static) ngx_mail_pop3_module (static) ngx_mail_imap_module (static) ngx_mail_smtp_module (static) ngx_mail_auth_http_module (static) ngx_mail_proxy_module (static) ngx_google_perftools_module (static)</pre>
          Nginx配置如下:

          user www-data;
          worker_processes auto;
          pid /run/nginx.pid;
          events {
           use epoll;
           multi_accept on;
           reuse_port on;
           worker_connections  1048576;
          }
          dso { # 動態加載功能模塊 /usr/share/nginx/modules
           load ngx_http_memcached_module.so;
           load ngx_http_limit_conn_module.so;
           load ngx_http_empty_gif_module.so;
           load ngx_http_scgi_module.so;
           load ngx_http_upstream_session_sticky_module.so;
           load ngx_http_user_agent_module.so;
           load ngx_http_referer_module.so;
           load ngx_http_upstream_least_conn_module.so;
           load ngx_http_uwsgi_module.so;
           load ngx_http_reqstat_module.so;
           load ngx_http_browser_module.so;
           load ngx_http_limit_req_module.so;
           load ngx_http_split_clients_module.so;
           load ngx_http_upstream_ip_hash_module.so;
          }
          http {
           include       /etc/nginx/mime.types;
           default_type  text/plain;
           access_log  off;
           sendfile        on;
           tcp_nopush    on;
           tcp_nodelay    on;
           server_tokens off; 
           keepalive_timeout  120;
           server_names_hash_bucket_size 512;
           server_name_in_redirect off;
           fastcgi_connect_timeout 3s;
           fastcgi_send_timeout 3s;
           fastcgi_read_timeout 3s;
           fastcgi_buffer_size 128k;
           fastcgi_buffers 8 128k;
           fastcgi_busy_buffers_size 256k;
           fastcgi_temp_file_write_size 256k;
           variables_hash_max_size  1024;
           set_real_ip_from 10.0.0.0/8;
           set_real_ip_from 172.28.0.0/16;
           set_real_ip_from 192.168.0.0/16;
           real_ip_header X-Forwarded-For;
           gzip off;
           gzip_disable "msie6";
           gzip_min_length 1k;
           gzip_buffers 16 64k;
           gzip_http_version 1.1;
           gzip_comp_level 6;
           gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
           gzip_vary on;
           ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
           ssl_prefer_server_ciphers on;
           access_log /var/log/nginx/access.log;
           error_log /var/log/nginx/error.log;
           server {
               listen       80 backlog=65535;
               charset utf-8;
               location / { # 打印Tengine狀態頁
                   stub_status on; # 開啟狀態頁,依賴 http_stub_status_module 模塊
                   access_log  off; #訪問過程不記日志
               }
               location ~ ^(.*)\/\.(svn|git|hg|bzr|cvs)\/ { # 屏蔽這些目錄
                   deny all;
                   access_log off;
                   log_not_found off;
               }
               location ~ /\. { # 屏蔽.開頭的目錄或文件,比如  .htaccess .bash_history
                   deny all;
                   access_log off;
                   log_not_found off;
               }
               location /do_not_delete.html {
                   access_log off;
                   empty_gif;
               }
           }
          }

          來自:http://my.oschina.net/anglix/blog/536957

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!