如果 Varnish 位於 HAProxy 和 Apache 之間,如何進行故障轉移

如果 Varnish 位於 HAProxy 和 Apache 之間,如何進行故障轉移

我正在考慮將 Varnish 置於 HAProxy 和 Apache 之間。這是可行的,但 HAProxy 正在監視 Varnish。如果 Apache 發生故障,它不會故障轉移到另一個 Apache。

HAProxy 中是否有可以解決此問題的配置?

答案1

如果 Varnish 位於 HAproxy 和 Apache 之間,您可以讓 Varnish 執行下列操作負載平衡,儘管它不如 HAproxy 提供的選項那麼強大。

更好的方法可能是讓 HAproxy 將靜態內容傳送到 Varnish,並將其餘內容直接傳送到後端伺服器。

Haproxy.com 有一篇非常好的文章介紹如何做到這一點這裡

如果你確實想讓 HAproxy 檢查 Varnish 的狀態同時使用 Apache(位於同一主機上),您有兩個選項:

  1. 在 HAProxy 中設定虛擬後端/伺服器,檢查 Apache 並讓匹配的 Varnish 伺服器追蹤虛擬:

    frontend HTTP-IN
      mode http
      default_backend Varnishes
    
    # All traffic goes here
    backend Varnishes
      mode http
      balance roundrobin 
      server Varnish-1 1.1.1.1:80 track Apache-1/Apache-1
      server Varnish-2 2.2.2.2:80 track Apache-2/Apache-2
    
    # No traffic ever goes here
    # Just used for taking servers out of rotation in 'backend Varnishes'
    backend Apache-1
      server Apache-1 1.1.1.1:8080 check
    
    backend Apache-2
      server Apache-2 2.2.2.2:8080 check
    
  2. 讓 Varnish 傳回與 Apache 狀態相符的運行狀況檢查結果(如果 Apache 已啟動,則為“OK”,否則為“FAILED”)。

    清漆.vcl

    backend default {
      .host = "127.0.0.1";
      .port = "8080";
    }
    
    # Health Check
    if (req.url == "/varnishcheck") {
      if (req.backend.healthy) {
        return(synth(751, "OK!"));
      } else {
        return(synth(752, "FAILED!"));
      }
    }
    
    sub vcl_synth {
      # Health Checks
      if (resp.status == 751) {
        set resp.status = 200;
        return (deliver);
      }
      if (resp.status == 752) {
        set resp.status = 503;
        return (deliver);
      }
    }
    

    haproxy.cfg

    frontend HTTP-IN
      mode http
      default_backend Varnishes
    
    backend Varnishes
      mode http
      balance roundrobin 
      option httpchk HEAD /varnishcheck
      http-check expect status 200
      server Varnish-1 1.1.1.1:80 check
      server Varnish-2 2.2.2.2:80 check
    

相關內容