LoginSignup
6
5

More than 5 years have passed since last update.

Ubuntu 14.04でcgroupsを使ってリソース制限

Posted at

インストール

apt-get install cgroup-bin

設定

/etc/cgconfig.conf
group group1 {
  cpu {
# デフォルト値は1024
    cpu.shares = 256;
  }
  memory {
    memory.limit_in_bytes = 4G;
  }
}

設定の反映

sudo cgconfigparser -l /etc/cgconfig.conf
sudo service cgroup-lite restart

cgexecを使った実行

/etc/cgconfig.confの設定を適用してコマンドを実行するにはcgexecコマンドを使う。

# CPU制限の適用
cgexec -g cpu:group1 stress --cpu 4
# メモリ制限の適用
cgexec -g memory:group1 stress --vm 1 --vm-bytes 8G 

cgrulesengdによるルールの適用

cgroup-binパッケージにはcgrulesengdというコマンドが含まれている。
cgrulesengdは/etc/cgrules.confの内容に従ってプロセスをcgroupに移動する。
Ubuntu 14.04ではcgrulesengdのinitスクリプトが含まれていないので、以下のファイルを作成する。

参考: http://frank2.net/cgroups-ubuntu-14-04/

/etc/init/cgrulesengd.conf
description "cgrulesengd"
author "Serge Hallyn <serge.hallyn@canonical.com>"

start on started cgroup-lite
stop on stopped cgroup-lite

pre-start script
test -x /usr/sbin/cgrulesengd || { stop; exit 0; }
end script

script
  # get default options
  OPTIONS="--syslog"
  CGRED_CONF=/etc/cgrules.conf
  if [ -r "/etc/default/cgrulesengd" ]; then
    . /etc/default/cgrulesengd
  fi

  # Don't run if no configuration file
  if [ ! -s "$CGRED_CONF" ]; then
    echo "Cgred unconfigured"
    stop
    exit 0
  fi

  # Make sure the kernel supports cgroups
  # This check is retained from the original sysvinit job, but should
  # be superfluous since we depend on cgconfig running, which will
  # have mounted this.
  grep -q "^cgroup" /proc/mounts || { stop; exit 0; }

  exec /usr/sbin/cgrulesengd --nodaemon $OPTIONS
end script

ルールの設定

ルールの設定ファイルを記述する。
以下はtd-agentをtd-agentという名前のcgroupに移動してCPUとメモリ制限をかける例。

参考: https://access.redhat.com/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-Moving_a_Process_to_a_Control_Group.html

/etc/cgrules.conf
td-agent  cpu,memory  td-agent/

ルール設定の適用

sudo service cgrulesengd restart
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5