在CentOS7修改由systemd控制的service開檔數目限制
今天在解決C10K問題的時候
發現一般Linux可以在sysctl控制開檔數目
但是在CentOS7一直被限制在1024個而已
不管任何修改方式都一樣
$ ulimit -n 1000000
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7272
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1000000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7272
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
可以看到已經修改過開檔數目
但是實際測試
還是只能接受1024個connection
錯誤都是
dial tcp 127.0.0.1:443: socket: too many open files
用lsof查看開檔狀況
lsof -a -p $(pidof proccessName) ' wc -l
數字都卡在102x
最後終於找到
在 CentOS 7 中用 systemd 控制的service
都會有自己的 limit config
所以我剛剛改的只是登入的 User 可開檔的數目
實際上要改的地方是systemd的service file
$ vim /etc/systemd/system/xxx.service
在 [Service] 區塊加入
[Service]
Type=simple
ExecStart=xxx
Restart=always
LimitCORE=infinity
LimitNOFILE=100000
LimitNPROC=100000
修改完要reload service
$ systemctl daemon-reload
重新啟動service之後
終於可以一次服務十萬人啦!!!!
如果要修改全域的設定
設定檔則在 /etc/systemd/system.conf 和 /etc/systemd/user.conf
以下列出這次搜尋找到的解決C10K的所有解法:
$ vim /etc/sysctl.conf
vm.overcommit_memory = 1
fs.nr_open = 10000000
fs.file-max = 10000000
net.nf_conntrack_max = 999999
net.ipv4.ip_local_port_range = 18000 65535
net.netfilter.nf_conntrack_tcp_timeout_established=600
net.ipv4.tcp_wmem = 4096 87380 4161536
net.ipv4.tcp_rmem = 4096 87380 4161536
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_tw_recycle = 1
$ sysctl -p
Ref:
http://smilejay.com/2016/06/centos-7-systemd-conf-limits/
http://www.chengweiyang.cn/2015/11/14/how-to-enlarge-linux-open-files-upper-cell/
http://colobu.com/2015/05/22/implement-C1000K-servers-by-spray-netty-undertow-and-node-js/
https://www.cyberciti.biz/tips/linux-increase-outgoing-network-sockets-range.html
https://fredrikaverpil.github.io/2016/04/27/systemd-and-resource-limits/
留言