Elasticsearch介紹
Elasticsearch簡稱ES,是一個基於Lucene的搜索服務器。它提供了一個分布式、高擴展、高實時的搜索與數據分析引擎,基於RESTful web接口,它能很方便的使大量數據具有搜索、分析和探索的能力。
Elasticsearch的實現原理主要分為以下幾個步驟,首先用戶將數據提交到Elasticsearch 數據庫中,再通過分詞控制器去將對應的語句分詞,將其權重和分詞結果一並存入數據,當用戶搜索數據時候,再根據權重將結果排名,打分,再將返回結果呈現給用戶。
應用場景
- 全文搜索
Elasticsearch提供了全文搜索的功能,適用於電商商品搜索、App搜索、企業內部信息搜索、IT系統搜索等。
打個比方,在我們數據庫做模糊查詢時,如LIKE語句,它會遍歷整張表,同時進行字符串匹配,這時候如果我們的表數據很多時,響應速度可想而知,所以這時候我們就可以引入ES。將數據庫與我們ES數據同步,通過ES來完成我們的大數據檢索,提高我們的查詢效率。
- 日志分析
復雜的業務場景通常會產生繁雜多樣的日志,如Apache Log、System Log、MySQL Log等,往往很難從繁雜的日志中獲取價值,卻要承擔其存儲的成本。Elasticsearch能夠借助Beats、Logstash等快速對接各種常見的數據源,並通過集成的Kibana高效地完成日志的可視化分析,讓日志產生價值。
接下來,我會對ES及ES的一些相關插件進行部署
ES與其他工具版本對應關系可參考https://www.elastic.co/cn/support/matrix#matrix_compatibility
Elasticsearch部署
該部署以Centos7為例
#這里我只使用了一台設備(192.168.111.129)來進行實驗
安裝前提:需先配置好1.8的JAVA環境,可參考JDK的安裝配置(Windows、Linux)
ElasticSearch與JDK版本的版本對應關系可參考https://www.elastic.co/cn/support/matrix#matrix_jvm
ElasticSearch下載地址: https://mirrors.huaweicloud.com/elasticsearch/?C=N&O=D
1.關閉防火牆和SELinux
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
vi /etc/selinux/config
SELINUX=enforcing改為SELINUX=disabled
2.解壓安裝包
tar -zxvf elasticsearch-7.6.1-linux-x86_64.tar.gz -C /usr/local/
3.配置ES配置文件
cd /usr/local/elasticsearch-7.6.1/config/
vi elasticsearch.yml
#將以下注釋去除並修改 #配置es的集群名稱 cluster.name: my-es #節點名稱 node.name: node-1 #設置索引數據的存儲路徑 path.data: /usr/local/elasticsearch-7.6.1/data #設置日志的存儲路徑 path.logs: /usr/local/elasticsearch-7.6.1/logs #設置當前的ip地址,通過指定相同網段的其他節點會加入該集群中 network.host: 0.0.0.0 #設置對外服務的端口 http.port: 9200 #首次啟動全新的Elasticsearch集群時,會出現一個集群引導步驟,該步驟確定了在第一次選舉中便對其票數進行計數的有資格成為集群中主節點的節點的集合(投票的目的是選出集群的主節點),單節點就配置一個即可 cluster.initial_master_nodes: ["node-1"]
4.創建ES相關配置文件
mkdir -p /usr/local/elasticsearch-7.6.1/data
mkdir -p /usr/local/elasticsearch-7.6.1/logs
5.創建ES用戶並授權
因為安全問題elasticsearch 不讓用root用戶直接運行,所以要創建新用戶
useradd es
echo "123" |passwd --stdin es
chown -R es:es /usr/local/elasticsearch-7.6.1/
6.配置資源使用
vi /etc/security/limits.conf
* soft nofile 65536 * hard nofile 131072 * soft nproc 65535 * hard nproc 65535 End of file
7.配置虛擬內存大小
vi /etc/sysctl.conf
#添加下面配置 vm.max_map_count=655360
sysctl -p
8.啟動ES
su - es
cd /usr/local/elasticsearch-7.6.1/bin/
nohup ./elasticsearch &
查看日志(因為是nohup啟動的,bin下面也會有一個nohup.out記錄日志)
cd /usr/local/elasticsearch-7.6.1/logs
tail -f my-es.log
9.訪問ES web頁面
訪問IP:9200即可
ES集群部署可參考Elasticsearch集群部署
Elasticsearch-head安裝
elasticsearch-head將是一款專門針對於elasticsearch的客戶端工具。elasticsearch-head是用於監控Elasticsearch狀態的客戶端插件,包括數據可視化、執行增刪改查操作等。
由於head插件本質上還是一個nodejs的工程,因此需要安裝node,使用npm來安裝依賴的包。
nodejs下載地址:https://nodejs.org/en/download/
ElasticSearch-head下載地址:https://github.com/mobz/elasticsearch-head
1.解壓nodejs安裝包
tar -xf node-v14.18.0-linux-x64.tar.xz -C /usr/local/
2.配置nodejs的環境變量
vi /etc/profile
export NODE_HOME=/usr/local/node-v14.18.0-linux-x64 export PATH=$PATH:$NODE_HOME/bin
source /etc/profile
node -v
v14.18.0
npm -v
6.14.15
3.建立軟連接
ln -s /usr/local/node-v14.18.0-linux-x64/bin/npm /usr/local/bin/
ln -s /usr/local/node-v14.18.0-linux-x64/bin/node /usr/local/bin/
4.安裝cnpm命令
npm install -g cnpm --registry=https://registry.npm.taobao.org
5.解壓ElasticSearch-head安裝包並授權
unzip elasticsearch-head-master.zip -d /usr/local/
cd /usr/local/elasticsearch-head-master/
cnpm install
chown -R es:es /usr/local/elasticsearch-head-master/
6.修改ElasticSearch-head配置文件
vim Gruntfile.js
#找到connect,然后在true后面加逗號,然后換行添加hostname: '*' (注意,冒號后面有空格)
vim _site/app.js
#可以通過/app.App = ui進行查找,將localhost修改為ES的IP地址
7.修改ElasticSearch配置文件
cd /usr/local/elasticsearch-7.6.1/config/
vim elasticsearch.yml
#添加如下參數,啟用CORS,這里注意后面不允許出現空格 http.cors.enabled: true http.cors.allow-origin: "*"
8.重啟ES並啟動head
可以先通過Kill命令殺掉ES進程
su - es
cd /usr/local/elasticsearch-7.6.1/bin/
nohup ./elasticsearch &
cd /usr/local/elasticsearch-head-master/
nohup npm start &
查看日志
tail -f nohup.out
查看進程
ps -ef|grep grunt es 4946 4934 0 4月05 pts/0 00:00:02 grunt
9.訪問ElasticSearch-head web頁面
訪問IP:9100即可
可參考elasticsearch-head頁面說明及使用繼續了解
Kibana安裝
Kibana下載地址:https://mirrors.huaweicloud.com/kibana/?C=N&O=D
1.解壓安裝包
tar -zxvf kibana-7.6.1-linux-x86_64.tar.gz -C /usr/local/
2.配置Kibana配置文件
cd /usr/local/kibana-7.6.1-linux-x86_64/config/
vim kibana.yml
#server.port: 5601 #本機IP地址 server.host: "192.168.111.129" #ES的端口及地址 elasticsearch.hosts: ["http://192.168.111.129:9200"] elasticsearch.requestTimeout: 90000 #中文化 i18n.locale: "zh-CN"
3.授權及啟動Kibana
chown -R es:es /usr/local/kibana-7.6.1-linux-x86_64/
su - es
cd /usr/local/kibana-7.6.1-linux-x86_64/bin/
nohup ./kibana &
查看日志
tail -f nohup.out
4.查看進程
Kibana的進程通過ps -ef|grep kibana是無法查看到的,所以我們可以通過端口來進行查看
netstat -tunlp|grep 5601 tcp 0 0 192.168.111.129:5601 0.0.0.0:* LISTEN 3974/./../node/bin/
要想通過ps來查看的話可以通過ps -ef|grep node來進行查看,不過node查看的話,不一定會准確
ps -ef|grep node es 3974 3816 1 4月05 pts/0 00:01:54 ./../node/bin/node ./../src/cli root 5029 4983 0 00:01 pts/0 00:00:00 grep --color=auto node
5.訪問Kibana web頁面
訪問IP:5601即可
6.測試連通性
接下來我們就可以了解一下Kibana的一些具體操作了,可參考Kibana使用說明
elasticsearch-analysis-ik分詞器插件安裝
作用:如果直接使用Elasticsearch的分詞器在處理中文內容的搜索時,ES會將中文詞語分成一個一個的漢字。當用Kibana作圖,按照term來分組的時候,也會將一個漢字單獨分成一組。這對於我們的使用是及其不方便的,因此我們引入es之中文的分詞器插件es-ik就能解決這個問題。
elasticsearch-analysis-ik分詞器下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
1.創建分詞器目錄
分詞器安裝在ES目錄下即可,所有在ES目錄下創建分詞器目錄
cd /usr/local/elasticsearch-7.6.1/plugins/
mkdir ik
2.解壓安裝包
unzip elasticsearch-analysis-ik-7.6.1.zip -d /usr/local/elasticsearch-7.6.1/plugins/ik/
3.重新對ES目錄授權
chown -R es:es /usr/local/elasticsearch-7.6.1/
4.重啟ES和Kibana
可以先通過Kill命令殺掉ES和Kibana進程
su - es
cd /usr/local/elasticsearch-7.6.1/bin/
nohup ./elasticsearch &
此時的ES日志是可以查看到是有加載分詞器插件的
cd /usr/local/kibana-7.6.1-linux-x86_64/bin/
nohup ./kibana &
日志分析場景實現可參考ELK與EFK部署