1. 程式人生 > >nginx與Elasticsearch結合使用

nginx與Elasticsearch結合使用

post fail oot load 高可用性 實時 主動 高性能 分享

Elasticsearch是一種先進的,高性能的,可擴展的開源搜索引擎,提供全文搜索和實時分析的結構化和非結構化的數據。

它的特定是可以通過HTTP使用 RESTful API,很容易的融入現有的web架構。因此在高並發的情況下,采用nginx反向代理負載均衡到多臺Elasticsearch 服務器上。

架構圖:

技術分享圖片

這種架構的優點是:

記錄每個API請求的日誌
支持大量的客戶端連接,不管有沒有啟用keepalives,比長連接(使用keepalives)到elasticsearch服務器小的多
負載均衡的請求Elasticsearch服務器
緩存數據,減少同一內容再次請求Elasticsearch服務器。NGINX Plus 還提供HTTP API 對緩存數據的清除接口

提供主動健康檢測(僅nginx plus),不斷檢測後端Elasticsearch服務器是否正常,並主動的進行切換。
報告豐富的監控指標(僅nginx plus),提供監控和管理。
支持通過HTTP API動態的配置上遊服務器組(僅nginx plus),可以從上遊服務器組中添加和刪除,上線或下線,改變權重。

技術分享圖片

當使用NGINX Plus高可用性主動或被動配置負載均衡的Elasticsearch 服務器集群時,Elasticsearch 客戶端通過nginx plus請求,而不是直接連接到Elasticsearch服務器,可以根據實際情況任意的擴展Elasticsearch服務器不用更新客戶端。

部署NGINX Plus + Elasticsearch

proxy_cache_path /var/cache/nginx/cache keys_zone=elasticsearch:10m inactive=60m;
upstream elasticsearch_servers {
    zone elasticsearch_servers 64K;
    server 192.168.187.132:9200;
    server 192.168.187.133:9200;
}
match statusok {
    status 200;
    header Content-Type ~ "application/json";
    body ~ '"status" : 200';
}
server {
    listen 9200;
    status_zone elasticsearch;
    location / {
        proxy_pass http://elasticsearch_servers;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_cache elasticsearch;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_connect_timeout 5s;
        proxy_read_timeout 10s;
        health_check interval=5s fails=1 passes=1 uri=/ match=statusok;
    }
    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    access_log logs/es_access.log combined;
}
server {
    listen 8080;
    root /usr/share/nginx/html;
    location / {
        index status.html;
    }
    location =/status {
        status;
    }
}

負載均衡、對有效的請求緩存10分鐘、主動的健康監測、狀態收集。

Elasticsearch是一個強大而靈活的搜索引擎,與nginx完美構建成一個可擴展高性能高可用性架構。

參考:https://www.elastic.co/blog/playing-http-tricks-nginx

nginx與Elasticsearch結合使用