Varnish是一款高性能且開源的反向代理服務(wù)器和http加速器。與傳統(tǒng)的Squid相比,Varnish具有性能更高、速度更快、管理更方便等諸多優(yōu)點(diǎn)。作者Poul-Henning Kamp是FreeBSD的內(nèi)核開發(fā)者之一。Varnish采用全新的軟件體系架構(gòu),和現(xiàn)在的硬件提交配合緊密。在1975年時,儲存媒介只有兩種:內(nèi)存與硬盤。但現(xiàn)在計算 機(jī)系統(tǒng)的內(nèi)存除了主存外,還包括了cpu內(nèi)的L1、L2,甚至有L3快取。硬盤上也有自己的快取裝置,因此squid cache自行處理物件替換的架構(gòu)不可能得知這些情況而做到最佳化,但操作系統(tǒng)可以得知這些情況,所以這部份的工作應(yīng)該交給操作系統(tǒng)處理,這就是 Varnish cache設(shè)計架構(gòu)。
挪威最大的在線報紙 Verdens Gang () 使用3臺Varnish代替了原來的12臺squid,性能居然比以前更好,這是Varnish最成功的應(yīng)用案例。
Varnish特點(diǎn):
1.基于內(nèi)存進(jìn)行緩存,重啟后數(shù)據(jù)將消失
2.利用虛擬內(nèi)存方式,I/O性能好
3.支持設(shè)置0~60秒的精確緩存時間
4.VCL配置管理比較靈活
5.32位機(jī)器上緩存文件大小最大為2G
6.具有強(qiáng)大的管理功能,例如top、stat、admin、list等
7.狀態(tài)機(jī)設(shè)置巧妙,結(jié)構(gòu)清晰
8.利用二叉堆管理緩存文件,可達(dá)到積極刪除目的
Varnish與Squid的對比
Squid是一個高性能的代理緩存服務(wù)器,它和varnish之間有諸多的異同點(diǎn),如下:
相同點(diǎn):
都是一個反向代理服務(wù)器
都是開源軟件
不同點(diǎn),也是Varnish的優(yōu)點(diǎn):
Varnish的穩(wěn)定性很高,兩者在完成相同負(fù)荷的工作時,Squid服務(wù)器發(fā)生故障的幾率要高于Varnish,因?yàn)槭褂肧quid要經(jīng)常重啟。
Varnish訪問速度更快,Varnish采用了“Visual Page Cache”技術(shù),所有緩存數(shù)據(jù)都直接從內(nèi)存讀取,而squid是從硬盤讀取,因而Varnish在訪問速度方面會更快。
Varnish可以支持更多的并發(fā)連接,因?yàn)閂arnish的TCP連接釋放要比Squid快。因而在高并發(fā)連接情況下可以支持更多TCP連接。
Varnish可以通過管理端口,使用正則表達(dá)式批量的清除部分緩存,而Squid是做不到的。
squid屬于是單進(jìn)程使用單核CPU,但Varnish是通過fork形式打開多進(jìn)程來做處理,所以是合理的使用所有核來處理相應(yīng)的請求。
當(dāng)然,與傳統(tǒng)的Squid相比,Varnish也是有缺點(diǎn)的,如下:
varnish進(jìn)程一旦掛起、崩潰或者重啟,緩存數(shù)據(jù)都會從內(nèi)存中完全釋放,此時所有請求都會發(fā)送到后端服務(wù)器,在高并發(fā)情況下,會給后端服務(wù)器造成很大壓力。
在varnish使用中如果單個url的請求通過HA/F5(負(fù)載均衡)每次請求不同的varnish服務(wù)器中,被請求varnish服務(wù)器都會被穿透到后端,而且同樣的請求會在多臺服務(wù)器上緩存,也會造成varnish的緩存的資源浪費(fèi),也會造成性能下降。
解決方案:
綜上所述在訪問量很大的情況下推薦使用varnish的內(nèi)存緩存方式啟動,而且后面需要跟多臺squid服務(wù)器。主要為了防止前面的varnish服務(wù)、服務(wù)器被重啟的情況下,前期肯定會有很多的穿透這樣squid可以擔(dān)當(dāng)?shù)诙觕ache,而且也彌補(bǔ)了varnish緩存在內(nèi)存中重啟都會釋放的問題。
這樣的問題可以在負(fù)載均衡上做url哈希,讓單個url請求固定請求到一臺varnish服務(wù)器上,可以解決該問題。
varnish的工作流程
1、進(jìn)程之間通信
varnish啟動或有2個進(jìn)程 master(management)進(jìn)程和child(worker)進(jìn)程。master讀入存儲配置命令,進(jìn)行初始化,然后fork,監(jiān)控child。child則分配線程進(jìn)行cache工作,child還會做管理線程和生成很多worker線程。
child進(jìn)程主線程初始化過程中,將存儲大文件整個加載到內(nèi)存中,如果該文件超出系統(tǒng)的虛擬內(nèi)存,則會減少原來配置mmap大小,然后繼續(xù)加載,這時候創(chuàng)建并初始化空閑存儲結(jié)構(gòu)體,放在存儲管理的struct中,等待分配。
接著varnish某個負(fù)責(zé)接口新http連接的線程開始等待用戶,如果有新的http連接,但是這個線程只負(fù)責(zé)接收,然后喚醒等待線程池中的work線程,進(jìn)行請求處理。
worker線程讀入uri后,將會查找已有的object,命中直接返回,沒有命中,則會從后端服務(wù)器中取出來,放到緩存中。如果緩存已滿,會根據(jù)LRU算法,釋放舊的object。對于釋放緩存,有一個超時線程會檢測緩存中所有object的生命周期,如果緩存過期(ttl),則刪除,釋放相應(yīng)的存儲內(nèi)存。
2、配置文件各結(jié)構(gòu)之間通信
Varnish安裝
代碼如下:
wget
tar xzf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
cd ../
varnish-3.0.4報錯如下:
varnishadm.c:48:33: error: editline/readline.h: No such file or directory
varnishadm.c: In function 'cli_write':
varnishadm.c:76: warning: implicit declaration of function 'rl_callback_handler_remove'
varnishadm.c:76: warning: nested extern declaration of 'rl_callback_handler_remove'
varnishadm.c: In function 'send_line':
varnishadm.c:179: warning: implicit declaration of function 'add_history'
varnishadm.c:179: warning: nested extern declaration of 'add_history'
varnishadm.c: In function 'varnishadm_completion':
varnishadm.c:216: warning: implicit declaration of function 'rl_completion_matches'
varnishadm.c:216: warning: nested extern declaration of 'rl_completion_matches'
varnishadm.c:216: warning: assignment makes pointer from integer without a cast
varnishadm.c: In function 'pass':
varnishadm.c:233: error: 'rl_already_prompted' undeclared (first use in this function)
varnishadm.c:233: error: (Each undeclared identifier is reported only once
varnishadm.c:233: error: for each function it appears in.)
varnishadm.c:235: warning: implicit declaration of function 'rl_callback_handler_install'
varnishadm.c:235: warning: nested extern declaration of 'rl_callback_handler_install'
varnishadm.c:239: error: 'rl_attempted_completion_function' undeclared (first use in this function)
varnishadm.c:300: warning: implicit declaration of function 'rl_forced_update_display'
varnishadm.c:300: warning: nested extern declaration of 'rl_forced_update_display'
varnishadm.c:303: warning: implicit declaration of function 'rl_callback_read_char'
varnishadm.c:303: warning: nested extern declaration of 'rl_callback_read_char'
make[3]: *** [varnishadm-varnishadm.o] Error 1
make[3]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin/varnishadm'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/lnmp/src/varnish-3.0.4'
make: *** [all] Error 2
報錯沒找到解決方法,選varnish-3.0.3
代碼如下:
wget
tar xzf varnish-3.0.3.tar.gz
cd varnish-3.0.3
export PKG_CONFIG_PATH=http://www.3lian.com/usr/local/lib/pkgconfig
./configure --prefix=http://www.3lian.com/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking --with-jemalloc
make && make install
/usr/bin/install -m 755 ./redhat/varnish.initrc /etc/init.d/varnish
/usr/bin/install -m 644 ./redhat/varnish.sysconfig /etc/sysconfig/varnish
/usr/bin/install -m 755 ./redhat/varnish_reload_vcl /usr/local/varnish/bin
useradd -M -s /sbin/nologin varnish
代碼如下:
ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
ln -s /usr/local/varnish/bin/varnish_reload_vcl /usr/bin/
ln -s /usr/local/varnish/bin/varnishadm /usr/bin/
代碼如下:
chkconfig --add varnish
chkconfig varnish on
生成varnish管理秘鑰:
代碼如下:
uuidgen > /usr/local/varnish/etc/varnish/secret
chmod 644 /usr/local/varnish/etc/varnish/secret
修改varnish啟動配置:
代碼如下:
sed -i "s@^VARNISH_VCL_CONF=http://www.3lian.com/etc/varnish/default.vcl@#VARNISH_VCL_CONF=http://www.3lian.com/etc/varnish/default.vclnVARNISH_
VCL_CONF=http://www.3lian.com/usr/local/varnish/etc/varnish/linuxeye.vcl@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_LISTEN_PORT=6081@#VARNISH_LISTEN_PORT=6081nVARNISH_LISTEN_PORT=80@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_SECRET_FILE=http://www.3lian.com/etc/varnish/secret@#VARNISH_SECRET_FILE=http://www.3lian.com/etc/varnish/secretnVARNISH_
SECRET_FILE=http://www.3lian.com/usr/local/varnish/etc/varnish/secret@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_STORAGE_FILE=http://www.3lian.com/var/lib/varnish/varnish_storage.bin@#VARNISH_STORAGE_FILE=http://www.3lian.com/var/lib/varnis
h/varnish_storage.bin
nVARNISH_STORAGE_FILE=http://www.3lian.com/usr/local/varnish/var/varnish_storage.bin@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_STORAGE_SIZE.*@VARNISH_STORAGE_SIZE=150M@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_STORAGE=.*@VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"@" /etc/sysconfig/varnish
假設(shè)你的服務(wù)器擁有多顆邏輯處理器,還可以做以下的設(shè)置:
/etc/sysconfig/varnish 里面還可以添加自定義的參數(shù),用”-p 參數(shù)“的方式添加,如:
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT}
-f ${VARNISH_VCL_CONF}
-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT}
-t ${VARNISH_TTL}
-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT}
-u varnish -g varnish
-S ${VARNISH_SECRET_FILE}
-s ${VARNISH_STORAGE}
-p thread_pools=2" #這里為添加項(xiàng)