varnish代理服務器部署
Varnish與Squid的對比
說到Varnish,不能不提Squid,Squid是一個高性能的代理緩存服務器,它和varnish之間有諸多的異同點,這里分析如下:
下面是他們之間的相同點:
(1)都是一個反向代理服務器。
(2)都是開源軟件。
下面是它們的不同點,也是Varnish的優點:
(1)Varnish的穩定性很高,兩者在完成相同負荷的工作時,Squid服務器發生故障的幾率要高于Varnish,因為使用Squid要經常重啟。
(2)Varnish訪問速度更快,Varnish采用了“Visual Page Cache”技術,所有緩存數據都直接從內存讀取,而squid是從硬盤讀取,因而Varnish在訪問速度方面會更快。
(3)Varnish可以支持更多的并發連接,因為Varnish的TCP連接釋放要比Squid快。因而在高并發連接情況下可以支持更多TCP連接。
(4)Varnish可以通過管理端口,使用正則表達式批量的清除部分緩存,而Squid是做不到的。
(5) squid屬于是單進程使用單核CPU,但Varnish是通過fork形式打開多進程來做處理,所以是合理的使用所有核來處理相應的請求。
(6)Varnish的性能更高,挪威最大的在線報紙 Verdens Gang 使用3臺Varnish代替了原來的12臺Squid,可見性能比以前更好。
當然,與傳統的Squid相比,Varnish也是有缺點的,列舉如下:
1) varnish進程一旦Hang、Crash或者重啟,緩存數據都會從內存中完全釋放,此時所有請求都會發送到后端服務器,在高并發情況下,會給后端服務器造成很大壓力。
2) 在varnish使用中如果單個url的請求通過HA/F5(負載均衡)每次請求不同的varnish服務器中,被請求varnish服務器都會被穿透到后端,而且同樣的請求會在多臺服務器上緩存,也會造成varnish的緩存的資源浪費,也會造成性能下降。
解決方案:
1) 綜上 所述在訪問量很大的情況下推薦使用varnish的內存緩存方式啟動,而且后面需要跟多臺squid服務器。主要為了防止前面的varnish服務、服務 器被重啟的情況下,前期肯定會有很多的穿透這樣squid可以擔當第二層CACHE,而且也彌補了varnish緩存在內存中重啟都會釋放的問題。
2) 這樣的問題可以在負載均衡上做url哈希,讓單個url請求固定請求到一臺varnish服務器上,可以解決該問題。
注:上面的解決方法還需要全面的測試,沒有經過證實。
下面我們就來部署varnish:
主機環境: rhel6 selinux and iptables disabled
實驗主機:www.westos.org bbs.westos.org 192.168.0.50 varnish
server1.example.com 192.168.0.1 apache
server2.example.com 192.168.0.2 apache (各主機做以上解析)
VCL處理流程圖
處理過程大致分為如下幾個步驟:
(1)Receive狀態,也就是請求處理的入口狀態,根據VCL規則判斷該請求應該是Pass或Pipe,或者進入Lookup(本地查詢)。
(2)Lookup狀態,進入此狀態后,會在hash表中查找數據,若找到,則進入Hit狀態,否則進入miss狀態。
(3)Pass狀態,在此狀態下,會進入后端請求,即進入fetch狀態。
(4)Fetch狀態,在Fetch狀態下,對請求進行后端的獲取,發送請求,獲得數據,并進行本地的存儲。
(5)Deliver 狀態, 將獲取到的數據發送給客戶端,然后完成本次請求。
1. 安裝(192.168.0.50)
http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/
yum localinstall -y varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
2. 配置
# vi /etc/varnish/default.vcl
###配置一個后端服務器
backend web1 {
.host = "192.168.0.1";
.port = "80";
}
###查看緩存命中情況
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
###配置varnish服務端口
# vi /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80
# service varnish start
3.測試緩存命中
# curl -I 192.168.0.50
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Mon, 20 Aug 2012 15:22:19 GMT
ETag: "1c13aa-16-4c7b4135e08a6"Content-Type: text/html; charset=UTF-8
Content-Length: 22
Accept-Ranges: bytes
Date: Fri, 24 Aug 2012 14:30:40 GMT
X-Varnish: 766467032
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache #未命中
# curl -I 192.168.0.50
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Mon, 20 Aug 2012 15:22:19 GMT
ETag: "1c13aa-16-4c7b4135e08a6"
Content-Type: text/html; charset=UTF-8
Content-Length: 22
Accept-Ranges: bytes
Date: Fri, 24 Aug 2012 14:30:54 GMT
X-Varnish: 766467033 766467032
Age: 14
Via: 1.1 varnish
Connection: keep-alive
X-Cache:HIT from westos cache #命中
### 通過 varnishadm 手動清除緩存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除index.html頁面緩存
# varnishadm ban.url /admin/$ #清除admin目錄緩存
4.定義多個不同域名站點的后端服務器
#vim /etc/varnish/default.vcl
backend web1 {
.host = "192.168.0.1";
.port = "80";
}
backend web2 {
.host = "192.168.0.2";
.port = "80";
}
#當訪問www.westos.org域名時從web1上取數據,訪問bbs.westos.org域名時到web2取數據,訪問其他頁面報錯。
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else { error 404 "westos cache";
}
}
sub vcl_deliver {
if (obj.hits > 0){
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
# service varnish reload
網頁訪問web:www.westos.org web:bbs.westos.org則會顯示不同的內容。
5.定義負載均衡
##添加虛擬主機
vim /etc/httpd/conf/httpd.conf(192.168.0.2上的操作)
NameVirtualHost *:80
DocumentRoot /var/www/html
ServerName www.westos.org
DocumentRoot /var/www/web2 (你要自己創建這么目錄哦!)
ServerName bbs.westos.org
#echo bbs.westos.org > /var/www/web2/index.html
#service httpd restart
##定義健康檢查
probe healthcheck {
.url = "/index.html"; # 哪個 url需要varnish請求
.interval = 5s; #檢查的間隔時間
.timeout = 1s; #等待多長時間探針超時
.window = 5; #維持5個sliding window的結果
.threshold = 3; #至少有三次window是成功的,就宣告bachend健康
}
backend web1 {
.host = "192.168.0.1";
.port = "80";
.probe = healthcheck;
}
backend web2 {
.host = "192.168.0.2";
.port = "80";
.probe = healthcheck;
}
director lb round-robin { #把多個后端聚合為一個組,并檢測后端健康狀況
{ .backend = web1; }
{ .backend = web2; }
}sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
return (pass); #為了測試方便,不進行緩存。
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
}
# service varnish reload
這時web訪問http://www.westos.org/就會顯示不同的內容,即實現了負載均衡。
6.varnish cdn推送平臺
http://code.google.com/p/varnish-php-bansys/
#需要安裝php支持
# unzip bansys.zip -d /var/www/html
# vi /var/www/html/bansys/config.php #只保留如下設置,其余注釋掉(注意:注釋掉并不是刪除掉,刪除會造成推送網頁的亂碼!)
$var_group1 = array(
'host' => array('192.168.0.50'),
'port' => '6082',
);
//varnish群組定義
//對主機列表進行綁定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish版本 //2.x和3.x推送命令不一樣
$VAR_VERSION = "3";
?>
#bansys有兩種工作模式,分別是:telnet和http模式。
#telnet模式需要關閉varnish服務管理端口的驗證,注釋掉/etc/sysconfig/varnish “ 文件中的 -S ${VARNISH_SECRET_FILE}”這行,重啟varnish服務即可。
#如果是http模式需要對varnish做以下設置:
# vi /etc/varnish/default.vcl
acl westos {
#設置訪問控制
"127.0.0.1";
"192.168.0.0"/24;
}
sub vcl_recv {
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
}
修改httpd的端口號
#vim /etc/httpd/conf/httpd.conf Listen 8080(大概是136行)
# service varnish reload
##推送是為了更新cache,讓原網頁失效,這樣從外網訪問內網的時候就是最新的數據。
--leeypp@gmail.com
來自:http://my.oschina.net/leeypp1/blog/297551