Capistrano 部署 Rails 项目速记中解决了代码更新时候 mongrel/backgroundrb 重启等问题,还需要保证系统重启时服务能够自动启动。
mongrel cluster 自带了 mongrel-cluster 启动管理脚本,Debian 系统的话链接 mongrel_cluster.yml 到 /etc/mongrel-cluster/sites-enabled/ 目录,系统启动的时候 /etc/init.d/mongrel-cluster 会自动启动 mongrel daemon,然而 backgroundrb 的启动问题还是没有解决,需要手工写启动脚本,google 了一番之后,找到 Monit 这个小工具:
# 摘自 monit deb package description. A utility for monitoring and managing daemons or similar programs monit is a utility for monitoring and managing daemons or similar programs running on a Unix system. It will start specified programs if they are not running and restart programs not responding. . monit supports: * Daemon mode - poll programs at a specified interval * Monitoring modes - active, passive or manual * Start, stop and restart of programs是否可以用 Monit 来启动 mongrel 和 backgroundrb 呢,答案是肯定的,事实上这也应该是推荐方式。
安装 Monit
$sudo aptitude install monit修改 /etc/default/monit 配置:
startup=1 CHECK_INTERVALS=30 # monit 监控检测时间价格,推荐设置成 30 秒配置 monit
$sudo grep '^[^#]' /etc/monit/monitrc set daemon 30 set logfile syslog facility log_daemon set httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # allow localhost to connect to the server and include /etc/monit/conf.d/* $sudo mkdir /etc/monit/conf.d
配置 Daemon 项
$cat 001_neo_mongrel.conf check process mongrel_neo_8000 with pidfile /project/code/path/shared/pids/mongrel.8000.pid start program = "/usr/bin/mongrel_rails cluster::start -C /project/code/path/current/config/mongrel_cluster.yml --clean --only 8000" stop program = "/usr/bin/mongrel_rails cluster::stop -C /project/code/path/current/config/mongrel_cluster.yml --only 8000" if totalmem is greater than 110.0 MB for 4 cycles then restart if cpu is greater than 80% for 4 cycles then restart if 20 restarts within 20 cycles then timeout group neo check process mongrel_neo_8001 with pidfile /project/code/path/shared/pids/mongrel.8001.pid start program = "/usr/bin/mongrel_rails cluster::start -C /project/code/path/current/config/mongrel_cluster.yml --clean --only 8001" stop program = "/usr/bin/mongrel_rails cluster::stop -C /project/code/path/current/config/mongrel_cluster.yml --only 8001" if totalmem is greater than 110.0 MB for 4 cycles then restart if cpu is greater than 80% for 4 cycles then restart if 20 restarts within 20 cycles then timeout group neo check process mongrel_neo_8002 with pidfile /project/code/path/shared/pids/mongrel.8002.pid start program = "/usr/bin/mongrel_rails cluster::start -C /project/code/path/current/config/mongrel_cluster.yml --clean --only 8002" stop program = "/usr/bin/mongrel_rails cluster::stop -C /project/code/path/current/config/mongrel_cluster.yml --only 8002" if totalmem is greater than 110.0 MB for 4 cycles then restart if cpu is greater than 80% for 4 cycles then restart if 20 restarts within 20 cycles then timeout group neo
$ cat 003_neo_brb.conf check process brb_imigu_11006 with pidfile /project/code/path/shared/pids/backgroundrb_11006.pid start program = "/bin/su -c 'cd /project/code/pathcurrent && RAILS_ENV=production nohup ./script/backgroundrb start > /dev/null 2>&1' josh" stop program = "/bin/su -c 'cd /home/josh/www/imigu/current && RAILS_ENV=production && ./script/backgroundrb stop' josh" group neo启动 Monit 和监测
$sudo /etc/init.d/monit restart $sudo monit -c /etc/monit/monitrc status #查看系统状态通过 monit 已经可以自动启动管理 mongrel 和 backgroundrb,那么谁来保证 monit 的稳定运行呢,答案是 linux 系统 init。 编辑 /etc/inittab 加入:
# Run monit in standard run-levels mo:2345:respawn:/usr/sbin/monit -Ic /etc/monit/monitrc mo:06:wait:/usr/sbin/monit -Ic /etc/monit/monitrc stop all重启 monit
$sudo /etc/init.d/monit stop $sudo telinit q
参考:
- Monit man page
- 2008, Ezra Zygmuntowicz and Bruce Tate, Deploying Rails Applications
No comments:
Post a Comment