Elasticsearch Cluster 安裝與配置
阿新 • • 發佈:2022-05-02
本文節選自《Netkiller Database 手札》作者:netkiller 網站: http://www.netkiller.cn
23.1.2. Elasticsearch Cluster
叢集模式需要兩個以上的節點,通常是一個 master 節點,多個 data 節點
首先在所有節點上安裝 elasticsearch,然後配置各節點的配置檔案,對於 5.5.1 不需要配置決定哪些節點屬於 master 節點 或者 data 節點。
curl -s https://raw.githubusercontent.com/oscm/shell/master/search/elasticsearch/elasticsearch-5.x.sh | bash
配置檔案
cluster.name: elasticsearch-cluster # 配置叢集名稱,所有伺服器伺服器保持一致 node.name: node-1 # 每個節點唯一標識,每個節點只需改動這裡,一次遞增 node-1, node-2, node-3 ... network.host: 0.0.0.0 discovery.zen.ping.unicast.hosts: ["172.16.0.20", "172.16.0.21","172.16.0.22"] # 所有節點的IP 地址寫在這裡 discovery.zen.minimum_master_nodes: 3 # 可以作為master的節點總數,有多少個節點就寫多少 http.cors.enabled: true http.cors.allow-origin: "*"
檢視節點狀態,使用curl工具: curl 'http://localhost:9200/_nodes/process?pretty'
root@netkiller /var/log/elasticsearch % curl 'http://localhost:9200/_nodes/process?pretty' { "_nodes" : { "total" : 2, "successful" : 2, "failed" : 0 }, "cluster_name" : "my-application", "nodes" : { "-lnKCmBXRpiwExLns0jc9g" : { "name" : "node-1", "transport_address" : "10.104.3.2:9300", "host" : "10.104.3.2", "ip" : "10.104.3.2", "version" : "5.5.1", "build_hash" : "19c13d0", "roles" : [ "master", "data", "ingest" ], "process" : { "refresh_interval_in_millis" : 1000, "id" : 23669, "mlockall" : false } }, "WVsgYi2HT8GWnZU1kUwFwA" : { "name" : "node-2", "transport_address" : "10.186.7.221:9300", "host" : "10.186.7.221", "ip" : "10.186.7.221", "version" : "5.5.1", "build_hash" : "19c13d0", "roles" : [ "master", "data", "ingest" ], "process" : { "refresh_interval_in_millis" : 1000, "id" : 12641, "mlockall" : false } } } }
啟動節點後回生成 cluster.name 為檔名的日誌檔案。
誰先啟動誰講成為master
[2017-08-11T17:42:46,018][INFO ][o.e.c.s.ClusterService ] [node-1] new_master {node-1}{-lnKCmBXRpiwExLns0jc9g}{rZcJDIynSzq2Td3yP2kN5A}{10.104.3.2}{10.104.3.2:9300}, added {{node-2}{WVsgYi2HT8GWnZU1kUwFwA}{X13ShUpAQa2zA1Mgcsm3bQ}{10.186.7.221}{10.186.7.221:9300},}, reason: zen-disco-elected-as-master ([1] nodes joined)[{node-2}{WVsgYi2HT8GWnZU1kUwFwA}{X13ShUpAQa2zA1Mgcsm3bQ}{10.186.7.221}{10.186.7.221:9300}]
如果master出現故障,其他節點會接管
[2017-08-11T17:44:52,797][INFO ][o.e.c.s.ClusterService ] [node-2] master {new {node-2}{WVsgYi2HT8GWnZU1kUwFwA}{vl8kQx8sQdGVVohrNQnZOQ}{10.186.7.221}{10.186.7.221:9300}}, removed {{node-1}{-lnKCmBXRpiwExLns0jc9g}{rZcJDIynSzq2Td3yP2kN5A}{10.104.3.2}{10.104.3.2:9300},}, added {{node-1}{-lnKCmBXRpiwExLns0jc9g}{odnoG9kpQpeX1ltx5KYTSw}{10.104.3.2}{10.104.3.2:9300},}, reason: zen-disco-elected-as-master ([1] nodes joined)[{node-1}{-lnKCmBXRpiwExLns0jc9g}{odnoG9kpQpeX1ltx5KYTSw}{10.104.3.2}{10.104.3.2:9300}]
[2017-08-11T17:44:53,184][INFO ][o.e.c.r.DelayedAllocationService] [node-2] scheduling reroute for delayed shards in [59.5s] (11 delayed shards)
[2017-08-11T17:44:53,929][INFO ][o.e.c.r.a.AllocationService] [node-2] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[information][0]] ...]).
master 節點恢復上線會提示
[2017-08-11T17:44:52,855][INFO ][o.e.c.s.ClusterService ] [node-1] detected_master {node-2}{WVsgYi2HT8GWnZU1kUwFwA}{vl8kQx8sQdGVVohrNQnZOQ}{10.186.7.221}{10.186.7.221:9300}, added {{node-2}{WVsgYi2HT8GWnZU1kUwFwA}{vl8kQx8sQdGVVohrNQnZOQ}{10.186.7.221}{10.186.7.221:9300},}, reason: zen-disco-receive(from master [master {node-2}{WVsgYi2HT8GWnZU1kUwFwA}{vl8kQx8sQdGVVohrNQnZOQ}{10.186.7.221}{10.186.7.221:9300} committed version [44]])
23.1.3. 負載均衡配置
因為 elasticsearch 沒有使用者認證機制我們通常在內網訪問他。如果對外提供服務需要增加使用者認證。
$ printf "john:$(openssl passwd -crypt s3cr3t)n" > /etc/nginx/passwords
建立 nginx 配置檔案 /etc/nginx/conf.d/elasticsearch.conf
upstream elasticsearch {
server 172.16.0.10:9200;
server 172.16.0.20:9200;
server 172.16.0.30:9200;
keepalive 15;
}
server {
listen 9200;
auth_basic "Protected Elasticsearch";
auth_basic_user_file passwords;
location ~* ^(/_cluster|/_nodes) {
return 403;
break;
}
location / {
if ($request_filename ~ _shutdown) {
return 403;
break;
}
proxy_pass http://elasticsearch;
proxy_http_version 1.1;
proxy_set_header Connection "Keep-Alive";
proxy_set_header Proxy-Connection "Keep-Alive";
}
}
反覆使用下面方法請求,最終你會發現 total_opened 會達到你的nginx 配置數量
$ curl 'http://test:test@localhost:9200/_nodes/stats/http?pretty' | grep total_opened
# "total_opened" : 15
23.2.1. elasticsearch-analysis-ik
安裝外掛
root@netkiller ~ % /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip
[=================================================] 100%
-> Installed analysis-ik
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}'
23.3. 節點管理
23.3.1. 檢視索引
root@netkiller ~ % curl 'http://localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open information oygxIi-dR1eB9NoIZtJrxQ 5 1 45 42 731kb 731kb
green open .kibana 9jBBaOomTO2EakZlZqnE-g 1 1 5 1 62.5kb 31.2kb
green open logstash-api WHXZhn3vRWiuVbhR8rGoEg 5 1 565 0 3.8mb 1.9mb
23.3.2. 節點健康狀態
root@netkiller ~ % curl 'http://localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1502445967 18:06:07 my-application yellow 2 2 17 11 0 0 5 0 - 77.3%
root@netkiller ~ % curl 'http://localhost:9200/_cluster/health'
{"cluster_name":"my-application","status":"yellow","timed_out":false,"number_of_nodes":2,"number_of_data_nodes":2,"active_primary_shards":11,"active_shards":17,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":5,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":77.27272727272727}
23.3.3. 節點http狀態
root@VM_3_2_centos ~ % curl 'localhost:9200/_nodes/stats/http?pretty'
{
"_nodes" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"cluster_name" : "my-application",
"nodes" : {
"-lnKCmBXRpiwExLns0jc9g" : {
"timestamp" : 1502446878773,
"name" : "node-1",
"transport_address" : "10.104.3.2:9300",
"host" : "10.104.3.2",
"ip" : "10.104.3.2:9300",
"roles" : [
"master",
"data",
"ingest"
],
"http" : {
"current_open" : 4,
"total_opened" : 29
}
},
"WVsgYi2HT8GWnZU1kUwFwA" : {
"timestamp" : 1502446878782,
"name" : "node-2",
"transport_address" : "10.186.7.221:9300",
"host" : "10.186.7.221",
"ip" : "10.186.7.221:9300",
"roles" : [
"master",
"data",
"ingest"
],
"http" : {
"current_open" : 0,
"total_opened" : 2
}
}
}
}