varnish-4.x源码安装配置

varnish-4.x源码安装配置

官方网站:
https://www.varnish-cache.org/
https://www.varnish-cache.org/docs/
https://www.varnish-cache.org/docs/4.0/installation/install.html
提示: 4.1源码包没有了redhat子目录(包含rhel的一些启动脚本,logrotate等人性化东西), 但可以借助官方的src rpm包来反推

varnish-book官方站点:
http://info.varnish-software.com/the-varnish-book   (pdf版需要注册后从邮件里的链接下载)

make book

make book #Builds book in PDF format
make sphinx #Builds book in HTML and ePub format
make slides #Builds slides in PDF format. This slides are to be used in a training course.
make clean #removes build/
make util/param.rst #Updates the default values of your Varnish installation. These values are the ones to be used in your book building.
make sourceupdate #Calls util/param.rst rule and will call other rules to update the sources of the book.


OS: CentOS 6.5 x64
varnish: varnish-4.0.3
以最新的稳定版4.0.3为例


源码安装
一.安装编译依赖库
RHEL:
yum -y install autoconf automake libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
yum -y install jemalloc-devel
提示:如果yum源里没有jemalloc-devel则需要源码安装jemalloc-devel
http://www.canonware.com/jemalloc/
wget https://github.com/jemalloc/jemalloc/releases/download/3.6.0/jemalloc-3.6.0.tar.bz2
tar -xvf jemalloc-3.6.0.tar.bz2 -C /usr/local/src
cd /usr/local/src/jemalloc-3.6.0 && ./configure && make -j4 && make install
ln -s /usr/local/lib/libjemalloc.so.1 /lib64/

Ubuntu:
apt-get -y install automake autotools-dev libedit-dev libjemalloc-dev libncurses-dev libpcre3-dev libtool pkg-config python-docutils python-sphinx



二.安装
wget https://repo.varnish-cache.org/source/varnish-4.0.3.tar.gz
tar -xvf varnish-4.0.3.tar.gz -C /usr/local/src/
cd /usr/local/src/varnish-4.0.3
./configure  --prefix=/opt/varnish && make -j4 && make install

三.配置
1.init脚本
cp /usr/local/src/varnish-4.0.3/redhat/varnish.initrc /etc/init.d/varnish
cp /usr/local/src/varnish-4.0.3/redhat/varnish_reload_vcl /opt/varnish/sbin
ln -s /opt/varnish/sbin/varnishd /usr/sbin
ln -s /opt/varnish/sbin/varnish_reload_vcl /usr/sbin
chmod 755 /etc/init.d/varnish
chkconfig varnish on

ln -s /opt/varnish/bin/* /usr/bin

cp /usr/local/src/varnish-4.0.3/redhat/varnishlog.initrc /etc/init.d/varnishlog
sed -i '/logfile=/c logfile="/opt/varnish/log/varnish.log"' /etc/init.d/varnishlog
mkdir /opt/varnish/log
chmod 755 /etc/init.d/varnishlog
chkconfig varnishlog on

cp /usr/local/src/varnish-4.0.3/redhat/varnishncsa.initrc /etc/init.d/varnishncsa
sed -i '/logfile=/c logfile="/opt/varnish/log/varnishncsa.log"' /etc/init.d/varnishncsa
chmod 755 /etc/init.d/varnishncsa
chkconfig varnishlog on

chown -R varnish: /opt/varnish

2.建用户varnish
useradd varnish
添加到sudo用户
varnish ALL=(ALL)       /etc/init.d/varnish*

3.配置文件
cp /usr/local/src/varnish-4.0.3/redhat/varnish.sysconfig /etc/sysconfig/varnish
可以根据实际修改监听地址和缓存大小
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
VARNISH_LISTEN_PORT=80

# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=2G

mkdir -p /etc/varnish
cp /usr/local/src/varnish-4.0.3/etc/example.vcl /etc/varnish/default.vcl
指定后端的web服务器
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "192.168.192.103";
    .port = "8080";
}

生成加密通信文件
dd if=/dev/random of=/etc/varnish/secret count=1

语法检测
varnishd -C -f /etc/varnish/default.vcl

https://www.varnish-cache.org/docs/4.0/users-guide/vcl-backends.html
示例1:多台后端主机
varnish-4.x源码安装配置

示例2:虚拟主机
varnish-4.x源码安装配置


示例3:带健康检查的负载均衡
varnish-4.x源码安装配置



varnish-4.x源码安装配置


4.启动
su - varnish
sudo /etc/init.dvarnish start

root@server3:share#netstat -tunlp|grep varnish
tcp            0 0.0.0.0:6081                0.0.0.0:*                   LISTEN      24725/varnishd
tcp            0 127.0.0.1:6082              0.0.0.0:*                   LISTEN      24723/varnishd
tcp            0 :::6081                     :::*                        LISTEN      24725/varnishd


四.管理
https://www.varnish-cache.org/docs/4.0/users-guide/vcl.html
a.varnishadm命令行接口
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082

b.telnet接口
telnet 127.0.0.1 6082

c.web接口


查看缓存状态
varnishstat
varnish-4.x源码安装配置



五.调优
1.rhel6关闭透明大页
https://www.varnish-cache.org/docs/4.0/installation/platformnotes.html
echo "never" > /sys/kernel/mm/redhat_transparent_hugepage/enabled (runtime)
 /etc/sysctl.conf (persisted.)
Varnish has a overhead of about 1KB per object


yum安装
varnish依赖jemalloc-devel,所以需要先配好epel源
yum -y install varnish jemalloc-devel



varnish-agent
提示:安装WebUI, varnish-agent需要如下依赖库,其中libvarnishapi(>=4.1)
varnish-agent官方站点:https://github.com/varnish/vagent2

yum -y install automake autoconf libcurl-devel python-docutils varnish-libs-devel libmicrohttpd-devel 
cd vagent2
./autogen.sh && ./configure && make -j4 && make install

原文地址:https://www.cnblogs.com/lixuebin/p/10814343.html