環境介紹:CentOS七、jdk11.0.五、Elasticsearch7.5.1html
準備三臺Linux系統,本教程使用的是CentOS7以下IP地址:node
192.168.28.129 192.168.28.130 192.168.29.131
注意:
以上三臺Linux系統都要安裝jdklinux
Linux: elasticsearch-7.5.1-linux-x86_64.tar.gzbootstrap
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.5.1-linux-x86_64.tar.gz
Elasticsearch不能在 root
用戶下啓動,咱們須要在三臺機器上分別建立一個普通用戶:vim
# 建立elastic用戶 useradd elastic # 設置用戶密碼 passwd elastic # 切換到elastic用戶 su elastic
分別在三臺機器上的 /home/elastic/
目錄下建立elasticsearch文件夾,而後在elasticsearch文件夾下分別建立data、logs文件夾:服務器
cd /home/elastic/ mkdir -p elasticsearch/data mkdir -p elasticsearch/logs
在生產環境下咱們要把Elasticsearch生成的索引文件數據存放到自定義的目錄下
data:存儲Elasticsearch索引文件數據
logs:存儲日誌文件app
咱們只須要簡單的配置一下Elasticsearch就能夠使用了curl
首先咱們將下載好的 elasticsearch-7.5.1-linux-x86_64.tar.gz
壓縮包上傳到 192.168.28.129
這臺機器上的 /home/elastic/elasticsearch/
目錄下,隨便那一臺機器均可以沒有順序區分。elasticsearch
解壓 elasticsearch-7.5.1-linux-x86_64.tar.gz
ide
tar -xvf elasticsearch-7.5.1-linux-x86_64.tar.gz
解壓完成後在 /home/elastic/elasticsearch/
目錄下輸入 ll
命令查看當前目錄下的全部文件和文件夾以下:
輸入以下命令修改 elasticsearch.yml
配置文件:
vi elasticsearch-7.5.1/config/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: my-application # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # node.name: node-1 # # Add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- Paths ------------------------------------ # # Path to directory where to store the data (separate multiple locations by comma): # path.data: /home/elastic/elasticsearch/data # # Path to log files: # path.logs: /home/elastic/elasticsearch/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.28.129 # # Set a custom port for HTTP: # http.port: 9200 # # For more information, consult the network module documentation. # # --------------------------------- 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.28.129", "192.168.28.130", "192.168.28.131"] # # Bootstrap the cluster using an initial set of master-eligible nodes: # cluster.initial_master_nodes: ["node-1", "node-2", "node-3"] # # For more information, consult the discovery and cluster formation module documentation. # # ---------------------------------- Gateway ----------------------------------- # # Block initial recovery after a full cluster restart until N nodes are started: # #gateway.recover_after_nodes: 3 # # For more information, consult the gateway module documentation. # # ---------------------------------- Various ----------------------------------- # # Require explicit names when deleting indices: # #action.destructive_requires_name: true # # ---------------------------------- xpack ------------------------------------- # xpack.monitoring.collection.enabled: true
主要修改以下幾處配置:
cluster.name
的值必需要相同。0.0.0.0
(任何地址都能訪問到)。9200
便可,固然你也能夠修改咱們已經配置好一臺Elasticsearch節點了接下來咱們只須要把這臺配置好的Elasticsearch複製到另外兩臺機器中在作一些簡單的修改就就能夠了。
咱們使用 scp
命令複製當前配置好的Elasticsearch到另外兩臺機器中:
scp -r /home/elastic/elasticsearch/elasticsearch-7.5.1 elastic@192.168.28.130:/home/elastic/elasticsearch/elasticsearch-7.5.1 scp -r /home/elastic/elasticsearch/elasticsearch-7.5.1 elastic@192.168.28.131:/home/elastic/elasticsearch/elasticsearch-7.5.1
分別在兩臺機器中修改幾處配置:192.168.28.130
這臺機器修改elasticsearch.yml配置文件以下:
node.name: node-2 network.host: 192.168.28.130
192.168.28.131
這臺機器修改elasticsearch.yml配置文件以下:
node.name: node-3 network.host: 192.168.28.131
Elasticsearch能夠從命令行啓動,以下所示:
./bin/elasticsearch
做爲後臺啓動
要將Elasticsearch做爲後臺程序運行,請在命令中指定-d
,而後使用-p
將進程ID記錄在文件中:
./bin/elasticsearch -d -p pid
分別在三臺機器上啓動Elasticsearch,啓動過程當中建議單個機器啓動成功後在啓動另外一臺。
注意:
在啓動過程當中若是出現錯誤請看下面章節
下面的全部修改在生產環境下均可能不會出現,若是出現了就作相應的修改便可
修改系統配置文件須要切換到 root
用戶下:
su root
會提示你輸入 root
用戶密碼,輸入正確的密碼便可
將虛擬內存設置大一些,不然在啓動elasticsearch時會出錯致使啓動失敗:
ERROR: [1] bootstrap checks failed [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
輸入 vi /etc/sysctl.conf
命令在 sysctl.conf
中配置以下內容:
vm.max_map_count=655360
接着輸入以下命令讓配置生效:
sysctl -p
在啓動Elasticsearch有可能會出現以下錯誤:
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
錯誤說明:elasticsearch過程的最大文件描述符 [4096]
過低,增長到
最少 [65536]
接下來咱們修改最大文件描述符,輸入 vi /etc/security/limits.conf
命令在 limits.conf
中配置以下內容:
* soft nofile 65536 * hard nofile 131072 * soft nproc 2048 * hard nproc 4096
在啓動Elasticsearch有可能會出現以下錯誤:
[2]: max number of threads [1024] for user [elsearch] is too low, increase to at least[4096]
錯誤說明:線程[1024]
用戶[elsearch]
的最大數量過低,增長至少[4096]
接下來咱們修改線程數,輸入 vi /etc/security/limits.d/20-nproc.conf
命令在 20-nproc.conf
中配置以下內容:
* soft nproc 4096
在啓動Elasticsearch有可能會出現以下錯誤:
[3]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
錯誤說明:系統調用過濾器安裝失敗;檢查日誌和修復配置或禁用系統調用過濾器須要您自擔風險
這是在由於 Centos7
不支持 SecComp
,而ES5.2.0以後默認 bootstrap.system_call_filter
爲true
進行檢測,因此致使檢測失敗,失敗後直接致使ES不能啓動。
接下來咱們修改配置文件,輸入 vim config/elasticsearch.yml
命令,在 elasticsearch.yml
中配置以下內容:
# ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # #bootstrap.memory_lock: true # 設置爲false不進行檢測 bootstrap.system_call_filter: false # # 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. #
上面咱們已經搭建好了三個節點的集羣,而且已經啓動了。
接下來咱們來檢查一下集羣是否已經造成,給三臺服務器中的任意一臺發送http請求:
http://192.168.28.129:9200/_cat/health?v
應該會反饋以下內容:
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1579067395 05:49:55 my-application green 3 3 22 11 0 0 0 0 - 100.0%
cluster:顯示的是當前集羣的名稱
status:顯示的是 green
表示當前集羣是健康的狀態
node.total:顯示 3
表示當前集羣有三個節點
最後咱們的Elasticsearch集羣已經搭建好了,本教程主要參考Elastic官方文檔。