企业实战--Varnish详解及反向代理实现、负载均衡实现、cdn加速实现
一、Varnish详解
Varnish是什么
Varnish作用是访问web速度的web加速器,被安装在web服务器之前,从而缓存web服务器的应用程序和数据,最后相应客户的请求。 功能与Squid服务器相似,都可以用来做HTTP缓存。与Squid不同之处在于,Squid是从硬盘读取缓存的数据,而Varnish把数据存放在内存中,直接从读取内存,避免了频繁在内存、磁盘中交换文件,所以Varnish要相对更高效,但也有缺点,内存中的缓存在服务器重启后会丢失。
Varnish架构:
Varnish初始化过程
varnish启动会产生两个进程:master进程 和 child进程
master进程:master进程负责启动工作,master进程会读取配置文件,vcl文件编译,varnish监控,初始化varnish,提供varnish管理接口,根据指定的配置信息做出相应的动作,例如管理员在配置文件中分配了2G内存大小,它就会在内存中创建2G的存储空间; master进程还会创建并管理child进程。
child进程: child进程来处理后续任务,它会分配一些线程来执行不同的工作,
例如:
(1). 接受http请求
(2).为缓存对象分配存储空间
(3).清除过期缓存对象
(4). 释放空间 碎片整理
分配缓存过程
有一个对象需要缓存时,根据这个对象的大小,到空闲缓存区中查找大小最适合的空闲块,找到后就把这个对象放进去
如果这个对象没有填满这个空闲块,就把剩余的空间做为一个新的空闲块
如果空闲缓存区中没地方了,就要先删除一部分缓存来腾出地方,删除是根据最近最少使用原则。
释放缓存过程
有一个线程来负责缓存的释放工作,他定期检查缓存中所有对象的生存周期,如果某个对象在指定的时间段内没有被访问,就把这个对象删除,释放其占用的缓存空间; 释放空间后,检查一下临近的内存空间是否是空闲的,如果是,就整合为一个更大的空闲块,实现空间碎片的整理
VCL(varnish configuration Langage)语言相关:
1)、VCL概念
VCL语言被使用在default.vcl这个文件中,用来设置varnish服务器对后端web服务器加速的一些规则; vcl是一种区域配置语言。在执行vcl时,varnish会将vcl语言转化未二进制代码; default.vcl文件中的vcl语言被分为多个子程序, 不同的子程序在不同的时间点执行,比如有的子程序在接收到客户端请求时执行,有的子程序在接收到后端服务器的文件时执行。
VCL处理过程:
2)、 backend servers(后端服务器)
一般来说就代表web服务器,提供varnish加速的内容,即varnish缓存内容的源头。
在/etc/varnish/default.vcl
(varnish主配置文件)文件中会配置相关信息和规则,告诉varnish服务器应该去哪里寻找缓存的内容
3)、vcl_recv
该子程序在请求的开始被调用,在接收、解析了客户端的请求后,该子程序会决定是否响应请求、怎么响应请求以及使用哪个后台服务器响应请求。
- 修改client请求,以减少缓存决策时的差异性
- 根据client请求,决定缓存策略
- 重定向请求
- 决定处理请求的backend(即后端webserver)
4)、vcl_deliver
在缓存数据将要发送到客户端时调用
5)、actions:
主要有以下动作 :
pass:当一个请求被 pass 后,这个请求将通过 varnish 转发到后端服务器,
但是它不会被缓存。pass 可以放在 vcl_recv 和 vcl_fetch 中。
lookup:当一个请求在 vcl_recv 中被 lookup 后,varnish 将从缓存中提取数
据,如果缓存中没有数据,将被设置为 pass,不能在 vcl_fetch 中设置 lookup。
pipe:pipe 和 pass 相似,都要访问后端服务器,不过当进入 pipe 模式后,在
此连接未关闭前,后续的所有请求都发到后端服务器
deliver:请求的目标被缓存,然后发送给客户端
6)、3个重要的数据结构:Requests
、Responses
、 objects
req:请求目标,当 varnish 接收到一个请求,这时 req object 就被创建了,
你在 vcl_recv 中的大部分工作,都是在 req object 上展开的。
beresp:后端服务器返回的目标,它包含返回的头信息,你在 vcl_fetch 中的大部分工作都是在 beresp object 上开展的。
obj:被 cache 的目标,只读的目标被保存于内存中,obj.ttl 的值可修改,其他的只能读。
二、Varnish反向代理实现
本此实验需要两台虚拟机:
主机名:server1
:varnish服务器,ip:172.25.63.1
主机名:server2
:web网站服务器 ip:172.25.63.2,
物理机(客户端)
varnish下载
可在varnish官网下载:
http://varnish-cache.org/releases/index.html
本次实验所使用版本为 6.3.1
redhat安装varnish
redhat安装varnish需要解决依赖性:
yum install -y jemalloc-3.6.0-1.el7.x86_64.rpm varnish-6.3.1-1.el7.x86_64.rpm
varnish 主配置文件:/etc/varnish/default.vcl
优化varnish
为了使varnish工作在最优状态下,varnish要求(在/usr/lib/systemd/system/varnish.service
文件中查看):
- 系统中最大打开文件数为131072
- 其需要锁定的内存空间是82M
因此需要进行如下操作:
查看系统最大文件数:
[root@server1 ~]# sysctl -a | grep file
fs.file-max = 47100
fs.file-nr = 960 0 47100
fs.xfs.filestream_centisecs = 3000
达不到要求的文件数,因此首先需要增大虚拟机的内存,增大虚拟机的内存后即可实现:
[root@server1 ~]# sysctl -a | grep file
fs.file-max = 149289 #大于131072
fs.file-nr = 1056 0 149289
fs.xfs.filestream_centisecs = 3000
之后修改系统限制文件:
vim /etc/security/limits.conf
写入:
62 varnish - nofile 131072
63 varnish - memlock 82000
实现varnish反向代理
1.修改varnish主配置文件
在server1:
vim /etc/varnish/default.vcl
修改:
17 backend web1 {
18 .host = "172.25.63.2"; #web服务器ip
19 .port = "80"; #Apache端口
20 }
2.配置web服务器
在server2安装apache并编写测试页:
[root@server2 ~]# yum install httpd -y
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vim index.html
[root@server2 html]# cat index.html
server2
[root@server2 ~]# systemctl start httpd
3.修改varnish端口
用户访问web服务器也是80端口,若要做反向代理,即表示用户访问varnish服务器是80端口。故需要更改varnish端口:
在server1:
vim /usr/lib/systemd/system/varnish.service
修改:
23
24 ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/defaul t.vcl -s malloc,256m
4.打开varnish
在server1:
[root@server1 ~]# systemctl start varnish
查看开启端口:
[root@server1 ~]# netstat -antlupe | grep varnish
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 42013 2742/varnishd
tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 0 42075 2742/varnishd
tcp6 0 0 :::80 :::* LISTEN 0 42014 2742/varnishd
tcp6 0 0 ::1:6082 :::* LISTEN 0 42074 2742/varnishd
5.测试
在客户端(物理机):
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server2
反向代理成功实现
三、判断是否加速
在varnish服务器主配置文件vcl_deliver
模块中写入:
[root@server1 ~]# vim /etc/varnish/default.vcl
70 sub vcl_deliver {
71 # Happens when we have all the pieces we need, and are about to send the
72 # response to the client.
73 #
74 # You can do accounting or modifying the final object here.
75 if (obj.hits > 0) {
76 set resp.http.X-Cache = "HIT from redhat cache"; #响应报文首部
77 }
78 else {
79 set resp.http.X-Cache = "MISS from redhat cache";
80 }
81 return(deliver);
82
83
84 }
[root@server1 ~]# systemctl restart varnish #重启服务
测试:
在客户端(物理机中:)
[root@foundation63 ~]# curl -I 172.25.63.1
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 16:24:05 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770
Age: 0
Via: 1.1 varnish (Varnish/6.3)
X-Cache: MISS from redhat cache #第一次访问是MISS
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation63 ~]# curl -I 172.25.63.1
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 16:24:05 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 7 32771
Age: 1
Via: 1.1 varnish (Varnish/6.3)
X-Cache: HIT from redhat cache #之后所有访问都是HIT(若没有清除缓存)
Accept-Ranges: bytes
Connection: keep-alive
表示加速成功
四、varnish缓存清除
1.命令
[root@server1 ~]# varnishadm ban req.url "~" / #清除所有
[root@server1 ~]# varnishadm ban req.url "~" /index.html #清除指定页面
ban
表示清理缓存中满足表达式的对象
2.使用varnish操作界面
[root@server1 ~]# varnishadm
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.10.0-514.el7.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
varnish-6.3.1 revision 6e96ff048692235e64565211a38c41432a26c055
Type 'help' for command list.
Type 'quit' to close CLI session.
varnish> ban req.url ~ "/index.html" #清除缓存
200
varnish> quit
500
Closing CLI connection
五、varnish负载均衡实现
本此实验需要两台虚拟机:
主机名:server1
:varnish服务器,ip:172.25.63.1
主机名:server2
:web1网站服务器 ip:172.25.63.2,
主机名:server3
:web2网站服务器 ip:172.25.63.3,
物理机(客户端)
1.修改主配置文件
在server1中:
[root@server1 ~]# vim /etc/varnish/default.vcl
17 backend web1 {
18 .host = "172.25.63.2";
19 .port = "80";
20 }
21
22 backend web2 {
23 .host = "172.25.63.3";
24 .port = "80";
25 }
2.配置web2服务器
在server3安装apache并编写测试页:
[root@server3 ~]# yum install httpd -y
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# vim index.html
[root@server3 html]# cat index.html
server3
[root@server2 ~]# systemctl start httpd
3.编写rev_recv
模块:
[root@server1 ~]# vim /etc/varnish/default.vcl
38 sub vcl_recv {
39 # Happens before we check if we have this in cache already.
40 #
41 # Typically you clean up the request here, removing cookies you don't ne ed,
42 # rewriting the request, etc.
43 if (req.http.host ~ "^(www.)?westos.org") {
44 set req.http.host = "www.westos.org";
45 set req.backend_hint = web1;
46
47 } elsif (req.http.host ~ "^bbs.westos.org") {
48 set req.backend_hint = web2;
49 } else {
50 return (synth(405));
51 }
52 }
此时,在客户端进行测试:
[root@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl westos.org
server2
[root@foundation63 ~]# curl bbs.westos.org
server3
若无法解析域名,需要在客户端编辑域名解析文件:
[root@foundation63 ~]# vim /etc/hosts
新增:
172.25.63.1 www.westos.org bbs.westos.org westos.org
4.负载均衡
使客户端访问 www.westos.org/westos.org
时指向server2/server3
,访问bbs.westos.org
时指向server3
即让server3
负担server2
的一部分访问压力,该实验的负载均衡采用轮询机制:即每个服务器负担一次访问
要使server3发布两个页面,需要在server3上设置虚拟主机
5.设置server3虚拟主机以及发布页面
server3上:
设置虚拟主机:
[root@server3 ~]# vim /etc/httpd/conf.d/vhost.conf
写入:
<VirtualHost *:80>
DocumentRoot /www
ServerName www.westos.org
</VirtualHost>
<Directory "/www">
Require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.westos.org
</VirtualHost>
<Directory "/bbs">
Require all granted
</Directory>
新建发布页面:
[root@server2 ~]# mkdir /www
[root@server2 ~]# mkdir /bbs
[root@server3 ~]# vim /www/index.html
[root@server3 ~]# cat /www/index.html
server3:www.westos.org #为了区分因此使用和server2不同的
[root@server3 ~]# vim /bbs/index.html
[root@server3 ~]# cat /bbs/index.html
server3:bbs.westos.org
6.定义负载均衡
在server1:
[root@server1 ~]# vim /etc/varnish/default.vcl
13 vcl 4.1;
14 import directors from "/usr/lib64/varnish/vmods/libvmod_directors.so"; #导入模块
15 # Default backend definition. Set this to point to your content server.
......
27 sub vcl_init { #定义负载均衡
28 new lb = directors.round_robin();
29 lb.add_backend(web1);
30 lb.add_backend(web2);
31 }
38 sub vcl_recv {
39 # Happens before we check if we have this in cache already.
40 #
41 # Typically you clean up the request here, removing cookies you don't need,
42 # rewriting the request, etc.
43 if (req.http.host ~ "^(www.)?westos.org") {
44 set req.http.host = "www.westos.org";
45 set req.backend_hint = lb.backend(); #加入负载均衡
46 return (pass); #清理缓存
47 } elsif (req.http.host ~ "^bbs.westos.org") {
48 set req.backend_hint = web2;
49 } else {
50 return (synth(405));
51 }
52 }
保存后重启varnish:
[root@server1 ~]# systemctl restart varnish
7.测试负载均衡
在客户端(物理机):
[root@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl www.westos.org
server3:www.westos.org
[root@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl www.westos.org
server3:www.westos.org
[root@foundation63 ~]# curl bbs.westos.org
server3:bbs.westos.org
六、varnish cdn推送平台 通过bansys实现
我们在实现CDN高速缓存时有些时候通过命令等对CDN的管理有些麻烦,我们就可以使用CDN推送的方法同步后端服务内容
1.安装bansys
在varnish服务器(server1)
首先需要安装:
yum install php unzip httpd -y
unzip bansys.zip -d /var/www/html/
mv /var/www/html/bansys/* /var/www/html/
2.更改端口
varnish服务器(server1)上的80端口已经被varnish占了,因此如果还要运行apache的话就需要更改apache的端口:
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
41 #Listen 12.34.56.78:80
42 Listen 8080 #将默认的80端口改为8080
43
44 #
45 # Dynamic Shared Object (DSO) Support
3.更改bansys主配置文件
在varnish服务器(server1)
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# ls
bansys class_socket.php config.php index.php purge_action.php static
[root@server1 html]# vim config.php
[root@server1 html]# cat config.php
<?php
//varnish主机列表
//可定义多个主机列表
$var_group1 = array(
'host' => array('172.25.63.1'),
'port' => '8080',
);
//varnish群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish版本
//2.x和3.x推送命令不一样
$VAR_VERSION = "3";
?>
之后开启apache服务:
[root@server1 html]# systemctl start httpd
4.推送
在客户端浏览器输入: http://172.25.63.1:8080/
出现如下界面表示安装成功:5.更改varnish主配置文件
在varnish服务器(server1)中:
[root@server1 html]# vim /etc/varnish/default.vcl
33 acl westos {
34 "127.0.0.1";
35 "172.25.63.0"/24;
36 }
38 sub vcl_recv {
39 # Happens before we check if we have this in cache already.
40 #
41 # Typically you clean up the request here, removing cookies you don't need,
42 # rewriting the request, etc.
52 if (req.method == "BAN") {
53 if (!client.ip ~ westos) {
54 return(synth(405,"Not Allowed"));
55 }
56 ban("req.url ~ " + req.url);
57 return(purge); #取出相应缓存内容然后清除
58 }
59
60
61 }
[root@server1 html]# systemctl restart varnish #重启服务
6.推送测试
更改客户端解析:
[root@foundation63 ~]# vim /etc/hosts
[root@foundation63 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.116 www.haha.org news.haha.org music.haha.org login.westos.org
#172.25.63.1 www.westos.org bbs.westos.org westos.org #将上个实验的解析注释掉
172.25.63.1 www.westos.org #新增解析
测试:
推送前:
[root@foundation63 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:22:01 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.3)
X-Cache: MISS from redhat cache
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation63 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:22:01 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 5 3
Age: 2
Via: 1.1 varnish (Varnish/6.3)
X-Cache: HIT from redhat cache
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation63 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:22:01 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770 3
Age: 3
Via: 1.1 varnish (Varnish/6.3)
X-Cache: HIT from redhat cache #第一次MISS后之后每次都是HIT
Accept-Ranges: bytes
Connection: keep-alive
推送:
推送后:
[root@foundation63 ~]# curl -I www.westos.org
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:27:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 9
Age: 0
Via: 1.1 varnish (Varnish/6.3)
X-Cache: MISS from redhat cache #直接就是MISS
Accept-Ranges: bytes
Connection: keep-alive
7.推送特定页面
推送前:
[root@foundation63 ~]# curl -I www.westos.org/index.html
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:29:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 21
Age: 0
Via: 1.1 varnish (Varnish/6.3)
X-Cache: MISS from redhat cache #第一次是MISS,之后全是HIT
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation63 ~]# curl -I www.westos.org/index.html
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:29:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32776 22
Age: 2
Via: 1.1 varnish (Varnish/6.3)
X-Cache: HIT from redhat cache
Accept-Ranges: bytes
Connection: keep-alive
[root@foundation63 ~]# curl -I www.westos.org/index.html
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:29:34 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 24 22
Age: 3
Via: 1.1 varnish (Varnish/6.3)
X-Cache: HIT from redhat cache
Accept-Ranges: bytes
Connection: keep-alive
推送内容填写:/index.html
推送后:
[root@foundation63 ~]# curl -I www.westos.org/index.html
HTTP/1.1 200 OK
Date: Wed, 19 Feb 2020 19:31:22 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Tue, 18 Feb 2020 16:05:58 GMT
ETag: "8-59edbd8fa9ad0"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 26
Age: 0
Via: 1.1 varnish (Varnish/6.3)
X-Cache: MISS from redhat cache #直接就是MISS
Accept-Ranges: bytes
Connection: keep-alive
还没有评论,来说两句吧...