一,前言
Elasticsearch(簡稱 ES)是一個基于 ??Apache Lucene?? 構建的開源分布式搜索與分析引擎。它支持??實時數據處理??,提供近實時的全文搜索能力,并通過 ??JSON 格式的 RESTful API?? 實現數據索引與檢索。其分布式架構設計可水平擴展至數百節點,適用于??日志分析、全文檢索、大數據實時查詢??等場景,是 Elastic Stack(ELK)生態的核心組件。
二,環境準備
- 操作系統:Centos7
- Java 版本:java 1.8
- Elasticsearch 版本:7.10.2
注意:如果Elasticsearch為8.x的版本,需要jdk17及以上版本
三,優化服務器參數
Elasticsearch對服務器的一些參數有限制,需要調節參數適配es
1,修改limits.conf文件
vim /etc/security/limits.conf# 在文件底部添加如下配置
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
2,調整vm.max_map_count參數
執行如下命令
# 設置參數值至少為 262144
echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p# 查看參數值
sysctl vm.max_map_count
# 預期輸出:vm.max_map_count = 262144
四,安裝Elasticsearch
1,創建es安裝目錄
mkdir -p /elk/elastic
cd /elk/elastic
2,下載Elasticsearch安裝包并解壓
方式1:本地下載再上傳服務器
下載地址:Elasticsearch 7.10.2 | Elastic
方式2:使用wget命令下載
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.2-linux-x86_64.tar.gz
解壓安裝包
tar -zxvf elasticsearch-7.10.2-linux-x86_64.tar.gz
3,修改jvm內存
Elasticsearch7.X版本默認的堆內存大小是1G,8.X版本默認的堆內存大小是4G,如果服務器內存不夠啟動會報錯,可以將堆內存設置小一點
vim /efk/elastic/elasticsearch-7.10.2/config/jvm.options# 根據服務器的內存來設置,如果服務器內存小可以將值調小一點
-Xms512m
-Xmx521m
4,創建es的用戶
// 創建用戶組
groupadd esgroup// 在esgroup用戶組下創建es用戶
useradd es -g esgroup// 賦權限
chown -R es:esgroup /efk/elastic/elasticsearch-7.10.2
5,修改es配置文件
vim /efk/elastic/elasticsearch-7.10.2/config/elasticsearch.yml內容如下:
# 集群名稱
cluster.name: my-es-cluster
# 節點名稱
node.name: node-1
# 運行遠程訪問
network.host: 0.0.0.0
http.port: 9200
# 單節點模式
discovery.type: single-node # 跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, Content-Type, Content-Length
6,啟動es
注意:如果安裝的es版本為8.x,es會默認開啟安全安全配置,第一次啟動es時會自動生成如下的配置代碼,如果需要訪問es可以關閉安全認證后再訪問
xpack.security.enabled: true? #? true表示開啟安全認證,開啟時訪問es需要數據賬號密碼xpack.security.enrollment.enabled: true
xpack.security.http.ssl:enabled: false # 是否啟用https,默認啟用,手動關閉,如果為true,服務es需要使用 httpskeystore.path: certs/elastic-certificates.p12truststore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl:enabled: trueverification_mode: certificatekeystore.path: certs/elastic-certificates.p12truststore.path: certs/elastic-certificates.p12
后臺啟動es
sudo -u es bin/elasticsearch -d
瀏覽器訪問ip+端口(9200)結果如下表示安裝成功
7,將es配置到systemd中
創建 elasticsearch.service 文件
// 創建service文件
vim /etc/systemd/system/elasticsearch.service內容如下:
[Unit]
Description=Elasticsearch Service
After=network.target[Service]
Type=simple
User=es # es用戶
Group=esgroup # es用戶組
LimitNOFILE=100000
LimitNPROC=100000
ExecStart=/efk/elastic/elasticsearch-8.17.0/bin/elasticsearch # es安裝路徑
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
PrivateTmp=true[Install]
WantedBy=multi-user.target
刷新配置
systemctl daemon-reload
基礎命令?
systemctl status elasticsearch.service # 查看狀態
systemctl start elasticsearch.service # 啟動
systemctl stop elasticsearch.service # 關閉
systemctl restart elasticsearch.service # 重啟
8,開啟安全認證
開啟認證前需要先刪除 /data 和 /logs 下的所有數據
[root@ali-kafka-vm1 elasticsearch-7.10.2]# rm -rf ./data/*
[root@ali-kafka-vm1 elasticsearch-7.10.2]# rm -rf ./logs/*
1,編輯es配置文件
vim /efk/elastic/elasticsearch-7.10.2/config/elasticsearch.yml添加如下配置
xpack.security.enabled: true
2,重啟es
systemctl restart elasticsearch.service
3,設置用戶密碼
Elasticsearch7.x版本設置密碼
該命令會為所有內置用戶設置密碼,不能跳過
./bin/elasticsearch-setup-passwords interactive
Elasticsearch8.x版本設置密碼
8.x版本可以指定設置內置用戶 elastic 的密碼
bin/elasticsearch-reset-password --username elastic -i
4,設置密碼后訪問es需要輸入賬號密碼
至此安裝成功!