Slide 1

Slide 1 text

webboxio http://webbox.io

Slide 2

Slide 2 text

Rack Uygulamalarını NginX ve Unicorn’la Koşturma

Slide 3

Slide 3 text

Uğur “vigo” Özyılmazel webBox.io kurucu ortağı, Yazılım Geliştirici https://twitter.com/vigobronx https://github.com/vigo http://ugur.ozyilmazel.com https://speakerdeck.com/vigo

Slide 4

Slide 4 text

Ruby için web sunucusu arayüzüdür. RESPONSE ve REQUEST için http wrapper’dır. Web sunucusu ve Ruby’nin kolay iletişim kurmasını sağlar Ara katmanlar yardımıyla ekstra kolaylıklar sağlar

Slide 5

Slide 5 text

Ruby Uygulama Web Sunucusu

Slide 6

Slide 6 text

Ruby Uygulama Web Sunucusu { Middleware } { Mongrel, WEBrick, CGI, Thin … } { Sinatra, Rails, Ramaze … }

Slide 7

Slide 7 text

`call` metoduna cevap veren Ruby nesnesi yeterlidir! return [ status, headers, body ]

Slide 8

Slide 8 text

require 'rack' ! class MyApplication def call(env) h = {"Content-Type" => "text/html; charset=utf-8"} [200, h, ["Merhaba Dünya"]] end end ! run MyApplication.new Basit bir Rack uygulaması

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Web Sunucusu Mongrel EventedMongrel SwiftipliedMongrel WEBrick FCGI CGI SCGI LiteSpeed Thin { Rack Handlers * Ebb Fuzed Glassfish v3 Phusion Passenger Puma Rainbows! Reel Unicorn unixrack uWSGI Zbatery

Slide 11

Slide 11 text

{ Middleware JSON-P Rack::Protection Rack::Cache Rack::Config Rack::Debug Rack::RespondTo Rack::GoogleAnalytics Rack::Throttle Rack::LinkedData Rack::Attack …

Slide 12

Slide 12 text

Camping Coset Espresso Halcyon Mack Maveric Merb Racktools::SimpleApplication Ramaze Ruby on Rails Rum Sinatra Sin Vintage Waves Wee … Ruby Uygulama +

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

HTTP ve Reverse Proxy sunucusu (Web sunucusu) Caching ve Load-Balancing özellikleri bulunuyor Düşük hafıza kullanımı ve performanslı olması tercih sebebi!

Slide 15

Slide 15 text

Event Driven Architecture Ana İşlem (Main Process) İşçi (Worker) İşçi (Worker) İşçi (Worker)

Slide 16

Slide 16 text

Nginx Kurulum # Ubuntu 12.04 <= add-apt-repository $ sudo aptitude install python-software-properties ! $ sudo add-apt-repository ppa:nginx/stable $ sudo aptitude update $ sudo aptitude install nginx ! # servisi başlatır! $ sudo service nginx start

Slide 17

Slide 17 text

Unicorn

Slide 18

Slide 18 text

Http Server (Ruby gem’i) NginX gibi worker’lar kullanıyor Request’i Unix socket’ine yönlendiriyor Kendi başına otomatik olarak worker’ları yumurtluyor ve organize ediyor!

Slide 19

Slide 19 text

Kurulum $ gem install unicorn source "https://rubygems.org" gem 'unicorn' Gemfile

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

# encoding: utf-8 require 'sinatra/base' ! class MyApplication < Sinatra::Base get '/' do "Merhaba Dünya!" end end application.rb

Slide 22

Slide 22 text

@dir = “/path/to/tmp/" ! worker_processes 2 working_directory @dir ! timeout 30 listen "#{@dir}sockets/unicorn.sock", :backlog => 64 pid_file = "#{@dir}pids/unicorn.pid" old_pid = "#{pid_file}.oldbin" pid pid_file stderr_path "#{@dir}log/unicorn.stderr.log" stdout_path "#{@dir}log/unicorn.stdout.log" preload_app true ! before_exec do |server| ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__)) end ! before_fork do |server, worker| if File.exists?(old_pid) && server.pid != old_pid begin Process.kill("QUIT", File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH # pass end end end unicorn.rb

Slide 23

Slide 23 text

upstream unicorn_mysocket { server unix:/path/to/tmp/sockets/unicorn.sock fail_timeout=0; } ! server { server_name localhost; listen 8080; root /path/to/project; client_max_body_size 4G; keepalive_timeout 5; ! location / { try_files $uri @app; } ! location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn_mysocket; } } nginx.conf

Slide 24

Slide 24 text

upstream unicorn_mysocket { server unix:/path/to/tmp/sockets/unicorn.sock fail_timeout=0; } ! server { server_name localhost; listen 8080; root /path/to/project; client_max_body_size 4G; keepalive_timeout 5; ! location / { try_files $uri @app; } ! location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn_mysocket; } } nginx.conf

Slide 25

Slide 25 text

# encoding: utf-8 require "rubygems" require "sinatra" ! require File.expand_path '../application.rb', __FILE__ run MyApplication $ bundle exec unicorn -c $PWD/unicorn.rb -D $PWD/config.ru config.ru

Slide 26

Slide 26 text

$ ps aux | grep unicorn

Slide 27

Slide 27 text

worker master 1396 $ kill 1396

Slide 28

Slide 28 text

VİDEO

Slide 29

Slide 29 text

worker işlemlerinin ölçeklenmesi, işlemci (CPU) ve hafızayla doğru orantılıdır. Yavaş istemcilerle (slow-client) NginX gibi sunucular uğraşmalı unicorn değil! Ölçeklendirmede asıl düşünülmesi gereken back-end kısmı unicorn değil!

Slide 30

Slide 30 text

Kaynaklar http://rack.github.io/ http://nginx.org/ http://unicorn.bogomips.org/ http://sinatrarb.com http://vagrantup.com https://github.com/vigo/owg2013-rack-unicorn-sinatra

Slide 31

Slide 31 text

http://webbox.io http://blog.webbox.io http://twitter.com/webboxio https://github.com/webBoxio http://facebook.com/webbox.io [email protected]