2

我们有一组 6 个后端服务于我们的网站。因为我们使用多个数据中心,所以我们发现当我们让 varnish 将请求发送到 localhost httpd 服务器(在端口 81 上运行)时,我们会获得最佳性能。这当然是 varnish 支持的非常基本的配置,可以使用 fallback director 完成:

director default fallback {
    { .backend = localbackend; }
    { .backend = web1; }
    { .backend = web2; }
    { .backend = web3; }
etc...
}

然而,后备主管会依次尝试其他后端,直到找到一个健康的后端。最大的问题是,在上面的配置中,如果 localbackend 失败,web1 将占用所有流量!!!这将使 web1 过载并且它会生病。然后所有请求都转到 web3 ......现在它将获得正常流量的 3 倍......导致级联故障。

因此,我们需要一个配置,如果它是健康的,则允许所有请求发送到 localhost httpd 服务器,但如果不是,则以循环类型的方式将请求发送到其他健康的服务器。

提前感谢您提供您可能想到的任何建议和解决方案......非常感谢您的帮助。

4

3 回答 3

2

它可以通过多种方式完成,最简单的一种是:

  1. 将 localhost 设置为默认后端
  2. 为其余后端创建一个循环导向器
  3. 检查您的vcl_recv默认后端是否健康,如果它没有切换到轮询导向器。

使用这种方法,如果当前请求失败,您甚至可以切换后端。

就像是:

probe my_probe {
  .url       = "/";
  .interval  = 1s;
  .timeout   = 0.2 s;
  .window    = 3;
  .threshold = 2;
  .initial   = 1;
}
backend default {
  .host  = 127.0.0.1;
  .probe = my_probe;
}
backend server1 {
  .host  = 192.168.8.21;
  .probe = my_probe;
}
backend server2 {
  .host  = 192.168.8.22;
  .probe = my_probe;
}
backend server3 {
  .host  = 192.168.8.23;
  .probe = my_probe;
}
director server_pool round-robin {
  { .backend = server1; }
  { .backend = server2; }
  { .backend = server3; }
}

sub vcl_recv {
  if ( req.backend == default
    && ! req.backend.healthy
  ) {
    set req.backend = server_pool;
  }
  /* Uncomment if you want to divert restarted requests to the server pool
   * if (req.restarts > 0) {
   *   set req.backend = server_pool;
   * }
   */
}
于 2014-01-25T14:10:39.170 回答
1

这是完全未经测试的,但理论上应该有效。

probe healthcheck {
  .url = "/status.php";
  .interval = 60s;
  .timeout = 0.3 s;
  .window = 8;
  .threshold = 3;
  .initial = 3;
  .expected_response = 200;
}

backend server1 {
  .host = "server1.example.com";
  .probe = healthcheck;
}
backend server2 {
  .host = "server2.example.com";
  .probe = healthcheck;
}
backend server3 {
  .host = "server3.example.com";
  .probe = healthcheck;
}

director fallback round-robin {
  { .backend = server1; }
  { .backend = server2; }
  { .backend = server3; }
}

sub vcl_recv {
  # Set backend to fallback director until we find the proper localhost backend.
  # If we can't figure out which is localhost, at least we still work.
  set req.backend = fallback;

  # Set the default backend to localhost by hostname
  if (server.hostname == "server1") {
    set req.backend = server1;
    set obj.http.X-LocalCache = "YES";
  }
  else if (server.hostname == "server2") {
    set req.backend = server2;
    set obj.http.X-LocalCache = "YES";
  }
  else if (server.hostname == "server3") {
    set req.backend = server3;
    set obj.http.X-LocalCache = "YES";
  }

  # If the localhost fails, go to fallback director.
  if (obj.http.X-LocalCache ~ "YES") {
    if (!req.backend.healthy) {
      set req.backend = fallback;
      unset obj.http.X-LocalCache
    }
  }
}

它将通过使用主机名自动确定哪个后端是 localhost。您只需确保后端名称与您的主机名匹配。

于 2014-01-25T04:51:21.690 回答
0

您可以使用 Varnish 进行一些基本的负载平衡。看一下这个:

https://www.varnish-cache.org/trac/wiki/LoadBalancing

于 2012-03-13T10:14:46.353 回答