iT邦幫忙

3

[ELK] Elasticsearch 安裝 (5.4.1版本)

為了工作專案搭建了ELK Cluster,正在陸續整理自己的筆記,希望分享給需要的人。

系統環境

Ubuntu 16.04
8 Core 64G RAM Server
500G Disk

環境需求

  • Java 8
    這邊使用的是Oracle Java 8,安裝指令:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

開始安裝

官網提供很多安裝方式,可以直接下載tar.gz或zip檔案解壓縮,CentOS可用rpm下載,Windows可下載msi安裝,也提供docker image直接使用。

Server的環境是Ubuntu,且我想讓Elasticsearch以Service方式運行,所以使用Deb安裝。

如果是第一次安裝elastic公司的產品,首先先安裝elasticsearch需要的package,並將repository definition儲存到/etc/apt/sources.list.d/elastic-5.x.list路徑下。

sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list

安裝指定版本的Elasticsearch

sudo apt-get update && sudo apt-get install elasticsearch=5.4.1

安裝完成後,可以使用systemd指令操作Elasticsearch Service:

# 開啟 Elasticsearch Service
sudo systemctl start elasticsearch.service
sudo systemctl stop elasticsearch.service
# 檢查 Elaticsearch Service 狀態
sudo systemctl status elasticsearch.service
# 將服務設置為開機自動啟動
sudo systemctl enable elasticsearch.service
# 檢查服務的開機自動啟動是否設置成功
sudo systemctl is-enabled elasticsearch.service
# 關閉 Elasticsearch Service
sudo systemctl stop elasticsearch.service

到這裡還沒更改Elasticsearch Config,建議先啟動服務簡單測試看看是否正常。
檢查內容:

  • Browser (send GET request)
    最簡單的檢查方法,打開瀏覽器,輸入 http://server_ip:9200
    看見類似以下訊息代表Elasticsearch的安裝大概沒什麼問題。
{
  "name" : "Cp8oag6",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA",
  "version" : {
  "number" : "5.4.1",
  "build_hash" : "f27399d",
  "build_date" : "2016-03-30T09:51:41.449Z",
  "build_snapshot" : false,
  "lucene_version" : "6.5.0"
  },
  "tagline" : "You Know, for Search"
}

如果出現任何錯誤,或是網頁打不開,先檢查server ip是否正確,是否需要開連線白名單或是防火牆擋住;
也可以從Log檔案debug。

  • Elasticsearch Log
    預設路徑 /var/log/elasticsearch/
  • Systemd Log
sudo journalctl -f --unit elasticsearch

修改elasticsearch.yml

接著來修改config,如果是透過deb安裝,config路徑會在/etc/elasticsearch/elasticsearch.yml
官網有建議修改某些config (Important Config),所有config詳細說明請再參考官網Config說明

path.data & path.logs

指定資料和log檔案的路徑,我們規劃是資料要存在特定路徑以利自動備份,所以修改路徑為:

path.data: ~/elk/elasticsearch/data
path.logs: ~/elk/elasticsearch/logs

假設需要將data分散存到多顆disk,也可以指定多個路徑

path:
  data:
    - /mnt/elasticsearch_1
    - /mnt/elasticsearch_2
    - /mnt/elasticsearch_3

特別要注意權限的問題,用deb安裝時,elasticsearch會在ubuntu server新增一組自己的帳號(即elasticsearch)。
如果修改了路徑,則指定路徑需要讓elasticsearch帳號可以讀取,否則啟動服務時會因權限不足而無法啟動。

sudo chown -R elasticsearch:elasticsearch ~/elk/elasticsearch/data
sudo chown -R elasticsearch:elasticsearch ~/elk/elasticsearch/logs

cluster.name

可自訂Cluster名稱,如果有多台elasticsearch node要加入cluster,則必須定義相同的cluster.name。

cluster.name: log_elk

node.name

Node.name會自動抓取hostname,如果想自訂就改一下

  • node.name: elk_1

bootstrap.memory_lock

這個config很重要,影響到elasticsearch的效能,elasticsearch底層的JVM
會進行swap,我們可以通過enable這個config,讓JVM不會swap到disk。

bootstrap.memory_lock: true

然而這個設定enable後要實質生效,需搭配Server本身的設定(官網詳細說明)。
以Ubuntu 16.04來說,先在/etc/systemd/system目錄下新增elasticsearch.service.d的資料夾

sudo mkdir /etc/systemd/system/elasticsearch.service.d

在自己的目錄先新增elasticsearch.conf檔案
(我的狀況是直接在/etc/systemd/system目錄下新增會有此檔案無法寫入訊息,所以用比較迂迴的作法)

cd ~/work/install_files
vi elasticsearch.conf

在新增的elasticsearch.conf檔案中,貼上這段設定,可以讓elasticsearch service不會限制memory,也就不會發生swap到disk的情況。

[Service]
LimitMEMLOCK=infinity

最後將elasticsearch.conf複製到 /etc/systemd/system/elasticsearch.service.d

sudo cp elasticsearch.conf /etc/systemd/system/elasticsearch.service.d

network.host

預設是綁定127.0.0.1
對開發用的Single Node來說,設定127.0.0.1是很方便的。
尤其是啟動了多個elasticsearch node在單一single node上,可以測試多個Elasticsearch的能力並模擬Cluster,但不建議在Production環境設定為127.0.0.1。

為了方便cluster中node之間互相溝通,node需要綁定non-loopback address,比如說:

network.host: 192.168.50.11

Elasticsearch有多種網路相關的設定,不過通常只需要定義network.host,如果有特殊需求,可以參考官網的network settingsnetwork.host特殊值

註:如果network.host不是使用預設值,則Elasticsearch會假定已從開發模式轉為Production模式,啟動時的檢查會從warnings躍升為exceptions,基本上沒有什麼大影響

discovery.zen.ping.unicast.hosts

預設狀況下,elasticsearch會綁定可用的loopback address,並掃描9300-9305 port,來試圖連線到同台server上的其他node。
所以若要建立Cluster,需要提供cluster中其他node的seed list給elasticsearch去做連線。
比如說:

discovery.zen.ping.unicast.hosts:
   - 192.168.1.10:9300
   - 192.168.1.11 
   - seeds.mydomain.com

我的環境只有一個Node,所以只需要設定一個。

discovery.zen.ping.unicast.hosts:
   - 192.168.50.11

discovery.zen.minimum_master_nodes

這個config是用來避免資料遺失,當cluster遇到網路故障時,cluster可能會被切為兩個獨立的cluster,即所謂的split brain,這種狀況會導致資料遺失。
此設定可以讓有master node資格的node知道"擁有master node資格的node最小的可用數量",才能形成一整個cluster。

計算方式為(master_eligible_nodes / 2) + 1
假設有3個擁有master node資格的node,則最小master node數量為(3/2)+1,也就是2。

不過我的環境只有一個Node,所以直接設為1。

discovery.zen.minimum_master_nodes: 2

修改JVM

elasticsearch default JVM大小為 1G,而在elasticsearch 5.0之後, default JVM大小為 2G,如果要用到更大的Memory,可修改/etc/elasticsearch/jvm.options設定。

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

# 改這兩個
-Xms12g
-Xmx12g
 
# the settings shipped with ES 5 were: -Xms2g
# the settings shipped with ES 5 were: -Xmx2g

註: 就算設定使用系統全部的Memory,elasticsearch最多也只會使用系統50%的Memory,且不建議設定超過32G,可能會影響效能


修改Log相關設定

Log相關設定在/etc/elasticsearch/log4j2.properties檔案中。

換檔規則

預設為TimeBased,每1天進行換檔,這邊續用不作更改。

appender.rolling.policies.time.type = TimeBasedTriggeringPolicy 
appender.rolling.policies.time.interval = 1 
appender.rolling.policies.time.modulate = true 

Log保留天數

如果希望只保留一段時間內的logs檔,可以更改delete action,預設是保留7天內的log,這邊改為5天。

appender.rolling.strategy.action.condition.age = 5D 

Log Level

Elasticsearch有多種log,可以依不同的log去設定log level。
Log Level可透過修改log4j2.properties後重啟elasticsearch,也可以送Request給elasticsearch動態修改。

從檔案修改:

logger.transport.level = debug

送Request動態修改(elasticsearch服務必須是啟動的)

curl -XPUT 'localhost:9200/_cluster/settings?pretty' -H 'Content-Type: application/json' -d'
{
  "transient": {
    "logger.org.elasticsearch.transport": "debug"
  }
}
'

Deprecation Log

這個log是記錄未來版本將棄用的action,當有升級到新版本的規劃時,需要檢查deprecation log,確保功能不受影響。
如果暫時沒有升級的規劃,這個log又一直有資料進來還蠻佔空間的,可以將此log level改為error,將不會再輸出deprecation log。

logger.deprecation.level = error

設定都調整完之後,重啟elasticsearch服務。

sudo systemctl restart elasticsearch.service

如同前述的方法,使用瀏覽器連到http://server_ip:9200,檢查是否正常運作。

以上就是elasticsearch安裝的過程,後續會整理logstash, kibanabeats的安裝過程,使用pluginsearch-guard為elasticsearch加上驗證功能,以及使用elastalert來建立監控系統等。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言