用apache支持的一台Django服务器,早上内存告警了
系统是centOS7
# uname -r
3.10.0-229.4.2.el7.x86_64
# free -m
total used free shared buff/cache available
Mem: 7823 7172 270 21 380 382
Swap: 8191 5151 3040
看起来比较严重啊,开始以为是服务过多导致的内存不足
#netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:9312 0.0.0.0:* LISTEN 32641/searchd
tcp 0 0 0.0.0.0:9313 0.0.0.0:* LISTEN 419/searchd
tcp 0 0 0.0.0.0:9314 0.0.0.0:* LISTEN 3241/searchd
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 20544/zabbix_agentd
tcp 0 0 0.0.0.0:9315 0.0.0.0:* LISTEN 4258/searchd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 3147/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 649/sshd
tcp 0 0 0.0.0.0:36093 0.0.0.0:* LISTEN -
tcp6 0 0 :::9090 :::* LISTEN 26777/java
tcp6 0 0 :::9000 :::* LISTEN 26777/java
tcp6 0 0 :::3306 :::* LISTEN 30417/mysqld
tcp6 0 0 :::9900 :::* LISTEN 9412/java
tcp6 0 0 :::9999 :::* LISTEN 11693/java
tcp6 0 0 :::111 :::* LISTEN 3147/rpcbind
tcp6 0 0 :::80 :::* LISTEN 20724/httpd
tcp6 0 0 :::20880 :::* LISTEN 26777/java
tcp6 0 0 :::35987 :::* LISTEN -
tcp6 0 0 :::22 :::* LISTEN 649/sshd
tcp6 0 0 :::8888 :::* LISTEN 14830/java
udp 0 0 0.0.0.0:68 0.0.0.0:* 509/dhclient
udp 0 0 0.0.0.0:111 0.0.0.0:* 3147/rpcbind
udp 0 0 0.0.0.0:778 0.0.0.0:* 3147/rpcbind
udp 0 0 0.0.0.0:33971 0.0.0.0:* 474/avahi-daemon: r
udp 0 0 0.0.0.0:5353 0.0.0.0:* 474/avahi-daemon: r
udp 0 0 0.0.0.0:63160 0.0.0.0:* 509/dhclient
udp6 0 0 :::65520 :::* 509/dhclient
udp6 0 0 :::111 :::* 3147/rpcbind
udp6 0 0 :::778 :::* 3147/rpcbind
停掉一些,还是不怎么见效
# free -m
total used free shared buff/cache available
Mem: 7823 7173 263 21 386 381
Swap: 8191 4967 3224
用top然后shift+m
#top
top - 10:13:18 up 485 days, 14:58, 1 user, load average: 0.00, 0.03, 0.05
Tasks: 125 total, 1 running, 124 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8011292 total, 269832 free, 7345488 used, 395972 buff/cache
KiB Swap: 8388604 total, 3301772 free, 5086832 used. 390616 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
20726 apache 20 0 4906480 2.262g 1756 S 0.0 29.6 82:49.22 httpd
20724 apache 20 0 5358792 1.256g 1828 S 0.0 16.4 196:22.30 httpd
21311 apache 20 0 4986316 871292 2604 S 0.0 10.9 320:27.95 httpd
11693 root 20 0 6121620 858384 6092 S 0.0 10.7 131:00.39 java
9412 root 20 0 5757844 753664 5180 S 0.7 9.4 5:36.23 java
20725 apache 20 0 3233060 593200 2152 S 0.0 7.4 75:40.15 httpd
14830 root 20 0 5738796 170916 156 S 0.0 2.1 125:50.75 java
26777 root 20 0 3800372 126436 3892 S 0.0 1.6 53:55.56 java
是apache占用的内存太多导致
编辑apache配置文件,调整了以下配置文件进程数量及几个参数的修改优化
优化1
# cat httpd-mpm.conf | grep -v "#" | grep -v "^$"
<IfModule !mpm_netware_module>
PidFile "logs/httpd.pid"
</IfModule>
<IfModule mpm_prefork_module>
StartServers 2
MinSpareServers 2
MaxSpareServers 5
MaxRequestWorkers 150
MaxConnectionsPerChild 10
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 55
MaxSpareThreads 150
ThreadsPerChild 20
MaxRequestWorkers 200
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_event_module>
StartServers 3
MinSpareThreads 75
MaxSpareThreads 250
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_netware_module>
ThreadStackSize 65536
StartThreads 250
MinSpareThreads 25
MaxSpareThreads 250
MaxThreads 1000
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_mpmt_os2_module>
StartServers 2
MinSpareThreads 5
MaxSpareThreads 10
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_winnt_module>
ThreadsPerChild 150
MaxConnectionsPerChild 0
</IfModule>
<IfModule !mpm_netware_module>
MaxMemFree 2048
</IfModule>
<IfModule mpm_netware_module>
MaxMemFree 100
</IfModule>
优化2
# cat httpd-default.conf | grep -v "#" | grep -v "^$"
Timeout 30
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
UseCanonicalName Off
AccessFileName .htaccess
ServerTokens Full
ServerSignature Off
HostnameLookups Off
<IfModule reqtimeout_module>
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
</IfModule>
然后# /usr/local/apache2/bin/apachectl graceful
效果显著
# free -m
total used free shared buff/cache available
Mem: 7823 2104 5304 21 414 5450
Swap: 8191 1352 6839