Varnish vmod_directors支持不同模式的后端负载均衡:round_robin,fallback,random和HASH。对于第一个,很清楚Varnish将如何处理生病的后端。
但是HASH导演会发生什么?它将重新分配所有关键空间?或者只移位1 / N键(N是后端数)?
或许您决定将后端分组为2对以实现HA并且不重复缓存? (董事可以使用其他董事作为后端)
sub vcl_init {
new h = directors.hash();
h.add_backend(backend_1, 1);
h.add_backend(backend_2, 1); // <------ sick
h.add_backend(backend_3, 1);
h.add_backend(backend_4, 1);
h.add_backend(backend_5, 1);
h.add_backend(backend_6, 1);
}
sub vcl_recv {
// pick a backend based on the request URL
set req.backend_hint = h.backend(req.url);
}
VS
new main_hash_director = directors.hash();
h.add_backend(hash_director_A, 1);
h.add_backend(hash_director_B, 1);
h.add_backend(hash_director_C, 1);
new hash_director_A = directors.hash();
h.add_backend(backend_1, 1);
h.add_backend(backend_2, 1); // <------ sick
new hash_director_B = directors.hash();
h.add_backend(backend_3, 1);
h.add_backend(backend_4, 1);
new hash_director_C = directors.hash();
h.add_backend(backend_5, 1);
h.add_backend(backend_6, 1);
答案 0 :(得分:1)
我做了一个清漆测试,看看究竟发生了什么,Varnish重新分配了当前健康后端的所有键。
VMOD可用于一致性散列:https://www.varnish-cache.org/vmod/vslp-stateless-persistence-consistent-hashing-director-vmod
varnishtest&#34;哈希导演&#34;
server s1 {
rxreq
txresp -hdr "Foo: 1"
rxreq
txresp -hdr "Foo: 1"
rxreq
txresp -hdr "Foo: 1"
rxreq
txresp -hdr "Foo: 1"
} -start
server s2 {
rxreq
txresp -hdr "Foo: 2"
rxreq
txresp -hdr "Foo: 2"
rxreq
txresp -hdr "Foo: 2"
rxreq
txresp -hdr "Foo: 2"
} -start
server s3 {
rxreq
txresp -hdr "Foo: 3"
rxreq
txresp -hdr "Foo: 3"
rxreq
txresp -hdr "Foo: 3"
rxreq
txresp -hdr "Foo: 3"
} -start
server s4 {
rxreq
txresp -hdr "Foo: 4"
rxreq
txresp -hdr "Foo: 4"
rxreq
txresp -hdr "Foo: 4"
rxreq
txresp -hdr "Foo: 4"
} -start
server s5 {
rxreq
txresp -hdr "Foo: 5"
rxreq
txresp -hdr "Foo: 5"
rxreq
txresp -hdr "Foo: 5"
rxreq
txresp -hdr "Foo: 5"
} -start
server s6 {
rxreq
txresp -hdr "Foo: 6"
rxreq
txresp -hdr "Foo: 6"
rxreq
txresp -hdr "Foo: 6"
rxreq
txresp -hdr "Foo: 6"
} -start
server s7 {
rxreq
txresp -hdr "Foo: 7"
rxreq
txresp -hdr "Foo: 7"
rxreq
txresp -hdr "Foo: 7"
rxreq
txresp -hdr "Foo: 7"
} -start
server s8 {
rxreq
txresp -hdr "Foo: 8"
rxreq
txresp -hdr "Foo: 8"
rxreq
txresp -hdr "Foo: 8"
rxreq
txresp -hdr "Foo: 8"
} -start
server s9 {
rxreq
txresp -hdr "Foo: 9"
rxreq
txresp -hdr "Foo: 9"
rxreq
txresp -hdr "Foo: 9"
rxreq
txresp -hdr "Foo: 9"
} -start
server s10 {
rxreq
txresp -hdr "Foo: 10"
rxreq
txresp -hdr "Foo: 10"
rxreq
txresp -hdr "Foo: 10"
rxreq
txresp -hdr "Foo: 10"
} -start
varnish v1 -vcl+backend {
director h1 hash {
{ .backend = s1; .weight = 1; }
{ .backend = s2; .weight = 1; }
{ .backend = s3; .weight = 1; }
{ .backend = s4; .weight = 1; }
{ .backend = s5; .weight = 1; }
{ .backend = s6; .weight = 1; }
{ .backend = s7; .weight = 1; }
{ .backend = s8; .weight = 1; }
{ .backend = s9; .weight = 1; }
{ .backend = s10; .weight = 1; }
}
sub vcl_recv {
set req.backend = h1;
return(pass);
}
} -start
# first run-----------------------------
client c1 {
txreq -url /12
rxresp
expect resp.http.foo == "1"
txreq -url /14
rxresp
expect resp.http.foo == "4"
txreq -url /45
rxresp
expect resp.http.foo == "7"
txreq -url /65
rxresp
expect resp.http.foo == "5"
txreq -url /78
rxresp
expect resp.http.foo == "9"
txreq -url /894
rxresp
expect resp.http.foo == "8"
txreq -url /115
rxresp
expect resp.http.foo == "2"
txreq -url /321341
rxresp
expect resp.http.foo == "6"
txreq -url /612
rxresp
expect resp.http.foo == "10"
txreq -url /33
rxresp
expect resp.http.foo == "3"
} -run
varnish v1 -cliok "backend.set_health s1 sick"
# second run-----------------------------
client c1 {
txreq -url /12
rxresp
# expect resp.http.foo == "1" FAIL
txreq -url /14
rxresp
expect resp.http.foo == "4"
txreq -url /45
rxresp
# expect resp.http.foo == "7" FAIL
txreq -url /65
rxresp
expect resp.http.foo == "5"
txreq -url /78
rxresp
# expect resp.http.foo == "9" FAIL
txreq -url /894
rxresp
expect resp.http.foo == "8"
txreq -url /115
rxresp
expect resp.http.foo == "2"
txreq -url /321341
rxresp
expect resp.http.foo == "6"
txreq -url /612
rxresp
# expect resp.http.foo == "10" FAIL
txreq -url /33
rxresp
expect resp.http.foo == "3"
} -run