Varnish的部署與使用
腳本及源碼安裝包鏈接
https://github.com/Liuhaiyuan/Varnish_Install.git
github腳本鏈接
概述
- Varnish是一款高性能且開(kāi)源的反向代理服務(wù)器和http加速器
- 與傳統(tǒng)的Squid相比,Varnish具有性能更高,速度更快闽瓢,管理更方便等諸多優(yōu)點(diǎn)尤揣。
編譯安裝
這里展示腳本文件的一部分
tar -xf $INSTALL_FILE
cd $CODE_DIR
[ -f configure ] && ./configure --prefix=/usr/local/varnish &> $NULL || exit 40
echo "configure success."
make &> $NULL
[ $? -eq 0 ] && echo "make success." || exit 41
make install &> $NULL
[ $? -eq 0 ] && echo "make install success." || exit 42
由于這是源碼包安裝疙筹,所以安裝后并不能像其他服務(wù)那樣會(huì)在etc下有相應(yīng)的配置文件等隔盛,
雖然這些文件在源碼包中都有呕臂,所以很明顯我們需要將一些對(duì)應(yīng)的文件進(jìn)行操作。
##進(jìn)行配置文件的復(fù)制操作查坪;
cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
cp redhat/varnish_reload_vcl /usr/bin/
ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
mkdir /etc/varnish
cp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/
uuidgen > /etc/varnish/secret
文件的具體說(shuō)明:
- /etc/varnish/ :配置文件目錄
- /etc/init.d/varnish :varnish的啟動(dòng)程序
- /etc/sysconfig/varnish :配置文件寸宏,varnish定義自身屬性
- /etc/varnish/default.vcl :默認(rèn)配置文件,定義后端節(jié)點(diǎn)的
- /usr/bin/varnish_reload_vcl :加載vcl
- /usr/bin/varnishadm : 客戶(hù)端程序
- /usr/bin/varnishstat :狀態(tài)監(jiān)控
配置文件參數(shù)說(shuō)明:
[root@svr5 ~]# vim /etc/sysconfig/varnish
VARNISH_VCL_CONF=/etc/varnish/default.vcl #vcl文件路徑
VARNISH_LISTEN_PORT=80 #默認(rèn)端口
VARNISH_SECRET_FILE=/etc/varnish/secret #密鑰文件
VARNISH_STORAGE_SIZE=64M #緩存大小
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" #基于內(nèi)存方式
[root@svr5 ~]# vim /etc/varnish/default.vcl
backend default {
.host = "192.168.4.205"; ##后端web服務(wù)器的ip
.port = "80"; ##后端web服務(wù)器的httpd使用的端口號(hào)
}
操作實(shí)例
實(shí)例需求
通過(guò)配置Varnish緩存服務(wù)器偿曙,實(shí)現(xiàn)如下目標(biāo):
- 使用Varnish加速后端Apache Web服務(wù)
- 使用varnishadm管理緩存頁(yè)面
- 使用varnishstat查看Varnish狀態(tài)
具體操作
使用3臺(tái)RHEL6虛擬機(jī)氮凝,其中一臺(tái)Web服務(wù)器,一臺(tái)Varnish代理服務(wù)器望忆,一臺(tái)作為測(cè)試用的Linux客戶(hù)機(jī)罩阵。
這幾臺(tái)服務(wù)器都不需要配網(wǎng)關(guān),客戶(hù)端計(jì)算機(jī)和web服務(wù)器不能之間ping通启摄,具體的ip地址見(jiàn)下文稿壁。
首先在進(jìn)行web服務(wù)器的搭建,這里我們進(jìn)行最簡(jiǎn)單的httpd的服務(wù)器就可以了歉备,以便于測(cè)試傅是。
[root@web02 ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 54:52:01:01:16:02
inet addr:192.168.2.16 Bcast:192.168.2.255 Mask:255.255.255.0
[root@web02 ~]# yum clean all
[root@web02 ~]# yum repolist
...
repolist: 3,819
[root@web02 ~]# yum -y install httpd
[root@web02 ~]# echo "This is index.html." > /var/www/html/index.html
[root@web02 ~]# service httpd restart
停止 httpd: [失敗]
正在啟動(dòng) httpd:httpd: apr_sockaddr_info_get() failed for web02.wolf.cn
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[確定]
[root@web02 ~]# chkconfig httpd on
//測(cè)試web服務(wù),測(cè)試web服務(wù)正常
[root@web02 ~]# curl http://192.168.2.16
This is index.html.
然后進(jìn)行代理服務(wù)器的配置和搭建蕾羊,這里使用自己編寫(xiě)的腳本進(jìn)行服務(wù)的安裝和部署喧笔,
腳本文件和源碼包都放在gethub上了,鏈接:
[root@proxy02 varnish]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 54:52:01:01:13:01
inet addr:192.168.4.6 Bcast:192.168.4.255 Mask:255.255.255.0
[root@proxy02 varnish]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 54:52:01:01:13:02
inet addr:192.168.2.6 Bcast:192.168.2.255 Mask:255.255.255.0
[root@proxy02 varnish]# ll
total 2008
-rwxr-xr-x. 1 root root 2969 Jan 5 12:27 install_varnish.sh
-rw-r--r--. 1 root root 2049810 Jan 5 09:47 varnish-3.0.6.tar.gz
[root@proxy02 varnish]# ./install_varnish.sh
configure success.
make success.
make install success.
//對(duì)于對(duì)應(yīng)的配置文件的修改肚豺,都在腳本中使用sed進(jìn)行修改溃斋,
//在這里就直接開(kāi)啟服務(wù),并設(shè)置為開(kāi)機(jī)啟動(dòng)即可吸申。
[root@proxy02 varnish]# service varnish restart
Stopping Varnish Cache: [確定]
Starting Varnish Cache: [確定]
[root@proxy02 varnish]# chkconfig varnish on
[root@proxy02 varnish]# chkconfig varnish --list
varnish 0:off 1:off 2:on 3:on 4:on 5:on 6:off
最后進(jìn)行客戶(hù)端的測(cè)試即可梗劫。
[root@client02 ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 54:52:01:01:15:01
inet addr:192.168.4.15 Bcast:192.168.4.255 Mask:255.255.255.0
//這里就可以看到我們?cè)L問(wèn)的是代理服務(wù)器的ip就可以訪問(wèn)web服務(wù)
[root@client02 ~]# curl http://192.168.4.6
This is index.html.
當(dāng)對(duì)網(wǎng)頁(yè)的信息更新頻率要求很高時(shí),就可以使用下列命令進(jìn)行設(shè)置
[root@proxy02 varnish]#ln -s /usr/local/varnish/bin/* /usr/bin/
[root@proxy02 varnish]#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url index.html
[root@proxy02 varnish]#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url ".*"
附腳本全文
#!/bin/bash
#LANG=en_US.UTF-8
# exit code
# exit 39 yum error
# exit 40 configure error
# exit 41 make error
# exit 42 make install errot
#static variable
INSTALL_FILE="varnish-3.0.6.tar.gz"
CODE_DIR=$(tar -tf $INSTALL_FILE | head -1)
NULL=/dev/null
test_yum () {
#set yum configure file do not display Red Hat Subscription Management info.
if [ -f /etc/yum/pluginconf.d/subscription-manager.conf ];then
sed -i '/enabled/s/1/0/' /etc/yum/pluginconf.d/subscription-manager.conf
fi
yum clean all &>$NULL
repolist=$(yum repolist 2>/dev/null |awk '/repolist:/{print $2}'|sed 's/,//')
if [ $repolist -le 0 ];then
exit 39
fi
}
test_yum
##安裝variable需要依賴(lài)包
yum -y install gcc* readline-devel pcre-devel &> $NULL
##添加對(duì)應(yīng)用戶(hù)截碴,以該用戶(hù)進(jìn)行操作后續(xù)
useradd -s /sbin/nologin varnish
tar -xf $INSTALL_FILE
cd $CODE_DIR
[ -f configure ] && ./configure --prefix=/usr/local/varnish &> $NULL || exit 40
echo "configure success."
make &> $NULL
[ $? -eq 0 ] && echo "make success." || exit 41
make install &> $NULL
[ $? -eq 0 ] && echo "make install success." || exit 42
##進(jìn)行配置文件的復(fù)制操作梳侨;
cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
cp redhat/varnish_reload_vcl /usr/bin/
ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
mkdir /etc/varnish
cp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/
uuidgen > /etc/varnish/secret
#最大的線程數(shù)和最小線程數(shù)受計(jì)算機(jī)的配置有關(guān),其中什么時(shí)候增加進(jìn)程日丹,在有些配置文件會(huì)添加一個(gè)空
#閑線程的參數(shù)走哺,通過(guò)該參數(shù)進(jìn)行相關(guān)的操作
#VARNISH_ADMIN_LISTEN_ADDRESS 最小線程數(shù)
#VARNISH_MAX_THREADS=1000 最大線程數(shù)
#VARNISH_STORAGE_SIZE=64M 緩存大小,緩存大小受業(yè)務(wù)和緩存總?cè)萘坑绊憽?#VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" 使用內(nèi)存緩存頁(yè)面,內(nèi)存大小為64M,
#還可以使用硬盤(pán)進(jìn)行緩存操作哲虾,不過(guò)那樣速度會(huì)慢丙躏,無(wú)法體現(xiàn)出varnish的優(yōu)勢(shì)
# VARNISH_VCL_CONF=/etc/varnish/default.vcl
#sed -n '/VARNISH_VCL_CONF=/p' /etc/sysconfig/varnish
#VARNISH_LISTEN_PORT 默認(rèn)端口,修改為httpd默認(rèn)的端口
sed -i '/VARNISH_LISTEN_PORT=/ s/=.*/=80/' /etc/sysconfig/varnish
sed -i '/VARNISH_MIN_THREADS=/ s/=.*/=1000/' /etc/sysconfig/varnish
sed -i '/VARNISH_MAX_THREADS=/ s/=.*/=10000/' /etc/sysconfig/varnish
sed -i '/VARNISH_STORAGE_SIZE=/ s/=.*/=128M/' /etc/sysconfig/varnish
sed -i '/VARNISH_STORAGE=/ s/=.*/="malloc,${VARNISH_STORAGE_SIZE}"/' /etc/sysconfig/varnish
#修改主配置文件(定義后臺(tái)服務(wù)器)default.vcl
#backend default {
# .host = "192.168.2.100";
# .port = "80";
# }
sed -i '/backend default/ s/.*back/back/' /etc/varnish/default.vcl
sed -i '/.host =/ s/.*\.host =.*/ .host = "192.168.2.16";/' /etc/varnish/default.vcl
sed -i '/.port =/ s/.*\.port =.*/ .port = "80";/' /etc/varnish/default.vcl
sed -i '/.port =/a }' /etc/varnish/default.vcl
#當(dāng)對(duì)網(wǎng)頁(yè)的信息更新頻率要求很高時(shí),就可以使用下列命令進(jìn)行設(shè)置
#ln -s /usr/local/varnish/bin/* /usr/bin/
#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url index.html
#varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 ban.url ".*"