【乾貨】Linux服務器高併發調優實戰

CentOS 安裝好之後的默認配置是不能滿足我們的高併發需求的。

面對高壓併發Linux調優,廢話不多說,直接上乾貨:

Linux系統連接數的優化

1、調整同時打開文件數量
ulimit -n 65535

注:linux 默認值 open files 和 max user processes 爲 1024

三種修改方式:
1. 在/etc/rc.local 中增加一行 ulimit -SHn 65535
2. 在/etc/profile 中增加一行 ulimit -SHn 65535
3. 在/etc/security/limits.conf 最後增加:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
具體使用哪種,在 CentOS 中使用第1 種方式無效果,使用第3 種方式有效果,而在Debian 中使用第2 種有效果


# ulimit -n
65535
# ulimit -u
65535
備註:ulimit 命令本身就有分軟硬設置,加-H 就是硬,加-S 就是軟默認顯示的是軟限制
soft 限制指的是當前系統生效的設置值。 hard 限制值可以被普通用戶降低。但是不能增加。 soft 限制不能設置的比 hard 限制更高。 只有 root 用戶才能夠增加 hard 限制值。


2、TCP最大連接數(somaxconn)
echo 10000 > /proc/sys/net/core/somaxconn


3、TCP連接立即回收、回用(recycle、reuse)
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle


4、不做TCP洪水抵禦
echo 0 > /proc/sys/net/ipv4/tcp_syncookies


一、應對十萬高併發測試配置(僅用於併發測試)

內核優化

vi /etc/sysctl.conf

CentOS5.5中可以將所有內容清空直接替換爲如下內容:


net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
使配置立即生效可使用如下命令:
/sbin/sysctl -p


二、網絡上流傳已久的sysctl.conf配置

#優化TCP

vi /etc/sysctl.conf


#禁用包過濾功能 
net.ipv4.ip_forward = 0  


#啓用源路由覈查功能 
net.ipv4.conf.default.rp_filter = 1  


#禁用所有IP源路由 
net.ipv4.conf.default.accept_source_route = 0  


#使用sysrq組合鍵是瞭解系統目前運行情況,爲安全起見設爲0關閉
kernel.sysrq = 0  


#控制core文件的文件名是否添加pid作爲擴展
kernel.core_uses_pid = 1  


#開啓SYN Cookies,當出現SYN等待隊列溢出時,啓用cookies來處理
net.ipv4.tcp_syncookies = 1  


#每個消息隊列的大小(單位:字節)限制
kernel.msgmnb = 65536  


#整個系統最大消息隊列數量限制
kernel.msgmax = 65536  


#單個共享內存段的大小(單位:字節)限制,計算公式64G*1024*1024*1024(字節)
kernel.shmmax = 68719476736  


#所有內存大小(單位:頁,1頁 = 4Kb),計算公式16G*1024*1024*1024/4KB(頁)
kernel.shmall = 4294967296  


#timewait的數量,默認是180000
net.ipv4.tcp_max_tw_buckets = 6000  


#開啓有選擇的應答
net.ipv4.tcp_sack = 1  


#支持更大的TCP窗口. 如果TCP窗口最大超過65535(64K), 必須設置該數值爲1
net.ipv4.tcp_window_scaling = 1  


#TCP讀buffer
net.ipv4.tcp_rmem = 4096 131072 1048576


#TCP寫buffer
net.ipv4.tcp_wmem = 4096 131072 1048576   


#爲TCP socket預留用於發送緩衝的內存默認值(單位:字節)
net.core.wmem_default = 8388608


#爲TCP socket預留用於發送緩衝的內存最大值(單位:字節)
net.core.wmem_max = 16777216  


#爲TCP socket預留用於接收緩衝的內存默認值(單位:字節)  
net.core.rmem_default = 8388608


#爲TCP socket預留用於接收緩衝的內存最大值(單位:字節)
net.core.rmem_max = 16777216


#每個網絡接口接收數據包的速率比內核處理這些包的速率快時,允許送到隊列的數據包的最大數目
net.core.netdev_max_backlog = 262144  


#web應用中listen函數的backlog默認會給我們內核參數的net.core.somaxconn限制到128,而nginx定義的NGX_LISTEN_BACKLOG默認爲511,所以有必要調整這個值
net.core.somaxconn = 262144  


#系統中最多有多少個TCP套接字不被關聯到任何一個用戶文件句柄上。這個限制僅僅是爲了防止簡單的DoS攻擊,不能過分依靠它或者人爲地減小這個值,更應該增加這個值(如果增加了內存之後)
net.ipv4.tcp_max_orphans = 3276800  


#記錄的那些尚未收到客戶端確認信息的連接請求的最大值。對於有128M內存的系統而言,缺省值是1024,小內存的系統則是128
net.ipv4.tcp_max_syn_backlog = 262144  


#時間戳可以避免序列號的卷繞。一個1Gbps的鏈路肯定會遇到以前用過的序列號。時間戳能夠讓內核接受這種“異常”的數據包。這裏需要將其關掉
net.ipv4.tcp_timestamps = 0  


#爲了打開對端的連接,內核需要發送一個SYN並附帶一個迴應前面一個SYN的ACK。也就是所謂三次握手中的第二次握手。這個設置決定了內核放棄連接之前發送SYN+ACK包的數量
net.ipv4.tcp_synack_retries = 1  


#在內核放棄建立連接之前發送SYN包的數量
net.ipv4.tcp_syn_retries = 1  


#開啓TCP連接中time_wait sockets的快速回收
net.ipv4.tcp_tw_recycle = 1  


#開啓TCP連接複用功能,允許將time_wait sockets重新用於新的TCP連接(主要針對time_wait連接)
net.ipv4.tcp_tw_reuse = 1  


#1st低於此值,TCP沒有內存壓力,2nd進入內存壓力階段,3rdTCP拒絕分配socket(單位:內存頁)
net.ipv4.tcp_mem = 94500000 915000000 927000000   


#如果套接字由本端要求關閉,這個參數決定了它保持在FIN-WAIT-2狀態的時間。對端可以出錯並永遠不關閉連接,甚至意外當機。缺省值是60 秒。2.2 內核的通常值是180秒,你可以按這個設置,但要記住的是,即使你的機器是一個輕載的WEB服務器,也有因爲大量的死套接字而內存溢出的風險,FIN- WAIT-2的危險性比FIN-WAIT-1要小,因爲它最多隻能吃掉1.5K內存,但是它們的生存期長些。
net.ipv4.tcp_fin_timeout = 15  


#表示當keepalive起用的時候,TCP發送keepalive消息的頻度(單位:秒)
net.ipv4.tcp_keepalive_time = 30  


#對外連接端口範圍
net.ipv4.ip_local_port_range = 2048 65000


#表示文件句柄的最大數量
fs.file-max = 102400

使配置立即生效可使用如下命令:
/sbin/sysctl -p


三、實際生產系統自動化部署中用的配置

# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.


# Controls IP packet forwarding
net.ipv4.ip_forward = 0


# Controls source route verification
net.ipv4.conf.default.rp_filter = 1


# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0


# Controls the System Request debugging functionality of the kernel


# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1


# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1


# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0


# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536


# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536


# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736


# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.netfilter.nf_conntrack_max = 1000000
kernel.unknown_nmi_panic = 0
kernel.sysrq = 0
fs.file-max = 1000000
vm.swappiness = 10
fs.inotify.max_user_watches = 10000000
net.core.wmem_max = 327679
net.core.rmem_max = 327679
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0

使配置立即生效可使用如下命令:
/sbin/sysctl -p

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章