一 序
本文屬于極客時間Elasticsearch核心技術與實戰學習筆記系列。
二 水準擴充的痛點
單叢集 - 當水準擴充時,節點數不能無限增加
- 當叢集的 meta 資訊(節點,索引,叢集狀态)過多,會導緻更新壓力變大,單個 Active Master 會成為性能瓶頸,導緻整個叢集無法正常工作
早期版本,通過 Tribe Node 可以實作多叢集通路的需求,但是還存在一定的問題
- Tribe Node 會以 Client Node 的方式加入叢集。叢集中 Master 節點的任務變更需要 Tribe Node 的回應才能繼續
- Tribe Node 不儲存 Cluster State 資訊,一旦重新開機,初始化很慢
- 當多個叢集存在索引重名的情況下,隻能設定一種 Perfer 規則
2.1 跨叢集搜尋 - Cross Cluster Search
早期 Tribe Node 的方案存在一定的問題,現已被 Deprecated
ES5.3 引入跨叢集搜尋的功能(Cross Cluster Search),推薦使用
- 允許任何節點扮演 federated 節點,以輕量的方式,将搜尋請求進行代理
- 不需要以 Client Node 的形式加入其它叢集
2.2 叢集的配置
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TP31ENVhUY2pkMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2ETN2EzMzEjMwIjNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.3 demo
//啟動3個叢集
bin/elasticsearch -E node.name=cluster0node -E cluster.name=cluster0 -E path.data=cluster0_data -E discovery.type=single-node -E http.port=9200 -E transport.port=9300
bin/elasticsearch -E node.name=cluster1node -E cluster.name=cluster1 -E path.data=cluster1_data -E discovery.type=single-node -E http.port=9201 -E transport.port=9301
bin/elasticsearch -E node.name=cluster2node -E cluster.name=cluster2 -E path.data=cluster2_data -E discovery.type=single-node -E http.port=9202 -E transport.port=9302
//在每個叢集上設定動态的設定
PUT _cluster/settings
{
"persistent": {
"cluster": {
"remote": {
"cluster0": {
"seeds": [
"127.0.0.1:9300"
],
"transport.ping_schedule": "30s"
},
"cluster1": {
"seeds": [
"127.0.0.1:9301"
],
"transport.compress": true,
"skip_unavailable": true
},
"cluster2": {
"seeds": [
"127.0.0.1:9302"
]
}
}
}
}
}
我沒有測試。
#cURL
curl -XPUT "http://localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'
{"persistent":{"cluster":{"remote":{"cluster0":{"seeds":["127.0.0.1:9300"],"transport.ping_schedule":"30s"},"cluster1":{"seeds":["127.0.0.1:9301"],"transport.compress":true,"skip_unavailable":true},"cluster2":{"seeds":["127.0.0.1:9302"]}}}}}'
curl -XPUT "http://localhost:9201/_cluster/settings" -H 'Content-Type: application/json' -d'
{"persistent":{"cluster":{"remote":{"cluster0":{"seeds":["127.0.0.1:9300"],"transport.ping_schedule":"30s"},"cluster1":{"seeds":["127.0.0.1:9301"],"transport.compress":true,"skip_unavailable":true},"cluster2":{"seeds":["127.0.0.1:9302"]}}}}}'
curl -XPUT "http://localhost:9202/_cluster/settings" -H 'Content-Type: application/json' -d'
{"persistent":{"cluster":{"remote":{"cluster0":{"seeds":["127.0.0.1:9300"],"transport.ping_schedule":"30s"},"cluster1":{"seeds":["127.0.0.1:9301"],"transport.compress":true,"skip_unavailable":true},"cluster2":{"seeds":["127.0.0.1:9302"]}}}}}'
#建立測試資料
curl -XPOST "http://localhost:9200/users/_doc" -H 'Content-Type: application/json' -d'
{"name":"user1","age":10}'
curl -XPOST "http://localhost:9201/users/_doc" -H 'Content-Type: application/json' -d'
{"name":"user2","age":20}'
curl -XPOST "http://localhost:9202/users/_doc" -H 'Content-Type: application/json' -d'
{"name":"user3","age":30}'
#查詢
GET /users,cluster1:users,cluster2:users/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 40
}
}
}
}
當然在Kibana中,打開“Management管理”,然後單擊“Index Patterns索引模式”。
這個比較實用,比如通常的日志搜集,Logstash通常以logstash-YYYY.MMM.DD格式建立一系列索引,這個可以适配多天的。索引模式包含通配符(*)以比對多個索引。