ruby-1.9.1でSwiftiply+merbクラスタ
EC2のサーバ上でruby-1.8.x
構成
入り口から順に並べるとこんな感じです。
- apache2 (:80)
- mod_proxy_
balancer (:80 -> :4000) - swiftiply (:4000 <- :30000)
- merb cluster with Swiftiplie
dMongrel adapter (:30000)
apache2はpassengerを使っているので外せないとして、
ruby-1.9.1
そこで、以前から目を付けていた Swiftiply を試す事にしました。 Swiftiplyはとても面白い設計のウェブサーバで、そのへんは rakutoさんの記事 「[Rails] Swiftiplyのアーキテクチャとベンチマーク」 を読むのがわかりやすいでしょう。
merbはbuilt-inでSwiftiply用のアダプター(Swiftiplie
しかし、例によってswiftiplyが依存しているeventmachi
あとは、swiftiplyの起動スクリプトを用意。
/etc/init.
1 #!/usr/bin/env ruby 2 3 PID = "/var/run/s wiftiply.p id" 4 5 case ARGV[0] 6 when 'start' 7 if File.exist?(PID) 8 puts "swiftiply is already started." 9 exit 1 10 end 11 print "Starting swiftiply: " 12 pid = fork do 13 Process.setsid 14 Process.exec "swiftiply", "-c", "/etc/swift iply.yml" 15 end 16 begin 17 open(PID, "w"){|file| file.write pid} 18 puts "done" 19 rescue Exception => e 20 puts e.message 21 Process.kill 9, pid 22 end 23 when 'stop' 24 print "Stopping swiftiply: " 25 begin 26 pid = open(PID).read.to_i 27 File.unlink PID 28 Process.kill 9, pid 29 puts "done" 30 rescue Exception => e 31 puts e.message 32 end 33 when 'restart' 34 system $0, 'stop' 35 system $0, 'start' 36 else 37 puts "usage: {start|stop|restart}" 38 end
アプリケーションの起動、停止は以下のようにおこないます。
1 cd #{current_path}; #{merb} -K all 2 cd #{current_pa th}; #{merb} -e production -a swift -c 1 -p 30000
これで動きますが、プロセスが死んだりすると不安なので、 monitを使って死活管理をおこなうようにします。
/etc/monit
1 check process swiftiply 2 with pidfile /var/run/swiftiply.p id 3 group root 4 start program = "/etc/init. d/swiftipl y start" 5 stop program = "/etc/init. d/swiftipl y stop" 6 if failed port 4000 then restart 7 if 5 restarts within 5 cycles then timeout 8 9 check process merbist 10 with pidfile /mnt/app/m erbist/cur rent/log/m erb.30000. pid 11 group root 12 start program = "/mnt/app/m erbist/sha red/script /start" 13 as uid app and gid app 14 stop program = "/mnt/app/m erbist/sha red/script /stop" 15 as uid app and gid app 16 if failed port 30000 then restart 17 if 5 restarts within 5 cycles then timeout 18 depends on swiftiply
以上で完了。
現在、http://mer
See Aslo
追記
しばらく運用してみたのですが、プロセスが数時間に一回ぐらいのペースで落ちる問題があったので、Swiftiplyの代わりにPoundを使う構成に変えました。 Swiftiplyは次のバージョン0.7.0から、クラスタリングのサポートが強化されるようなので、それまで待ってみた方が良いかもしれないですね。