LoginSignup
5
4

More than 5 years have passed since last update.

ElasticBeanstalk(Java)でMaxOpenFilesを増やす

Last updated at Posted at 2016-03-19

Javaを使用してアプリケーションサーバを実行する場合、1プロセスで多くのリクエストを処理することととなるので
リクエスト数が増えるとToo many open filesエラーが発生することがあるかと思います。

アプリケーションサーバ起動時にはPAMを通らない=/etc/security/limits.conf に記述しても効かないので、
通常はアプリケーションサーバの起動スクリプト等にulimit -nを記述したりすることで対応するかと思いますが、
ElasticBeansTalkではアプリケーションの起動周りにcloud-init→foreman/supervisordが使われており、起動スクリプトに追記することが難しくなっています。

現象確認

まずは変更前の値をcat /proc/プロセスID/limits | grep "Max open files"で確認します。

Max open files            1024                 1024                 files

Max open filesはSoft/Hardともに1024となっています。(左の1024がSoft Limit、右の1024がHard Limitです)

Javaの起動元プロセス確認

ElasticBeansTalkで作成したEC2にてpstreeで確認したところ、

init-+-agetty
     |-atd
    略
     |-supervisord-+-java---28*[{java}]
     |             `-python
    略

となっていましたので、
Max open filesが子プロセスに引き継がれることを利用して
supervisordのMax open filesを増やす方針で対応します。

修正

/etc/supervisor/conf.d/supervisord.conf にあるminfdsの値をEC2起動時に書き換えます。

/etc/supervisor/conf.d/supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/var/elasticbeanstalk/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisor/conf.d/application.conf /etc/supervisor/conf.d/containerlistener.conf

↑ここでは上記のminfds=1024をminfds=60000に変更します。

supervisordが起動する前に、上記設定ファイルを書き換える必要がありますので
/opt/elasticbeanstalk/hooks/appdeploy/pre
にsupervisord.confを書き換えるスクリプトを配置します。
即ち、.ebextensions配下に以下のような設定ファイルを配置してeb create/eb deployします。

00_supervisord.config
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/00_supervisord.sh" :
    mode: "000744"
    owner: root
    group: root
    content: |
      #!/bin/bash
      sed -i "s/^minfds=1024/minfds=60000/" /etc/supervisor/conf.d/supervisord.conf

修正の確認

アプリケーション起動後にcat /proc/プロセスID/limits | grep "Max open files" で確認すると、無事増えていることが確認できます。

Max open files            60000                60000                files

参考
http://supervisord.org/configuration.html
http://staffblog.yumemi.jp/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%83%87%E3%82%A3%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%82%BF%E6%95%B0%E3%81%AE%E4%B8%8A%E9%99%90%E5%A4%89%E6%9B%B4%E3%81%A8limits-conf%E3%81%AE%E7%BD%A0-2/
http://blog.father.gedow.net/2012/08/08/ulimit-configuration/

5
4
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
5
4