varnish部署笔记
1、varnish-3.0.3安装脚本
#!/bin/bash
#安装依赖
yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
#下载源代码到/tmp/varnish-tar.gz
wget -nc -P /tmp http://repo.varnish-cache.org/source/varnish-tar.gz
tar -zxvf /tmp/varnish-tar.gz -C /tmp
cd /tmp/varnish-3
#自动编译
./autogen
./configure --prefix=/usr/local/varnish
make
make install
#添加启动用户组
/usr/sbin/groupadd varnish
/usr/sbin/useradd -g varnish -d /var/varnish -s /sbin/nologin varnish
#创建缓存和日志目录
mkdir -p /data/varnish/cache
mkdir -p /data/varnish/logs
chown -R varnish:varnish /data/varnish/
chmod 775 -R /data/varnish
#复制启动脚本和配置文件
cp -f redhat/varnish.initrc /etc/init.d/varnish
cp -f redhat/varnish.sysconfig /etc/sysconfig/varnish
cp -f redhat/varnish_reload_vcl /usr/local/varnish/bin
chmod 755 /etc/init.d/varnish
#声明启动服务
/sbin/chkconfig --add varnish
/sbin/chkconfig --level 2345 varnish on
2、修改/etc/init.d/varnish配置文件
# 我们修改了varnish启动脚本,除修改相关路径之外,还添加了varnish日志进程的启动关闭功能
#! /bin/sh
# Source function library.
. /etc/init.d/functions
retval=0
pidfile=/var/run/varnish.pid
pidlogfile=/var/run/varnish_log.pid
#exec="/usr/sbin/varnishd"
exec="/usr/local/varnish/sbin/varnishd"
#reload_exec="/usr/bin/varnish_reload_vcl"
log_exec="/usr/local/varnish/bin/varnishncsa"
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"
prog="varnishd"
prog_log="varnishncsa"
#config="/etc/sysconfig/varnish"
config="/usr/local/varnish/etc/varnish/varnish.cfg"
lockfile="/var/lock/subsys/varnish"
locklogfile="/var/lock/subsys/varnish_log"
# Include varnish defaults
#[ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish
[ -e /usr/local/varnish/etc/varnish/varnish.cfg ] && . /usr/local/varnish/etc/varnish/varnish.cfg
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 Cache: "
# 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 --pidfile $pidfile $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
retval=$?
if [ $retval -eq 0 ]
then
touch $lockfile
echo_success
echo
else
echo_failure
echo "varnish start failure"
return $retval
fi
fi
if [ -f $pidfile ]
then
echo "Starting Varnish Logging: "
daemon --pidfile $pidlogfile "$log_exec" -P $pidlogfile -w "$LOG_FILE" -n "$LOG_CACHE" &> /dev/null 2>&1
sleep 3
if [ ! -f $pidlogfile ]
then
echo_failure
echo
echo -n "Stopping Varnish Cache: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] &&rm -f $lockfile
return $retval
else
touch $locklogfile
echo_success
echo
fi
fi
}
stop() {
echo -n "Stopping Varnish Logging: "
killproc -p $pidlogfile $prog_log
retval_log=$?
echo
[ $retval_log -eq 0 ] &&rm -f $locklogfile
echo -n "Stopping Varnish Cache: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] &&rm -f $lockfile
return $retval
}
……
rh_status() {
status -p $pidfile $prog
status -p $pidlogfile $prog_log
}
3、添加/usr/local/varnish/etc/varnish/varnish.cfg配置文件
#红色的是比较重要的性能参数,最后两行中的变量会被/etc/init.d/varnish使用
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#
# Maximum number of open files (for ulimit -n)
NFILES=131072
# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=1000000
## Alternative 2, Configuration with VCL
DAEMON_OPTS="-a 0.0.0.0:8089 \
-f /usr/local/varnish/etc/varnish/default.vcl \
-T 127.0.0.1:3500 \
-w 10,5120,10 \
-t 3600s \
-p thread_pools=4 \
-p listen_depth=4096 \
-u varnish -g varnish \
-n /data/varnish/cache \
-s file,/data/varnish/cache/varnish_cache.data,4G"
LOG_FILE="/data/varnish/logs/varnish.log"
LOG_CACHE="/data/varnish/cache"
4、VCL配置 (略)
5、日志切割脚本(bin/cut_varnish_log.sh)
#!/bin/sh
HOME_PATH=/data/varnish
LOG_FILE=${HOME_PATH}/logs/varnish.log
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mv ${LOG_FILE} ${HOME_PATH}/logs/varnish-${date}.log
/usr/local/varnish/bin/varnishncsa -P /var/run/varnish_log.pid -w /data/varnish/logs/varnish.log -n /data/varnish/cache &
#每天0点运行
echo "00 0 * * * /usr/local/varnish/bin/cut_varnish_log.sh" >> /etc/crontab
6、内核参数优化(编辑/etc/sysctl.conf)
#此优化比较必要,在我们的环境中,性能差距(主要是吞吐量)可达到3-5倍
net.ipv4.ip_local_port_range = 1024 65536
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_recycle = 1
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save = 1
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
运行/sbin/sysctl –p
7、ganglia-varnish安装
#确保/usr/local/ganglia/lib/ganglia/modpython.so和/etc/ganglia/conf.d/modpython.conf存在且有效挂载
#通过/usr/local/ganglia/sbin/gmond –d10来验证
#下载gmond-python插件
wget -nc -P /tmp https://github.com/ganglia/gmond_python_modules/archive/master.tar.gz
cd /tmp
tar -zxvf gmond_python_modules-master.tar.gz
cd gmond_python_modules-master/varnish/
cp python_modules/varnish.py /usr/local/ganglia/lib64/ganglia/python_modules/
cp conf.d/varnish.pyconf /etc/ganglia/conf.d/
sed -i "s/\varnishstat -1/\/usr\/local\/varnish\/bin\/varnishstat -1 -n \/data\/varnish\/cache/g" /etc/ganglia/conf.d/varnish.pyconf
#通过/usr/local/ganglia/sbin/gmond -m来验证配置有效性
/sbin/service gmond restart
浙公网安备 33010602011771号