第四章 ELK介绍

一、数据备份与恢复

1.安装npm工具

1)下载上传包

#下载地址:http://nodejs.cn/download/

[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r--  1 root root  21609252 2020-12-02 17:28 node-v14.15.1-linux-x64.tar.xz

2)解压

[root@es01 ~]# tar xf node-v14.15.1-linux-x64.tar.xz
[root@es01 ~]# mv node-v14.15.1-linux-x64 node

3)配置环境变量

[root@es01 ~]# vim /etc/profile.d/npm.sh 
export PATH=/root/node/bin:$PATH

[root@es01 ~]# source /etc/profile

4)更新国内源

[root@es01 ~]# npm config set registry http://registry.npm.taobao.org/

5)安装备份工具

[root@es01 ~]# npm install elasticdump -g

2.备份工具

1)备份参数

#我们运维需要掌握的
--input:来源文件或地址
--output:目标文件或地址
--type:备份内容类型(settings, analyzer, data, mapping, alias, template)

2)备份到另一台ES节点

elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=http://staging.es.com:9200/test 
  --type=analyzer
  
elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=http://staging.es.com:9200/test 
  --type=mapping
  
elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=http://staging.es.com:9200/test 
  --type=data

3)备份数据成json文件

elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=/data/test_mapping.json 
  --type=mapping
  
elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=/data/test_data.json 
  --type=data
  
elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=/data/test_alias.json 
  --type=alias
  
elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=/data/test_template.json 
  --type=template

elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=/data/test_analyzer.json 
  --type=analyzer

4)备份成压缩文件

#当文件导出不是为了使用,只是为了保存,可以压缩
elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=$ | gzip > /data/test_data.json.gz

5)备份指定条件的数据

elasticdump 
  --input=http://10.0.0.91:9200/test 
  --output=/data/test_query.json 
  --searchBody='{"query":{"term":{"name": "lhd"}}}'

4.导入命令

elasticdump 
  --input=/data/test_alias.json 
  --output=http://10.0.0.91:9200/test 
  --type=alias

elasticdump 
  --input=/data/test_analyzer.json 
  --output=http://10.0.0.91:9200/test 
  --type=analyzer
  
elasticdump 
  --input=/data/test_data.json 
  --output=http://10.0.0.91:9200/test 
  --type=data
  
elasticdump 
  --input=/data/test_template.json 
  --output=http://10.0.0.91:9200/test 
  --type=template
  
elasticdump 
  --input=/data/test_mapping.json 
  --output=http://10.0.0.91:9200/test 
  --type=mapping
  
#注意:恢复的时候,如果已存在相同的数据,会覆盖原来的数据,如果不存在数据,则无影响

5.备份脚本

#!/bin/bash
echo '要备份的机器是:'${1}
index_name='
test_2020-11-30
student
linux7
'
for index in `echo $index_name`
do
	echo "start input index ${index}"
	elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
	elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_analyzer.json --type=analyzer &> /dev/null
	elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_data.json --type=data &> /dev/null
	elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
	elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_template.json --type=template &> /dev/null
done

6.导入数据脚本

#!/bin/bash
echo '要导入的机器是:'${1}
index_name='
test
student
linux7
'
for index in `echo $index_name`
do
    echo "start input index ${index}"
    elasticdump --input=/data/${index}_alias.json --output=http://${1}:9200/${index} --type=alias &> /dev/null
    elasticdump --input=/data/${index}_analyzer.json --output=http://${1}:9200/${index} --type=analyzer &> /dev/null
    elasticdump --input=/data/${index}_data.json --output=http://${1}:9200/${index} --type=data &> /dev/null
    elasticdump --input=/data/${index}_template.json --output=http://${1}:9200/${index} --type=template &> /dev/null
done

二、中文分词器

https://github.com/medcl/elasticsearch-analysis-ik/

1.插入测试数据

POST /index/text/1
{"content":"美国留给伊拉克的是个烂摊子吗"}
POST /index/text/2
{"content":"公安部:各地校车将享最高路权"}
POST /index/text/3
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
POST /index/text/4
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

2.检测数据

POST /index/_search
{
  "query" : { "match" : { "content" : "中国" }},
  "highlight" : {
      "pre_tags" : ["<tag1>", "<tag2>"],
      "post_tags" : ["</tag1>", "</tag2>"],
      "fields" : {
          "content" : {}
      }
  }
}

#查询时分词有问题,中国被拆成两个字进行建立索引

3.配置中文分词器

1)上传插件的安装(集群中所有机器都执行)

[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r--  1 root root   4504556 2020-05-19 00:22 elasticsearch-analysis-ik-6.6.0.zip

2)解压

[root@es01 ~]# mkdir /usr/share/elasticsearch/plugins/ik -p
[root@es01 ~]# unzip elasticsearch-analysis-ik-6.6.0.zip -d /usr/share/elasticsearch/plugins/ik

3)编辑配置文件

[root@es03 ~]# vim /usr/share/elasticsearch/plugins/ik/config/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>IK Analyzer 扩展配置</comment>
    <!--用户可以在这里配置自己的扩展字典 -->
    <entry key="ext_dict">/etc/elasticsearch/config/my.dic</entry>
        <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <!-- <entry key="remote_ext_dict">words_location</entry> -->
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

4)编辑分词文件

[root@es03 ~]# cat /etc/elasticsearch/config/my.dic
中国

5)重启服务

[root@es01 ~]# systemctl restart elasticsearch.service

6)重新插入数据

1.建立索引
PUT /news

3.创建mapping
POST /news/text/_mapping
{
	"properties": {
		"content": {
			"type": "text",
			"analyzer": "ik_max_word",
			"search_analyzer": "ik_smart"
		}
	}
}

3.插入数据
POST /news/text/1
{"content":"美国留给伊拉克的是个烂摊子吗"}
POST /news/text/2
{"content":"公安部:各地校车将享最高路权"}
POST /news/text/3
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
POST /news/text/4
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

7)再次查询关键字

POST /news/_search
{
	"query" : { "match" : { "content" : "中国" }},
	"highlight" : {
		"pre_tags" : ["<tag1>", "<tag2>"],
		"post_tags" : ["</tag1>", "</tag2>"],
		"fields" : {
			"content" : {}
		}
	}
}

#分词正确

三、ELK介绍

1.什么是ELK

ELK是三个软件组成的
E:elasticsearch		#java程序  存储,查询日志
L:logstash			#java程序  收集,过滤日志
K:kibana			#java程序  展示,数据页面化

F:filebeat			#go语言  收集,过滤日志

2.ELK的作用

1.收集:收集所有服务器的日志
2.传输:把日志稳定的传输到ES或者消息队列
3.存储:ES能有效的存储数据
4.分析:通过web页面和作图进行分析
5.监控:监控集群架构

3.ELK优点

1.处理数据方式很灵活
2.配置简单
3.查询数据性能高
4.集群扩展方便
5.页面直观,好看

4.为什么使用ELK

1.web日志收集
2.业务日志收集
3.系统日志收集
4.分析以上日志

#在公司,统计分析数据
1.用户访问量统计
2.访问量前十的IP
3.站点访问次数最多的URL
4.查询一上午以上三个值   8:10-12:30
5.查询一下午以上三个值   13:30-17:30
6.对比一下以上数据
7.对比本周每一天的数据

#如果有ELK以上信息很容易查询

四、logstash介绍

1.搭建logstash

1)安装java环境

[root@web01 ~]# yum localinstall -y jdk-8u181-linux-x64.rpm

2)时间同步

[root@web01 ~]# ntpdate time1.aliyun.com

3)安装logstash

[root@web01 ~]# rz
[root@web01 ~]# yum localinstall -y logstash-6.6.0.rpm

4)授权

[root@web01 ~]# ll /usr/share/logstash/
[root@web01 ~]# chown -R logstash.logstash /usr/share/logstash/

#启动文件
[root@web01 ~]# ll /usr/share/logstash/bin/logstash
-rwxr-xr-x 1 logstash logstash 2354 Jan 24  2019 /usr/share/logstash/bin/logstash

2.logstash插件

INPUT:使Logstash能够读取特定的事件源。
OUTPUT:将事件数据发送到特定的目的地,OUTPUT是事件流水线中的最后阶段。
INPUT支持事件源 OUTPUT支持输出源 CODEC编解码器支持编码
azure_event_hubs(微软云事件中心) elasticsearch(搜索引擎数据库) avro(数据序列化)
beats(filebeat日志收集工具) email(邮件) CEF(嵌入式框架)
elasticsearch(搜索引擎数据库) file(文件) es_bulk(ES中的bulk api)
file(文件) http(超文本传输协议) Json(数据序列化、格式化)
generator(生成器) kafka(基于java的消息队列) Json_lines(便于存储结构化)
heartbeat(高可用软件) rabbitmq(消息队列 OpenStack) line(行)
http_poller(http api) redis(缓存、消息队列、NoSQL) multiline(多行匹配)
jdbc(java连接数据库的驱动) s3*(存储) plain(纯文本,事件间无间隔)
kafka(基于java的消息队列) stdout(标准输出) rubydebug(ruby语法格式)
rabbitmq(消息队列 OpenStack) tcp(传输控制协议)
redis(缓存、消息队列、NoSQL) udp(用户数据报协议)
s3*(存储)
stdin(标准输入)
syslog(系统日志)
tcp(传输控制协议)
udp(用户数据报协议)

3.logstash输入输出插件测试

1)配置环境变量

[root@web01 ~]# vim /etc/profile.d/logstash.sh
export PATH=/usr/share/logstash/bin/:$PATH

2)收集标准输入到标准输出

[root@web01 ~]# logstash -e 'input { stdin {} } output { stdout {} }'

34567890
{
	   #收集到的内容
       "message" => "34567890",
    #时间戳
    "@timestamp" => 2020-12-03T09:27:18.886Z,
    	  #收集到数据的主机
          "host" => "web01",
      #收集的版本
      "@version" => "1"
}

3)收集标准输入到标准输出指定格式

[root@web01 ~]# logstash -e 'input { stdin {} } output { stdout { codec => rubydebug } }'

123
{
       "message" => "123",
      "@version" => "1",
    "@timestamp" => 2020-12-03T09:33:40.563Z,
          "host" => "web01"
}

4)收集标准输入到文件

[root@web01 ~]# logstash -e 'input { stdin {} } output { file { path => "/tmp/1.txt" } }'
123
[INFO ] 2020-12-03 17:40:50.731 [[main]>worker0] file - Opening file {:path=>"/tmp/1.txt"}
234
345

#验证文件写入
[root@web01 ~]# tail -f /tmp/1.txt 
{"message":"123","@timestamp":"2020-12-03T09:40:50.333Z","host":"web01","@version":"1"}
{"message":"234","@timestamp":"2020-12-03T09:41:27.302Z","host":"web01","@version":"1"}
{"message":"345","@timestamp":"2020-12-03T09:41:45.527Z","host":"web01","@version":"1"}

5)收集标准输入到ES

[root@web01 ~]# logstash -e 'input { stdin {} } output { elasticsearch { hosts => ["10.0.0.71:9200"] index => "test" } }'

#启动后随便输入点东西,去ES查看

[root@web01 ~]# logstash -e 'input { stdin {} } output { elasticsearch { hosts => ["10.0.0.71:9200"] index => "test_%{+YYYY-MM-dd}" } }'
原文地址:https://www.cnblogs.com/jhno1/p/14095620.html