4月 272010
Varnishで再起動を伴わずに設定の再読込を行いたいときはvarnishadmかtelnetで入って以下のような感じでコマンドを打たないといけません
varnishadm -T 192.168.1.123:6082 vcl.load testvcl /etc/varnish/default.vcl varnishadm -T 192.168.1.123:6082 vcl.use testvcl
正直めんどくさいです
かといって普通に/etc/init.d/varnish reloadとかやると再起動していしまいせっかく溜め込んだキャッシュがクリアされてしまいます
/etc/init.d/varnishの一部から
restart() {
stop
start
}
reload() {
restart
}
ということでgracefulオプションを追加してreloadもそれに割り当ててみました
/etc/init.d/varnish
#! /bin/sh
#
# varnish Control the varnish HTTP accelerator
#
# chkconfig: - 90 10
# description: Varnish is a high-perfomance HTTP accelerator
# processname: varnishd
# config: /etc/sysconfig/varnish
# pidfile: /var/run/varnish/varnishd.pid
### BEGIN INIT INFO
# Provides: varnish
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog
# Short-Description: start and stop varnishd
# Description: Varnish is a high-perfomance HTTP accelerator
### END INIT INFO
# Source function library.
. /etc/init.d/functions
retval=0
pidfile=/var/run/varnish.pid
exec="/usr/sbin/varnishd"
prog="varnishd"
config="/etc/sysconfig/varnish"
lockfile="/var/lock/subsys/varnish"
################################
ADM=`ps axut | grep varnishd|grep -v grep |head -n 1|sed -e "s/$/ /g"|sed -e "s/^.\+-T \+\([^ ]\+\).\+$/\1/g"`
VCL=`ps axut | grep varnishd|grep -v grep |head -n 1|sed -e "s/$/ /g"|sed -e "s/^.\+-f \+\([^ ]\+\).\+$/\1/g"`
NOW=`date +%s`
################################
# Include varnish defaults
[ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish
start() {
if [ ! -x $exec ]
then
echo $exec not found
exit 5
fi
if [ ! -f $config ]
then
echo $config not found
exit 6
fi
echo -n "Starting varnish HTTP accelerator: "
# Open files (usually 1024, which is way too small for varnish)
ulimit -n ${NFILES:-131072}
# Varnish wants to lock shared memory log in memory.
ulimit -l ${MEMLOCK:-82000}
# $DAEMON_OPTS is set in /etc/sysconfig/varnish. At least, one
# has to set up a backend, or /tmp will be used, which is a bad idea.
if [ "$DAEMON_OPTS" = "" ]; then
echo "\$DAEMON_OPTS empty."
echo -n "Please put configuration options in $config"
return 6
else
# Varnish always gives output on STDOUT
daemon $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
retval=$?
if [ $retval -eq 0 ]
then
touch $lockfile
echo_success
echo
else
echo_failure
fi
return $retval
fi
}
stop() {
echo -n "Stopping varnish HTTP accelerator: "
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
graceful() {
echo reload VCL file: $VCL
varnishadm -T $ADM vcl.load reload$NOW $VCL || error
varnishadm -T $ADM vcl.use reload$NOW || error
echo Current configs:
echo_success
echo
varnishadm -T $ADM vcl.list
}
error(){
echo_failure
echo
exit 1
}
restart() {
stop
start
}
reload() {
graceful
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# See how we were called.
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
graceful)
$1
;;
reload)
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|graceful|force-reload}"
exit 2
esac
exit $?
元ファイルとのDIFF
[root@LIP-APP-01 ~]# diff /etc/init.d/varnish varnish
30,34c30
< ################################
< ADM=`ps axut | grep varnishd|grep -v grep |head -n 1|sed -e "s/$/ /g"|sed -e "s/^.\+-T \+\([^ ]\+\).\+$/\1/g"`
< VCL=`ps axut | grep varnishd|grep -v grep |head -n 1|sed -e "s/$/ /g"|sed -e "s/^.\+-f \+\([^ ]\+\).\+$/\1/g"`
< NOW=`date +%s`
< ################################
---
>
90,103c86
< graceful() {
< echo reload VCL file: $VCL
< varnishadm -T $ADM vcl.load reload$NOW $VCL || error
< varnishadm -T $ADM vcl.use reload$NOW || error
< echo Current configs:
< echo_success
< echo
< varnishadm -T $ADM vcl.list
< }
< error(){
< echo_failure
< echo
< exit 1
< }
---
>
110c93
< graceful
---
> restart
138,140d120
< graceful)
< $1
< ;;
141a122
> rh_status_q || exit 7
155c136
< echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|graceful|force-reload}"
---
> echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
これで
/etc/init.d/varnish graceful
をすることでいま読み込んでる設定ファイルを再読み込みします
実行結果
[root@LIP-APP-01 ~]# date;ps axut | grep varnish;/etc/init.d/varnish reload;ps axut | grep varnish
2010年 4月 27日 火曜日 20:45:04 JST
root 27984 0.0 0.1 106492 1096 ? Ss 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
varnish 27985 0.0 0.3 281728 3604 ? Sl 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
root 28135 0.0 0.0 65368 828 pts/0 S+ 20:45 0:00 grep varnish
reload VCL file: /etc/varnish/default.vcl
VCL compiled.
Current configs:
[ OK ]
available 0 boot
available 0 reload1272368689
available 1 reload1272368699
active 0 reload1272368704
root 27984 0.0 0.1 106492 1100 ? Ss 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
varnish 27985 0.0 0.3 283796 3620 ? Sl 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
root 28164 0.0 0.0 65368 836 pts/0 S+ 20:45 0:00 grep varnish
[root@LIP-APP-01 ~]#
失敗の場合もプロセスは死んでない
[root@LIP-APP-01 ~]# date;ps axut | grep varnish;/etc/init.d/varnish reload;ps axut | grep varnish
2010年 4月 27日 火曜日 20:47:05 JST
root 27984 0.0 0.1 106492 1100 ? Ss 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
varnish 27985 0.0 0.3 283796 3620 ? Sl 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
root 28241 0.0 0.0 65368 832 pts/0 S+ 20:47 0:00 grep varnish
reload VCL file: /etc/varnish/default.vcl
Command failed with error code 106
[失敗]
root 27984 0.0 0.1 106492 1100 ? Ss 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
varnish 27985 0.0 0.3 283796 3620 ? Sl 20:43 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 192.168.1.123:6082 -f /etc/varnish/default.vcl -u varnish -g varnish -s malloc,700M
root 28263 0.0 0.0 65368 836 pts/0 S+ 20:47 0:00 grep varnish
[root@LIP-APP-01 ~]#
見てもらえばわかるとおもうのですが
動くのが第一目的だったためPORTの取得とかをかなり強引な方法(grepとかのあわせ技)でやりましたので(デフォルトのsysconfigでいけたらいいなとおもったので・・・)
不具合とか出たら教えてもらえるとありがたいなーと
参考サイト
Easy reloading of varnish’ VCL
sysconfigに変数定義できれば楽なんだよなーそのバージョンも一応書こうかしら?
[…] This post was mentioned on Twitter by iara, \いわなちゃん/. \いわなちゃん/ said: [blog] Varnishで再起動無しで設定ファイルを適用する方法(reload) http://is.gd/bJIcO […]