本文紀錄 Linux 安裝及配置 Elasticsearch 單節點以及 cluster 流程:

Download and Install

前往 Elasticsearch 下載頁面,選擇 platform Linux 並且解壓縮

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-linux-x86_64.tar.gz
tar -zxvf elasticsearch-7.16.2-linux-x86_64.tar.gz -C /opt/mudule
cd /opt/module/elasticsearch-7.16.2/

建立 Elasticsearch sys user 及配置權限

Elasticsearch 基於安全考量,不允許使用 root 運行,因此需要創建 elasticsearch sys user 用於運行 elasticsearch

useradd esuser
passwd esuser

//若建立錯誤可刪除再重建
userdel -r esuser

設定執行文件夾權限,讓 elasticsearch sys user 可以執行

chown -R esuser:esuser /opt/module/elasticsearch-7.16.2/

單節點設定檔文件

開啟 elasticsearch.yml ,以下是一個 Node 的設定參考:

開啟 elasticsearch.yml 文件,進行設定

vim /opt/modules/elasticsearch-7.16.2/config/elasticsearch.yml

單節點設定參考 (這台 host 給予一個 etc/hosts 設為 eshost01,如果純粹單一節點可以用 0.0.0.0 表示為本機 IP)

cluster.name: elasticsearch
node.name: node01
network.host: eshost01
http.port: 9200
cluster.initial_master_nodes: ["node01"]

多節點設定文件

單一 Elasticsearch 提供服務,因為單台機器容量有限、出現故障及不可用,併發處理能力有限,因此容易產生不可用的情況,所以通常只會在測試環境中使用。

在生產環境,為了符合高可用原則,都會將 Elasticsearch 以 Cluster 方式來運行。

Elasticsearch 的 Cluster 的 Node 沒有數量限制,只要超過 2 Node 就可視為一個 Cluster。

一個 Cluster 需要指定 cluster_name,在該 Cluster 的 Node 都是透過這個名稱來聚集,每一個 Node 都是一個獨立運行的 Elasticsearch service。

在其他機器依照上方步驟及設定,先下載安裝及設定權限

接著進行配置 elasticsearch.yml

Master node 主要負責管理 cluster, node 及 index ,不負責管理 document。Data node 負責管理 document;Node 不建議身兼多職。

# 集群名稱(同樣的 cluster 要保持一致)
cluster.name: elasticsearch

# 節點名稱 (在集群內,每個節點名稱必須各自唯一,不能重複)
node.name:node02

# 節點身份,預設為 master & data 為 true,建議做區隔
node.master: false
node.data: true

# 可以被推選為 master node 的節點列表,這裡先把當前節點推選為主節點
# cluster.inintial_master_nodes: ["node-1001", "node-1002", "node-1003"]
 cluster.inintial_master_nodes: ["node01"]
 
# 本節點 IP 位置
network.host: eshost2

# 本節點 http port
http.port: 9200

# tcp 監聽 port 
transport.tcp.port: 9200

# 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.max_content_length: 200mb

# --Discovery 發現其他機器--
# 第一台機器不用加,這裡設定的是候選 master node 位置,在啟動服務後可以被選為 master node
discovery.seed_hosts:["eshost1:9200", "eshost2:9200", "eshost3:9200"]
gateway.recover_after_nodes: 2
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5


network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true

# ----依照主機規格可調整以下參數----

# cluster 同時啟動數據任務數(default 2)
cluster.routing.allocation.cluster_concurrent_rebalance: 16

# 調控 新增/刪除 node 及負載平衡時,併發恢復的線程數 (default 2)
cluster.routing.allocation.cluster_concurrent_recoveriew: 16
# 初始化數據恢復時,並發線程數 (default 4)
cluster.routing.allocation.cluster_initial_primaries_recoveries: 16

系統配置

調整系統配置,調高 es user 的資料夾文件數量限制,以避免 Elasticsearch 會產生大量文件會超過系統預設。

vim /etc/security/limits.conf

設定參考

# 調整每個 process 可以開啟文件數的限制
esuser soft nofile 65536
esuser hard nofile 65536

調整系統配置,設定使用者的執行緒限制

vim /etc/security/limits.d/20-nproc.conf

設定參考

# 調整每個 process 可以開啟文件數的限制
esuser soft nofile 65536
esuser hard nofile 65536

調整系統配置

vim /etc/sysctl.conf

設定參考

# 一個進程可以擁有的 VMA(虛擬內存區塊)數量,預設為 65536
vm.max_map_count=655360

重新加載系統配置

sysctl -p

啟動 Elasticsearch 服務

前面提到,Elasticsearch 不允許 root 用戶執行服務,因此前面設定完成後,要切換到 elasticsearch sys user 並且啟動服務,要記得將該文件夾權限設定為 esuser

su esuser
cd /opt/module/elasticsearch-7.16.2/
bin/elasticsearch

查看 Cluster 健康狀況

request GET 
'http://eshost1:9200/_cluster/health'

response

{
    "cluster_name": "elasticsearch",
    "status": "green", //健康狀況
    "timed_out": false,
    "number_of_nodes": 3, //cluster 中的 node 數量
    ...
}

查看 Cluster node 狀況

request GET 
'http://eshost1:9200/_cat/nodes'