ÔõÑùʹÓÃLinux¾ÙÐÐϵͳ×ÊÔ´ÏÞÖƺͿØÖÆ
ÔõÑùʹÓÃlinux¾ÙÐÐϵͳ×ÊÔ´ÏÞÖƺͿØÖÆ
ÔÚLinuxϵͳÖУ¬ÎÒÃǾ³£ÐèÒª¶Ôϵͳ×ÊÔ´¾ÙÐÐÏÞÖƺͿØÖÆ£¬ÒÔÈ·±£ÏµÍ³µÄÎȹÌÐÔºÍÇå¾²ÐÔ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃLinuxÄÚ½¨µÄ×ÊÔ´¿ØÖÆ»úÖÆÀ´ÏÞÖƺͿØÖÆϵͳ×ÊÔ´µÄʹÓá£ÎÒÃÇ»áÖصãÏÈÈÝcgroup (control group) ºÍulimitÕâÁ½¸ö³£ÓõŤ¾ß£¬ÒÔ¼°ËüÃǵÄʹÓÃʾÀý¡£
Ò»¡¢cgroup
cgroupÊÇLinuxÄÚºËÌṩµÄ×ÊÔ´ÖÎÀí»úÖÆ£¬Í¨¹ýËüÎÒÃÇ¿ÉÒÔ½«Àú³Ì×éÖ¯ÔÚÒ»Æ𣬲¢¶ÔÕâЩÀú³Ì×é¾ÙÐÐ×ÊÔ´ÏÞÖƺͿØÖÆ¡£cgroup¿ÉÒÔ¿ØÖƺÍÏÞÖÆCPU¡¢ÄÚ´æ¡¢I/OµÈϵͳ×ÊÔ´µÄʹÓá£
×°ÖÃcgroup¹¤¾ß
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃcgroup¹¤¾ß¡£ÔÚ´ó´ó¶¼Linux¿¯ÐаæÉÏ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî×°Öãº
sudo apt-get install cgroup-tools
µÇ¼ºó¸´ÖÆ
½¨Éècgroup
ÎÒÃÇ¿ÉÒÔʹÓÃcgcreateÏÂÁîÀ´½¨ÉèÒ»¸öcgroup£¬ÀýÈ磬½¨ÉèÒ»¸öÃûΪmygroupµÄcgroup£º
sudo cgcreate -g cpu,memory:mygroup
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁÉèÁËÒ»¸öÃûΪmygroupµÄcgroup£¬²¢ÇÒͬʱ¶ÔÆä¾ÙÐÐÁËCPUºÍÄÚ´æµÄÏÞÖÆ¡£
ÉèÖÃcgroupµÄ×ÊÔ´ÏÞÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖÃcgroupµÄ×ÊÔ´ÏÞÖÆ¡£ÀýÈ磬ÎÒÃǽ«CPUÏÞÖÆΪ×î¶àʹÓÃ50%µÄʱ¼äƬ£¬²¢½«ÄÚ´æÏÞÖÆΪ×î¶àʹÓÃ1GBµÄÄڴ棺
sudo cgset -r cpu.cfs_quota_us=50000 -r memory.limit_in_bytes=1G mygroup
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁmygroupµÄCPUÏÞÖÆΪ×î¶àʹÓÃ50%µÄʱ¼äƬ£¬²¢½«ÄÚ´æÏÞÖÆΪ×î¶àʹÓÃ1GBµÄÄÚ´æ¡£
½«Àú³Ì¼ÓÈëcgroup
×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃcgclassifyÏÂÁÀú³Ì¼ÓÈëµ½Ö¸¶¨µÄcgroupÖС£ÀýÈ磬½«Àú³ÌPIDΪ12345µÄÀú³Ì¼ÓÈëµ½mygroup£º
sudo cgclassify -g cpu,memory:mygroup 12345
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬Àú³ÌPIDΪ12345µÄÀú³Ì½«Êܵ½mygroupµÄ×ÊÔ´ÏÞÖÆ¡£
¶þ¡¢ulimit
³ýÁËcgroup£¬Linux»¹ÌṩÁËÁíÒ»¸öÇ¿Ê¢µÄ×ÊÔ´¿ØÖƹ¤¾ßulimit¡£ulimitÔÊÐíÎÒÃÇÏÞÖƵ¥¸öÓû§¿ÉÒÔʹÓõÄϵͳ×ÊÔ´¡£
Éó²éÄ¿½ñ×ÊÔ´ÏÞÖÆ
ÎÒÃÇ¿ÉÒÔʹÓÃulimitÏÂÁîÀ´Éó²éÄ¿½ñ×ÊÔ´ÏÞÖƵÄÉèÖá£ÀýÈ磬Éó²éÄ¿½ñÓû§¿ÉÒÔʹÓõÄ×î´óÎļþ¾ÞϸÏÞÖÆ£º
ulimit -f
µÇ¼ºó¸´ÖÆ
ÉèÖÃ×ÊÔ´ÏÞÖÆ
ÎÒÃÇ¿ÉÒÔʹÓÃulimitÏÂÁîÀ´ÉèÖÃ×ÊÔ´ÏÞÖÆ¡£ÀýÈ磬½«×î´óÎļþ¾ÞϸÏÞÖÆÉèÖÃΪ1GB£º
ulimit -f 1000000000
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁ×î´óÎļþ¾ÞϸÏÞÖÆÉèÖÃΪ1GB¡£
ÁíÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃulimitÏÂÁîÉèÖÃÆäËû×ÊÔ´ÏÞÖÆ£¬ÈçCPUʱ¼ä¡¢ÄÚ´æ¡¢·¿ªÎļþÊýµÈ¡£
ÓÀÊÀÐÞ¸Ä×ÊÔ´ÏÞÖÆ
ͨ¹ýulimitÏÂÁîÉèÖõÄ×ÊÔ´ÏÞÖƽö¶ÔÄ¿½ñ»á»°ÓÐÓã¬Ò»µ©»á»°¿¢Ê£¬ÉèÖý«Ê§Ð§¡£ÈôÊÇÏëÒªÓÀÊÀÐÞ¸Ä×ÊÔ´ÏÞÖÆ£¬ÎÒÃÇ¿ÉÒÔÐÞ¸ÄϵͳÉèÖÃÎļþ/etc/security/limits.conf¡£
ÀýÈ磬ÈôÊÇÏëÒª½«×î´ó·¿ªÎļþÊýÏÞÖÆÐÞ¸ÄΪ10000£¬¿ÉÒÔÔÚ/etc/security/limits.confÖмÓÈëÒÔÏÂÉèÖãº
* hard nofile 10000
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖý«×î´ó·¿ªÎļþÊýÏÞÖÆÐÞ¸ÄΪ10000¡£
½áÂÛ
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃlinux¾ÙÐÐϵͳ×ÊÔ´ÏÞÖƺͿØÖÆ¡£ÎÒÃÇÖصãÏÈÈÝÁËcgroupºÍulimitÕâÁ½¸ö³£ÓõŤ¾ß£¬²¢¸ø³öÁËÏìÓ¦µÄʹÓÃʾÀý¡£Í¨¹ýºÏÀíµØʹÓÃÕâЩ¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÓÐÓõؿØÖÆϵͳ×ÊÔ´µÄʹÓ㬰ü¹ÜϵͳµÄÎȹÌÐÔºÍÇå¾²ÐÔ¡£Ï£Íû±¾ÎĶÔʹÓÃLinux¾ÙÐÐϵͳ×ÊÔ´ÏÞÖƺͿØÖƵĶÁÕßÓÐËù×ÊÖú¡£
²Î¿¼×ÊÁÏ£º
https://www.man7.org/linux/man-pages/man7/cgroups.7.html
https://www.man7.org/linux/man-pages/man2/setrlimit.2.html
https://linux.die.net/man/1/ulimit
ÒÔÉϾÍÊÇÔõÑùʹÓÃLinux¾ÙÐÐϵͳ×ÊÔ´ÏÞÖƺͿØÖƵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡