原创

Elastic Stack入门

课程地址:https://www.imooc.com/learn/920

elastic相关产品文档:https://www.elastic.co/guide/index.html

注意:
>
elastic相关产品之间保持同一版本,否则启动会有错误,本次统一使用 5.6.3

1. ElasticSearch入门

下载地址: https://www.elastic.co/cn/downloads/past-releases#elasticsearch

安装步骤

Run bin/elasticsearch (or bin\elasticsearch.bat on Windows)

Run curl http://localhost:9200/ or Invoke-RestMethod http://localhost:9200 with PowerShell

本次在windows上运行

{
  name: "江节胜",
  cluster_name: "elasticsearch",
  cluster_uuid: "TxilW4SKRG2sg9woxf2Jgw",
  version: {
    number: "7.4.2",
    build_flavor: "default",
    build_type: "zip",
    build_hash: "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
    build_date: "2019-10-28T20:40:44.881551Z",
    build_snapshot: false,
    lucene_version: "8.2.0",
    minimum_wire_compatibility_version: "6.8.0",
    minimum_index_compatibility_version: "6.0.0-beta1"
  },
  tagline: "You Know, for Search"
}

1.1 ElasticSearch配置说明

配置文件位于config目录中

  • elasticsearch.yml es的相关配置
  • jvm.options jvm的相关参数
  • log4j2.properties 日志相关配置

elasticsearch.yml es关键配置说明

  • cluster.name 集群名称,以此作为是否同一集群的判断条件
  • node.name 节点名称,以此作为集群中不同节点的区分条件
  • network.host/http.port 网络地址和端口,用于http和transport服务使用
  • pathdata 数据存储地址
  • path.log 日志存储地址

Development 与 Production模式说明

  • 以transport的地址是否绑定localhost为判断标准 network.host
  • Development 模式下在启动时会以warning的方式提示配置检查异常
  • Production 模式下在移动时会以error的方式提示配置检查异常并退出

参数修改的第二种方式

  • bin/elasticsearch -Ehttp.port=19200

1.2 ElasticSearch 本地启动集群的方式

  1. bin/elasticsearch
  2. bin/elasticsearch -Ehttp.port=7200 -Epath.data=node2
  3. bin/elasticsearch -Ehttp.port=8200 -Epath.data=node3

通过 http://localhost:8200/_cat/nodes 查询集群

?貌似都要使用带参数的命名行启动,并且使用参数中端口查看

127.0.0.1 25 45 5    dilm * 江节胜    ---主节点
127.0.0.1 27 45 5    dilm - 江节胜

2 Kibana入门

下载:https://www.elastic.co/cn/downloads/past-releases#kibana ,选择5.6.3

2.1 Kibana运行

Open config/kibana.yml in an editor
Set elasticsearch.hosts to point at your Elasticsearch instance 
[有的可能是 elasticsearch.url ]

Run bin/kibana (or bin\kibana.bat on Windows)

Point your browser at http://localhost:5601

2.2 Kibana配置

配置位于config文件夹中

  • server.host/server.port 访问 kibana用的地址和端口
  • elasticsearch.url(也可能是elasticsearch.hosts)待访问 elasticsearch的地址

    2.3 Kibana常用功能说明

    • Discover 数据搜索查看
    • Visualize 图标制作
    • Dashboard 仪表盘制作
    • Timelion 时序数据的高级可视化分析
    • DevTools 开发者配置
    • Management 配置

      2.4 Kibana 常用术语

    • Document 文档数据
    • Index 索引(可以理解成MySQL中的一个数据库)
    • Type 索引中的数据类型(可以理解成MySQL中的一张表)
    • Field 字段,文档的属性
    • Query DSL 查询语法

      2.5 Kibana CRUD

      GET POST DELETE等请求方式全大写
    • Create 创建文档
    • Read 读取文档
    • Update 更新文档
    • Delete 删除文档

ElasticSearch Create
```
Postman 执行【可以使用Kibana中的Dev Tools:在Console可以保留多个请求语句】
POST /accounts/person/1
{
"name":"jiang",
"lastname":"sheng",
"job_description":"Systems administrator and linux specialit"
}

返回结果
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}

> ElasticSearch Read

GET /accounts/person/1

返回结果
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "jiang",
"lastname" : "sheng",
"job_description" : "Systems administrator and linux specialit"
}
}

> ElasticSearch Update

POST /accounts/person/1/_update
{
"doc":{
"job_description":"Systems administrator and linux specialist-update"
}
}

返回结果
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "jiang",
"lastname" : "sheng",
"job_description" : "Systems administrator and linux specialit"
}
}

> ElasticSearch Update

POST /accounts/person/1/_update
{
"doc":{
"job_description":"Systems administrator and linux specialist-update"
}
}

返回结果
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}

> ElasticSearch Delete

DELETE /accounts/person/1
or
DELETE /accounts/

返回结果
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 3,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
or
{
"acknowledged" : true //通过通配符
}

> ElasticSearch Query
> 再新建一条测试数据

POST /accounts/person/2
{
"name":"jiang2",
"lastname":"sheng2",
"job_description":"Systems administrator and linux specialit2"
}

- Query String

GET /accounts/person/_search?q=jiang //精确查询

返回:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "jiang",
"lastname" : "sheng",
"job_description" : "Systems administrator and linux specialit"
}
}
]
}
}

- Query DSL

GET /accounts/person/_search //精确查询
{
"query":{
"match":{
"name":"jiang"
}
}
}
返回同上

## 3 Beats入门
> 简介:相当于Kibana中的数据来源?

**LightWeight Data Shipper**
- Filebeat 日志文件
- Metricbeat 度量数据
- Packetbeat 网络数据
- Winlogbeat Windows 数据
- Heartbeat 健康检查

<div align=center>
    <img src="https://jiangjiesheng.gitee.io/resources/projects/191109-elasticsearch-getstart/imgs/Beats%E6%95%B0%E6%8D%AE%E6%BA%90.png" width = "450" height = "300" alt="Beats数据源.png" align=center>
</div>

 ### 3.1 Filebeat入门
 **处理流程**
 - 输入Input
 - 处理Filter
 - 输出Output

 <div align=center>
    <img src="https://jiangjiesheng.gitee.io/resources/projects/191109-elasticsearch-getstart/imgs/Filebeat.png" width = "450" height = "300" alt="Beats数据源.png" align=center> 
</div>

 #### 3.1.1 Filebeat Input 配置简介
  **yaml 语法**

filebeat.prospectors: //数组

- input_type:log
paths:
  - /var/log/apache/httpd-*.log
- input_type:log
paths: //数组
  - /var/log/messages
  - /var/log/*.log

 #### 3.1.2 Filebeat Filter 配置简介
 **Input 时处理**
 - include_lines
 - exclude_lines
 - exclude_files

  **Output 前处理 -- Processor**
 - drop_event
 - drop_fields
 - Decode_json_fields
 - Include_fields

示例1:
processors:

- drop_event:
   when:
     regexp:
        message:"^DBG:" //以debug开头的drop掉

示例2:
processors:

- decode_json_fields:
   fields:["inner"]

{"outer":"value","inner":{\"data\":\"value\"}
==>
{"outer":"value","inner":{"data":"value"}


 #### 3.1.3 Filebeat Output 配置简介
 - Console
 - Elasticsearch
 - Logstash
 - Kafka
 - Redis
 - File

output.elasticsearch:
hosts:["http://localhost:9200"]
username:"admin"
password:"xxxxx"
...
output.console:
pretty:true

 #### 3.1.4 Filebeat + Elasticsearch Ingest Node
 - Filebeat 缺乏数据转换的能力
 - Elasticsearch Ingest Node
    - 新增的node类型
    - 在数据写入 es 前对数据进行处理转换
    - pipeline api

 #### 3.1.5 Filebeat Module 简介
 - 对于社区常见需求进行配置封装增加易用性
    - nginx
 - 封装内容
    - filebeat.yml 配置
    - ingest node pipeline 配置
    - Kibana dashboard

 #### 3.1.6 Filebeat 收集 nginx log
 - 通过 stdin 收集日志

 > 下载Filebeat:https://www.elastic.co/cn/downloads/past-releases#filebeat **选择5.6.4**

 基本使用:

Edit the filebeat.yml configuration file

Start the daemon by running sudo ./filebeat -e -c filebeat.yml

 查看nginx日志:

//应该是通过linux的head命名查nginx日志
//下载nginx http://nginx.org/download/nginx-1.16.1.zip
//nginx访问:http://127.0.0.1:81/

head -n 2 D:/dev_tools/nginx-1.16.1/logs/nginx.log //window下直接去看文件

 #### 3.1.6.1 设置Filebeat配置
 从filebeat.full.yml 复制出 ngxin.yml,并释放stdin,output注释。
  • input_type: stdin

    ...

output.console:

Boolean flag to enable or disable the output module.

#enabled: true

Pretty print json event

pretty: true

 **使用 filebeat -e -c nginx.yml 启动。教程中使用 ==head -n 2 ~/Downloads/nginx_logs/nginx.logs|./filebeat -e -c nginx.yml== 启动**

### 3.2 Packetbeat入门
 - Packetbeat 简介
    - 实时抓取网络包
    - 自动解析引用层协议
        - ICMP(v4 and v6)
        - DNS
        - HTTP
        - MySQL
        - Redis
        - ......
    - Wireshark

#### 3.2.1 Packet 解析http 协议
    - 解析 elasticsearch http 请求
 packetbeat.interfaces:lo0  //网卡 linux下可以使用any(所有)
 packetbeat.protocols.http: ports:[9200]
 send_request:true
 include_body_for:["application/json","x-www-form-urlencoded"]
 output.console:
    pretty:true
```

3.2.2 Packetbeat 运行

- sudo ./packetbeat -e -c es.yml -strict.perms=false (es.yml应该是自己配置的)
- window上运行  packetbeat -e -c packetbeat.yml -strict.perms=false
下载:https://www.elastic.co/cn/downloads/past-releases#packetbeat 选择5.6.4

3.2 Logstash入门

简介:

  • Data Shipper

    • ETL
    • Extract
    • Transform
    • load

      官方定义:Logstash is an open source server-side data processing pipeline that ingests data from a multitude of sources simultaneously,transforms it, and then sends it to your favorite "stash"

    3.2.1 处理流程

  • Input

    • file
    • redis
    • beats
    • kafka
      • Filter
    • grok //类似正则
    • mutate //可能是数据处理
    • drop
    • date
      • Output
    • stdout
    • elaticsearsh
    • redis
    • kafka

    3.2.1.1 Input 和 Output 处理流程

      input {file {path => "/tmp/abc.log"}}
      output {stdout{codec => rubydebug}} //开发时推荐配置
    

    3.2.1.2 Filter 处理流程

  • Grok
    • 急于正则表达式,提供丰富可重用的模式(pattern)
    • 急于此可以将非结构化数据结构化处理
  • Date
    • 将字符串类型的时间字段转换为时间戳类型,方便后续数据处理
  • Mutate
    • 进行增加、修改、删除、替换等字段相关的处理
  • ......

    3.2.1.3 Filter 配置 Grok 示例

     55.3.244.1 GET /index.html 15824 0.043
     ==>
     %{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}
    

    3.2.1.4 收集 nginx log

    下载地址:https://www.elastic.co/cn/downloads/past-releases#logstash 选择5.6.4
    (无window版本)

    4 实战:分析 ElasticSearch 查询语句

    目标:

  • 收集 ElasticSearch 集群的查询语句
  • 分析查询语句的常用语句、响应时长等

方案:

  • 应用 Packetbeat + Logstash 完成数据收集工作
  • 使用Kibana + Elasticsearch 完成数据分析工作

    Beats数据源.png


    Beats数据源.png
正文到此结束
本文目录