Elasticsearch是一個非常好用的搜尋引擎,和Solr一樣,他們都是基于反向索引的。今天我們就看一看Elasticsearch如何進行安裝。
下載下傳和安裝
今天我們的目的是搭建一個有3個節點的Elasticsearch叢集,是以我們找了3台虛拟機,ip分别是:
- 192.168.73.130
- 192.168.73.131
- 192.168.73.132
然後我們要下載下傳ES,這裡我們采用的版本是
7.6.0
。我們進入到
/opt
目錄下,下載下傳elasticsearch7.6.0
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-linux-x86_64.tar.gz
下載下傳的過程比較慢。下載下傳完成後,我們解壓:
tar -zxvf elasticsearch-7.6.0-linux-x86_64.tar.gz
在啟動elasticsearch之前,這裡有一個重點:ES在啟動的時候是不允許使用root賬戶的,是以我們要建立一個elasticsearch使用者:
useradd elasticsearch
然後把
elasticsearch-7.6.0
這個目錄和目錄下所有的檔案的擁有者都改成elasticsearch:
chown elasticsearch:elasticsearch -R elasticsearch-7.6.0
然後,我們切換到
elasticsearch
使用者:
su - elasticsearch
我們将ES安裝在
/opt
目錄下,先進入到
/opt
目錄,
cd /opt/elasticsearch-7.6.0
我們啟動一下,看看單機版能不能啟動成功。
./bin/elasticsearch
可以啟動成功。但是我們通過浏覽器通路這個ip的9200端口時,是不成功的。我們需要對elasticsearch進行配置,才可以在别的機器上通路成功。
ES的配置
es的所有配置檔案都在
${ES_HOME}/config
這個目錄下,首先我們設定一下jvm參數,打開jvm.options檔案,
vim jvm.options
################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms256m
-Xmx256m
我們改一下堆記憶體的大小,我這裡使用的虛拟機,隻配置設定了1g的記憶體,是以,我這裡統一調整為256m記憶體,大家可以根據自己機器的記憶體情況進行調整。
然後,我們再打開
elasticsearch.yml
檔案,配置一下這裡邊的參數。
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: cluster-a
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-130
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
我們先配置一下叢集的名字,也就是
cluster.name
,在這裡,我們叫做
cluster-a
。在另外兩台機器上,叢集的名字也要叫做
cluster-a
,這樣才能夠組成一個叢集。在ES中,叢集名字相同的節點,會組成ES叢集。
然後,我們再修改
node.name
節點名稱,這個名稱是每一個節點的,是以,每個節點的名稱都不能相同。這裡我們以ip命名,130這台機器,節點名稱就叫
node-130
,另外兩台叫做
node-131
和
node-132
。
我們再接着看後面的配置,
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.73.130
#
# Set a custom port for HTTP:
#
#http.port: 9200
路徑和記憶體,咱們使用預設的就好,咱們重點看一下網絡。我們需要指定一下ES綁定的位址,如果不設定,那麼預設綁定的就是localhost,也就是127.0.0.1,這樣就隻有本機能夠通路了,其他機器是通路不了的。是以這裡我們要綁定每台機器的位址,分别是
192.168.73.130
,
192.168.73.131
192.168.73.132
接下來,我們看一下叢集的相關配置,
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["192.168.73.130", "192.168.73.131","192.168.73.132"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node-130", "node-131", "node-132"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
也就是Discovery這一段的配置,我們先設定一下叢集中節點的位址,也就是
discovery.seed_hosts
這一段,我們把3台機器的ip寫在這裡。然後再把3台機器的節點名稱寫在
cluster.initial_master_nodes
,好了,叢集的配置到這裡就告一段落了。
系統配置
接下來我們再看看重要的系統配置。在ES的官網上,有這樣一句話,
Ideally, Elasticsearch should run alone on a server and use all of the resources available to it.
翻譯過來是,合理的做法是,ES應該在一個服務中單獨運作,并且可以使用這個機器中所有的可用資源。
隻要你在配置檔案中配置了
network.host
,ES就認為你将釋出生産環境,如果你的一些配置不正确,那麼ES就不會啟動成功。在這裡,ES也要求我們對系統的一些配置做出修改。
ulimit調整
首先,我們要修改Linux系統的檔案打開數,将其調到65535。
su -
ulimit -n 65535
exit
然後再修改
limits.conf
檔案,我們同樣切換到root使用者,打開
limits.conf
檔案,
vim /etc/security/limits.conf
在檔案的最後添加
elasticsearch - nofile 65535
,然後儲存退出。
關閉swapping
其次,在ES的官方文檔上,要求
Disabled Swapping
,我們要關掉它。執行以下指令:
sudo swapoff -a
這隻是臨時的關閉swapping,重新開機linux後,會失效。如果要永久的關閉swapping,需要編輯
/etc/fstab
檔案,将包含swap的行的注釋掉。
/dev/mapper/centos-root / xfs defaults 0 0
UUID=6a38540f-2ba9-437b-ac8b-8757f5754fff /boot xfs defaults 0 0
# /dev/mapper/centos-swap swap swap defaults 0 0
調整mmapfs的數值
由于ES是使用
mmapfs
存儲索引,但是系統的預設值太低了,我們調高一點。
sysctl -w vm.max_map_count=262144
線程的數量
確定elasticsearch使用者最少可建立4096個線程。我們還是要以root使用者去設定。
su -
ulimit -u 4096
同樣,這知識臨時的方案,linux重新開機後會失效,我們需要修改配置檔案
/etc/security/limits.conf
,将
nproc
設定為4096。
elasticsearch - nproc 4096
好,到這裡我們所有的配置就完成了,現在依次啟動3個節點的ES。啟動完成後,我們在浏覽器中檢查以下叢集的狀态,
http://192.168.73.130:9200/_cluster/health
{"cluster_name":"cluster-a","status":"green","timed_out":false,"number_of_nodes":3,"number_of_data_nodes":3,"active_primary_shards":0,"active_shards":0,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":0,"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":100.0}
我們看到status是green。說明我們的ES叢集搭建成功了。