Golangの本番環境(production)にsupervisorを使ってみる

参考

Supervisor: A Process Control System — Supervisor 3.3.0 documentation

supervisorのインストール

# pip --version
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)
# pip install supervisor
Successfully installed meld3-1.0.2 supervisor-3.3.0

ディレクトリの作成

# mkdir -p /etc/supervisor/conf.d
# mkdir -p /var/log/supervisor

設定ファイルの作成

  • /etc/supervisor/conf.d/application.conf
[program:app]
command=XXXXXXXXXXX
autostart=true
autorestart=true
stopsignal=TERM
stdout_logfile=/var/log/supervisor/app.stdout.log
stderr_logfile=/var/log/supervisor/app.stderr.log
user=XXXXXXX
directory=XXXXXXXXXXXXXXXX

[group:application]
programs=app
  • /etc/supervisor/conf.d/supervisord.conf
[unix_http_server]
file=/var/run/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/run/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:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = /etc/supervisor/conf.d/*.conf

フォアグラウンドとして起動

# supervisord -c /etc/supervisor/conf.d/supervisord.conf --nodaemon

ログ

tail -f /var/log/supervisor/app.stderr.log

アプリのプロセスをすべて再起動(supervisordのプロセスは停止しない)

supervisorctl -c /etc/supervisor/conf.d/supervisord.conf restart all