Thursday, June 14, 2012

Deploy tornado application

Goal: deploy tornado application with Nginx.

Nginx+Tornado is absolutely a perfect combination for high performance web service. I used to configure Nginx as reverse proxy and load balancer in front of multiple tornado instances.

And I use the supervisord which is a Linux process management tool to make sure tornado applications are up and running.
supervisord: http://supervisord.org/ 

1, install supervisor by python setuptool:
$easy_install supervisor
update
since Ubuntu 12.04, supervisord had been indexed in apt-get repos. Install by command
apt-get install supervisor
put your configuration with .conf file ext in /etc/supervisord/conf.d/

2, generate a config file by template
echo_supervisord_conf > /etc/supervisord.conf

3, append configuration at end of the file
(here goes a simple example)
[program:mytornado] <-----prgoram name: mytornado
command=python /home/feifan/Desktop/tor.py 888%(process_num)01d <----you'd better provide a port parameter in application entrance.
process_name=%(program_name)s_%(process_num)01d <---progress name format, "mytornado_8880" and "mytornado_8881"
redirect_stderr=true
stdout_logfile=/tmp/mytornado.log
numprocs=2 <---- how many instances

4, start processes
We've registered the tornado processes in supervisord configuration, start the service:
$supervisord
To add supervisord at startup, check out item #8
If you installed by apt-get, make sure supervisord in running by command
service supervisor status
and reload new added supervisor configuration file by command
$supervisorctl update
check process up and running by command
$supervisorctl status


5, check tornado application running on specified ports
$lsof -i:8880
$lsof -i:8881
Kill one of the instance, you will get a new instance start up :)


6 config Nginx
upstream backends{
 server 127.0.0.1:8880;
 server 127.0.0.1:8881;
}
server {
 listen 8878;
 server_name localhost;
 location /favicon.ico {
  alias /var/www/favicon.ico;
 }
 location / {
  proxy_pass_header Server;                       
  proxy_set_header Host $http_host;                       
  proxy_set_header X-Real-IP $remote_addr;                       
  proxy_set_header X-Scheme $scheme;                       
  proxy_pass http://backends;                       
  proxy_next_upstream error;
 }
 access_log /var/log/nginx/tor.access_log;
 error_log /var/log/nginx/tor.error_log;
}

7 done
$service nginx reload
Now, Nginx is listening on port 8878, and requests send to this port will proxy to tornado applications on port 8880 and 8881

8 extra
If you installed supervisor by easy_install rather than apt-get, you probably need to make supervisor works as a service(upstart system)
a) (Ubuntu)create start script at /etc/init.d/supervisord
#! /bin/bash -e

SUPERVISORD=/usr/local/bin/supervisord
PIDFILE=/tmp/supervisord.pid
OPTS="-c /etc/supervisord.conf"

test -x $SUPERVISORD || exit 0

. /lib/lsb/init-functions

export PATH="${PATH:+$PATH:}/usr/local/bin:/usr/sbin:/sbin"

case "$1" in
  start)
    log_begin_msg "Starting Supervisor daemon manager..."
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $SUPERVISORD -- $OPTS || log_end_msg 1
    log_end_msg 0
    ;;
  stop)
    log_begin_msg "Stopping Supervisor daemon manager..."
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE || log_end_msg 1
    log_end_msg 0
    ;;

  restart|reload|force-reload)
    log_begin_msg "Restarting Supervisor daemon manager..."
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
    start-stop-daemon --start --quiet --pidfile /var/run/sshd.pid --exec $SUPERVISORD -- $OPTS || log_end_msg 1
    log_end_msg 0
    ;;

  *)
    log_success_msg "Usage: /etc/init.d/supervisor
{start|stop|reload|force-reload|restart}"
    exit 1
esac

exit 0

b) make script executable
chmod +x /etc/init.d/supervisord

c) add to the startups
update-rc.d supervisord defaults

No comments:

Post a Comment