2012-03-07 19 views
1

我們有一組6個後端爲我們的網站提供服務。因爲我們使用多個數據中心,所以當我們將清單發送到localhost httpd服務器(運行在端口81上)時,我們發現最佳性能發生在我們身上。這當然是一個非常基本的配置深受清漆的支持,並可以使用回退導演來完成:如何使用varnish將請求發送到localhost httpd服務器,但是如果不健康將請求循環發送到一組其他節點

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

然而後備導演試圖在其他後端爲了直到它找到一個健康的人。 BIG問題是,在上面的配置中,如果localbackend失敗,web1將採取所有流量!這會超載web1,它會生病。然後,所有請求都會轉到web3 ...現在它將獲得正常流量的3倍...導致級聯失敗。

因此,我們需要一個配置,它允許所有請求發送到本地主機httpd服務器,如果它是健康的,但如果不是,則以循環方式將請求發送到其他健康的服務器。

非常感謝您提供任何您可能會想到的建議和解決方案......您的幫助非常感謝。

+1

一種方式我能想到的解決這個問題的,是巨大的權重設置爲你的第一個後端和循環主任。 當第一臺服務器出現故障時,會隨機選擇其他服務器來提供流量。 但是,您仍然有1%的機會從另一個緩存中獲取服務器內容。 我不確定在vcl_fetch中是否可以更改後端導向器,但是當第一個後端不健康時,您可以測試此方法以更改後端。 或者您可以嘗試「重新啓動」方法https://www.varnish-cache.org/trac/wiki/VCLExampleRestarts – 2012-04-16 23:14:48

+0

vcl標籤是德爾福用戶,請標記問題爲varnish-vcl – 2013-01-21 15:37:33

回答

0

這是完全未經測試,但理論上應該工作。

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。您只需確保後端名稱與您的主機名稱匹配。

1

它可以通過多種方式來實現,最簡單的一個是:

  1. 設置localhost作爲默認後端
  2. 之上
  3. 檢查後端的其餘部分創建一個圓robind導演你vcl_recv如果默認後端是健康的,並且它沒有切換到全局robind導演。

使用這種方法,您甚至可以在當前請求失敗時切換後端。

喜歡的東西:

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; 
    * } 
    */ 
} 
相關問題