• ELK学习(一)


    一  ELK 

    1  ELK 概述

    ELK是一整套解决方案,是三个软件产品的首字母缩写,很多公司都在使用,如:Sina、携程、华为、美团等

    Elasticsearch:负责日志检索和储存

    Logstash:负责日志的收集和分析、处理

    Kibana:负责日志的可视化

    2  项目架构图

    二  Elasticsearch

    1  Elasticsearch概述

    elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTfulAPI的Web接口

    elasticsearch 的一大优势是拥有集群协调子系统。集群协调子系统可以从只有几个节点的小集群无缝扩展到拥有数百个节点的大集群

    2  主要特点

    • 实时分析,文档导向,分布式实时文件存储
    • 高可用性,易扩展,支持集群 (Cluster) 、分片和复制(Shards 和 Replicas)
    • 接口友好,支持JSON
    • 没有典型意义的事务
    • 是一种面向文档的数据库

    3  名词解释

    Node装有一个ES服务器的节点
    Cluster有多个Node组成的集群
    Shards索引的分片,每一个分片就是一个Shard
    Replicas索引的拷贝
    Index拥有相似特征的文档的集合
    Type一个索引中可以定义一种或多种类型
    Document一个可被搜索的基础信息单元
    Filed是ES的最小单位,相当于数据的某一列

    4  Elasticsearch 与关系型数据库对比

    5  部署Elasticsearch集群

    (1)准备工作

    • 更新 yum车软件仓库

    1. # 添加 ELK 软件包到自定义 Yum 仓库
    2. [root@ecs-proxy s4]# rsync -av elk/ /var/localrepo/elk/
    3. [root@ecs-proxy s4]# createrepo --update /var/localrepo
    • 配置服务主机

    主机IP地址配置
    es-0001192.168.1.21最低配置2核4G
    es-0002192.168.1.22最低配置2核4G
    es-0003192.168.1.23最低配置2核4G
    es-0004192.168.1.24最低配置2核4G
    es-0005192.168.1.25最低配置2核4G

    (2)集群安装

    • 部署 es-0001

    1. [root@es-0001 ~]# vim /etc/hosts
    2. 192.168.1.21 es-0001
    3. 192.168.1.22 es-0002
    4. 192.168.1.23 es-0003
    5. 192.168.1.24 es-0004
    6. 192.168.1.25 es-0005
    7. [root@es-0001 ~]# dnf install -y elasticsearch
    8. [root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
    9. 17: cluster.name: my-es # 集群名称
    10. 23: node.name: es-0001 # 集群主机名
    11. 56: network.host: 0.0.0.0 # 集群广播成员
    12. 70: discovery.seed_hosts: ["es-0001", "es-0002", "es-0003"] # 集群创始人成员
    13. 74: cluster.initial_master_nodes: ["es-0001", "es-0002", "es-0003"]
    14. [root@es-0001 ~]# systemctl enable --now elasticsearch
    15. # 服务启动成功
    16. [root@es-0001 ~]# curl http://127.0.0.1:9200
    17. {
    18. "name" : "es-0001",
    19. "cluster_name" : "my-es",
    20. "cluster_uuid" : "_na_",
    21. "version" : {
    22. "number" : "7.17.8",
    23. "build_flavor" : "default",
    24. "build_type" : "rpm",
    25. "build_hash" : "120eabe1c8a0cb2ae87cffc109a5b65d213e9df1",
    26. "build_date" : "2022-12-02T17:33:09.727072865Z",
    27. "build_snapshot" : false,
    28. "lucene_version" : "8.11.1",
    29. "minimum_wire_compatibility_version" : "6.8.0",
    30. "minimum_index_compatibility_version" : "6.0.0-beta1"
    31. },
    32. "tagline" : "You Know, for Search"
    33. }
    • 部署 es-0002

    1. # 验证集群状态失败
    2. [root@es-0002 ~]# curl http://es-0001:9200/_cat/nodes?pretty
    3. {
    4. "error" : {
    5. "root_cause" : [
    6. {
    7. "type" : "master_not_discovered_exception",
    8. "reason" : null
    9. }
    10. ],
    11. "type" : "master_not_discovered_exception",
    12. "reason" : null
    13. },
    14. "status" : 503
    15. }
    16. # 部署服务
    17. [root@es-0002 ~]# vim /etc/hosts
    18. 192.168.1.21 es-0001
    19. 192.168.1.22 es-0002
    20. 192.168.1.23 es-0003
    21. 192.168.1.24 es-0004
    22. 192.168.1.25 es-0005
    23. [root@es-0002 ~]# dnf install -y elasticsearch
    24. [root@es-0002 ~]# vim /etc/elasticsearch/elasticsearch.yml
    25. 17: cluster.name: my-es
    26. 23: node.name: es-0002
    27. 56: network.host: 0.0.0.0
    28. 70: discovery.seed_hosts: ["es-0001", "es-0002", "es-0003"]
    29. 74: cluster.initial_master_nodes: ["es-0001", "es-0002", "es-0003"]
    30. [root@es-0002 ~]# systemctl enable --now elasticsearch
    31. # 验证集群状态
    32. [root@es-0002 ~]# curl http://es-0001:9200/_cat/nodes?pretty
    33. 192.168.1.21 16 89 2 0.15 0.06 0.04 cdfhilmrstw * es-0001
    34. 192.168.1.22 6 88 61 1.00 0.23 0.08 cdfhilmrstw - es-0002

    (3)集群扩容

    集群结点数量较多时,一般使用 Ansible 批量部署

    在所有 es 主机安装 Elasticsearch

    1. [root@ecs-proxy ~]# mkdir es
    2. [root@ecs-proxy ~]# cd es
    3. [root@ecs-proxy es]# vim ansible.cfg
    4. [defaults]
    5. inventory = hostlist
    6. host_key_checking = False
    7. [root@ecs-proxy es]# vim hostlist
    8. [es]
    9. 192.168.1.[21:25]
    10. [root@ecs-proxy es]# rsync -av 192.168.1.21:/etc/elasticsearch/elasticsearch.yml elasticsearch.j2
    11. [root@ecs-proxy es]# vim elasticsearch.j2
    12. 23: node.name: {{ ansible_hostname }}
    13. [root@ecs-proxy es]# vim es_install.yaml
    14. ---
    15. - name: Elasticsearch 集群安装
    16. hosts: es
    17. tasks:
    18. - name: 设置 /etc/hosts
    19. copy:
    20. dest: /etc/hosts
    21. owner: root
    22. group: root
    23. mode: '0644'
    24. content: |
    25. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    26. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    27. {% for i in groups.es %}
    28. {{ hostvars[i].ansible_eth0.ipv4.address }} {{ hostvars[i].ansible_hostname }}
    29. {% endfor %}
    30. - name: 安装 ES 服务
    31. dnf:
    32. name: elasticsearch
    33. state: latest
    34. update_cache: yes
    35. - name: 拷贝配置文件
    36. template:
    37. src: elasticsearch.j2
    38. dest: /etc/elasticsearch/elasticsearch.yml
    39. owner: root
    40. group: elasticsearch
    41. mode: '0660'
    42. - name: 配置 ES 服务
    43. service:
    44. name: elasticsearch
    45. state: started
    46. enabled: yes
    47. [root@ecs-proxy es]# ansible-playbook es_install.yaml
    48. [root@ecs-proxy es]# curl http://192.168.1.21:9200/_cat/nodes?pretty
    49. 192.168.1.21 32 88 0 0.04 0.01 0.00 cdfhilmrstw * es-0001
    50. 192.168.1.22 16 87 0 0.13 0.04 0.01 cdfhilmrstw - es-0002
    51. 192.168.1.23 6 86 1 0.64 0.21 0.07 cdfhilmrstw - es-0003
    52. 192.168.1.24 18 86 0 0.44 0.13 0.05 cdfhilmrstw - es-0004
    53. 192.168.1.25 6 86 1 0.67 0.21 0.07 cdfhilmrstw - es-0005

    (4)集群管理

    • head插件

    它展现ES集群的拓扑结构,并且可以通过它来进行索引(Index)和节点(Node) 级别的操作

    它提供一组针对集群的查询API,并将结果以ison和表格形式返回

    它提供一些快捷菜单,用以展现集群的各种状态

    • Head插件原理图

    Head插件的本质就是一组网页

    在es-0001上安装 web 服务,并部署插件页面

    通过 ELB映射8080端口,发布 web 服务 

    •  部署插件页面
    1. # 拷贝插件 public/head.tar.gz 到 es-0001 主机
    2. [root@ecs-proxy s4]# rsync -av public/head.tar.gz 192.168.1.21:./
    3. #-------------------------------------------------
    4. # 在 es-0001 上安装 web 服务,并部署插件
    5. [root@es-0001 ~]# dnf install -y nginx
    6. [root@es-0001 ~]# systemctl enable --now nginx
    7. [root@es-0001 ~]# tar zxf head.tar.gz -C /usr/share/nginx/html/

    通过 ELB 的 8080 端口,发布服务到互联网

    • 认证和代理
    1. [root@es-0001 ~]# vim /etc/nginx/default.d/esproxy.conf
    2. location ~* ^/es/(.*)$ {
    3. proxy_pass http://127.0.0.1:9200/$1;
    4. auth_basic "Elasticsearch admin";
    5. auth_basic_user_file /etc/nginx/auth-user;
    6. }
    7. [root@es-0001 ~]# dnf install -y httpd-tools
    8. [root@es-0001 ~]# htpasswd -cm /etc/nginx/auth-user admin
    9. New password:
    10. Re-type new password:
    11. Adding password for user admin
    12. [root@es-0001 ~]# systemctl reload nginx

    通过 head 插件管理 elasticsearch 集群

    6  API原语管理

    拓展提高知识

    Elasticsearch 提供基于http协议的管理方式 (API)

    http 请求由三部分组成

            —  分别是:请求行、消息报头、请求正文

            —  请求行: Method Request-URI HTTP-Version

    http 请求方法

            —  常用方法 GET,POST,HEAD

            —  其他方法OPTIONS,PUT,DELETE,TRACE 和CONNECT

    与Elasticsearch交互的数据需使用json格式

    HEAD:Content-Type: application/json

    Elasticsearch使用的请求方法

    PUT
    DELETE
    POST
    GET

    在 linux 中 curl 是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具。它支持多种请求模式,自定义请求头等强大功能

    使用格式:curl  -X  “请求方法”  -H  “请求头”  http://请求地址

    _cat 关键字用来查询集群状态,节点信息等

    • 显示详细信息(?v),5显示帮助信息(?help)
    • json 使用易读的调试格式显示 (?pretty)
    • 例如:查询集群中的 master 是谁
    (1)集群状态查询
    1. # 查询支持的关键字
    2. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/
    3. # 查具体的信息
    4. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master
    5. # 显示详细信息 ?v
    6. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?v
    7. # 显示帮助信息 ?help
    8. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?help
    9. # 显示易读格式 ?pretty
    10. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?pretty
    (2)创建索引
    • 指定索引的名称,指定分片数量,指定副本数量
    1. # 设置默认分片副本数量
    2. [root@es-0001 ~]# curl -XPUT -H 'Content-Type: application/json' \
    3. http://127.0.0.1:9200/_template/index_defaults -d '{
    4. "template": "*",
    5. "settings": {
    6. "number_of_shards": 5,
    7. "number_of_replicas": 1
    8. }
    9. }'
    10. {"acknowledged":true}
    11. # 创建 tedu 索引
    12. [root@es-0001 ~]# curl -XPUT http://127.0.0.1:9200/tedu?pretty
    13. {
    14. "acknowledged" : true,
    15. "shards_acknowledged" : true,
    16. "index" : "tedu"
    17. }
    (3)增加数据
    1. [root@es-0001 ~]# curl -XPUT -H "Content-Type: application/json" \
    2. http://127.0.0.1:9200/tedu/teacher/1?pretty -d '{
    3. "职业": "诗人","名字": "李白","称号": "诗仙","年代": "唐"
    4. }'
    (4)查询数据
    1. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/tedu/teacher/_search?pretty
    2. [root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/tedu/teacher/1?pretty
    (5)修改数据
    1. [root@es-0001 ~]# curl -XPOST -H "Content-Type: application/json" \
    2. http://127.0.0.1:9200/tedu/teacher/1/_update -d '{
    3. "doc": {"年代":"公元701"}
    4. }'
    (6)删除数据
    1. # 删除一条
    2. [root@es-0001 ~]# curl -XDELETE http://127.0.0.1:9200/tedu/teacher/1
    3. # 删除索引
    4. [root@es-0001 ~]# curl -XDELETE http://127.0.0.1:9200/tedu

    三  Logstash

    1  Logstash概述

    是一个数据采集、加工处理以及传输的工具

    Logstash特点

    • 所有类型的数据集中处理
    • 不同模式和格式数据的正常化
    • 自定义日志格式的迅速扩展
    • 为自定义数据源轻松添加插件

    3  Logstash结构原理

    logstash 支持的数据类型

    布尔、字节、字符串、数值、数组、键值对

    logstash 支持的判断方法

    ==、!=、>、>=
    logstash 支持的逻辑判断

    in、notin、and、or、nand、xor

    4  Logstash 安装部署

    logstash 使用Java开发,没有默认的配置文件,需要手动配置

    logstash 安装在 /usr/share/logstash 目录下,配置文件安装在 /etc/logstash 目录下,由于程序找不到配置文件路径,需要在 /usr/share/logstash 目录下创建一个名为 config的 /etc/logstash 的软链接

    (1)准备工作

    主机名称IP地址配置
    logstash192.168.1.27最低配置4核8G

    (2)安装部署

    1. [root@logstash ~]# vim /etc/hosts
    2. 192.168.1.21 es-0001
    3. 192.168.1.22 es-0002
    4. 192.168.1.23 es-0003
    5. 192.168.1.24 es-0004
    6. 192.168.1.25 es-0005
    7. 192.168.1.27 logstash
    8. [root@logstash ~]# dnf install -y logstash
    9. [root@logstash ~]# ln -s /etc/logstash /usr/share/logstash/config

    (3)最简单的配置

    1. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    2. input {
    3. stdin {}
    4. }
    5. filter{
    6. }
    7. output{
    8. stdout{}
    9. }
    10. [root@logstash ~]# /usr/share/logstash/bin/logstash

    (4)插件与调试格式

    • json格式字符串: {"a":"1", "b":"2", "c":"3"}
    1. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    2. input {
    3. stdin { codec => "json" } // 标准输入的数据是json 格式编码
    4. }
    5. filter{
    6. }
    7. output{
    8. stdout{ codec => "rubydebug" } // 标准输出的数据想使用调试格式编码
    9. }
    10. [root@logstash ~]# /usr/share/logstash/bin/logstash

    测试 json 字符串 

    (5)插件管理 

    使用插件

    logstash 对数据的处理依赖插件,上面的配置文件使用了logstash-input-stdin 和 logstash-output-stdout 两个插件

    管理命令 /usr/share/logstash/bin/logstash-plugin

    1. 查看插件    list
    2. 安装插件    install
    3. 删除插件    uninstall

     (6)插件与模块的关系

    只能用于input 模块的插件:logstash-input-xxx

    只能用于output模块的插件:ogstash-output-xxx

    只能用于filter模块的插件:logstash-filter-xxx

    编码插件不受模块的限制:logstash-codec-xxx

    5  input 模块

    (1)file 插件

    • file插件是我们平常应用最广泛插件之一,他的主要用途是从本地文件中获取数据,并实时监控文件的变化
    • 手册地址:[Logstash Reference [8.10] | Elastic]

    (2)文件插件语法格式

    (3)file插件基本配置

    1. // 创建测试日志文件
    2. [root@logstash ~]# touch /tmp/{a,b}.log
    3. [root@logstash ~]# echo 'string 01' >>/tmp/a.log
    4. [root@logstash ~]# echo 'string 02' >>/tmp/a.log
    5. [root@logstash ~]# echo 'string 03' >>/tmp/a.log
    6. // 配置 file 插件,读取日志文件
    7. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    8. input {
    9. file {
    10. path => ["/tmp/a.log", "/tmp/b.log"]
    11. }
    12. }
    13. # filter { 不做任何修改 }
    14. # output { 不做任何修改 }
    15. # 启动程序,等待数据输出
    16. [root@logstash ~]# /usr/share/logstash/bin/logstash
    17. #---------------------------------------------------
    18. # 在另一个终端模拟写入日志
    19. [root@logstash ~]# echo 'string 10' >>/tmp/b.log
    20. [root@logstash ~]# echo 'string 11' >>/tmp/a.log

    (4)核心参数

    path 要监控的文件路径

    path => ["/tmp/a.log","/tmp/b.log"]

    start_position 第一次读取文件位置 [beginning end]

    start position => "beginning"

    sincedb_path 记录读取文件的位置

    sincedb path => "/var/lib/logstash/sincedb-access"

    (5)file插件高级配置

    1. # 删除默认书签文件
    2. [root@logstash ~]# rm -rf /var/lib/logstash/plugins/inputs/file/.sincedb_*
    3. [root@logstash ~]# cat /tmp/{a.log,b.log} >/tmp/c.log
    4. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    5. input {
    6. file {
    7. path => ["/tmp/c.log"]
    8. start_position => "beginning"
    9. sincedb_path => "/var/lib/logstash/sincedb"
    10. }
    11. }
    12. # filter { 不做任何修改 }
    13. # output { 不做任何修改 }
    14. [root@logstash ~]# /usr/share/logstash/bin/logstash

    6  filter 模块

    (1)grok 插件

    解析各种非结构化的日志数据插件

    grok 使用正则表达式把非结构化的数据结构化在分组匹配

    正则表达式需要根据具体数据结构编写

    虽然编写困难,但适用性极广

    几乎可以应用于各类数据

    (2)准备测试数据

    1. # 从 web 服务器查找一条日志写入到日志文件
    2. [root@logstash ~]# echo '60.26.217.109 - admin [13/Jan/2023:14:31:52 +0800] "GET /es/ HTTP/1.1" 200 148209 "http://127.70.79.1/es/" "curl/7.61.1"' >/tmp/c.log
    3. # 调试技巧:设置路径为 /dev/null 可以多次反复测试
    4. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    5. input {
    6. file {
    7. path => ["/tmp/c.log"]
    8. start_position => "beginning"
    9. sincedb_path => "/dev/null"
    10. }
    11. }
    12. # filter { 不做任何修改 }
    13. # output { 不做任何修改 }
    14. [root@logstash ~]# /usr/share/logstash/bin/logstash

    (3)日志格式解析

     正则表达式分组匹配格式

    调用格式:(?<名字>正则表达式)

    调用宏表达式的格式

    调用格式:%{宏名称:名字]

    grok自带的宏定义在

    /usr/share/logstash/vendor/bundle/jruby/2.5.0/gems/logstash-patterns-core-4.3.4/patterns

    (4)匹配IP地址测试(获取IP地址)

    1. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    2. # input { 不做任何修改 }
    3. filter {
    4. grok { // 使用正则表达式获取 IP 地址
    5. match => { "message" => "(?((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d))" }
    6. }
    7. grok { // 使用正则表达式宏获取 IP 地址
    8. match => { "message" => "%{IP:clientIP}" }
    9. }
    10. }
    11. # output { 不做任何修改 }
    12. [root@logstash ~]# /usr/share/logstash/bin/logstash

    (5)使用宏解析WEB日志

    1. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    2. # input { 不做任何修改 }
    3. filter{
    4. grok {
    5. match => { "message" => "%{HTTPD_COMBINEDLOG}" }
    6. remove_field => ["message"]
    7. }
    8. }
    9. # output { 不做任何修改 }
    10. [root@logstash ~]# /usr/share/logstash/bin/logstash

    7  output 模块

    (1)elasticsearch 插件

    elasticsearch 插件是我们日志分析系统的数据输出插件,他的主要用途是把通过 filter 处理过的json数据写入到elasticsearch集群中

    (2)elasticsearch插件语法格式

    (3) 核心参数

    hosts elasticsearch节点的地址,数组格式

            hosts => ["es-0002:9200""es-0003:9200"]

    index存储数据索引的名称
            index => "weblog
    index还支持安日期生成索引,其中YYYY表示年,MM表示月份,dd表示日期

            index => "weblog-{ +YYYY.MM.dd)"

    (4)把数据写入到elasticsearch集群的weblog索引中

    1. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    2. # input { 不做任何修改 }
    3. # filter { 不做任何修改 }
    4. output{
    5. stdout{ codec => "rubydebug" }
    6. elasticsearch {
    7. hosts => ["es-0002:9200","es-0003:9200"]
    8. index => "weblog-%{+YYYY.MM.dd}"
    9. }
    10. }
    11. [root@logstash ~]# /usr/share/logstash/bin/logstash
    • 访问页面,查看 Head 插件,验证数据写入 Elasticsearch 成功

    四  获取web日志

    通过网络收集

    • 在 Web 服务器上部署 filebeat 日志采集程序
    • filebeat 是使用 Golang 实现的轻量型日志采集器,也是Elasticsearch stack 里面的一员。
    • filebeat 占用资源非常小,可以忽略不计
    • filebeat 本质上是一个agent ,可以安装在应用服务器各个节点上,根据配置读取对应位置的日志文件,并通过网络上报到相应的服务中

    1  beats 插件

    (1)beats插件概述

    beats 是 logstash input 模块的插件

    专门用来接收 filebeat 发送过来的日志

    默认监听在5044端口上

    可以同时接收多台不同主机发送过来的日志信息

    (2)使用 logstash 配置日志接收服务器

    1. # 配置日志接受服务器
    2. [root@logstash ~]# vim /etc/logstash/conf.d/my.conf
    3. input {
    4. beats {
    5. port => 5044
    6. }
    7. }
    8. # filter { 不做任何修改 }
    9. # output { 不做任何修改 }
    10. # 启动logstash接受数据
    11. [root@logstash ~]# /usr/share/logstash/bin/logstash

    (3)filebeat安装配置

    部署日志收集客户端

    • 软件安装路径: /usr/share/filebeat
    • 配置文件: /etc/filebeat/filebeat.yml

    安装 filebeat,并设置开机自启动

    1. [root@web-0001 ~]# dnf install -y filebeat
    2. [root@web-0001 ~]# systemctl enable filebeat
    3. [root@web-0001 ~]# vim /etc/filebeat/filebeat.yml
    4. 25: id: my-filestream-id # 如果同时配置多个收集器,id不能重复
    5. 28: enabled: true # 打开收集模块
    6. 32: - /var/log/httpd/access_log # 日志文件路径
    7. 135: # 注释掉 Elasticsearch 配置
    8. 137: # 注释掉 Elasticsearch 配置
    9. 148: output.logstash: # 设置输出模块
    10. 150: hosts: ["192.168.1.27:5044"] # 输出给logstash
    11. 163: # processors: 注释(用于收集系统信息)
    12. 164: # - add_host_metadata: 注释掉(收集主机信息)
    13. 165: # when.not.contains.tags: forwarded 注释掉(判断是否为容器)
    14. 166: # - add_cloud_metadata: ~ 注释掉(收集 cloud 信息)
    15. 167: # - add_docker_metadata: ~ 注释掉(收集 docker 信息)
    16. 168: # - add_kubernetes_metadata: ~ 注释掉(收集 kubernetes 信息)
    17. [root@web-0001 ~]# rm -f /var/log/httpd/*
    18. [root@web-0001 ~]# systemctl restart filebeat httpd
    19. # 测试验证: 访问页面,观察 logstash 是否能够获取数据
    20. [root@web-0001 ~]# curl http://192.168.1.11/info.php

    2  多日志标签

    如果系统中有多种类型的日志,我们可以使用标签区分日志种类

    (1)设置标签

    1. [root@web ~]# vim /etc/filebeat/filebeat.yml
    2. # 设置识别标签
    3. 49: fields:
    4. 50: logtype: apache_log
    5. # 清理冗余数据
    6. 164: processors:
    7. 165: - drop_fields:
    8. 166: fields:
    9. 167: - log
    10. 168: - offset
    11. 169: - agent
    12. 170: - ecs
    13. [root@web ~]# systemctl restart filebeat
    14. # 测试验证: 访问页面,观察 logstash 输出的数据变化
    15. [root@web-0001 ~]# curl http://192.168.1.11/info.php

    (2)使用标签

    1. [root@logstash ~]# cat /etc/logstash/conf.d/my.conf
    2. input {
    3. beats {
    4. port => 5044
    5. }
    6. }
    7. filter{
    8. if [fields][logtype] == "apache_log" { // logstash 中基于标签配置不同规则
    9. grok {
    10. match => { "message" => "%{HTTPD_COMBINEDLOG}" }
    11. remove_field => ["message"]
    12. }}
    13. }
    14. output{
    15. stdout{ codec => "rubydebug" }
    16. if [fields][logtype] == "apache_log" { // 为不同日志定义各自索引
    17. elasticsearch {
    18. hosts => ["es-0004:9200", "es-0005:9200"]
    19. index => "weblog-%{+YYYY.MM.dd}"
    20. }}
    21. }
    22. [root@logstash ~]# /usr/share/logstash/bin/logstash

    (3)批量部署filebeat

    filebeat 安装部署

    • 在调试好一个结点之后,我们需要在整个 Web 集群安装部署 filebeat
    • 编写 playbook 完成批量安装部署
    1. [root@ecs-proxy ~]# cd website
    2. [root@ecs-proxy website]# rsync -av 192.168.1.11:/etc/filebeat/filebeat.yml filebeat.j2
    3. [root@ecs-proxy website]# vim filebeat.yaml
    4. ---
    5. - name: 集群安装部署 filebeat
    6. hosts: web
    7. tasks:
    8. - name: 安装 filebeat
    9. dnf:
    10. name: filebeat
    11. state: latest
    12. update_cache: yes
    13. - name: 同步配置文件
    14. template:
    15. src: filebeat.j2
    16. dest: /etc/filebeat/filebeat.yml
    17. owner: root
    18. group: root
    19. mode: '0600'
    20. - name: 设置启动服务
    21. service:
    22. name: filebeat
    23. enabled: yes
    24. - name: 清理历史日志
    25. file:
    26. path: "{{ item }}"
    27. state: absent
    28. loop:
    29. - /var/log/httpd/access_log
    30. - /var/log/httpd/error_log
    31. - name: 重启服务
    32. service:
    33. name: "{{ item }}"
    34. state: restarted
    35. loop:
    36. - httpd
    37. - filebeat
    38. [root@ecs-proxy website]# ansible-playbook filebeat.yaml

    (4)验证日志收集

    1. # 删除日志数据
    2. [root@ecs-proxy ~]# curl -XDELETE "http://192.168.1.21:9200/weblog-*"
    3. {"acknowledged":true}
    4. # 通过浏览器访问 Web 集群页面,观察 head 插件,是否有数据写入

    五  kibana

    1  kibana概述

    数据可视化平台工具

    2  特点

    灵活分析和可视化平台

    实时流量统计报表

    种类繁多的数据图表

    为不同的用户定制分析界面

    3  图列

    4  安装部署

    (1)准备工作

    主机IP地址配置
    kibana192.168.1.26最低配置2核4G

    (2)安装kibana

    1. [root@kibana ~]# vim /etc/hosts
    2. 192.168.1.21 es-0001
    3. 192.168.1.22 es-0002
    4. 192.168.1.23 es-0003
    5. 192.168.1.24 es-0004
    6. 192.168.1.25 es-0005
    7. 192.168.1.26 kibana
    8. [root@kibana ~]# dnf install -y kibana
    9. [root@kibana ~]# vim /etc/kibana/kibana.yml
    10. 02: server.port: 5601
    11. 07: server.host: "0.0.0.0"
    12. 23: server.publicBaseUrl: "http://192.168.1.26:5601"
    13. 32: elasticsearch.hosts: ["http://es-0004:9200", "http://es-0005:9200"]
    14. 115: i18n.locale: "zh-CN"
    15. [root@kibana ~]# systemctl enable --now kibana
    • 使用 ELB 发布端口 5601,通过 WEB 浏览器访问验证
    • 使用 kibana 完成数据统计分析

    5  流量数据分析

    (1)指定索引

    (2)创建索引

  • 相关阅读:
    【牛客刷题】带你在牛客刷题第五弹(简单排序)
    手机和电脑文件互传
    webpack5 基本配置
    Spring Boot集成protobuf快速入门Demo
    Docker 自动化部署(保姆级教程)
    VS code运行vue项目
    【Hadoop】5、集群运行
    UVM学习笔记—快速入门篇
    ChatGPT通用人工智能:初心与未来
    Playwright直接控制本地Chrome浏览器的方法
  • 原文地址:https://blog.csdn.net/2301_79227925/article/details/134074190