vcl处理过程
处理过程大致分为如下几个步骤:
(1)Receive 状态,也就是请求处理的入口状态,根据 VCL 规则判断该请求应该是 Pass 或
Pipe,或者进入 Lookup(本地查询)。
(2)Lookup 状态,进入此状态后,会在 hash 表中查找数据,若找到,则进入 Hit 状态,否则进
入 miss 状态。
(3)Pass 状态,在此状态下,会进入后端请求,即进入 fetch 状态。
(4)Fetch 状态,在 Fetch 状态下,对请求进行后端的获取,发送请求,获得数据,并进行本地
的存储。
(5)Deliver 状态, 将获取到的数据发送给客户端,然后完成本次请求。
1.下载rhel6.5镜像,安装虚拟机(最小安装即可)。
2.虚拟机的配置
1)修改主机名称
2)添加本地解析
3)配置网卡
4)配置yum仓库(网络yum源搭建完成)
5)安装必要的软件
yum install vim openssh-clients lftp -y
6)删除70-persistent-net.rules
rm -fr /etc/udev/rules.d/70-persistent-net.rules
1.生成三个虚拟机的快照
cd /var/lib/libvirt/images/
qemu-img create -f qcow2 -b rhel6.5.qcow2 server1
qemu-img create -f qcow2 -b rhel6.5.qcow2 server2
qemu-img create -f qcow2 -b rhel6.5.qcow2 server3
2.打开虚拟机管理器,安装快照虚拟机
3.修改虚拟机的主机名称与ip
server1 | 172.25.14.1 | varnish |
server2 | 172.25.14.2 | apache |
server3 | 172.25.14.3 | apache |
安装包:
varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
[root@server1 ~]# yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
1)查找varnish的配置文件
[root@server1 ~]# rpm -qc varnish-3.0.5-1.el6.x86_64
/etc/logrotate.d/varnish
/etc/sysconfig/varnish ##varnish的主配置文件
/etc/varnish/default.vcl
2)查看varnish的主配置文件,更改系统配置
[root@server1 ~]#vim /etc/sysconfig/varnish
查看系统配置
[root@server1 ~]# sysctl -a | grep file
fs.file-nr = 448 0 98866
fs.file-max = 98866 ##系统最大文件访问数量
[root@server1 ~]#
因为系统最大文件访问数量不满足varnish要求,所以此时更改虚拟机配置,增加内存至2048M
[root@server1 ~]# sysctl -a | grep file
fs.file-nr = 384 0 188465
fs.file-max = 188465 ##系统最大文件访问数量
[root@server1 ~]#
此时系统最大文件访问数量满足varnish要求
3)更改varnish接受请求的端口号
[root@server1 ~]# vim /etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80 ##将接收请求的端口号改为80
4)编辑varnish用户的限制文件(与varnish配置文件中限制相对应)
[root@server1 ~]# vim /etc/security/limits.conf
52 varnish - nofile 131072
53 varnish - memlock 82000
54 varnish - nproc unlimited
[root@server1 ~]# vim /etc/varnish/default.vcl ##修改调度主机ip
6 #
7 backend default {
8 .host = "172.25.14.2"; ##接受调度的主机ip
9 .port = "80";
10 }
[root@server1 ~]# /etc/init.d/varnish start ##开启varnish服务
Starting Varnish Cache: [ OK ]
[root@server1 ~]# cat /etc/passwd
varnish:x:498:499:Varnish Cache:/var/lib/varnish:/sbin/nologin ##此时系统自动创建了一个varnish的用户
[root@server1 ~]# ps aux | grep varnish ##可以查看到root与varnish的进程,root的是用于监控的进程,而系统真正运行varnish进程的是varnish的进程
root 1046 0.0 0.0 112300 1152 ? Ss 10:45 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /etc/varnish/default.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish -S /etc/varnish/secret -s file,/var/lib/varnish/varnish_storage.bin,1G
varnish 1047 0.0 0.2 2270320 5484 ? Sl 10:45 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /etc/varnish/default.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish -S /etc/varnish/secret -s file,/var/lib/varnish/varnish_storage.bin,1G
root 1161 0.0 0.0 103244 856 pts/0 S+ 10:51 0:00 grep varnish
1)server2开启httpd服务
[root@server2 ~]# yum install httpd -y ##安装httpd服务
[root@server2 ~]# vim /var/www/html/index.html ##编写httpd发布文件
www.westos.org -server2
[root@server2 ~]# /etc/init.d/httpd start ##开启httpd服务
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.14.2 for ServerName
[ OK ]
2)测试:
访问的是172.25.14.1,但是最终访问的是被调度的server2
[root@server2 ~]# curl 172.25.14.1 ##可以获得172.25.14.1指向的页面
www.westos.org -server2
[root@server1 ~]# vim /etc/varnish/default.vcl ##插入以下代码,可以再用户访问时提示是否有时访问的缓存数据
12 sub vcl_deliver {
13 if (obj.hits > 0) {
14 set resp.http.X-Cache = "HIT from westos cache"; ##当有缓存时,提示HIT
15 }
16 else {
17 set resp.http.X-Cache = "MISS from westos cache"; ##没有缓存时,提示MISS
18 }
19
20 }
[root@server1 ~]# /etc/init.d/varnish reload ##重新加载varnish服务
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2019-02-20T11:14:43
VCL compiled.
available 1 boot
active 0 reload_2019-02-20T11:14:43
Done
[root@server1 ~]# curl -I 172.25.14.1 ##-I可以查看具体的访问信息
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 20 Feb 2019 02:55:01 GMT
ETag: "7fafd-21-5824a7bed8236"
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Accept-Ranges: bytes
Date: Wed, 20 Feb 2019 03:14:58 GMT
X-Varnish: 1259047530
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache ##第一次访问是MISS,访问的时后端数据
[root@server1 ~]# curl -I 172.25.14.1 ##第二次访问的是缓存数据,此后也都是缓存数据
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 20 Feb 2019 02:55:01 GMT
ETag: "7fafd-21-5824a7bed8236"
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Accept-Ranges: bytes
Date: Wed, 20 Feb 2019 03:15:03 GMT
X-Varnish: 1259047531 1259047530
Age: 5
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
[root@server1 ~]# curl -I 172.25.14.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 20 Feb 2019 02:55:01 GMT
ETag: "7fafd-21-5824a7bed8236"
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Accept-Ranges: bytes
Date: Wed, 20 Feb 2019 03:15:06 GMT
X-Varnish: 1259047532 1259047530
Age: 9
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
[root@server1 ~]# varnishadm ban.url .*$ ##清除cdn缓存数据
[root@server1 ~]# curl -I 172.25.14.1 ##再次访问,可以查看到缓存被清除,访问的后端数据
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 20 Feb 2019 02:55:01 GMT
ETag: "7fafd-21-5824a7bed8236"
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Accept-Ranges: bytes
Date: Wed, 20 Feb 2019 03:16:00 GMT
X-Varnish: 1259047533
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@server1 ~]# curl -I 172.25.14.1 ##继续访问,又是从缓存得到的数据
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 20 Feb 2019 02:55:01 GMT
ETag: "7fafd-21-5824a7bed8236"
Content-Type: text/html; charset=UTF-8
Content-Length: 33
Accept-Ranges: bytes
Date: Wed, 20 Feb 2019 03:16:01 GMT
X-Varnish: 1259047534 1259047533
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache
主机名 | ip | 已开启的服务 |
server1 | 172.25.14.1 | varnish |
server2 | 172.25.14.2 | apache |
server3 | 172.25.14.3 |
[root@server3 ~]# vim /var/www/html/index.html
www.westos.org -server3
[root@server3 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.14.3 for ServerName
[ OK ]
[root@server3 ~]# curl 172.25.14.3
www.westos.org -server3
[root@server1 ~]# vim /etc/varnish/default.vcl
7 backend web1 { ##编辑后端服务器web1的ip与接收端口
8 .host = "172.25.14.2";
9 .port = "80";
10 }
11 backend web2 {
12 .host = "172.25.14.3"; ##后端服务器web2的ip与接收端口
13 .port = "80";
14 }
15
16 sub vcl_recv {
17 if (req.http.host ~ "^(www.)?westos.org") { ##如果访问的域名是www.开头,那么调用web1,让客户访问web1
18 set req.http.host = "www.westos.org";
19 set req.backend = web1;
20 }
21 elsif (req.http.host ~ "^bbs.westos.org") { ##如果访问的域名是bbs开头
22 set req.backend = web2; ##调用web2,让客户去访问web2
23 } else {
24 error 404 "westos cache";
25 }
26 }
27 sub vcl_deliver {
28 if (obj.hits > 0) {
29 set resp.http.X-Cache = "HIT from westos cache"; ##如果是访问的缓存数据,就显示HIT
30 }
31 else {
32 set resp.http.X-Cache = "MISS from westos cache"; ##如果访问的是后端服务器,就显示MISS
33 }
34
35 }
[root@server1 ~]# /etc/init.d/varnish reload ##重新加载varnish服务
Loading vcl from /etc/varnish/default.vcl
Current running config name is reload_2019-02-20T11:14:43
Using new config name reload_2019-02-20T11:41:46
VCL compiled.
available 0 boot
available 2 reload_2019-02-20T11:14:43
active 0 reload_2019-02-20T11:41:46
Done
[root@server1 ~]# vim /etc/hosts ##在进行测试的主机上添加本地解析
172.25.14.1 server1 www.westos.org bbs.westos.org
[root@server1 ~]# curl www.westos.org ##访问的是server2
www.westos.org -server2
[root@server1 ~]# curl bbs.westos.org ##访问的是server3
www.westos.org -server3
www.westos.org与bbs.westos.org是同一台主机,但是最终访问的后端服务器不同,调度成功
[root@server1 ~]# vim /etc/varnish/default.vcl
7 backend web1 {
8 .host = "172.25.14.2";
9 .port = "80";
10 }
11 backend web2 {
12 .host = "172.25.14.3";
13 .port = "80";
14 }
15
16 director lb round-robin { ##负载均衡,轮循模式,客户端访问lb时,将服务器web1与web2轮流提供服务
17 { .backend = web1; }
18 { .backend = web2; }
19 }
20
21 sub vcl_recv {
22 if (req.http.host ~ "^(www.)?westos.org") { ##当以域名www开头来访问服务器时,自动让客户端按照lb模式访问
23 set req.http.host = "www.westos.org";
24 set req.backend = lb;
25 return (pass); ##不保留缓存数据,表语同一台主机测试
26 }
27 elsif (req.http.host ~ "^bbs.westos.org") {
28 set req.backend = web2;
29 } else {
30 error 404 "westos cache";
31 }
32 }
33 sub vcl_deliver {
34 if (obj.hits > 0) {
35 set resp.http.X-Cache = "HIT from westos cache";
36 }
37 else {
38 set resp.http.X-Cache = "MISS from westos cache";
39 }
40
41 }
[root@server1 ~]# /etc/init.d/varnish reload ##重新加载varnish服务
Loading vcl from /etc/varnish/default.vcl
Current running config name is reload_2019-02-20T11:41:46
Using new config name reload_2019-02-20T13:24:01
VCL compiled.
available 0 boot
available 0 reload_2019-02-20T11:14:43
available 2 reload_2019-02-20T11:41:46
active 0 reload_2019-02-20T13:24:01
Done
[root@server1 ~]# curl www.westos.org
www.westos.org -server2
[root@server1 ~]# curl www.westos.org
www.westos.org -server3
[root@server1 ~]# curl www.westos.org
www.westos.org -server2
[root@server1 ~]# curl www.westos.org
www.westos.org -server3
连续访问www.westos.org ,server2与server3轮循工作
通过健康检查来确定服务器和应用的健康状况是负载均衡器 的一个非常重要的功能,没有负载负载均衡器,客户端可能会将请求发送到已经停机的服务器上。网络管理员必须手动干预替换掉这台服务器或者排除这台服务器的 故障。有时服务器可能没有停机,由于某种原因,比如说软件的漏洞问题,导致服务器上运行的应用系统不能正常运行;此时,负载均衡器能够检测这些情况并立即将客户端的请求定向到正常的服务器而不需要管理员的手动干预。
[root@server2 ~]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
[root@server1 ~]# curl www.westos.org ##此时server2服务停止,调度器server1自动停止对server2的调度
www.westos.org -server3
[root@server1 ~]# curl www.westos.org
www.westos.org -server3
[root@server1 ~]# curl www.westos.org
www.westos.org -server3
[root@server1 ~]#