{"id":1299,"date":"2018-05-22T22:12:24","date_gmt":"2018-05-22T20:12:24","guid":{"rendered":"https:\/\/hitco.at\/blog\/?p=1299"},"modified":"2018-05-22T22:15:09","modified_gmt":"2018-05-22T20:15:09","slug":"linux-limit-resources-memory-systemd","status":"publish","type":"post","link":"https:\/\/hitco.at\/blog\/linux-limit-resources-memory-systemd\/","title":{"rendered":"Speicher-Verbrauch von Linux-Diensten mittels systemd limitieren"},"content":{"rendered":"<p>Linux-Dienste lassen sich seit Umstellung auf <a href=\"https:\/\/de.wikipedia.org\/wiki\/Systemd\" target=\"_blank\" rel=\"noopener\">systemd<\/a> mittels weniger (generischer) Konfigurationsanpassungen in den maximal nutzbaren Ressourcen (Memory, CPU, IO, &#8230;) limitieren. M\u00f6glich wird dies mittels <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cgroups\" target=\"_blank\" rel=\"noopener\">cgroups<\/a>. Nachfolgend sei dies am Beispiel einer Hauptspeicher-Limitierung des Apache-Dienstes auf einem Ubuntu 16.04 LTS System kurz demonstriert:<\/p>\n<p>Das Apache2 Service erwartet seine systemd-Konfiguration(en) unter dem Namen &#8222;apache2.service&#8220;, dies ermitteln wir wie folgt:<\/p>\n<pre>root@nc:~# <span style=\"color: #ff0000;\"><strong>systemctl list-units --type=service --all | grep -i Apache<\/strong><\/span>\r\n<span style=\"color: #008000;\"><strong>apache2.service<\/strong><\/span> loaded active running LSB: Apache2 web server<\/pre>\n<p>Wir legen daher ein Verzeichnis mit nachfolgendem Namen an (Achtung: das &#8222;<strong><span style=\"color: #ff0000;\">.d<\/span><\/strong>&#8220; am Ende des Verzeichnis-Namens beachten), und darin eine Konfigurationsdatei mit beliebigem Namen und Datei-Endung &#8222;<strong><span style=\"color: #ff0000;\">.conf<\/span><\/strong>&#8220; in welchem wir das gew\u00fcnschte Memory-Limit konfigurieren:<\/p>\n<pre><span style=\"color: #ff0000;\">mkdir \/etc\/systemd\/system\/<strong>apache2.service.d<\/strong><\/span>\r\n<span style=\"color: #ff0000;\">vi \/etc\/systemd\/system\/apache2.service.d\/limits.conf<\/span><\/pre>\n<blockquote>\n<pre><span style=\"color: #0000ff;\">[Service]<\/span>\r\n<span style=\"color: #0000ff;\">MemoryLimit=1100M\r\nRestart=on-failure\r\nRestartSec=42\r\n<\/span><\/pre>\n<\/blockquote>\n<p>Die soeben erstellte Konfigurationsdatei lassen wir nun vom systemd neu einlesen und starten den Dienst Apache neu:<\/p>\n<pre><span style=\"color: #ff0000;\">systemctl daemon-reload<\/span>\r\n<span style=\"color: #ff0000;\">systemctl restart apache2<\/span><\/pre>\n<p>Eine Status-Abfrage zeigt nun folgende Ver\u00e4nderung:<\/p>\n<pre>root@nc:~# <strong><span style=\"color: #ff0000;\">systemctl status apache2.service<\/span><\/strong>\r\n\u25cf apache2.service - LSB: Apache2 web server\r\nLoaded: loaded (\/etc\/init.d\/apache2; bad; vendor preset: enabled)\r\nDrop-In: \/lib\/systemd\/system\/apache2.service.d\r\n          \u2514\u2500apache2-systemd.conf\r\n        <strong><span style=\"color: #0000ff;\">\/etc\/systemd\/system\/apache2.service.d<\/span><\/strong>\r\n<strong><span style=\"color: #0000ff;\">        \u2514\u2500limits.conf<\/span><\/strong>\r\nActive: active (running) since Di 2018-05-22 19:08:57 CEST; 2h 16min ago\r\nDocs: man:systemd-sysv-generator(8)\r\nProcess: 26721 ExecStop=\/etc\/init.d\/apache2 stop (code=exited, status=0\/SUCCESS)\r\nProcess: 26747 ExecStart=\/etc\/init.d\/apache2 start (code=exited, status=0\/SUCCESS)\r\nTasks: 11\r\n<strong><span style=\"color: #0000ff;\">Memory: 240.3M (limit: 1.0G)<\/span><\/strong>\r\n...<\/pre>\n<p>Das erstellte Konfigurations-AddOn in Form der limits.conf wurde verarbeitet, das Memory-Limit ist wirksam. Weitere konfigurierbare Limits findet man in den <a href=\"http:\/\/man7.org\/linux\/man-pages\/man7\/systemd.directives.7.html\" target=\"_blank\" rel=\"noopener\">systemd-Manpages<\/a>.<\/p>\n<figure id=\"attachment_1306\" aria-describedby=\"caption-attachment-1306\" style=\"width: 881px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1306\" src=\"https:\/\/hitco.at\/blog\/wp-content\/uploads\/linux-systemd-apache.png\" alt=\"systemd apache2.service Konfiguration\" width=\"881\" height=\"834\" srcset=\"https:\/\/hitco.at\/blog\/wp-content\/uploads\/linux-systemd-apache.png 881w, https:\/\/hitco.at\/blog\/wp-content\/uploads\/linux-systemd-apache-300x284.png 300w, https:\/\/hitco.at\/blog\/wp-content\/uploads\/linux-systemd-apache-768x727.png 768w, https:\/\/hitco.at\/blog\/wp-content\/uploads\/linux-systemd-apache-80x76.png 80w\" sizes=\"auto, (max-width: 881px) 100vw, 881px\" \/><figcaption id=\"caption-attachment-1306\" class=\"wp-caption-text\">systemd apache2.service Konfiguration<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Linux-Dienste lassen sich seit Umstellung auf systemd mittels weniger (generischer) Konfigurationsanpassungen in den maximal nutzbaren Ressourcen (Memory, CPU, IO, &#8230;) limitieren. M\u00f6glich wird dies mittels cgroups. Nachfolgend sei dies am Beispiel einer Hauptspeicher-Limitierung des Apache-Dienstes auf einem Ubuntu 16.04 LTS System kurz demonstriert: Das Apache2 Service erwartet seine systemd-Konfiguration(en) unter dem Namen &#8222;apache2.service&#8220;, dies ermitteln wir wie folgt: root@nc:~# systemctl&#8230; <\/p>\n","protected":false},"author":1,"featured_media":1306,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[3,13],"tags":[229,228,226,227],"class_list":["post-1299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-it","category-server","tag-apache","tag-linux","tag-memory-usage","tag-resource-usage"],"_links":{"self":[{"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/posts\/1299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/comments?post=1299"}],"version-history":[{"count":8,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions\/1339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/media\/1306"}],"wp:attachment":[{"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hitco.at\/blog\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}