{"id":526,"date":"2025-05-04T17:03:48","date_gmt":"2025-05-04T09:03:48","guid":{"rendered":"https:\/\/cococat.top\/?p=526"},"modified":"2025-05-04T17:03:48","modified_gmt":"2025-05-04T09:03:48","slug":"cgroup-%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2025\/05\/04\/cgroup-%e7%ac%94%e8%ae%b0\/","title":{"rendered":"cgroup \u7b14\u8bb0"},"content":{"rendered":"<h1>1. \u672f\u8bed<\/h1>\n<ul>\n<li><strong>cgroup<\/strong> : A <em>cgroup<\/em> is a collection of processes that are bound to a set of limits or parameters defined via the cgroup filesystem.<\/li>\n<li><strong>subsystem<\/strong>: \u7528\u4e8e\u9650\u5236 cgroup \u4e2d\u8fdb\u7a0b\u7684\u8d44\u6e90\u548c\u884c\u4e3a\uff0c\u4f8b\u5982\u63a7\u5236 CPU \u548c\u5185\u5b58\u5360\u7528\uff0c\u751a\u81f3\u6682\u65f6\u51bb\u7ed3\u8fdb\u7a0b\u7684\u8fd0\u884c\u3002\u6709\u65f6\u4e5f\u88ab\u79f0\u4e3a resource controllers \/ controllers<\/li>\n<li><strong>task<\/strong> : \u63a5\u53d7 cgroup \u9650\u5236\u7684<strong>\u8fdb\u7a0b<\/strong>\u3002\u5728 cgroup v1 \u6216 v2 \u7684\u9ad8\u7ea7\u7528\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u662f\u7ebf\u7a0b\u3002<\/li>\n<li><strong>hierarchy<\/strong>: \u7531\u4e8e cgroup \u662f\u4ee5 fs \u7684\u65b9\u5f0f\u5b58\u653e\uff0c\u56e0\u6b64\u591a\u4e2a cgroup \u53ef\u4ee5\u6784\u6210\u5c42\u7ea7\u7ed3\u6784\uff0c\u5b50\u7ea7 cgroup \u4f1a\u7ee7\u627f\u7236\u7ea7\u7684\u8d44\u6e90\u9650\u5236<\/li>\n<\/ul>\n<h1>2. cgroup v1 \u7684\u4f8b\u5b50<\/h1>\n<h2>2.1. \u65b0\u5efa\u5b50\u7cfb\u7edf<\/h2>\n<p>\u65b0\u5efa\u7528\u4e8e\u9650\u5236\u8fdb\u7a0b\u5185\u5b58\u7684\u5b50\u7cfb\u7edf\u3002<br \/>\n\u7531\u4e8e cgroup \u662f\u4ee5 FS \u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7684\uff0c\u6240\u4ee5\u521b\u5efa\u7684\u65b9\u6cd5\u662f mount\u3002<br \/>\n\u901a\u5e38\u6765\u8bf4\uff0c\u73b0\u4ee3\u7684\u7cfb\u7edf\u542f\u52a8\u65f6\uff0csystemd \u4f1a\u81ea\u52a8\u628a\u6240\u6709\u652f\u6301\u7684\u5b50\u7cfb\u7edf\uff08\u542b cpu\u3001memory \u7b49\uff09\u6302\u8f7d\u5230 <code>\/sys\/fs\/cgroup<\/code> \uff0c\u56e0\u6b64\u8fd9\u7b2c\u4e00\u6b65\u901a\u5e38\u662f\u53ef\u4ee5\u8df3\u8fc7\u7684\u3002<\/p>\n<pre><code class=\"language-bash\">mount -t cgroup -o memory none \/sys\/fs\/cgroup\/memory<\/code><\/pre>\n<blockquote>\n<p>mount \u7684\u53c2\u6570\uff1a<\/p>\n<ul>\n<li><code>-t<\/code> : \u6587\u4ef6\u7cfb\u7edf\u7c7b\u578b<\/li>\n<li><code>-o<\/code> : \u6307\u5b9a\u7684 fs \u652f\u6301\u7684 options\u3002cgroup fs \u652f\u6301\u7684 options \u5c31\u662f\u5404\u79cd subsystem \u7684\u540d\u79f0\uff0c\u4f8b\u5982 cpu, memory<\/li>\n<li>none: \u5bf9\u4e8e mount \u547d\u4ee4\uff0c\u8fd9\u91cc\u5e94\u8be5\u662f device \u7684\u540d\u79f0\uff0c\u7531\u4e8e cgroup fs \u5e76\u4e0d\u5b58\u5728\u771f\u5b9e\u7684\u8bbe\u5907\uff0c\u56e0\u6b64\u53ea\u662f\u4e00\u4e2a\u5360\u4f4d\u7b26\u7f62\u4e86<\/li>\n<\/ul>\n<\/blockquote>\n<h2>2.2. \u521b\u5efa\u5b50 cgroup<\/h2>\n<p>\u5728 memory \u4e0b\u65b0\u5efa\u4e00\u4e2a test<\/p>\n<pre><code class=\"language-bash\">mkdir \/sys\/fs\/cgroup\/memory\/test<\/code><\/pre>\n<p>\u6267\u884c\u6210\u529f\u540e\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5728 test \u4e0b\u65b0\u5efa\u4e00\u7cfb\u5217\u6587\u4ef6\uff0c\u6bcf\u4e2a\u6587\u4ef6\u8d1f\u8d23\u5b58\u50a8\u4e00\u9879\u914d\u7f6e\uff0c\u7528\u4e8e\u63a7\u5236\u8fdb\u7a0b\u7ec4\u7684\u8d44\u6e90\u4f7f\u7528\u3002<\/p>\n<h2>2.3. \u4fee\u6539\u8d44\u6e90\u9650\u5236\uff0c\u7ed1\u5b9a\u8fdb\u7a0b<\/h2>\n<pre><code class=\"language-bash\">echo 1234567 &gt; \/sys\/fs\/cgroup\/test\/memory.limit_in_bytes\necho ${task_pid} &gt; \/sys\/fs\/cgroup\/test\/tasks<\/code><\/pre>\n<h1>3. cgroup v2<\/h1>\n<p>v1 \u7248\u672c\u5b58\u5728\u4e00\u4e9b\u5386\u53f2\u95ee\u9898\uff1a\u5404\u4e2a\u5b50\u7cfb\u7edf\u5f00\u53d1\u76f8\u5bf9\u72ec\u7acb\u3001\u4e0d\u591f\u534f\u8c03\uff0c\u6700\u7ec8\u51fa\u73b0\u4e86\u8bb8\u591a\u4e0d\u4e00\u81f4\uff0c\u7ed9\u591a\u5c42\u7ea7\u7684 cgroup \u7ef4\u62a4\u5e26\u6765\u4e86\u6781\u5927\u7684\u590d\u6742\u6027\u3002\u8fd9\u4f7f\u5f97\u5f00\u53d1\u8005\u793e\u533a\u6700\u7ec8\u51b3\u5b9a\u7814\u53d1 v2\uff0c\u5e76\u5728 kernel v4.5 \u4e2d\u6b63\u5f0f release\u3002<\/p>\n<p>\u5f53\u524d\u7684 linux \u5185\u6838\u540c\u65f6\u652f\u6301 cgroup v1 \u548c v2 \uff0c\u4e14\u5141\u8bb8\u5c06\u5b83\u4eec\u540c\u65f6\u6302\u8f7d\u5230\u4e00\u4e2a OS \u4e2d\u3002\u8fd9\u662f\u56e0\u4e3a\u622a\u81f3\u76ee\u524d v2 \u5e76\u672a\u5305\u542b\u6240\u6709 v1 \u7684\u652f\u6301\u7684\u5b50\u7cfb\u7edf\uff0c\u4ee5\u53ca\u51fa\u4e8e\u540e\u5411\u517c\u5bb9\u6027\u7684\u8003\u8651\u3002\u552f\u4e00\u7684\u9650\u5236\u662f\uff0c\u4e0d\u5141\u8bb8\u540c\u4e00\u4e2a\u5b50\u7cfb\u7edf\u540c\u65f6\u88ab\u6302\u8f7d\u5230 v1 \u548c v2 \u4e2d\u3002<\/p>\n<p>cgroup  v1 \u5141\u8bb8\u5404\u4e2a\u5b50\u7cfb\u7edf\u6302\u8f7d\u5230\u4e0d\u540c\u7684\u5c42\u7ea7\u4e2d\u3002\u4ece\u8bbe\u8ba1\u89d2\u5ea6\u6765\u8bf4\uff0c\u8fd9\u6837\u662f\u4e3a\u4e86\u6700\u5927\u5316\u914d\u7f6e\u7684\u7075\u6d3b\u6027\uff1b\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u53d1\u73b0\uff0c\u8fd9\u79cd\u7075\u6d3b\u6027\u57fa\u672c\u6ca1\u6709\u4f7f\u7528\u573a\u666f\uff0c\u53cd\u800c\u5f92\u589e\u4e86 cgroup \u7cfb\u7edf\u7684\u590d\u6742\u6027\u3002\u56e0\u6b64\u5728 v2 \u7684\u8bbe\u8ba1\u4e2d\uff0c\u6240\u6709\u7684\u5b50\u7cfb\u7edf\u90fd\u5f3a\u5236\u8981\u6c42\u6302\u8f7d\u5230\u540c\u4e00\u5c42\u7ea7\u4e2d\u3002<\/p>\n<p>\u6b64\u5916\uff0ccgroup v2 \u53ea\u5141\u8bb8\u8fdb\u7a0b\u88ab\u7ed1\u5b9a\u5230\u53f6\u5b50\u5c42\u7ea7\u7684 cgroup \uff08No internal processes rule\uff09\u3002\u5373\uff0c\u5982\u679c\u4e00\u4e2a\u5c42\u7ea7\u542b\u6709\u5b50 cgroup \uff0c\u5219\u8be5\u5c42\u7ea7\u4e0d\u53ef\u4ee5\u6709\u7ed1\u5b9a\u7684\u8fdb\u7a0b\u3002\u8fd9\u662f\u4e3a\u4e86\u89e3\u51b3\u5728\u7236\u5b50\u5c42\u7ea7\u4e4b\u95f4\u5206\u914d\u8d44\u6e90\u7684\u96be\u9898\u3002<\/p>\n<p>\u5728 cgroup v2 \u4e0b\uff0c\u67e5\u770b\u4e00\u4e2a cgroup \u7684\u7ed1\u5b9a\u8fdb\u7a0b\u7684\u6587\u4ef6\u4e0d\u518d\u662f <code>tasks<\/code>\uff0c\u800c\u662f <code>cgroup.procs<\/code>\u3002<\/p>\n<h1>4. \u6742<\/h1>\n<p>\u901a\u8fc7 <code>cat \/proc\/$pid\/cgroup<\/code> \u67e5\u770b\u4e00\u4e2a\u8fdb\u7a0b\u7684 cgroup\u3002<\/p>\n<p>\u4e0e namespace \u5408\u4f5c\uff0c\u6784\u6210\u5bb9\u5668\u6280\u672f\u7684\u57fa\u77f3\u3002ns \u8d1f\u8d23\u9694\u79bb\uff08FS\u3001\u8fdb\u7a0b\u3001\u7f51\u7edc\uff09\uff0ccgroup \u8d1f\u8d23\u9650\u5236\uff08cpu\u3001\u5185\u5b58\uff09<\/p>\n<h1>5. \u53c2\u8003<\/h1>\n<ul>\n<li><a href=\"https:\/\/man7.org\/linux\/man-pages\/man7\/cgroups.7.html\">https:\/\/man7.org\/linux\/man-pages\/man7\/cgroups.7.html<\/a><\/li>\n<li><a href=\"https:\/\/o.alldu.cn\/docs\/%E6%B7%B1%E5%85%A5%E5%89%96%E6%9E%90kubernetes\/06-%E7%99%BD%E8%AF%9D%E5%AE%B9%E5%99%A8%E5%9F%BA%E7%A1%80%E4%BA%8C%E9%9A%94%E7%A6%BB%E4%B8%8E%E9%99%90%E5%88%B6\/\">https:\/\/o.alldu.cn\/docs\/%E6%B7%B1%E5%85%A5%E5%89%96%E6%9E%90kubernetes\/06-%E7%99%BD%E8%AF%9D%E5%AE%B9%E5%99%A8%E5%9F%BA%E7%A1%80%E4%BA%8C%E9%9A%94%E7%A6%BB%E4%B8%8E%E9%99%90%E5%88%B6\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. \u672f\u8bed cgroup : A cgroup is a collection of processes th [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18,20],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-linux","tag-k8s","tag-linux"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}