我的目标是为电子商务应用实现零停机部署,并且我会尽可能以最佳方式尝试此事。
我在nginx / unicorn / django设置上执行此操作,以及为单独的服务器设置nginx / unicorn / rails。
我的策略是在preload_app=true
/ guincorn.py
文件中设置unicorn.rb
,然后通过向运行服务器的PID发送USR2信号来重新加载。这会激活该过程及其子项,pre_fork
/ before_fork
可以接收此过程并发送后续的QUIT信号。
以下是我的pre_fork在guincorn版本中所做的事情的一个例子:
# ...
pidfile='/opt/run/my-website/my-website.pid'
# socket doesn't come back after QUIT
bind='unix:/opt/run/my-website/my-website.socket'
# works, but I'd prefer the socket for security
# bind='localhost:8333'
# ...
def pre_fork(server, worker):
old_pid_file = '/opt/run/my-website/my-website.pid.oldbin'
if os.path.isfile(old_pid_file):
with open(old_pid_file, 'r') as pid_contents:
try:
old_pid = int(pid_contents.read())
if old_pid != server.pid:
os.kill(old_pid, signal.SIGQUIT)
except Exception as err:
pass
pre_fork=pre_fork
这是我的sysv脚本中的一个选择,它执行重新加载:
DESC="my website"
SITE_PATH="/opt/python/my-website"
ENV_PATH="/opt/env/my-website"
RUN_AS="myuser"
SETTINGS="my.settings"
STDOUT_LOG="/var/log/my-website/my-website-access.log"
STDERR_LOG="/var/log/my-website/my-website-error.log"
GUNICORN="/opt/env/my-website/bin/gunicorn.py"
CMD="$ENV_PATH/bin/python $SITE_PATH/manage.py run_gunicorn -c $GUNICORN >> $STDOUT_LOG 2>>$STDERR_LOG"
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
run () {
if [ "$(id -un)" = "$RUN_AS" ]; then
eval $1
else
su -c "$1" - $RUN_AS
fi
}
reload () {
echo "Reloading $DESC"
sig USR2 && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$DESC' instead"
run "$CMD"
}
action="$1"
case $action in
reload)
reload
;;
esac
我选择了preload_app = true,以实现零停机时间的吸引力。由于工作人员已将应用程序预加载到内存中,因此只要我正确切换进程,就应该模拟零停机时间结果。无论如何,这就是想法。
这适用于我通过端口收听但我无法通过套接字进行操作。
我的问题如下:
preload_app=true
和HUP。upstart
而不是sysv
执行此操作?我理想地喜欢这样做,我通过植绒PID看到an interesting way of accomplishing that。对于暴发户来说这是一个挑战,因为一旦来自gunicorn / unicorn的exec-fork接管,新贵就不再监视它最初管理的流程,需要以某种方式重新建立。答案 0 :(得分:2)
你应该看一下GDS同事的unicornherder,这是专为管理这个问题而设计的:
Unicorn Herder是一个实用程序,旨在协助Upstart和类似的主管与Unicorn一起使用。