{"id":208687,"date":"2021-01-09T10:29:16","date_gmt":"2021-01-09T02:29:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=208687"},"modified":"2021-01-03T14:31:11","modified_gmt":"2021-01-03T06:31:11","slug":"ulimits-pit-filling-record","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ulimits-pit-filling-record.html","title":{"rendered":"Ulimit\u7684\u586b\u5751\u8bb0\u5f55"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6700\u8fd1\u9047\u5230\u4e00\u4e2a\u975e\u5e38\u6709\u8da3\u7684\u95ee\u9898\u3002\u5176\u4e2d\u6709\u4e00\u7ec4HAProxy\uff0c\u9891\u7e41\u51fa\u73b0\u95ee\u9898\u3002\u767b\u5f55\u4e0a\u670d\u52a1\u5668\uff0ccpu\u3001\u5185\u5b58\u3001\u7f51\u7edc\u3001io\u4e00\u987f\u731b\u67e5\u3002\u6700\u7ec8\u53d1\u73b0\uff0c\u673a\u5668\u4e0a\u5904\u4e8eTIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\uff0c\u591a\u8fbe6\u4e07\u591a\u4e2a\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6700\u8fd1\u9047\u5230\u4e00\u4e2a\u975e\u5e38\u6709\u8da3\u7684\u95ee\u9898\u3002\u5176\u4e2d\u6709\u4e00\u7ec4HAProxy\uff0c\u9891\u7e41\u51fa\u73b0\u95ee\u9898\u3002\u767b\u5f55\u4e0a\u670d\u52a1\u5668\uff0ccpu\u3001\u5185\u5b58\u3001\u7f51\u7edc\u3001io\u4e00\u987f\u731b\u67e5\u3002\u6700\u7ec8\u53d1\u73b0\uff0c\u673a\u5668\u4e0a\u5904\u4e8eTIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\uff0c\u591a\u8fbe6\u4e07\u591a\u4e2a\u3002<\/p>\n

\"\"<\/p>\n

TIME_WAIT\u72b6\u6001\uff0c\u4e00\u822c\u90fd\u4f1a\u51fa\u73b0\u5728HAProxy\u3001Nginx\u8fd9\u79cd\u4ee3\u7406\u673a\u5668\u4e0a\uff0c\u4e3b\u8981\u662f\u7531\u4e8e\u9891\u7e41\u7684\u4e3b\u52a8\u5173\u95ed\u6240\u9020\u6210\u7684\u3002\u901a\u8fc7\u4fee\u6539reuse\u548c\u56de\u6536\u53c2\u6570\uff0c\u53ef\u4ee5\u6bd4\u8f83\u5feb\u901f\u7684\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n

\u7f51\u7edc\u72b6\u6001\u7684\u7edf\u8ba1\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n

netstat -ant|awk '\/^tcp\/ {++S[$NF]} END {for(a in S) print (a,S[a])}' \r\nESTABLISHED 70 \r\nFIN_WAIT2 30 \r\nCLOSING 33 \r\nTIME_WAIT 65520 \r\n<\/pre>\n

\u8fd9\u672c\u6765\u6ca1\u4ec0\u4e48\u795e\u5947\u7684\uff0c\u4f4665535\u8fd9\u4e2a\u6570\u5b57\uff0c\u5b9e\u5728\u662f\u592a\u8fc7\u4e8e\u654f\u611f\u3002\u5e94\u8be5\u662f\u89e6\u53d1\u4e86\u67d0\u79cd\u4e0a\u9650\u3002<\/p>\n

\"\"<\/p>\n

\u4f7f\u6211\u4eec\u66f4\u52a0\u611f\u5230\u7591\u60d1\u7684\u662f\uff1a\u4e3a\u4ec0\u4e48TIME_WAIT\u72b6\u6001\u7684\u8fde\u63a5\uff0c\u4ec5\u4ec5\u8fbe\u5230\u4e8665535\uff0c\u670d\u52a1\u5c31\u4e0d\u53ef\u7528\u4e86?<\/p>\n

\u5230\u5904\u53f7\u79f0\u7684\u5355\u673a\u767e\u4e07\u8fde\u63a5\uff0c\u662f\u5728\u5439\u725b\u76ae\u4e48?\u600e\u4e48\u8fd9\u4e48\u7ecf\u4e0d\u8d77\u6298\u817e?<\/p>\n

65535\uff0c\u8868\u793a\u7b49\u4e8e2\u768416\u6b21\u65b9\u51cf\u4e00\uff0c\u662f\u4e00\u4e2a\u795e\u5947\u7684\u6570\u5b57\u3002\u5148\u628a\u8fd9\u5c0f\u6570\u5b57\u6254\u5728\u4e00\u8fb9\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0bLinux\u5230\u5e95\u80fd\u652f\u6301\u591a\u5c11\u4e2a\u8fde\u63a5\u3002<\/p>\n

1. Linux\u80fd\u591f\u652f\u6301\u591a\u5c11\u8fde\u63a5?<\/strong><\/div>\n

\u7b54\u6848\u662f\u65e0\u6570\u4e2a\u3002\u53ef\u662f\u7aef\u53e3\u53ea\u670965535\u4e2a\u554a\u3002<\/p>\n

\u4e3a\u4ec0\u4e48\u7aef\u53e3\u53ea\u670965535\u4e2a?<\/p>\n

\u8fd9\u662f\u4e00\u4e2a\u5386\u53f2\u539f\u56e0\uff0c\u56e0\u4e3a\u5728TCP\u3001UDP\u534f\u8bae\u7684\u5f00\u5934\uff0c\u4f1a\u5206\u522b\u670916\u4f4d\u6765\u5b58\u50a8\u6e90\u7aef\u53e3\u53f7\u548c\u76ee\u6807\u7aef\u53e3\u53f7\u3002\u5f88\u9057\u61be\u7684\u662f\uff0c\u8fd9\u4e2a\u503c\u662fshort\u7c7b\u578b\u7684\uff0c\u5927\u5c0f\u4e5f\u662f2^16-1\u3002<\/p>\n

\u56e0\u4e3a\u5386\u53f2\u539f\u56e0\u9020\u6210\u7684\u4e0d\u53ef\u6539\u53d8\u7684\u6807\u51c6\uff0c\u5c31\u662f\u90a3\u4e48\u6839\u6df1\u8482\u56fa\u3002<\/p>\n

\u90a3Linux\u5230\u5e95\u80fd\u652f\u6301\u591a\u5c11\u4e2a\u8fde\u63a5\u5462?\u7b54\u6848\u662f\u65e0\u6570\u4e2a\u3002<\/p>\n

\u62ffnginx\u6765\u8bf4\uff0c\u6211\u4eec\u628a\u5b83\u76d1\u542c\u572880\u7aef\u53e3\u4e0a\u3002\u8fd9\u65f6\u5019A\u673a\u5668\u53bb\u8fde\u63a5Nginx\uff0c\u53ef\u4ee5\u53d1\u8d77\u591a\u8fbe6w\u591a\u6761\u957f\u8fde\u63a5\u3002\u5982\u679cB\u673a\u5668\u53bb\u8fde\u63a5Nginx\uff0c\u540c\u6837\u4e5f\u53ef\u4ee5\u53d1\u8d776w\u591a\u6761\u8fde\u63a5\u3002\u8fd9\u662f\u7531\u4e8e\u786e\u5b9a\u4e00\u6761\u8fde\u63a5\uff0c\u662f\u7531src\u548cdst\u6765\u5171\u540c\u51b3\u5b9a\u7684\u3002<\/p>\n

\u8ba4\u4e3aLinux\u53ea\u80fd\u63a5\u53d765535\u6761\u8fde\u63a5\u7684\u60f3\u6cd5\uff0c\u53ea\u80fd\u8bf4\u662f\u72af\u4e86\u975e\u5e38\u6d45\u663e\u7684\u60f3\u5f53\u7136\u4e3b\u4e49\u3002<\/p>\n

65535\u4e2a\u7aef\u53e3\uff0c\u4f5c\u4e3a\u538b\u6d4b\u673a\u53ef\u80fd\u5bf9\u4f60\u6765\u8bf4\u592a\u5c0f\u4e86\u4e00\u4e9b\u3002\u4f46\u5bf9\u4e8e\u670d\u52a1\u5668\u6765\u8bf4\uff0c\u5df2\u7ecf\u7ef0\u7ef0\u6709\u4f59\u4e86\u3002<\/p>\n

2. \u5982\u4f55\u652f\u6301\u767e\u4e07\u8fde\u63a5?<\/strong><\/div>\n

\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u5230\uff0c\u8fde\u63a5\u6570\uff0c\u662f\u6ca1\u6709\u9650\u5236\u7684\u3002\u4f46Linux\u8fd8\u6709\u4e00\u5c42\u9632\u62a4\uff0c\u90a3\u5c31\u662f\u6587\u4ef6\u53e5\u67c4\u6570\u3002\u901a\u8fc7lsof\u547d\u4ee4\u67e5\u770b\u5230\u7684\u90a3\u4e9b\u4e1c\u897f\uff0c\u5c31\u662f\u6240\u8c13\u7684\u6587\u4ef6\u53e5\u67c4\u3002<\/p>\n

\u5148\u6765\u770b\u4e00\u4e0b\u51e0\u4e2a\u547d\u4ee4\u7684\u5c55\u793a\u3002<\/p>\n

ulmit\uff0c\u5c55\u793a\u4e86\u6bcf\u4e2a\u8fdb\u7a0b\u6240\u80fd\u5360\u7528\u7684\u6587\u4ef6\u53e5\u67c4\u6570\u91cf\u3002<\/p>\n

ulimit -n \r\n65535 \r\n<\/pre>\n

file-max\uff0c\u5c55\u793a\u4e86\u64cd\u4f5c\u7cfb\u7edf\u80fd\u591f\u5360\u7528\u7684\u6587\u4ef6\u53e5\u67c4\u6570\u91cf\u603b\u548c\uff0c\u9488\u5bf9\u7684\u662f\u6240\u6709\u7684\u8fdb\u7a0b\u3002<\/p>\n

cat \/proc\/sys\/fs\/file-max \r\n766722 \r\n<\/pre>\n

file-nr\uff0c\u5c55\u793a\u4e86\u5f53\u524d\u5df2\u7ecf\u4f7f\u7528\u7684\u53e5\u67c4\u6570\u91cf\u548c\u603b\u7684\u53e5\u67c4\u6570\u91cf\u3002\u53ef\u4ee5\u62ff\u6765\u505a\u76d1\u63a7\u3002<\/p>\n

cat \/proc\/sys\/fs\/file-nr \r\n1824  0 766722 \r\n<\/pre>\n

\u8981\u652f\u6301\u767e\u4e07\u8fde\u63a5\uff0c\u65e2\u8981\u653e\u5f00\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u53e5\u67c4\uff0c\u4e5f\u8981\u653e\u5f00\u8fdb\u7a0b\u7ea7\u522b\u7684\u53e5\u67c4\u3002\u4e5f\u5c31\u662f\u8bf4\uff0culimit\u548cfile-max\u7684\u663e\u793a\uff0c\u90fd\u8981\u5927\u4e8e\u767e\u4e07\u624d\u6210\u3002<\/p>\n

3. \u5982\u4f55\u8bbe\u7f6e?<\/strong><\/div>\n

\u8bbe\u7f6e\u8fdb\u7a0b\u7684\u53e5\u67c4\u4e2a\u6570\uff0c\u5e38\u7528\u7684\u65b9\u5f0f\u5c31\u6709ulimit\uff0c\u4f46\u662f\u975e\u5e38\u975e\u5e38\u4e0d\u63a8\u8350\u3002\u539f\u56e0\u65e0\u4ed6\uff0c\u53ea\u6709\u5728\u540c\u4e00\u4e2ashell\u4e2d\u542f\u52a8\u7684\u8fdb\u7a0b\uff0culimit\u7684\u8bbe\u7f6e\u624d\u4f1a\u751f\u6548\u3002\u4f60\u6253\u5f00\u53e6\u5916\u4e00\u4e2ashell\uff0c\u6216\u8005\u91cd\u542f\u673a\u5668\uff0culimit\u7684\u6539\u52a8\u90fd\u4f1a\u4e22\u5931\u3002\u5c31\u662f\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\uff1a<\/p>\n

ulimit -n 1000000 \r\n<\/pre>\n

\u6b63\u786e\u7684\u65b9\u5f0f\uff0c\u662f\u4fee\u6539\/etc\/security\/limits.conf\u6587\u4ef6\u3002\u6bd4\u5982\u4e0b\u9762\u7684\u5185\u5bb9\u3002<\/p>\n

root soft nofile 1000000 \r\nroot hard nofile 1000000 \r\n* soft nofile 1000000 \r\n* hard nofile 1000000 \r\n<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u4e8e\u7279\u5b9a\u7684\u7528\u6237\uff0c\u4fee\u6539\u5176\u53e5\u67c4\u6570\u91cf\u3002\u8fd9\u5728\u5b89\u88c5es\u7b49\u5e94\u7528\u65f6\uff0c\u7ecf\u5e38\u78b0\u5230\u3002<\/p>\n

es  -  nofile  65535 \r\n<\/pre>\n

\u4f46\u5373\u4f7f\u662f\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4e5f\u8981\u6c42\u4f60\u9700\u8981\u6253\u5f00\u4e00\u4e2a\u65b0\u7684shell\u8fdb\u884c\u64cd\u4f5c\u3002\u5728\u5f53\u524d\u4fee\u6539\u7684shell\u91cc\u6216\u8005\u4fee\u6539\u4e4b\u524d\u7684shell\u91cc\uff0c\u540c\u6837\u4e0d\u751f\u6548\u3002xjjdog\u5c31\u66fe\u9047\u5230\u8fc7\u591a\u8d77\u8fd9\u6837\u660e\u660e\u653e\u5f00\u4e86\u9650\u5236\uff0c\u4f46\u8fd8\u662f\u53d1\u751f\u95ee\u9898\u7684\u6848\u4f8b\u3002<\/p>\n

\u8981\u770b\u5230\u8fd9\u4e9b\u6539\u53d8\u662f\u5426\u5df2\u7ecf\u5bf9\u8fdb\u7a0b\u751f\u6548\uff0c\u53ef\u4ee5\u67e5\u770b\u8fdb\u7a0b\u7684\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u3002\u6bd4\u5982cat \/proc\/180323\/limits\uff0c\u5176\u4e2d\u4f1a\u6709\u8be6\u7ec6\u7684\u5c55\u793a\u3002<\/p>\n

\u8fd9\u4e2a\u6570\u503c\uff0c\u4e5f\u5e76\u4e0d\u662f\u60f3\u8981\u8bbe\u591a\u5927\u5c31\u591a\u5927\u7684\u3002\u5b83\u7684\u5927\u5c0f\u4e0a\u9650\uff0c\u662f\u7531nr_open\u51b3\u5b9a\u7684\u3002\u60f3\u8981\u66f4\u5927\uff0c\u5c31\u8981\u4fee\u6539\/ect\/sysct.conf \u4e2dfs.nr_open\u7684\u503c\u3002<\/p>\n

cat \/proc\/sys\/fs\/nr_open \r\n1048576 \r\n<\/pre>\n

\u90a3file-max\u53c8\u8be5\u5982\u4f55\u4fee\u6539\u5462?\u5efa\u8bae\u4fee\u6539\/etc\/sysctl.conf\u6587\u4ef6\uff0c\u52a0\u5165\u4e0b\u9762\u5185\u5bb9\u3002\u8db3\u8db3\u67096\u767e\u591a\u4e07!<\/p>\n

fs.file-max = 6553560 \r\n<\/pre>\n

\u5f53\u6587\u4ef6\u6570\u91cf\u8d85\u51fa\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u62a5kernel: VFS: file-max limit 65535 reached\u7684\u9519\u8bef\u3002<\/p>\n

\u603b\u7ed3\u4e00\u4e0b\u3002<\/p>\n

\"\"<\/p>\n

Linux\u5373\u4f7f\u653e\u5f00\u4e00\u4e2a\u7aef\u53e3\uff0c\u80fd\u591f\u63a5\u53d7\u7684\u8fde\u63a5\u4e5f\u662f\u6d77\u91cf\u7684\u3002\u8fd9\u4e9b\u8fde\u63a5\u7684\u4e0a\u9650\uff0c\u53d7\u5230\u5355\u8fdb\u7a0b\u6587\u4ef6\u53e5\u67c4\u6570\u91cf\u548c\u64cd\u4f5c\u7cfb\u7edf\u6587\u4ef6\u53e5\u67c4\u6570\u91cf\u7684\u9650\u5236\uff0c\u4e5f\u5c31\u662fulimit\u548cfile-max\u3002<\/p>\n

\u4e3a\u4e86\u80fd\u591f\u5c06\u53c2\u6570\u4fee\u6539\u6301\u4e45\u5316\uff0c\u6211\u4eec\u503e\u5411\u4e8e\u5c06\u6539\u52a8\u5199\u5165\u5230\u6587\u4ef6\u91cc\u3002\u8fdb\u7a0b\u7684\u6587\u4ef6\u53e5\u67c4\u9650\u5236\uff0c\u53ef\u4ee5\u653e\u5728\/etc\/security\/limits.conf\u4e2d\uff0c\u5b83\u7684\u4e0a\u9650\u53d7\u5230fs.nr_open\u7684\u5236\u7ea6;\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u53e5\u67c4\u9650\u5236\uff0c\u53ef\u4ee5\u653e\u5230\/etc\/sysctl.conf\u6587\u4ef6\u4e2d\u3002\u6700\u540e\uff0c\u522b\u5fd8\u4e86\u5728\/proc\/$id\/limits\u6587\u4ef6\u4e2d\uff0c\u786e\u8ba4\u4fee\u6539\u662f\u5426\u5bf9\u8fdb\u7a0b\u751f\u6548\u4e86\u3002<\/p>\n

\u5982\u6b64\uff0c\u767e\u4e07\u8fde\u63a5\u624d\u540d\u4e0d\u865a\u4f20\u3002\u6211\u6bd4\u8f83\u5947\u602a\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48Linux\u4e0d\u9ed8\u8ba4\u653e\u5f00\u8fd9\u4e9b\u914d\u7f6e\u5462?\u505a\u621065535\u4e5f\u8ba4\u554a\uff0c\u4e3a\u4ec0\u4e48\u641e\u4e2a1024?<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6700\u8fd1\u9047\u5230\u4e00\u4e2a\u975e\u5e38\u6709\u8da3\u7684\u95ee\u9898\u3002\u5176\u4e2d\u6709\u4e00\u7ec4HAProxy\uff0c\u9891\u7e41\u51fa\u73b0\u95ee\u9898\u3002\u767b\u5f55\u4e0a\u670d\u52a1\u5668\uff0ccpu\u3001\u5185\u5b58\u3001\u7f51\u7edc\u3001io\u4e00\u987f […]<\/p>\n","protected":false},"author":1329,"featured_media":208694,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-208687","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=208687"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208687\/revisions"}],"predecessor-version":[{"id":208733,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/208687\/revisions\/208733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208694"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=208687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=208687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=208687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}