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集羣搭建成功了。