varnish基本应用(二)

2023年 7月 16日 57.1k 0

2015-06-06_212141tuopu 作为一个缓存来讲,它首先的是一个代理。varnish本身来讲http协议的反向代理,后端可以拖动多个主机来提供内容,一旦有多个主机,varnish如何从多个后端挑选一个主机来响应用户的请求。有实现定制的必要性。因此,它也能支持负载均衡调度的方式实现对后端主机的指定。
在官方文档中说明了如何实现,backend,方法非常简单、
Varnish使用多台后端主机
Varnish中可以使用director指令将一个或多个近似的后端主机定义为一个逻辑组,并可以指定的调度方式(也叫挑选方法)来轮流将请求发送至这些主机上。不同的director可以使用同一个后端主机,而某director也可以使用“匿名”后端主机(在director中直接进行定义)。每个director都必须有其专用名,且在定义后必须在VCL中进行调用,VCL中任何可以指定后端主机的位置均可以按需将其替换为调用某已定义的director。
backend web1 {
.host = "backweb";
.port = "80";
}
director webservers random {
.retries = 5;
{
.backend = web1;
.weight = 2;
}
{
.backend = {
.host = "backweb2";
.port = "80";
}
.weight = 3;
}
}
我提供了两台后端主机,着两台都可以定义成backend,和haproxy一样,后端存在的主机就是为了前端调用。一旦定义了多台主机,在vcl_recv中必然要指明用户给请求到达时,对于不同的用户请求应该由哪一个后端主机,在什么情况下由哪些主机响应。
比如说: 1,定义主机 # vim /etc/varnish/default.vcl backend web1{ .host = "172.16.249.99"; .port = "80"; } backend web2 { .host = "172.16.249.69"; .port = "80"; } OK。已经定义了两个后端主机,我们期望访问主页是由第一个后端主机响应,访问test.html时由第二个后端主机响应
2,指明后端主机 if (req.url ~"test.html"){ set req.backend = web1; }else{ set req.backend = web2; } 如果访问的是test.html由web1响应,否则全部给web2 ************************************
*一旦没有默认主机,必须指明后端主机。
************************************
3,装载配置
设置好后进行装载
[root@[node0] ~]varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 varnish> vcl.load backend default.vcl 200
VCL compiled.
varnish>vcl.use backend 200
varnish> vcl.list 200
available 0 boot
active 0 backend
varnish>
4,配置后端主机测试页面
node3:172.16.249.99:web1
[root@[node3] html]# cat index.html test.html

node3test page on web1

[root@[node3] html]#
node2:172.16.249.69:web2
[root@[node2] html]# cat index.html test.html

node2test page on web2

[root@[node2] html]#
当我们访问默认页面时跳转至node2,也就是web2,访问test.html时跳转node3,显示est page on web1 2015-06-07_141605 backend ========================================================================
&定义要使用后端主机:
& backend NAME {
& .host = 主机名或地址
& .port = 端口或服务
& }
&
& vcl_recv { 定义在引擎的某个位置
& ...
& if (CONDITION) { 指明条件,当符合条件是使用后端那个主机
& set req.backend = BE_NAME;比如指明set req.backend= BE_NAME
& }
& }
&
=========================================================================
二,动静分离
我们在node2上安装php
(node2:172.16.249.69:web2) (node3:172.16.249.99:web1) 我们希望能把动态内容请求道后端主机node2,静态到达node3
示例:
1,在noed2上安装php并且测试。
[root@[node2] html]# cat /var/www/html/index.php

[root@[node2] html]# service httpd restart
[root@[node2] html]# curl 172.16.249.69/index.php
在node0修改配置文件
1,############################################## backend static { .host = "172.16.249.99"; .port = "80"; } backend app { .host = "172.16.249.69"; .port = "80"; } 2,################################################ if (req.url ~".php$"){ set req.backend = app; }else{ set req.backend = static; } 3,#################################################
varnish>vcl.load app1 default.vcl 200
VCL compiled. varnish> vcl.use app1
200
varnish> vcl.list 200
available 0 boot
available 2 backend
active 0 app1
varnish>
打开浏览器测试,会发现php的图片不见了;很明显,它是去静态web1页面找去了。url动静分离,url指向静态内容位置。如果很不幸的出现这种现象,就需要url重写。 2015-06-07_144148动静分离 *************************************************************
三,varnish健康状态监测
Varnish可以检测后端主机的健康状态,在判定后端主机失效时能自动将其从可用后端主机列表中移除,而一旦其重新变得可用还可以自动将其设定为可用。为了避免误判,Varnish在探测后端主机的健康状态发生转变时(比如某次探测时某后端主机突然成为不可用状态),通常需要连续执行几次探测均为新状态才将其标记为转换后的状态。 硬状态:online状态,持续down好多次,无法自举 软状态:online状态,突然down up也一样,up到down需要慎重,down到up激进
每个后端服务器当前探测的健康状态探测方法通过.probe进行设定,其结果可由req.backend.healthy变量获取,也可通过varnishlog中的Backend_health查看或varnishadm的debug.health查看。 backend web1 { .host = "www.linuxea.com"; .probe = { .url = "/.healthtest.html"; .interval = 1s; .window = 5; .threshold = 2; } } .probe中的探测指令常用的有:
(1) .url:探测后端主机健康状态时请求的URL,默认为“/”;
(2) .request: 探测后端主机健康状态时所请求内容的详细格式,定义后,它会替换.url指定的探测方式;比如:
.request =
"GET /.healthtest.html HTTP/1.1"
"Host: www.linuxea.com"
"Connection: close";
(3) .window:设定在判定后端主机健康状态时基于最近多少次的探测进行,默认是8;
(4) .threshold:在.window中指定的次数中,至少有多少次是成功的才判定后端主机正健康运行;默认是3;
(5) .initial:Varnish启动时对后端主机至少需要多少次的成功探测,默认同.threshold;
(6) .expected_response:期望后端主机响应的状态码,默认为200;
(7) .interval:探测请求的发送周期,默认为5秒;
(8) .timeout:每次探测请求的过期时长,默认为2秒
开始吧:
从官方的示例:
probe healthcheck {
.url = "/status.cgi";
.interval = 60s;
.timeout = 0.3 s;
.window = 8;
.threshold = 3;
.initial = 3;
.expected_response = 200;
}
backend www {
.host = "www.example.com";
.port = "http";
.probe = healthcheck;
} 我们定义的示例: 1, # vim /etc/varnish/default.vcl probe chk { .url = "/test.html"; .window = 5; .threshold = 3; .initial = 3; .timeout = 1s; } backend static {
.host = "172.16.249.99";
.port = "80"; .probe = chk; }
backend app {
.host = "172.16.249.69";
.port = "80"; .probe = chk; }
2,进入varnish界面
varnish> vcl.load probe1 default.vcl 200
VCL compiled.
varnish>vcl.use probe1 200
varnish>backend.list 列出后端主机和后端状态监测机制 200 Backend name Refs Admin Probe web1(172.16.249.99,,80) 2 probe Healthy (no probe) web2(172.16.249.69,,80) 2 probe Healthy (no probe) static(172.16.249.99,,80) 2 probe Healthy 5/5 app(172.16.249.69,,80) 2 probe Healthy 5/5 varnish> 对于后端主机而言,5秒钟会产生一个日志信息 # tail -f /var/log/httpd/access_log - - - [07/Jun/2015:15:39:02 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:07 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:12 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:17 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:22 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:27 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:32 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:37 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:42 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:39:47 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
但是,在生成环境中监控状态监测会对系统造成大量的I/O资源,是需要关闭的。
验证健康状态监测。
到web1上
[root@[node3] html]# cd /var/www/html/
[root@[node3] html]# ls
index.html test.html
[root@[node3] html]# cat test.html

test page on web1

[root@[node3] html]# mv test.html 1.html [root@[node3] html]# tail /var/log/httpd/access_log
- - - [07/Jun/2015:15:43:39 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:43:44 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:43:49 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:43:54 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:43:59 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:44:04 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
- - - [07/Jun/2015:15:44:09 +0800] "GET /test.html HTTP/1.1" 200 27 "-" "-"
-- - [07/Jun/2015:15:44:14 +0800] "GET /test.html HTTP/1.1" 404 286 "-" "-" - - - [07/Jun/2015:15:44:19 +0800] "GET /test.html HTTP/1.1" 404 286 "-" "-" - - - [07/Jun/2015:15:44:24 +0800] "GET /test.html HTTP/1.1" 404 286 "-" "-" 这时候响应便是404了 当多次响应后,varnish认为他挂掉了:sick varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.249.99,,80) 2 probe Healthy (no probe)
web2(172.16.249.69,,80) 2 probe Healthy (no probe)
static(172.16.249.99,,80) 2 probeSick 0/5 app(172.16.249.69,,80) 2 probe Healthy 5/5
varnish> OK,我们将node3上的test.html改回来 [root@[node3] html]# mv 1.html test.html 在来查看,探测,探测中,有三次在线就可以上线了,因为在配置中interval指定过
varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.249.99,,80) 2 probe Healthy (no probe)
web2(172.16.249.69,,80) 2 probe Healthy (no probe) static(172.16.249.99,,80) 2 probe Sick 1/5 app(172.16.249.69,,80) 2 probe Healthy 5/5
varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.249.99,,80) 2 probe Healthy (no probe)
web2(172.16.249.69,,80) 2 probe Healthy (no probe) static(172.16.249.99,,80) 2 probe Sick 2/5 app(172.16.249.69,,80) 2 probe Healthy 5/5
varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.249.99,,80) 2 probe Healthy (no probe)
web2(172.16.249.69,,80) 2 probe Healthy (no probe) static(172.16.249.99,,80) 2 probe Healthy 3/5 app(172.16.249.69,,80) 2 probe Healthy 5/5
varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.249.99,,80) 2 probe Healthy (no probe)
web2(172.16.249.69,,80) 2 probe Healthy (no probe) static(172.16.249.99,,80) 2 probe Healthy 4/5 app(172.16.249.69,,80) 2 probe Healthy 5/5
varnish> backend.list
200
Backend name Refs Admin Probe
web1(172.16.249.99,,80) 2 probe Healthy (no probe)
web2(172.16.249.69,,80) 2 probe Healthy (no probe) static(172.16.249.99,,80) 2 probe Healthy 5/5 app(172.16.249.69,,80) 2 probe Healthy 5/5
varnish>
上线后,直接访问就OK
**********************************************************************
4,实现多台后端主机做负载均衡
Varnish中可以使用director指令将一个或多个近似的后端主机定义为一个逻辑组,并可以指定的调度方式(也叫挑选方法)来轮流将请求发送至这些主机上。不同的director可以使用同一个后端主机,而某director也可以使用“匿名”后端主机(在director中直接进行定义)。每个director都必须有其专用名,且在定义后必须在VCL中进行调用,VCL中任何可以指定后端主机的位置均可以按需将其替换为调用某已定义的director。
backend web1 {
.host = "backweb1.linuxea.com";
.port = "80";
}
director webservers random { random:随机调度
.retries = 5;
{
.backend = web1;
.weight = 2; weight:权重
}
{
.backend = {
.host = "backweb2.linuxea.com";
.port = "80";
}
.weight = 3;
}
}
如上示例中,web1为显式定义的后端主机,而webservers这个directors还包含了一个“匿名”后端主机(backweb2.linuxea.com)。webservers从这两个后端主机中挑选一个主机的方法为random,即以随机方式挑选。
Varnish的director支持的挑选方法中比较简单的有round-robin和random两种。其中,round-robin类型没有任何参数,只需要为其指定各后端主机即可,挑选方式为“轮叫”,并在某后端主机故障时不再将其视作挑选对象;random方法随机从可用后端主机中进行挑选,每一个后端主机都需要一个.weight参数以指定其权重,同时还可以director级别使用.retires参数来设定查找一个健康后端主机时的尝试次数。
Varnish 2.1.0后,random挑选方法又多了两种变化形式client和hash。client类型的director使用client.identity作为挑选因子,这意味着client.identity相同的请求都将被发送至同一个后端主机。client.identity默认为client.ip,但也可以在VCL中将其修改为所需要的标识符。类似地,hash类型的director使用hash数据作为挑选因子,这意味着对同一个URL的请求将被发往同一个后端主机,其常用于多级缓存的场景中。然而,无论是client还hash,当其倾向于使用后端主机不可用时将会重新挑选新的后端其机。
另外还有一种称作fallback的director,用于定义备用服务器,如下所示:
director b3 fallback {
{ .backend = www1; }
{ .backend = www2; } // will only be used if www1 is unhealthy.
{ .backend = www3; } // will only be used if both www1 and www2
// are unhealthy.
}
1,
# vim /etc/varnish/default.vcl probe chk { .url = "/test.html"; .window = 5; .threshold = 3; .initial = 3; .timeout = 1s; } backend static { .host = "172.16.249.99"; .port = "80"; .probe = chk; } backend app { .host = "172.16.249.69"; .port = "80"; .probe = chk; } 定义director
=======定义===============================================
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+如果不是要会话保持,可以随机调度,这里也不可能会话保持。
+作为缓存服务器进行调度。粘性,会话保持这里是不理想的
+++++++++++++++++++++++++++++++++++++++++++++++++++++++ directormys random { .retries = 3; { .backend =app; .weight = 1; } { .backend = static; .weight = 1; } } =====调用==================================================
2, if (req.url ~".php$"){ set req.backend = app; }else{ set req.backend =mys; } =====静态内容两台主机都可以响应,动态内容发送给app============
只要定义了,必须要调用,否则将出现语法错误。
3,
varnish> vcl.load t1 default.vcl
200
VCL compiled.
varnish> vcl.use t1
200
varnish>
4,
我门把缓存关闭,让它不要在查缓存 if (req.url ~ "/test.html"){ return(pass); } 在测试 2015-06-07_162254多台主角负载

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论