這篇文章會記錄如何從 0 -> 1 建立一個 elasticsearch cluster, 其中包含 3 個 node.

之後有機會再補充透過 ansible 建置的方法, 和其他相關驗證指令

因為大多數公司的環境並無法直接連網, 因此都是從官網下載特定版本後, 在透過 scp 導入, 如果可以直接從網路下載就可以直接跳過前面 scp 的部分直接跳到後方修改 yaml file 的部分

ENV

名稱 描述
IP 192.168.80.241 | 192.168.80.242 | 192.168.80.243
主機名稱 pawpaw-elk-c1 | pawpaw-elk-c2 | pawpaw-elk-c3
OS version Ubuntu 22.04.2
CPU core 2 cores
Memory 8 G
Disk 30 G
Version 8.13.2

Elasticsearch Path

名稱描述 路徑(Path)
ES設定檔 /etc/elasticsearch/elasticsearch.yml
ES jvm 設定檔 /etc/elasticsearch/jvm.options
ES data /var/lib/elasticsearch
ES log /var/log/elasticsearch/albert-cluster.log
ES 憑證目錄位置 /etc/elasticsearch/certs
ES 執行檔檔案 /usr/share/elasticsearch/bin/elasticsearch
ES 啟動執行檔 /lib/systemd/system/elasticseasrch.service

Install

設定 /etc/hosts

如果本身環境有 AD(Active Directory) 這步驟就可以省略

1
2
3
4
5
vim /etc/hosts

192.168.80.241 pawpaw-elk-c1
192.168.80.242 pawpaw-elk-c2
192.168.80.243 pawpaw-elk-c3

設定虛擬記憶體數量

1
2
3
4
5
6
7
8
vim /etc/sysctl.conf

# 新增或修改
# 一個 process 可以使用的 memory 數量
vm.max_map_count = 262144


systctl -p

Private network

建立管理目錄

1
2
mkdir -p /admin/elasticsearch
cd /admin/elasticsearch

透過 window cmd scp 傳輸到指定 path

1
2
scp <file_path> <user>@<node_ip>:<path>
scp C:\Users\User\Downloads\elasticsearch-8.13.2-amd64.deb root@192.168.80.241:/admin/elasticsearch

安裝.deb檔案, 安裝完成後會出現預設的 elastic 密碼, 沒有記錄到需要重新產生

1
dpkg -i elasticsearch-8.13.2-adm64.deb

Public network

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# import GPG key
# 用來確保此安裝包(deb)是官方提供, 若安裝包有被異動過(非官方), 會有告警訊息.
# 必要安裝, 在 import repository 之後需要 update repository, 會用來驗證
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

# 確保 apt install 過程中是 https
apt-get install apt-transport-https

# 將 repository 存入路徑 /etc/apt/sources.list.d/ 
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

# 確認可以使用的版本
apt list -a | grep ^elasticsearch

# 指定版本, 不指定版本預設妝最新版
apt install -y elasticsearch=8.13.2

確認憑證檔

在 Elasticsearch 8.X 版本後預設會開啟 TLS.(路徑預設會存在 /etc/elasticsearch/certs/*)

1
ls /etc/elasticsearch/certs/*
File Desc
http_ca.crt 用於簽署Elasticsearch集群的HTTP層的CA證書
http.p12 密鑰庫包含該節點的HTTP層的密鑰和憑證
transport.p12 密鑰庫包含所有節點的HTTP層的密鑰和憑證

將憑證檔 scp 到各個 node

選定一台機器進行 scp 到其他 node 的操作(這邊使用 pawpaw-elk-c1 當操作 node)

1
2
scp /etc/elasticsearch/certs/* root@192.168.80.242:/etc/elasticsearch/certs/
scp /etc/elasticsearch/elasticsearch.keystore  root@192.168.80.242:/etc/elasticsearch/

設定 Elasticsearch 記憶體的使用量

Elasticsearch 所需使用的記憶體量 = 當前server總記憶體大小 / 2.

* 最高上限是32G.

1
2
3
4
5
vim /etc/elasticsearch/jvm.options

# 因為測試機只有 8G memory, 所以這邊修改為 4G 
-Xms4g
-Xmx4g

修改 Elasticsearch yml file

以下指令可以排除空行或#開頭的內容

cat /etc/elasticsearch/elasticsearch.yml | grep -Ev "^$|#"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
vim /etc/elasticsearch/elasticsearch.yml

cluster.name: pawpaw-elk 
node.name: pawpaw-elk-c1
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
bootstrap.memory_lock: true # 控制elasticsearch只使用memory不使用SWAP
network.host: 0.0.0.0 # 設定ES服務的IP位置(不限定來源ip)
http.port: 9200 # 設定 ES 溝通的端口
discovery.seed_hosts: ["pawpaw-elk-c1", "pawpaw-elk-c2", "pawpaw-elk-c3"] # 添加至集群的列表
xpack.security.enabled: true # 開啟ES安全性設定
xpack.security.enrollment.enabled: true # 啟動ES註冊令牌的功能
xpack.security.http.ssl:
  enabled: true # 開啟ES TLS協定
  keystore.path: certs/http.p12 # 指定keystore檔案路徑 (keystore儲存金鑰和證書)
xpack.security.transport.ssl:
  enabled: true # 開啟ES節點內部通訊SSL協定
  verification_mode: certificate # 設定內部TLS驗證模式
  keystore.path: certs/transport.p12 # 指定keystore檔案路徑
  truststore.path: certs/transport.p12 # 指定驗證的檔案路徑
cluster.initial_master_nodes: ["pawpaw-elk-c1", "pawpaw-elk-c2", "pawpaw-elk-c3"] # 指定集群第一次啟動的時可以選舉master的節點, 預設使用hostaname, 建議可改成elasticsearch.yml檔案中的node.name
http.host: 0.0.0.0 # 設定此節點走的HTTP地址

修改 elasticsearch service

1
2
3
4
5
vim /lib/systemd/system/elasticsearch.serviceㄗ

# 添加在 [service] 底下
# LimitMEMLOCK 鎖定內存不使用 SWAP
LimitMEMLOCK=infinity

啟動 elasticsearch

1
2
3
4
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service

確認 cluster 狀態

列出 elasticsearch version

curl -k -u elastic:<password> -XGET https://192.168.80.241:9200

elasticsearch-version

列出 elasticsearch cluster nodes 資訊

curl -k -u elastic:<password> -XGET https://192.168.80.241:9200/_cat/nodes?v

elasticsearch-nodes

重設 elasticsearch 密碼

1
2
3
4
# 隨機生成密碼
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic
# 自行設定密碼
/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i