Elasticsearch 安裝

Alt Elasticsearch

因為工作上需要, 所以記錄一下!!

安裝 Java SDK

確認JAVA版本

查看系統當前的java版本:

java -version

如果發現是老版本,可以進一步查詢系統已經安裝的JDK
如果是舊的就先移除

rpm -qa | grep -E '^(java|jdk)'

下載JDK

官網下載 64位元的JDK
這邊要留意 JDK 的版本, 目前 Elasticsearch 6.X 版本,需要JDK 1.8 以上
下載之後進行安裝

安裝JDK

[root@ELK]# rpm -ivh jdk-8u192-linux-x64.rpm

更多資訊: SDK 下載

安裝 Elasticsearch

前置準備 - 更新Linux下載源

在 /etc/yum.repos.d/新增一個 elasticsearch.repo 檔
並加入以下設定

[elasticsearch-6.x]
name=Elasticsearch repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

yum安裝

加好之後就用 yum 直接安裝

[root@ELK]# yum install elasticsearch

服務啟動與確認

如果安裝過程沒有意外,可以先啟動看看

[root@ELK]# systemctl start elasticsearch.service

確認狀態,應該會得到類似以下第4行active 狀態與第15行的回應

[root@ELK]# systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2018-12-07 16:04:07 CST; 2 days ago
Docs: http://www.elastic.co
Main PID: 13469 (java)
CGroup: /system.slice/elasticsearch.service
├─13469 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -Xss1m ...
└─13548 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Dec 07 16:04:07 ELK.localdomain systemd[1]: Stopped Elasticsearch.
Dec 07 16:04:07 ELK.localdomain systemd[1]: Started Elasticsearch.

[root@ELK]# curl "http://localhost:9200/_cat/nodes"
localhost 10 51 0 0.00 0.01 0.05 mdi * MkpewMs

也可以用 http://localhost:9200/_cluster/health 取得運作資訊

[root@ELK conf.d]# curl http://localhost:9200/_cluster/health
{
"cluster_name":"elasticsearch",
"status":"yellow",
"timed_out":false,
"number_of_nodes":1,
"number_of_data_nodes":1,
"active_primary_shards":14,
"active_shards":14,
"relocating_shards":0,
"initializing_shards":0,
"unassigned_shards":10,
"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":58.333333333333336
}

因為我們目前只安裝一台,所以上述的status為yellow,表示資料完整,雖可運作但少了一份副本。如果Cluster機制運作正常,那應該就會顯示綠燈。
但萬一顯示為紅燈的話,那可能代表連API服務都無法正常運作。

更多資訊: Elasticsearch 官方安裝教學

Elasticsearch設定

Elasticsearch裝好後的設定目錄在/etc/elasticsearch/,主要的設定檔則是 elasticsearch.yml
初始裝好後會發現用本機用 curl "http://localhost:9200/_cat/nodes"是有回應的
但是 curl 對外的網址,例: curl "http://192.97.140.16:9200/_cat/nodes" 卻會得到
curl: (7) Failed connect to localhost:9200; Connection refused的回應

這時要修改 elasticsearch.yml 裡 network.host 設定

elasticsearch.yml

將在elasticsearch.yrml裡將 network.host 改成主機對外提供服務的IP

network.host: 192.97.140.16

這樣在本機上詢問就會正常了

[root@ELK]# curl "http://192.97.140.16:9200/_cat/nodes"
192.97.140.16 11 51 0 0.00 0.01 0.05 mdi * MkpewMs

防火牆相關參數

除了設定檔要改之外,防火牆相關的設定也要開,否則對外的電腦用瀏覽器也會問不到的

firewall-cmd --add-port=9200/tcp --permanent
firewall-cmd --reload

好站分享

您可能也會喜歡…

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *