Windows 環境下的單節點 Elasticsearch 安裝指南
TLDR
- 建議將
path.data與path.logs設定在安裝目錄之外,以便於後續進行小版本升級。 - JVM 記憶體建議設定為系統可用記憶體的 50%,且
-Xms與-Xmx應設為相同數值以減少效能損耗。 - 設定 SSL/TLS 時,
xpack.security.http.ssl.keystore.path必須使用相對於config目錄的路徑,不可使用絕對路徑。 - 若啟動後
cluster_uuid顯示為_na_,代表叢集未正確初始化,請檢查cluster.initial_master_nodes設定。 - 使用
elasticsearch-certutil產生的憑證檔案,需透過elasticsearch-keystore指令將密碼寫入 keystore,否則服務無法讀取憑證。
基本設定與最佳實踐
YAML 設定檔 (config/elasticsearch.yml)
在進行基礎配置時,建議將資料與日誌路徑移至安裝目錄外,確保升級時資料的連續性。
- 節點與叢集設定:yaml
node.name: node-1 cluster.initial_master_nodes: ["node-1"] - 路徑設定:yaml
path.data: /path/to/data path.logs: /path/to/logs - 網路與 CORS 設定: 什麼情況下會遇到這個問題:當需要從瀏覽器或外部服務透過 API 存取 Elasticsearch 時,若未開啟 CORS 會導致連線被拒。yaml
network.host: 0.0.0.0 http.cors.enabled: true http.cors.allow-origin: "*"
JVM 記憶體設定 (config/jvm.options)
什麼情況下會遇到這個問題:當 JVM 記憶體配置不當,導致頻繁觸發記憶體重新分配,進而影響系統效能。
- 建議將
-Xms與-Xmx設定為相同數值。 - 記憶體配置不應超過系統總量的 50%,並至少預留 2GB 給作業系統。
安全性設定:SSL 與憑證管理
建立 SSL 憑證與 Keystore
什麼情況下會遇到這個問題:當啟用 xpack.security.http.ssl 後,若未正確配置 keystore 密碼或路徑,Elasticsearch 將無法啟動。
- 使用
elasticsearch-certutil http產生憑證。 - 建立 keystore 並加入憑證密碼:bash
elasticsearch-keystore create elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password - 踩雷紀錄:在
elasticsearch.yml中,xpack.security.http.ssl.keystore.path必須使用相對於config目錄的路徑(例如certs/http.p12),若使用絕對路徑會導致啟動失敗。
建立 x.509 傳輸憑證
什麼情況下會遇到這個問題:在多節點環境或特定伺服器配置下,若未設定 transport 層的 SSL,可能導致節點間通訊異常。
- 產生憑證:bash
elasticsearch-certutil cert --ca elastic-stack-ca.p12 --days 1825 - 設定 keystore:bash
elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
TIP
使用 elasticsearch-certutil http 產生的 http.p12 檔案已包含 CA 憑證,可同時作為 Keystore 與 Truststore 使用。若未指定 xpack.security.http.ssl.truststore.path,系統會自動沿用 Keystore 的設定。
服務啟動與管理
啟動與驗證
手動啟動時請使用系統管理員權限執行 bin/elasticsearch.bat。啟動後可透過瀏覽器存取 https://localhost:9200 進行驗證。
- 驗證結果:若回傳的 JSON 中
cluster_uuid為_na_,請檢查cluster.initial_master_nodes是否與node.name一致。
註冊 Windows 服務
為了避免視窗關閉導致服務中斷,建議將其註冊為 Windows 服務:
batch
elasticsearch-service.bat install註冊後,請至 Windows 服務管理員將 Elasticsearch 服務設定為「自動」啟動。
異動歷程
- 2025-01-23 初版文件建立。
- 2025-03-05 增加 x.509 憑證設定。
- 2025-03-18 補充 keystore 描述。
