varnish - 动态添加或删除后端主机到 Varnish

标签 varnish varnish-vcl

在不停机的情况下以编程方式向 Varnish director 添加或删除单个后端服务器的最佳方法是什么?我一直在寻找这方面的一个很好的例子,但找不到。

我希望能够根据需要扩展和缩减我的后端服务器。

谢谢!

山姆

最佳答案

虽然这不是添加后端的最优雅甚至动态的方式,但我会通过在单独的 VCL 中定义后端并将其包含在 default.vcl 中使用

include "backend.vcl";

定义后端和 Controller 的位置。例如

probe healthcheck {
   .url = "/online";
   .interval = 15s;
   .timeout = 0.3 s;
   .window = 3;
   .threshold = 1;
   .initial = 1;
}

backend web1 {
  .host = "10.1.2.1";
  .port = "80";
  .connect_timeout = 300s;
  .first_byte_timeout = 5000s;
  .between_bytes_timeout = 300s;
  .probe = healthcheck;
}

director backendpool round-robin {
  { .backend = web1; }
}

并使用backendpool 作为后端。然后使用 shell 脚本、ssh、自定义守护程序或任何最适合您需求的方法通过添加后端更新 backend.vcl 并为 Varnish 重新加载。

这种方法的问题是 Varnish 实际上并没有删除已经从 backend.vcl 中删除的后端。即使不使用它们,Varnish 也会继续探测它们。从长远来看,这可能会导致意外行为。如果重新使用后端名称,至少后端健康探测的结果会变得困惑。例如,在上例中多次重命名 web1 后端,然后将其主机更改为无效主机后,这是恢复到 Backend_health 后的轮询结果以上配置仅定义了有效的 web1 后端。

0 Backend_health - web1 Still healthy 4--X-RH 3 1 3 0.001141 0.001055 HTTP/1.1 200 OK
0 Backend_health - web1 Still sick ------- 0 1 3 0.000000 0.000000
0 Backend_health - web2 Still healthy 4--X-RH 3 1 3 0.001061 0.001111 HTTP/1.1 200 OK
0 Backend_health - web3 Still healthy 4--X-RH 3 1 3 0.001007 0.001021 HTTP/1.1 200 OK

a patch对于 Varnish 2.1 更精细的后端处理,但据我所知它不适用于 Varnish 3。

关于varnish - 动态添加或删除后端主机到 Varnish,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14206734/

相关文章:

使用 Varnish 时 Magento Onepagechekout 登录问题

magento2 - 每个实例的Magento 2 Varnish缓存

wordpress - 调试Varnish HTTP清除

caching - Varnish 4 x缓存未命中

Varnish 使 header 配置过期

varnish - 升级到 Varnish v4

caching - Varnish :如何将命中/未命中统计信息发送到后端

varnish - 返回(通过)HIT或MISS

nginx - 如何强制浏览器在代码部署后重新加载静态 Assets ?

caching - Varnish :具有多个 IP 的后端(或使用其他东西)