一.搭建elasticsearch
1. 上傳jdk-8u181-linux-x64.tar.gz和elasticsearch-6.1.4.tar.gz文件到系統(tǒng)任意目錄,
安裝java
解壓jdk-8u181-linux-x64.tar.gz文件
tar -zxvf jdk-8u181-linux-x64.tar.gz
編輯profile文件,添加jdk環(huán)境變量
vim /etc/profile
在最后添加
#JDK環(huán)境變量
JAVA_HOME=/usr/local/jdk1.8.0_181
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH
引用環(huán)境變量
source /etc/profile
直接執(zhí)行java? –version確定java已可用
解壓elasticsearch-6.1.4.tar.gz文件
tar –zxvf elasticsearch-6.1.4.tar.gz
編輯修改elasticsearch的配置文件elasticsearch.yml
vim elasticsearch-6.1.4/config/elasticsearch.yml
主要修改以下信息(數(shù)據(jù)存放目錄以及日志存放目錄需要添加執(zhí)行權(quán)限,)

配置文件解析如下:

開啟服務(wù)(可以使用-d后臺(tái)啟用)
/usr/local/elasticsearch-6.1.4/bin/elasticsearch
樂維Tips :在啟動(dòng)服務(wù)時(shí),不能以root用戶開啟服務(wù),elasticsearch需要以其他用戶開啟服務(wù);
1.需要?jiǎng)?chuàng)建用戶;
添加es用戶
useradd es -m
設(shè)置es用戶密碼
passwd es
2.需要修改elasticsearch-6.1.4目錄、數(shù)據(jù)存放目錄、日志存放目錄的所屬組和所屬用戶
chown es:es /usr/local/elasticsearch-6.1.4/data
chown es:es /usr/local/elasticsearch-6.1.4/logs
chown es:es /usr/local/elasticsearch-6.1.4/
3、ERROR:bootstrap checks failed:max file descriptors [4096] for elasticsearchprocess likely too low, increase to at least [65536]
原因:無(wú)法創(chuàng)建本地文件問題,用戶最大可創(chuàng)建文件數(shù)太小
解決方案:
切換到root用戶,編輯limits.conf配置文件,
添加類似如下內(nèi)容:
vi /etc/security/limits.conf
添加如下內(nèi)容:
*? soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
注:* 代表Linux所有用戶名稱(比如hadoop)
保存、退出、重新登錄才可生效
4、max virtualmemory areas vm.max_map_count [65530] likely too low, increase to at least[262144]
原因:最大虛擬內(nèi)存太小
解決方案:切換到root用戶下,修改配置文件sysctl.conf
vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并執(zhí)行命令:sysctl -p
5、max number ofthreads [1024] for user [es] likely too low, increase to at least [2048]
原因:無(wú)法創(chuàng)建本地線程問題,用戶最大可創(chuàng)建線程數(shù)太小
解決方案:
切換到root用戶,進(jìn)入limits.d目錄下,修改90-nproc.conf 配置文件。
vi /etc/security/limits.d/90-nproc.conf
修改 * soft nproc 1024 為* soft nproc 2048
以es用戶啟動(dòng)服務(wù)(-d 可以后臺(tái)啟動(dòng))
su es
/usr/local/elasticsearch-6.1.4/bin/elasticsearch
開啟防火墻端口9200 TCP
firewall-cmd --zone=public --add-port=9200/tcp–permanent
在WEB上輸入IP:9200測(cè)試是否可以正常訪問使用

二.?與zabbix對(duì)接
需要修改zabbix_server的配置文件
修改HistoryStorageURL=http://192.168.150.10:9200

修改zabbix.conf.php文件
vi/usr/local/nginx/html/zabbix/conf/zabbix.conf.php

重啟zabbix_server
同步成功后

方法1、ElasticSearch Head可以直接在谷歌瀏覽器上進(jìn)行安裝(推薦)
直接把文件拖到擴(kuò)展程序進(jìn)行添加

方法2、這里介紹一個(gè) Elasticsearch 可視化的管理插件 elasticsearch-head,可方便的查看,刪除,管理數(shù)據(jù)
安裝elasticsearch-head插件需要nodejs的支持
[if !supportLists]1.? [endif]nodejs安裝
[if !supportLists]2.?????[endif]head安裝配置
請(qǐng)參考https://blog.csdn.net/zoubf/article/details/79007908
三. Elasticsearch常用命令
(由于沒有自帶的數(shù)據(jù)清理功能,需要手動(dòng)編寫腳本)
1、curl 命令的使用
curl -X'://:/?'-d ''
VERB???????HTTP方法:GET, POST, PUT, HEAD, DELETE
PROTOCOL???http或者h(yuǎn)ttps協(xié)議(只有在Elasticsearch前面有https代理的時(shí)候可用)
HOST???????Elasticsearch集群中的任何一個(gè)節(jié)點(diǎn)的主機(jī)名
PORT???????Elasticsearch HTTP服務(wù)所在的端口,默認(rèn)為9200
PATH???????API路徑,資源路徑(例如_count將返回集群中文檔的數(shù)量)
QUERY_STRING??? 一些可選的查詢請(qǐng)求參數(shù),例如?pretty參數(shù)將返回易讀的JSON數(shù)據(jù)
BODY???????一個(gè)JSON格式的請(qǐng)求主體(如果請(qǐng)求需要的話)
具體詳細(xì)方法,請(qǐng)參考
https://www.linuxidc.com/Linux/2016-10/136548.htm
添加索引(zabbix為數(shù)據(jù)庫(kù)名)
curl -XPUT'http://192.168.1.20:9090/zabbix?pretty'
創(chuàng)建成功后,默認(rèn)會(huì)分配五個(gè)主分片(創(chuàng)建后不可更改,通過(guò)算法將數(shù)據(jù)存放在這五個(gè)分片中的一個(gè),增刪改都是針對(duì)主分片的)和一個(gè)復(fù)制分片(可修改,每個(gè)主分片都對(duì)應(yīng)一個(gè)復(fù)制分片),這兩個(gè)默認(rèn)值都可以在配置文件中修改,也可以在創(chuàng)建的時(shí)候指定,如
curl -XPUT 'http://192.168.1.20:9090/zabbix?pretty'-d '{
??? "settings": {
??????? "number_of_shards" : 2,???? #2個(gè)主分片
??????? "number_of_replicas" : 0??? #0個(gè)復(fù)制分片
??? }
}'
查看索引
curl -XGET'http://192.168.1.20:9090/zabbix?pretty'
查詢方式;
例如:
查詢所有文檔等同于 curl
-XGET ‘http://localhost:9200/test/article/_search?pretty’
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d '
{
??? "query": {
??????? "match_all": {}
??? }
}'
#返回
{
? # 用時(shí) 毫秒
? "took" : 4,
? "timed_out" : false,
? #分片信息
? "_shards": {
??? "total" : 5,
??? "successful" : 5,
??? "failed" : 0
? },
? "hits": {
??? #文檔數(shù)
??? "total" : 3,
??? "max_score" : 1.0,
??? "hits": [ {
????? "_index" : "test",
????? "_type" : "article",
????? "_id" : "AVf_6fM1vEkwGPLuUJqp",
????? "_score" : 1.0,
????? "_source": {
??????? "id" : 2,
??????? "subject" : "第二篇文章標(biāo)題",
??????? "content" : "第二篇文章內(nèi)容",
??????? "author" : "jam"
????? }
??? }, {
????? "_index" : "test",
????? "_type" : "article",
????? "_id" : "4",
????? "_score" : 1.0,
????? "_source": {
??????? "id" : 4,
??????? "subject" : "第四篇文章標(biāo)題",
??????? "content" : "第四篇文章內(nèi)容-更新后",
??????? "author" : "tomi"
????? }
??? }, {
????? "_index" : "test",
????? "_type" : "article",
????? "_id" : "3",
????? "_score" : 1.0,
????? "_source": {
??????? "id" : 3,
??????? "subject" : "第三篇文章標(biāo)題",
??????? "content" : "第三篇文章內(nèi)容",
??????? "author" : "jam"
????? }
??? } ]
? }
}
查詢作者是名字包含“jam” 的文檔,返回id 是2和3 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d '
{
??? "query": {
??????? "match": {
??????????? "author": "jam"
??????? }
??? }
}'
查詢文章內(nèi)容包含“更新” 的文檔,返回id 是4 的文檔
curl -XGET 'http://localhost:9200/test/article/_search?pretty' -d '
{
??? "query": {
??????? "match": {
??????????? "content": "更新"
??????? }
??? }
}'
查詢?nèi)克饕?/p>
curl -XGET 'http://192.168.150.10:9200/_cat/indices/?v'
刪除所有數(shù)據(jù)包括自行添加的索引
curl -XDELETE 'http://192.168.150.10:9200/*'
2、Elasticsearch清理數(shù)據(jù)
由于Elasticsearch沒有自帶的數(shù)據(jù)刪除配置,所以需要腳本進(jìn)行清理數(shù)據(jù)
1)刪除索引是會(huì)立即釋放空間的,不存在所謂的“標(biāo)記”邏輯。
2)刪除文檔的時(shí)候,是將新文檔寫入,同時(shí)將舊文檔標(biāo)記為已刪除。磁盤空間是否釋放取決于新舊文檔是否在同一個(gè)segment file里面,因此ES后臺(tái)的segment merge在合并segment file的過(guò)程中有可能觸發(fā)舊文檔的物理刪除。
但因?yàn)橐粋€(gè)shard可能會(huì)有上百個(gè)segment file,還是有很大幾率新舊文檔存在于不同的segment里而無(wú)法物理刪除。想要手動(dòng)釋放空間,只能是定期做一下force merge,并且將max_num_segments設(shè)置為1。
刪除文檔‘’

釋放空間‘’

配置計(jì)劃任務(wù)

文件下載鏈接:https://pan.baidu.com/share/init?surl=FMQrjRapGQmXvkwWauTLZQ
密碼:x6I7
文章轉(zhuǎn)自elasticsearch搭建與zabbix對(duì)接
http://www.lwops.cn/forum.php?mod=viewthread&tid=70&fromuid=1
(出處: 樂維論壇)