流量不大,卻Connection time 過久,造成網頁出不來

一台Ubuntu 12.04 x64 裝 Apache,
是VM 主機,2 Core,4GB Ram,
連網頁時,會發生 Connection time 過久到 timeout,
這台是用worker module,
修正參數如下
<IfModule mpm_worker_module>
StartServers 2
ServerLimit 200
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxClients 5000
MaxRequestsPerChild 0
</IfModule>

明顯的改善到10~20ms

後記
發現Log會有 kernel: nf_conntrack: table full, dropping packet 的情形,
(因為此機器,有使用iptables,如果不用,不會有這問題)
而造成在某一時段時,會突然Connection time拉到1~4秒,連線變很慢,
解決table full 的方法很多
廠商給的建議是調整以下兩參數,
ip_conntrack_max
ip_conntrack_tcp_timeout_established
在Ubuntu 12.04 下是在
/proc/sys/net/ipv4/netfilter
ip_conntrack_max = 655350
ip_conntrack_tcp_timeout_established = 10800

可寫到 sysctl.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.netfilter.ip_conntrack_max = 655350
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 10800
記得要 sysctl -p

又或寫到 rc.local
echo 655350 > /proc/sys/net/ipv4/netfilter/ip_conntrack_max
echo 10800 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established

心得
機器上線前,要考量可能的連線數,
不只調整Apache,也記得要調整核心參數,
尤其是有用iptables時


參考
http://blog.yorkgu.me/2012/02/09/kernel-nf_conntrack-table-full-dropping-packet/
http://storysky.blog.51cto.com/628458/243835

後後記...
攻擊無所不在,
kern.log會出現一堆 unexpectedly shrunk window的訊息,
再安裝fail2ban,來防止此問題

我們需要增加檔案 /etc/fail2ban/filter.d/shrunk-window.conf
內容如下
[Definition]
failregex = TCP\: Peer <HOST>\:.* unexpectedly shrunk window.*repaired+
ignoreregex =
修改 /etc/fail2ban/jail.conf 加入
[shrunk-window]
enabled  = true
filter = shrunk-window
logpath = /var/log/kern.log
port = all
banaction= iptables-allports
port = anyport
maxretry = 1
可以利用 fail2ban-regex 指令檢查shrunk-window.conf內的failregex有無問題!
http://linux.die.net/man/1/fail2ban-regex
# fail2ban-regex /var/log/kern.log /etc/fail2ban/filter.d/shrunk-window.conf
如果列出了那些警告訊息,那就表示成功了!
此引用參考
http://petonline.dyndns.org/node/114

後後後記.............
在高峰時段,流量雖沒衝到100M以上,但Connection time就是會超過一秒,
因為流量小,不代表連線數少,
大約會有四五仟左右,不含time wait,syn_recv等....
因此再修正
<IfModule mpm_worker_module>
   StartServers 2
   ServerLimit 200
   MinSpareThreads 25
   MaxSpareThreads 75
   ThreadsPerChild 50
   MaxClients 10000
   MaxRequestsPerChild 0
</IfModule>
再度明顯改善.......
要時時觀察 Connection 數,來做調整....

~~


沒有留言:

張貼留言