varnish设定多个后端主机和健康监测

2018-08-04  本文已影响108人  任总

一、varnish设定多个后端主机示例

[root@varnish50 ~]# vim /etc/varnish/default.vcl 
.....
# new 4.0 format.

vcl 4.0;
import directors;#调用模块

.......
# Default backend definition. Set this to point to your content server.
backend websrv1 {
    .host = "192.168.1.11";
    .port = "80";
}
backend websrv2 {
    .host = "192.168.1.12";
    .port = "80";
}
sub vcl_init {   #定义后端服务器组
       new websrvs = directors.round_robin();
       websrvs.add_backend(websrv1);
       websrvs.add_backend(websrv2);
}
sub vcl_recv {
    set req.backend_hint = websrvs.backend();#使用服务器组

vcl.load test9 default.vcl
200        
VCL compiled.
 vcl.use test9
200        
VCL 'test9' now active
[root@vs ~]# curl -X BAN  http://192.168.1.50/index.html#清理缓存

[root@vs ~]# curl http://192.168.1.50/index.html
<h1>Backend Server1</h1>#后端服务器1
[root@vs ~]# curl -X BAN  http://192.168.1.50/index.html#清理缓存

[root@vs ~]# curl http://192.168.1.50/index.html
<h1>Backend Server2</h1>#后端服务器2

二、不同内容指向不同主机示例:

[root@varnish50 ~]# vim /etc/varnish/default.vcl 
......
    backend imgsrv1 {
        .host = "192.168.10.11";
        .port = "80";
    }
    
    backend imgsrv2 {
        .host = "192.168.10.12";
        .port = "80";
    }   
    
    backend appsrv1 {
        .host = "192.168.10.21";
        .port = "80";
    }
    
    backend appsrv2 {
        .host = "192.168.10.22";
        .port = "80";
    }
    
    sub vcl_init {
        new imgsrvs = directors.random();#图片组
        imgsrvs.add_backend(imgsrv1,10);
        imgsrvs.add_backend(imgsrv2,20);
        
        new staticsrvs = directors.round_robin();#静态资源组
        appsrvs.add_backend(appsrv1);
        appsrvs.add_backend(appsrv2);
        
        new appsrvs = directors.hash();
        appsrvs.add_backend(appsrv1,1);
        appsrvs.add_backend(appsrv2,1);     
    }
    
    sub vcl_recv {
        if (req.url ~ "(?i)\.(css|js)$" {
            set req.backend_hint = staticsrvs.backend();
        }       
        if (req.url ~ "(?i)\.(jpg|jpeg|png|gif)$" {
            set req.backend_hint = imgsrvs.backend();
        } else {        
            set req.backend_hint = appsrvs.backend(req.http.cookie);#其他内容cooki
        }
    }

三、基于cookie的session sticky:

[root@varnish50 ~]# vim /etc/varnish/default.vcl 
......
                sub vcl_init {
                    new h = directors.hash();
                    h.add_backend(one, 1);   // backend 'one' with weight '1'
                    h.add_backend(two, 1);   // backend 'two' with weight '1'
                }

                sub vcl_recv {
                    // pick a backend based on the cookie header of the client
                    set req.backend_hint = h.backend(req.http.cookie);
                }               

四、后端主机的健康监测:

.probe:定义健康状态检测方法;
.url:检测时要请求的URL,默认为”/";
.request:发出的具体请求;
.request =
"GET /.healthtest.html HTTP/1.1"
"Host: www.magedu.com"
"Connection: close"
.window:基于最近的多少次检查来判断其健康状态;
.threshold:最近.window中定义的这么次检查中至有.threshhold定义的次数是成功的;
.interval:检测频度;
.timeout:超时时长;
.expected_response:期望的响应码,默认为200;

健康状态检测的配置方式:

格式:
(1) probe PB_NAME { }
backend NAME = {
.probe = PB_NAME;
...
}
(2) backend NAME {
.probe = {
...
}
}
probe www_probe {
.url = "/index.html";
.timeout = 1s;
.interval = 1s;
.window = 8;
.threshold = 5;
}

[root@varnish50 ~]# vim /etc/varnish/default.vcl 
# Default backend definition. Set this to point to your content server.
backend websrv1 {
    .host = "192.168.1.11";
    .port = "80";
    .probe ={  #方法一:设置监测项目
            .url = "/index.html";
            .timeout = 1s;
            .interval = 1s;
            .window = 8;
            .threshold = 5;

}
}
backend websrv2 {
    .host = "192.168.1.12";
    .port = "80";
    .probe =www_probe;#方法二:调用www_probe
}

[root@varnish50 ~]# varnishadm  -S /etc/varnish/secret -T 127.0.0.1:6082 
200        
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.10.0-693.el7.x86_64,x86_64,-sfile,-smalloc,-hcritbit
varnish-4.0.5 revision 07eff4c29

Type 'help' for command list.
Type 'quit' to close CLI session.

vcl.load test10 default.vcl#编译
200        
VCL compiled.
vcl.use test10#使用
200        
VCL 'test10' now active#活动状态
backend.list   #查询后端主机状态
200        
Backend name                   Refs   Admin      Probe
default(192.168.1.12,,80)      8      probe      Healthy (no probe)
websrv1(192.168.1.11,,80)      2      probe      Healthy 8/8#后端1服务器8次监测都成功
websrv2(192.168.1.12,,80)      2      probe      Healthy 8/8#后端2服务器8次监测都成功
backend.set_health websrv2 Sick#自定义后端主机健康状态为sick或Healthy
200        

backend.list#查询后端主机状态
200        
Backend name                   Refs   Admin      Probe#后端名称、引用次数、检测状态、检测次数
default(192.168.1.12,,80)      8      probe      Healthy (no probe)
websrv1(192.168.1.11,,80)      2      probe      Healthy 8/8
websrv2(192.168.1.12,,80)      2      sick       Healthy 8/8#后端2服务器健康状态为sick

五varnish 性能调整参数

1、varnish的运行时参数:

最大并发连接数 =thread_pools 乘以 thread_pool_max

2、Timer计时器相关的参数:

服务器端侧:

connect_timeout:连接后端主机超时时间

客户端侧:

设置方式:
vcl.param
param.set

永久有效的方法:
varnish.params
DEAMON_OPTS="-p PARAM1=VALUE -p PARAM2=VALUE"

六、varnish日志区域:

shared memory log
计数器
日志信息

1、varnishstat - Varnish Cache statistics#查询缓存计数器

-1:一次性显示所有信息,其中MAIN段重要,查看并计算命中率
-1 -f FILED_NAME:查看指定的字段对应的值
-l:可用于-f选项指定的字段名称列表;

MAIN.cache_hit:命中
MAIN.cache_miss:未命中

# varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_miss#查看指定的字段对应的值
# varnishstat -l -f MAIN -f MEMPOOL#查看menpool相关

2、varnishtop - Varnish log entry ranking#查询日志并排序

-1 一次性显示区域的所有信息
-i taglist,可以同时使用多个-i选项,也可以一个选项跟上多个标签(过滤);
-I <[taglist:]regex>从所有中基于正则表达式过滤列表
-x taglist:排除列表
-X <[taglist:]regex>从所有中基于正则表达式排除列表

# varnishtop -i BereqHeader X-Forwarded-For#过滤发往后端服务有多少
# varnishtop -i BereqHeader RespStatus#过滤发往后端返回的响应码
# varnishtop -x BereqHeader,BerespHeader#排除指定多个字段

3、varnishlog:显示Varnish日志,

#varnislog -i RespStatus #查询日志指定字段信息

4、 varnishncsa :显示NCSA 格式日志

 #varnishncsa -h #帮助命令
#systemctl start varnishncsa.service#如果使用日志,启动
#systemctl enable varnishncsa.service#设置开机启动
#tail /var/log/varnish/varnishncsa.log#查看日志内容
上一篇下一篇

猜你喜欢

热点阅读