linux企业实战varnish(2)

接上一篇博客

varnish进程的工作模式

varnish启动会产生两个进程,manger(管理mangerment)进程,然后fork一个worker子进程

manger进程作用:读入更新配置,vcl文件编译,varnish监控,初始化,varnish及提供varnish管理接口。Managerment进程会每个几秒探测一下Child进程以判断其是否正常运行,如果在指定的时长内未得到Child进程的回应,Managerment将会重启此Child进程

每一个入站请求流经 Varnish 时,会被 VCL 定义的策略处理。你可以通过修改 VCL 代码,改变对请求的处理。Varnish 不仅仅是一个缓存,它还是一个强大的 HTTP 处理器,你可以对 HTTP 请求做如下的事情:

  • 将某些请求调度至某些后端服务器
  • 对请求和响应进行修改
  • 根据请求和响应的任意属性,做特定的操作
    [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缓存清除

[root@server1 ~]# varnishadm ban req.url "~" /			#清除所有

[root@server1 ~]# varnishadm ban req.url "~" /index.html			#清除指定页面

使用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.254.1
主机名:server2:web1网站服务器 ip:172.25.254.2,
主机名:server3:web2网站服务器 ip:172.25.254.3,
物理机(客户端)

1.修改主配置文件

在server1中:
 

[root@server1 ~]# vim /etc/varnish/default.vcl
 17 backend web1 {
 18     .host = "172.25.254.2";
 19     .port = "80";
 20 }
 21 
 22 backend web2 {
 23     .host = "172.25.254.3";
 24     .port = "80";
 25 }

.配置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

编写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

负载均衡

使客户端访问 www.westos.org/westos.org时指向server2/server3,访问bbs.westos.org时指向server3

即让server3负担server2的一部分访问压力,该实验的负载均衡采用轮询机制:即每个服务器负担一次访问

要使server3发布两个页面,需要在server3上设置虚拟主机

设置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

定义负载均衡在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
测试负载均衡在客户端(物理机)

[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/

更改端口

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

更改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.254.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
推送在客户端浏览器输入: http://172.25.254.1:8080/出现如下界面表示安装成功:

更改varnish主配置文件在varnish服务器(server1)中:

[root@server1 html]# vim /etc/varnish/default.vcl
 33 acl westos {
 34 "127.0.0.1";
 35 "172.25.254.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				#重启服务

推送测试更改客户端解析:

[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

推送特定页面推送前:

[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
发布了102 篇原创文章 · 获赞 14 · 访问量 2404

猜你喜欢

转载自blog.csdn.net/qq_41871875/article/details/104397485