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),可以從上游服務器組中添加和刪除,上線或下線,改變權重。
下表比較Elasticsearch 、Elasticsearch + nginx F/OSS、Elasticsearch + NGINX Plus 的功能:
Elasticsearch client | nginx F/OSS | NGINX Plus | |
Horizontal scalability | Y | Y | Y |
Keepalive optimization | Y | Y | |
Centralized HTTP access logs | Y | Y | |
Queuing and concurrency control | Y | ||
Response caching | Y | Y | |
Failover of failed connections | Y | Y | Y |
Active monitoring of Elasticsearch nodes | Y (some clients) | Y | |
Advanced load balancing methods | Y | Y | |
Weighted load balancing | Y | Y | |
Dynamic reconfiguration | Y | ||
Status monitoring | Y | ||
General-purpose HTTP load balancing | Y | Y |
當使用NGINX Plus高可用性主動或被動配置負載均衡的Elasticsearch 服務器集羣時,Elasticsearch 客戶端通過nginx plus請求,而不是直接連接到Elasticsearch服務器,可以根據實際情況任意的擴展Elasticsearch服務器不用更新客戶端。
部署NGINX Plus + Elasticsearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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完美構建成一個可擴展高性能高可用性架構。
更多關於安全性的問題可以參見:
Securing Elasticsearch with Nginx
Playing HTTP Tricks with Nginx