楽々運用!Supevsord+unicorn/gunicornで作る、Linux daemon 手順 - trac/bloodhound -
前回、supervisorや、unicornherderを用いた、Webアプリプロセスのdaemon化の概要について説明しました。
簡単な仕組みや、役割分担についても何となくご理解頂けたと思います。
1.概要編
2.手順編
2.1.trac with gunicorn(bloodhound)←今ここ
2.2.redmine with unicorn
3.その他
さて、今回は、trac(bloodhound) のDaemon化の具体的な設定を通じて、daemon化を共有したいと思います。
tracは、今回はApache bloodhoundを利用してみましょう。(事例も少ないので
設定手順は以下の通りです。
0.環境構築
1.Webアプリのgunicorn対応
2.Webアプリのunicornherder対応
3.supervisorからの、Webアプリの起動
4.supervisorの、init scriptの登録
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
0.環境構築
pythonは、virtualenvを使って、そのアプリ特有の環境を構築する事が出来ます。
幸い公式の構築手順もvirtualenvを使う設定方法を提供しています。
https://issues.apache.org/bloodhound/wiki/BloodhoundInstall
今回は、bloodhound自体のインストールは主題ではないので割愛します。
各自実施してください。
また、以下のコマンドを追加で実行してください。
sudo pip install gunicorn supervisor unicornherder
1.Webアプリのgunicorn/unicorn対応
bloodhound/tracは標準でWSGIスクリプトが提供されていますので、gunicorn対応は非常に簡単です。
公式手順に従えば、installerの配下に、bloodhound/site/cgi-binというディレクトリが出来ており、
trac.wsgiというファイルがそれに当たります。
bloodhound/site/cgi-binに移動し、ファイル名を変更するだけです。
2.Webアプリのgunicorn / unicornherderからの起動
ここでは、gunicorn / unicornherderをコマンドで起動してみましょう。
そうすることで、次の手順の入力内容を検証できます。
コマンドを実行したら,表示されたURLにアクセスし、正しく表示されるか確かめてみてください。
http://127.0.0.1:8000
■gunicornを利用して起動
■実行結果
(venv)bh@buntu:~/apache-bloodhound-0.7/installer/bloodhound/site/cgi-bin$ gunicorn -w 4 bh_wsgi:application
2013-12-03 23:50:23 [9003] [INFO] Starting gunicorn 18.0
2013-12-03 23:50:23 [9003] [INFO] Listening at: http://127.0.0.1:8000 (9003)
2013-12-03 23:50:23 [9003] [INFO] Using worker: sync
2013-12-03 23:50:23 [9008] [INFO] Booting worker with pid: 9008
2013-12-03 23:50:23 [9009] [INFO] Booting worker with pid: 9009
2013-12-03 23:50:23 [9010] [INFO] Booting worker with pid: 9010
2013-12-03 23:50:23 [9011] [INFO] Booting worker with pid: 9011
■unicornherderを利用して起動
■コマンド例
unicornherder -u gunicorn ― [gunicorn option] bh_wsgi:application■実行結果
(venv)bh@buntu:~/apache-bloodhound-0.7/installer/bloodhound/site/cgi-bin$ python /home/bh/.virtualenvs/bh/bin/unicornherder -u gunicorn -- -w 4 bh_wsgi:application
2013-12-03 23:51:09,142 INFO Unicorn booted (PID 9018)
reverse proxyを通じて bloodhound を利用する場合は、SCRIPT_NAME(詳しくはPEP333参照)を指定する必要があります。
(SCRIPT_NAMEは環境変数です。)
SCRIPT_NAME=“/sub/path” unicornherder -u gunicorn ― [gunicorn option] bh_wsgi:application
これで、http://127.0.0.1:8000/sub/path で、bloodhoundが動作する用になります。
検索用(私が中々辿りつけなかったので)
subpath / suburl / sub path / sub url
3.supervisorからの、Webアプリの起動
ここまでくればsupervisordの入力内容は非常に簡単です。
が、その前に supervisordのコンフィグひな形を準備しましょう
$ sudo echo_supervisord_conf > /etc/supervisord.conf
これで、/etc/supervisord.conf にコンフィグファイルが出力されているはずです。
各自修正を行ってください。
今回に関係のあるセクションのみ記載します。
/path/to/ になっているところは適宜修正を行ってください。
※ 全てが同じ内容ではないので注意
■/etc/supervisord.conf
[program:bloodhound]
command=/path/to/gunicornherder ; the program (relative uses PATH, can take args)
process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
directory=/tmp ; directory to cwd to before exec (def no cwd)
umask=022 ; umask for process (default None)
priority=999 ; the relative start priority (default 999)
autostart=true ; start at supervisord start (default: true)
autorestart=unexpected ; whether/when to restart (default: unexpected)
startsecs=1 ; number of secs prog must stay running (def. 1)
startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=false ; SIGKILL the UNIX process group (def false)
user=chrism ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/path/to/logfile_stdout ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false ; emit events on stdout writes (default false)
stderr_logfile=/path/to/logfile_stderr ; stderr log path, NONE for none; default AUTO
stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stderr_events_enabled=false ; emit events on stderr writes (default false)
environment=SCRIPT_NAME=“/path/to”,PATH=/path/to/venv/bin ; process environment additions (def no adds)
serverurl=AUTO ; override serverurl computation (childutils)
設定ができたら、以下のコマンドを実行しましょう
supervisord -c /etc/supervisord.conf
設定を変更したら、以下のコマンドで有効化出来ます。
stdout_logfile, stderr_logfile に指定したファイルなどを参考にしながら、
起動できるか試してみてください。
supervisorctl reload
4.supervisorの、init scriptの登録
さて、最後に supervisord 自身の自動起動の設定をしておきたいと思います。
init script の実行順序の管理は、各OS毎に利用しているシステム・ツールが異なっています。
Ubuntu/debian 仕組み:LSB(Linux Standard Base )Init, 管理ツール:sysv-rc-conf
RHEL/CentOS 仕組み:Upstart , 管理ツール:chkconfig
Gentoo 仕組み:Upstart , 管理ツール:insserve
Mac 仕組み:Launchd ,管理ツール:launchctl
次世代のSystemdというものも開発されているみたいですね。
Githubには昔ながらのinit script(SysVinit準拠)等が公開されています。
https://github.com/Supervisor/initscripts
Ubuntuのスクリプトは、コマンドを叩くだけの簡易なスクリプトのようです。(これで十分ですが
いかのコマンドを実行すれば、自動起動が有効になります。
$ sudo curl -o supervisord https://raw.github.com/Supervisor/initscripts/master/ubuntu
$ sudo chmod 755 supervisord
$ sudo mv supervisord /etc/init.d/
$ sudo update-rc.d supervisord defaults
Adding system startup for /etc/init.d/supervisord ...
/etc/rc0.d/K20supervisord -> ../init.d/supervisord
/etc/rc1.d/K20supervisord -> ../init.d/supervisord
/etc/rc6.d/K20supervisord -> ../init.d/supervisord
/etc/rc2.d/S20supervisord -> ../init.d/supervisord
/etc/rc3.d/S20supervisord -> ../init.d/supervisord
/etc/rc4.d/S20supervisord -> ../init.d/supervisord
/etc/rc5.d/S20supervisord -> ../init.d/supervisord
キモは、一番下のupdate-rc.dです。
initscript内部に書いてある、# Default-Start: # Default-Stop: 等の、runlevelを指定する行を読み取って、
内部で必要なシンボリックリンクを形成してくれます。
S20等の意味は、また別途次回紹介したいと思います。
スクリプト自身が上手く動かない場合はスライドにも記載したように、以下のコマンドにて実行しましょう。
$ sudo sh -x /etc/init.d/supervisord [action]
参考
http://serverfault.com/questions/96499/how-to-automatically-start-supervisord-on-linux-ubuntu
最後に、今回はセキュリティやOSへの副作用を無視した手順を紹介しました。
社内システムであれば問題ないかもしれませんが、社外に公開するシステムであれば、ユーザーを専用に用意するなどの対応を行ってください。
次回は簡単に無視したものの覚書をしておこおうと思います。