zoukankan      html  css  js  c++  java
  • ELK-日志管理平台

    elk日志收集工具

    1、日志在工作当中的重要性

     
     
     
     
     
     
    1
    分析日志的意义:
    2
    1、分析日志监控系统运行的状态
    3
    2、分析日志来定位程序的bug
    4
    3、分析日志监控网站访问流量
    5
    4、分析日志可以知道那些sql语句需要优化
    6
       。。。
     
     

    2、使用MySQL数据库做查询可不可行

     
     
     
     
     
     
    1
    1、mysql查询的方式是全表扫描,用时长,效率不高
    2
     
     

    3、收集日志工具分类

     
     
     
     
     
     
    1
    1、日志易:监控、审计、权限管理,收费软件
    2
    2、splunk:按流量收费,是国外的软件
    3
       主要三个部件 Indexer、Search Head、Forwarder
    4
    3、elk:日志收集、分析、展示
     
     

    4、elk简单介绍

     
     
     
     
     
     
    1
    1、elk/efk它是一个组合软件:
    2
         elasticsearch、logstash、kibana
    3
    2、elasticsearch:负责数据的存储、数据的分析、数据的搜索
    4
       基于luncene做的二开发,
    5
    3、logstash:负责客户端日志收集工具,替代工具(filebeat)
    6
    4、kibana:负责数据的展示,查询
     
     
     
     
     
     
     
     
    1
    关系型的数据库:
    2
    mysql orcal sql-server。。。
    3
    非关系型的数据库:
    4
    redis  mongo elasticsearch。。
     
     

    5、什么是倒排索引

     
     
     
     
     
     
    1
    1、信息存储到es时,首先把每条语句拆分成一个一个的词语
    2
    2、根据搜索的内容,进行匹配,有匹配到的权重加1
    3
    3、把匹配到的语句给用记呈现出来
     
     

    6、elasticsearch的应用场景

     
     
     
     
     
     
    1
    1、电商平台
    2
    2、高亮显示搜索的词条信息
    3
    3、日志分析elk
     
     

    7、elasticsearch特点

     
     
     
     
     
     
    1
    1、高性能:
    2
           es可以支持一主多从,水平扩展方便,
    3
    2、高可用性:一个主节点宕机后不影响用户的使用,
    4
    3、用户使用方便快捷:es采用java开发,即使不懂java代码,一样可以使用
    5
    4、功能丰富,配置简单
    6
    5、采用restful封装的接口,可以通过http发起请求
    7
           
     
     

    8、如何部署es

     
     
     
     
     
     
    1
    1、rpm包的方式来部署
    2
    2、tar包的方式来部署
    3
    3、ansible来部署
    4
    4、docker的方式来部署
     
     

    9、部署es的流程

     
     
     
     
     
     
    1
    1、安装一个java的环境
    2
    mkdir /opt/es-software
    3
    cd /opt/es-software
    4
    rpm -ivh jdk-8u102-linux-x64.rpm
    5
    #检查安装信息
    6
    java -version
    7
    java version "1.8.0_102"
    8
    Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
    9
    Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
    10
    #上传es安装包
    11
    rpm -ivh elasticsearch-6.6.0.rpm
    12
    13
    #看下都安装了那些东西
    14
    rpm -qc elasticsearch
    15
    /etc/elasticsearch/elasticsearch.yml#es的主要配置文件
    16
    /etc/elasticsearch/jvm.options#调整jvm内存配置文件
    17
    /etc/init.d/elasticsearch#启动脚本
    18
    /etc/sysconfig/elasticsearch#配置环境变量
    19
    /usr/lib/sysctl.d/elasticsearch.conf#配置环境变量
    20
    /usr/lib/systemd/system/elasticsearch.service#启动服务用的文件
    21
    #修改配置文件
    22
    vim /etc/elasticsearch/elasticsearch.yml
    23
    [root@es01 es-software]# egrep -v '^$|#' /etc/elasticsearch/elasticsearch.yml
    24
    node.name: oldboy01
    25
    path.data: /var/lib/elasticsearch
    26
    path.logs: /var/log/elasticsearch
    27
    bootstrap.memory_lock: true
    28
    network.host: 10.0.0.240,127.0.0.1
    29
    http.port: 9200
    30
    #启动es服务
    31
    systemctl start elasticsearch
    32
    #在启动时发生内存锁定失败的问题
    33
    #官方文档介绍
    34
    https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-system-settings.html#sysconfig
    35
    #解决方案
    36
    systemctl edit elasticsearch 
    37
    [Service]
    38
    LimitMEMLOCK=infinity
    39
    #需要重新载入
    40
    systemctl daemon-reload
    41
    systemctl restart elasticsearch
    42
    #检验是否能正常工作
    43
    命令行的方式:curl 10.0.0.240:9200
    44
    浏览器的方式:http://10.0.0.240:9200
    45
    [root@es01 ~]# curl localhost:9200
    46
    {
    47
      "name" : "oldboy01",
    48
      "cluster_name" : "elasticsearch",
    49
      "cluster_uuid" : "yJ0eJGDyTWmcPywIJp5vDw",
    50
      "version" : {
    51
        "number" : "6.6.0",
    52
        "build_flavor" : "default",
    53
        "build_type" : "rpm",
    54
        "build_hash" : "a9861f4",
    55
        "build_date" : "2019-01-24T11:27:09.439740Z",
    56
        "build_snapshot" : false,
    57
        "lucene_version" : "7.6.0",
    58
        "minimum_wire_compatibility_version" : "5.6.0",
    59
        "minimum_index_compatibility_version" : "5.0.0"
    60
      },
    61
      "tagline" : "You Know, for Search"
    62
    }
    63
     
     

    10、索引、类型、docs

     
     
     
     
     
     
    1
    1、索引:好比一个mysql当中一个数据库
    2
    2、类型:好比一个库当中的一个表
    3
    3、docs:好比表中的字段内容
     
     

    11、es互动的方式

    11.1、命令行的方式

     
     
     
     
     
     
    1
    #创建个索引
    2
    curl -XPUT '10.0.0.240:9200/oldboy?pretty'
    3
    #往这个索引里面插入一条指定id号为1的数据
    4
    curl -XPUT '10.0.0.240:9200/oldboy/student/1?pretty' -H 'Content-Type: application/json' -d'
    5
    {
    6
        "first_name" : "zhang",
    7
        "last_name": "san",
    8
        "age" : 25,
    9
        "about" : "I love to go rock climbing", 
    10
        "interests": [ "sports" ]
    11
    }'
    12
    #往这个索引里面插入一条随机生成一个id的数据
    13
    curl -XPUT  '10.0.0.240:9200/oldboy/student/2?pretty' -H 'Content-Type: application/json' -d' {
    14
    "first_name": "li",
    15
    "last_name" : "si",
    16
    "age" : 32,
    17
    "about" : "I like to collect rock albums", 
    18
    "interests": [ "music" ]
    19
    }'
    20
    #查询索引当中的一条数据
    21
    curl -XGET  '10.0.0.240:9200/oldboy/student/1?pretty'
    22
    #删除索引当中的一条数据
    23
    curl -XDELETE  '10.0.0.240:9200/oldboy/student/1?pretty'
    24
    #删除指定的索引
    25
    curl -XDELETE  '10.0.0.240:9200/oldboy/?pretty'
     
     

    11.2、通过插件(head)的方式和es互动

     
     
     
     
     
     
    1
    #下载epel源
    2
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    3
    4
    1、docker方式来部署head插件
    5
    docker pull alivv/elasticsearch-head
    6
    docker run --name es-head -p 9100:9100 -dit elivv/elasticsearch-head
    7
    2、nodejs  npm的方式来部署head
    8
    使用nodejs编译安装elasticsearch-head
    9
    yum install nodejs npm openssl screen -y#配置nodejs环境
    10
    node -v
    11
    npm  -v
    12
    #安装组件
    13
    npm install -g cnpm --registry=https://registry.npm.taobao.org 
    14
    cd /opt/
    15
    #克隆代码
    16
    官网https://github.com/mobz/elasticsearch-head
    17
    git clone git://github.com/mobz/elasticsearch-head.git
    18
    cd elasticsearch-head/
    19
    cnpm install
    20
    screen -S es-head
    21
    cnpm run start
    22
    Ctrl+A+D
    23
    #通过cnpm的方式来运行head时, 发现无法链接es服务器
    24
    vim /etc/elasticsearch/elasticsearch.yml
    25
    http.cors.enabled: true
    26
    http.cors.allow-origin: "*"
    27
    3、在谷歌浏览器中安装一个插件
    28
     
     

    1584623605534

    11.3、在浏览器中检验head能否连接es服务器

    1584622548170

    11.4、使用kibana和es进行交互

     
     
     
     
     
     
    1
    #部署kibana工具
    2
    cd /opt/es-software
    3
    #上传需要的安装包并安装
    4
    rpm -ivh kibana-6.6.0-x86_64.rpm
    5
    #修改配置文件
    6
    vim /etc/kibana/kibana.yml
    7
    [root@es01 es-software]# egrep -v '^$|#' /etc/kibana/kibana.yml
    8
    server.port: 5601
    9
    server.host: "10.0.0.240"
    10
    elasticsearch.hosts: ["http://10.0.0.240:9200"]
    11
    kibana.index: ".kibana"
    12
    #启动kibana服务
    13
    systemctl start kibana
    14
    #在浏览器中测试kibana服务是否正常
    15
    http://10.0.0.240:5601
     
     

    1584622571763

    11.5、在kibana界面往es系统提交数据

     
     
     
     
     
     
    1
    dev tools--->console
    2
    #创建一个索引
    3
    PUT oldboy
    4
    #在oldboy索引中插入一条数据
    5
    PUT oldboy/student/1
    6
    {
    7
      "name": "zhang san",
    8
      "age": "29",
    9
      "xuehao": 1
    10
    }
    11
    #随机插入一条数据
    12
    POST oldboy/student/
    13
    {
    14
      "name": "oldboy",
    15
      "age": "19",
    16
     "xuehao": 5
    17
    }
    18
    #查询数据
    19
    GET oldboy/student/2
    20
    GET oldboy/_search
    21
    #删除一条数据
    22
    DELETE oldboy/student/1
    23
    #修改系统默认的分片数和副本数
    24
    PUT _template/template_http_request_record 
    25
    {
    26
     "index_patterns": ["*"],
    27
     "settings": {
    28
             "number_of_shards" : 3,
    29
             "number_of_replicas" : 1
    30
         }
    31
     }
     
     

    备注:

     
     
     
     
     
     
    1
    1、即使用有某些索引不再使用,不要立马删除,可以先关闭掉,确定一段时间内没有人使用,再删除
    2
    2、一旦索引被创建,分片的数量不能再二次修改,副本的数量可以修改
     
     

    kibana页面无法打开,如何解决

     
     
     
     
     
     
    1
    #报错的信息
    2
    "Another Kibana instance appears to be migrating the index. Waiting for that migration to complete. If no other Kibana instance is attempting migrations, you can get past this message by deleting index .kibana_1 and restarting Kibana."}
    3
    #解决的方案
    4
    curl -XDELETE http://10.0.0.240:9200/.kibana_1
    5
    systemctl restart kibana
     
     

    11.6、修改副本数量的命令

     
     
     
     
     
     
    1
    PUT /oldboy/_settings
    2
    {
    3
        "number_of_replicas": 2
    4
    }
     
     

    12、部署es集群流程

     
     
     
     
     
     
    1
    #上传需要安装的包
    2
    mkdir /opt/es-software
    3
    cd /opt/es-software
    4
    jkd  elasticsearch
    5
    #安装包
    6
    rpm -ivh jdk-8u102-linux-x64.rpm
    7
    rpm -ivh elasticsearch-6.6.0.rpm 
     
     

    12.1、修改各节点的配置文件

     
     
     
     
     
     
    1
    #在10.0.0.240主机操作
    2
    vim /etc/elasticsearch/elasticsearch.yml
    3
    [root@es01 es-software]# grep -E -v '^$|#' /etc/elasticsearch/elasticsearch.yml
    4
    cluster.name: oldboy-cluster
    5
    node.name: oldboy01
    6
    path.data: /var/lib/elasticsearch
    7
    path.logs: /var/log/elasticsearch
    8
    bootstrap.memory_lock: true
    9
    network.host: 10.0.0.240,127.0.0.1
    10
    http.port: 9200
    11
    discovery.zen.ping.unicast.hosts: ["10.0.0.240", "10.0.0.241"]
    12
    discovery.zen.minimum_master_nodes: 2 
    13
    http.cors.enabled: true
    14
    http.cors.allow-origin: "*"
    15
    16
    #在10.0.0.241主机操作
    17
    vim /etc/elasticsearch/elasticsearch.yml
    18
    [root@es02 es-software]# grep -E -v '^$|#' /etc/elasticsearch/elasticsearch.yml
    19
    cluster.name: oldboy-cluster
    20
    node.name: oldboy02
    21
    path.data: /var/lib/elasticsearch
    22
    path.logs: /var/log/elasticsearch
    23
    bootstrap.memory_lock: true
    24
    network.host: 10.0.0.241,127.0.0.1
    25
    http.port: 9200
    26
    discovery.zen.ping.unicast.hosts: ["10.0.0.241", "10.0.0.242"]
    27
    discovery.zen.minimum_master_nodes: 2 
    28
    http.cors.enabled: true
    29
    http.cors.allow-origin: "*"
    30
    31
    #在10.0.0.242主机操作
    32
    vim /etc/elasticsearch/elasticsearch.yml
    33
    [root@es03 es-software]# grep -E -v '^$|#' /etc/elasticsearch/elasticsearch.yml
    34
    cluster.name: oldboy-cluster
    35
    node.name: oldboy03
    36
    path.data: /var/lib/elasticsearch
    37
    path.logs: /var/log/elasticsearch
    38
    bootstrap.memory_lock: true
    39
    network.host: 10.0.0.242,127.0.0.1
    40
    http.port: 9200
    41
    discovery.zen.ping.unicast.hosts: ["10.0.0.242", "10.0.0.240"]
    42
    discovery.zen.minimum_master_nodes: 2 
    43
    http.cors.enabled: true
    44
    http.cors.allow-origin: "*"
     
     

    12.2、检查集群是否能正常运行

     
     
     
     
     
     
    1
    #通过head插件来检查状态
     
     

    12.3、分片和副本的含义

     
     
     
     
     
     
    1
    分片:把存储到索引里面的数据,分隔一块块的,均匀的存储到不同的节点上面,构成分步式存储分担集群的压力
    2
    分片各类:主分片和副本分片
    3
    主分片:主要是响应修改数据的请求,同时提供查询的功能
    4
    副本分片:主要是提供查询的功能
    5
    副本:副本就是主分片的备份
     
     

    12.4、3台服务器集群,最多可以宕机多少台

     
     
     
     
     
     
    1
    1、如果一台一台的宕机,最多可以宕机2台(需要手动修改配置文件才能保障服务正常使用),正常情况下,一台一台的宕机(宕机一台修好一台,不影响集群的健康状态)
    2
    2、前提是1副本,如果同时宕机2台,那么数据就会丢失,集群处于红色状态
    3
    3、前提是2个副本,如果同一台一台的宕机没有什么影响,即使是同时宕机2台,通过修改配置文件,也可以正常提供服务。
     
     

    12.5、生产中注意事项

     
     
     
     
     
     
    1
    1、索引一旦创建成功,分片的数量是不能再修改,但是副本的数量可以人为的修改
    2
    2、分片数量多少合适,如果集群中有3个节点,分片的数量为3*3个
    3
       如果集群中有2个节点,你可以采用默认的分片数量,也可分4个分片
    4
    3、在配置主机相互发现时,可以两两主机串起来配置
     
     

    12.6、如何监控集群运行状态

     
     
     
     
     
     
    1
    .curl '10.0.0.240:9200/_cluster/health?pretty'
    2
    .GET _cat/nodes
    3
    .GET _cat/health
     
     

    12.7、部署备份索引工具elasticdump

     
     
     
     
     
     
    1
    #nodejs官网地址
    2
    https://nodejs.org/en/
    3
    #部署node和nmp的环境,要有epel源
    4
    yum install nodejs npm openssl screen -y
    5
    #检查版本信息
    6
    node -v
    7
    npm -v
    8
    #使用国内淘宝镜像源(可能要等几分钟)
    9
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    10
    #安装elasticdump工具
    11
    官网地址https://github.com/taskrabbit/elasticsearch-dump
    12
    #使用elasticdump工具备份索引
    13
    mkdir /data
    14
    elasticdump 
    15
      --input=http://10.0.0.240:9200/oldboy 
    16
      --output=/data/oldboy.json 
    17
      --type=data
    18
    #通过备份的数据来恢复
    19
    elasticdump 
    20
    --input=/data/oldboy.json 
    21
    --output=http://10.0.0.240:9200/oldboy
    22
    #把索引可以备份为一个压缩文件的形式(如果要恢复,必须要解压)
    23
    elasticdump 
    24
      --input=http://10.0.0.240:9200/oldboy 
    25
      --output=$ 
    26
      | gzip > /data/oldboy.json.gz
     
     

    备注:使用nodejs_12.16.1会导致不兼容的情况,需要升级

     
     
     
     
     
     
    1
    npm install -g n
    2
    n latest
     
     

    13、中文分词器

     
     
     
     
     
     
    1
    1、有些词语原本不是一个词语,通过中文分词器,人为的把不是一个词语的词,当成一个完整的词语,然后存到es内部的词典当中。比例“老男孩”
     
     

    13.1、下载安装包

     
     
     
     
     
     
    1
    #官网下载地址
    2
    3
    https://github.com/medcl/elasticsearch-analysis-ik/releases
    4
    #下载到指定目录
    5
    cd /opt/es-software
    6
    #使用命令安装(如果集群有多台机器,每台服务器都要安装)
    7
    cd /usr/share/elasticsearch
    8
    ./bin/elasticsearch-plugin install  file:///opt/es-software/elasticsearch-analysis-ik-6.6.0.zip
    9
    #把es服务重启一下
    10
    systemctl restart elasticsearch
    11
    #创建一个新的索引(在kibana界面)
    12
    PUT news
    13
    #应用中文分词器的模板
    14
    curl -XPOST http://localhost:9200/news/text/_mapping -H 'Content-Type:application/json' -d'
    15
    {
    16
            "properties": {
    17
                "content": {
    18
                    "type": "text",
    19
                    "analyzer": "ik_max_word",
    20
                    "search_analyzer": "ik_smart"
    21
                }
    22
            }
    23
    }'
    24
    #往已创建好的索引里面写数据
    25
    POST /news/text/1
    26
    {"content":"美国留给伊拉克的是个烂摊子吗"}
    27
    28
    POST /news/text/2
    29
    {"content":"公安部:各地校车将享最高路权"}
    30
    31
    POST /news/text/3
    32
    {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
    33
    34
    POST /news/text/4
    35
    {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
    36
    #通过kibana进行验证词语是否能正常切分
    37
    POST /news/_search
    38
    {
    39
        "query" : { "match" : { "content" : "中国" }},
    40
        "highlight" : {
    41
            "pre_tags" : ["<tag1>", "<tag2>"],
    42
            "post_tags" : ["</tag1>", "</tag2>"],
    43
            "fields" : {
    44
                "content" : {}
    45
            }
    46
        }
    47
    }
    48
     
     

    1584622596627

    13.2、热动态更新词典

     
     
     
     
     
     
    1
    #安装nginx服务
    2
    yum install nginx -y
    3
    #添加一个配置文件(gbk字符会出现冲突,如果发有问题,可以删除gbk)
    4
    cd /etc/nginx/conf.d/
    5
    vim www.conf
    6
    server {
    7
        listen 80;
    8
        server_name elk.oldboy.com;
    9
        location / {
    10
            root /usr/share/nginx/html/download;
    11
            charset utf-8,gbk;
    12
            autoindex on;
    13
            autoindex_localtime on;
    14
            autoindex_exact_size off;
    15
          }
    16
        }
    17
    #检查代码是否误
    18
    nginx - t
    19
    #创建目录
    20
    mkdir /usr/share/nginx/html/download
    21
    #新建一个文件,存放临时要加入的词语
    22
    vim dic.txt
    23
    老男孩
    24
    老朋友
    25
    。。。
    26
    #域名解析
    27
    要修改一下host文件 
    28
    10.0.0.240 elk.oldboy.com
    29
    #在浏览器中测试一下,(当dic.txt文件发生改变时,关注modifily-time etag是否发生变化)
    30
    elk.oldboy.com
    31
    #修改IKAnalyzer.cfg.xml(集群中要是有多台服务器,每台服务器都要修改)
    32
    cd /etc/elasticsearch/analysis-ik
    33
    vim IKAnalyzer.cfg.xml
    34
    <entry key="remote_ext_dict">http://10.0.0.240/download/dic.txt</entry>
    35
    #重启elasticsearch服务
    36
    systemctl restart elasticsearch
    37
    #关注日志文件,看是否有扩展的字典内容加载,如果有加载说明扩展词典已经生效
    38
    #在kibana界面测试,根据我们新加入的词语,来创建一个词条
    39
    POST /news/text/6
    40
    {"content":"北京老女孩教育"}
    41
    POST /news/text/7
    42
    {"content":"北京老男孩教育余小闯"}
    43
    #当词条写入成功之后,可以用命令检查
    44
    POST /news/_search
    45
    {
    46
        "query" : { "match" : { "content" : "余小闯" }},
    47
        "highlight" : {
    48
            "pre_tags" : ["<tag1>", "<tag2>"],
    49
            "post_tags" : ["</tag1>", "</tag2>"],
    50
            "fields" : {
    51
                "content" : {}
    52
            }
    53
        }
    54
    }
    55
    56
     
     

    14、收集日志在工作当中具体的应用

     
     
     
     
     
     
    1
    1、今天上午10点访问web站点排名前10的ip
    2
    2、统计今天访问前10的url
    3
    3、把今天上午10点之前访问量排名前10的ip和昨天这个时间段对比一下,有什么区别
    4
    4、发现页面的响应时间超1s,导致web服务响应过慢,找出是否爬虫ip
    5
    5、信息在5分钟内给我结果
     
     

    15、工作中那些服务器需要收集日志

     
     
     
     
     
     
    1
    代理层:nginx
    2
    web层:nginx tomcat php java
    3
    数据库:mysql  mariadb  es mongo
    4
    系统层:message  rsyslog  secure
     
     

    16、部署filebeat工具,收集nginx日志

    16.1、安装nginx服务

     
     
     
     
     
     
    1
    #准备epel源
    2
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    3
    #安装nginx
    4
    yum install nginx -y
    5
    #添加配置文件
    6
    cd /etc/nginx/conf.d/
    7
    vim www.conf
    8
    server {
    9
        listen 80;
    10
        server_name elk.oldboy.com; 
    11
        location / {
    12
            root   /code/www;
    13
            index  index.html index.htm;
    14
        }
    15
    }
    16
    #创建目录
    17
    mkdir /code/www -p
    18
    echo "Hello Oldboy Search Nginx Log! " > /code/www/index.html
    19
    #启动nginx服务
    20
    nginx -t
    21
    systemctl start nginx
    22
    #如果配置了域名,要记得做域名解析
    23
    24
    #在浏览器中访问,可以产生一些访问日志
    25
    elk.oldboy01.com
     
     

    16.2、部署filebeat软件

     
     
     
     
     
     
    1
    #上传需要安装的软件包并安装
    2
    rpm -ivh filebeat-6.6.0-x86_64.rpm
    3
    #修改配置文件
    4
    vim /etc/filebeat/filebeat.yml
    5
    [root@web01-243 es-software]# egrep -v '^$|#' /etc/filebeat/filebeat.yml 
    6
    filebeat.inputs:
    7
    - type: log
    8
      enabled: true
    9
      paths:
    10
        - /var/log/nginx/access.log
    11
    12
    output.elasticsearch:
    13
      hosts: ["10.0.0.240:9200"]
    14
    #启动filebeat服务
    15
    systemctl start filebeat
    16
    #在head插件界面,检查是否日志已经收集成功
    17
    18
     
     

    1584622613399

    16.3、在kibana界面来添加index-patterns(management--index patterns)

    1584622620753

    1584622625704

    16.4、在kibana界面展示收集到的日志信息

    1584622635951

    16.5、使用filebeat收集nginx的json日志

     
     
     
     
     
     
    1
    #修改nginx的配置文件
    2
    cd /etc/nginx
    3
    vim nginx.conf
    4
    log_format json '{ "time_local": "$time_local", '
    5
                              '"remote_addr": "$remote_addr", '
    6
                              '"referer": "$http_referer", '
    7
                              '"request": "$request", '
    8
                              '"status": $status, '
    9
                              '"bytes": $body_bytes_sent, '
    10
                              '"agent": "$http_user_agent", '
    11
                              '"x_forwarded": "$http_x_forwarded_for", '
    12
                              '"up_addr": "$upstream_addr",'
    13
                              '"up_host": "$upstream_http_host",'
    14
                              '"upstream_time": "$upstream_response_time",'
    15
                              '"request_time": "$request_time"'
    16
        ' }';
    17
    18
        access_log  /var/log/nginx/access.log  json;
    19
    #检查代码是否有误
    20
    nginx -t
    21
    #重启nginx服务
    22
    systemctl restart nginx
    23
    #通过浏览器来访问nginx站点,生产日志,确保日志格式是json
    24
    #虽然nginx日志格式已经为json格式,但是filebeat无法解析json格式的日志(格式对齐)
    25
    vim /etc/filebeat/filebeat.yml
    26
    filebeat.inputs:
    27
     - type: log
    28
       enabled: true
    29
       paths:
    30
         - /var/log/nginx/access.log
    31
       json.keys_under_root: true
    32
       json.overwrite_keys: true
    33
    34
    #重启filebeat服务
    35
    systemctl restart filebeat
    36
    37
    #在head插件界面,检查日志是否收集成功,是已经把日志切分开
     
     

    1584622662722

    16.6、在kibana界面添加index-patterns

    management--->index-patterns

    image-20200318104916089

    1584622688533

    158462270240616.7、优化索引格式

     
     
     
     
     
     
    1
    1、索引名称可以自定
    2
    2、把日志可以按月来收集,如果按天收集太乱
    3
    3、如果使用自定义的模板,要加入相应的配置信息
    4
     
     

    16.8、收集nginx的两种日志

     
     
     
     
     
     
    1
    #修改filebeat的配置文件
    2
    vim filebeat.yml
    3
    filebeat.inputs:
    4
     - type: log
    5
       enabled: true
    6
       paths:
    7
         - /var/log/nginx/access.log
    8
       json.keys_under_root: true
    9
       json.overwrite_keys: true
    10
     - type: log
    11
       enabled: true
    12
       paths:
    13
         - /var/log/nginx/error.log
    14
    output.elasticsearch:
    15
      hosts: ["10.0.0.240:9200"]
    16
      indices:
    17
      - index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
    18
        when.contains:
    19
          source: "/var/log/nginx/access.log"
    20
      - index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
    21
        when.contains:
    22
          source: "/var/log/nginx/error.log"
    23
    setup.template.name: "nginx"
    24
    setup.template.pattern: "nginx-*"
    25
    setup.template.enabled: false
    26
    setup.template.overwrite: true
    27
    #重新启动服务
    28
    systemctl restart filebeat
    29
    #在head界面把之前的老索引删除
    30
    #重新再生产一些访问的日志和错误日志
    31
    #再回到head界面检查日志是否正常收集
    32
     
     

    1584622721972

    17、使用filebeat收集tomcat日志

    17.1、部署tomcat平台

     
     
     
     
     
     
    1
    #上传java安装包并安装
    2
    cd /opt/es-software
    3
    rpm -ivh jdk-8u102-linux-x64.rpm
    4
    #上传tomcat安装包并解压
    5
    tar xf apache-tomcat-8.5.49.tar.gz -C /opt/
    6
    ln -s /opt/apache-tomcat-8.5.49 /opt/tomcat
    7
    #启动tomcat服务,正常情况下会监听8080端口
    8
    cd /opt/tomcat/bin/
    9
    ./startup.sh
    10
    #为了更好的分析日志,需要对tomcat的日志格式进行修改(162行)
    11
    cd /opt/tomcat/conf/server.xml
    12
    pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/>
    13
    #重启tomcat服务,让修改后的配置文件生效
    14
    ./shutdown.sh
    15
    ./startup.sh
    16
    #在浏览器中再次访问tomcat首页,产生一些新的日志
    17
    #把新的json日志,做校验处理,是否符合json格式
     
     

    17.2、修改filebeat的配置文件

     
     
     
     
     
     
    1
    #进行filebeat主配置文件所在的目录
    2
    cd /etc/filebeat/
    3
    vim filebeat.yml
    4
    [root@web01-243 logs]# cat /etc/filebeat/filebeat.yml
    5
    filebeat.inputs:
    6
     - type: log
    7
       enabled: true
    8
       paths:
    9
         - /var/log/nginx/access.log
    10
       json.keys_under_root: true
    11
       json.overwrite_keys: true
    12
       tags: ["access"]
    13
     - type: log
    14
       enabled: true
    15
       paths:
    16
         - /var/log/nginx/error.log
    17
       tags: ["error"]
    18
     - type: log
    19
       enabled: true
    20
       paths:
    21
         - /opt/tomcat/logs/localhost_access_log.2020-03-18.txt
    22
       json.keys_under_root: true
    23
       json.overwrite_keys: true
    24
       tags: ["tomcat"]
    25
    26
    output.elasticsearch:
    27
      hosts: ["10.0.0.240:9200"]
    28
      indices:
    29
      - index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
    30
        when.contains:
    31
           tags: "access"
    32
      - index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
    33
        when.contains:
    34
           tags: "error"
    35
      - index: "tomcatl-access-%{[beat.version]}-%{+yyyy.MM}"
    36
        when.contains:
    37
           tags: "tomcat"
    38
    setup.template.name: "nginx"
    39
    setup.template.pattern: "nginx-*"
    40
    setup.template.enabled: false
    41
    setup.template.overwrite: true
    42
    #重启filebeat服务
    43
    systemctl restart filebeat
    44
    #到head插件界面验证,日志是否能正常收集
    45
     
     

    1584622735559

    17.3、在kibana界面来展示tomcat日志

    1584622742838

    18、filebeat收集java日志

    18.1、部署一个java的工作环境

     
     
     
     
     
     
    1
    #使用elasticsearch这个环境,修改es的配置文件
    2
    vim /etc/elasticsearch/elasticsearch.yml
    3
    aanode.name: oldboy02
    4
    #重启es服务
    5
    systemctl restart elasticsearch
    6
    #使用命令查看elasticsearch日志信息,发现有报错的信息输出
    7
     tail -f /var/log/ealsticsearch/elasticsearch.log
    8
     
     

    18.2、安装收集日志工具

     
     
     
     
     
     
    1
    #安装filebeat工具,修改相应的主配置文件,把默认的配置文件改一名
    2
    vim /etc/filebeat/filebeat.yml
    3
    filebeat.inputs:
    4
    - type: log
    5
      enabled: true
    6
      paths:
    7
        - /var/log/elasticsearch/elasticsearch.log
    8
      multiline.pattern: '^['
    9
      multiline.negate: true
    10
      multiline.match: after
    11
    12
    output.elasticsearch:
    13
      hosts: ["10.0.0.240:9200"]
    14
      index: "es-%{[beat.version]}-%{+yyyy.MM}"
    15
    setup.template.name: "es"
    16
    setup.template.pattern: "es-*"
    17
    setup.template.enabled: false
    18
    setup.template.overwrite: true
    19
    #重启filebeat服务
    20
    systemctl restart filebeat
    21
    #head界面检查日志是否能正常收集
     
     

    1584622756867

    18.3、在kibana界面展示收集的日志信息

    1584622761899

    1584622769696

    19、使用filebeat收集mysql慢日志

    19.1、部署一个mysql环境

     
     
     
     
     
     
    1
    #安装mariadb
    2
    yum install mariadb-server -y
    3
    #启动mariadb服务
    4
    systemctl start mariadb
    5
    #确认慢日志功能是否开启
    6
    show variables like '%slow_query_log%';
    7
    --------------------+--------------------+
    8
    | Variable_name       | Value              |
    9
    +---------------------+--------------------+
    10
    | slow_query_log      | OFF                |
    11
    | slow_query_log_file | web01-243-slow.log 
    12
    #开启慢日志功能
    13
    vim /etc/my.cnf
    14
    [mysqld]
    15
    slow_query_log=ON
    16
    slow_query_log_file=/var/log/mariadb/slow.log
    17
    long_query_time=1
    18
    #重新启动mariadb服务
    19
    systemctl restart mariadb
    20
    #生成一些慢查询语句
    21
    mysql
    22
    select sleep(2) user,host from mysql.user;
    23
    #确认慢日志是否生成
    24
    cat /var/log/mariadb/slow.log
     
     

    19.2、修改filebeat的配置文件,启用filebeat对应的模块

     
     
     
     
     
     
    1
    #开启模块化功能(加动配置文件最下方)
    2
    vim /etc/filebeat/filebeat.yml
    3
    filebeat.config.modules:
    4
      path: ${path.config}/modules.d/*.yml
    5
      reload.enabled: true
    6
      reload.period: 10s
    7
    #重启filebeat服务
    8
    systemctl restart filebeat
    9
    #打开mysql模块(也可以通过修改模块的名称来开启)
    10
    filebeat modules list
    11
    filebeat modules enable mysql
    12
    #修改mysql模块配置
    13
    vim /etc/filebeat/modules.d/mysql.yml
    14
    15
    - module: mysql
    16
      error:
    17
        enabled: true
    18
        var.paths: ["/var/log/mariadb/mariadb.log"]
    19
    20
      slowlog:
    21
        enabled: true
    22
        var.paths: ["/var/log/mariadb/slow.log"]
    23
    #修改filebeat的主配置文件
    24
    vim /etc/filebeat/filebeat.yml
    25
    output.elasticsearch:
    26
      hosts: ["10.0.0.240:9200"]
    27
      indices:
    28
      - index: "mysql-error-%{[beat.version]}-%{+yyyy.MM}"
    29
        when.contains:
    30
           source: "/var/log/mariadb/mariadb.log"
    31
      - index: "mysql-slow-%{[beat.version]}-%{+yyyy.MM}"
    32
        when.contains:
    33
           source: "/var/log/mariadb/slow.log"
    34
    setup.template.name: "mysql"
    35
    setup.template.pattern: "mysql-*"
    36
    setup.template.enabled: false
    37
    setup.template.overwrite: true
    38
    filebeat.config.modules:
    39
      path: ${path.config}/modules.d/*.yml
    40
      reload.enabled: true
    41
      reload.period: 10s
    42
    #重启filebeat服务
    43
    systemctl restart filebeat
    44
    #为了测试需要,别外生成一些慢日志和错误
    45
    #在head界面检查日志是否收集成功
    46
     
     

    1584622779913

    19.3、在kibana界面添加index-patterns

    1584622786085

    19.4、使用filebeat的nginx模块功能,收集nginx的普通日志

     
     
     
     
     
     
    1
    #修改nginx日志的格式
    2
    vim /etc/nginx/nginx.conf
    3
    access_log  /var/log/nginx/access.log  main;
    4
    #检查语法
    5
    nginx -t
    6
    #启动nginx服务
    7
    systemctl start nginx
    8
    #检查日志信息
    9
     
     

    19.5、打开nginx模块

     
     
     
     
     
     
    1
    #进入相应的目录
    2
    cd /etc/filebeat/modules.d
    3
    #打开nginx模块功能
    4
    filebeat modules enble nginx
    5
    mv nginx.yml.disabled nginx.yml
    6
    #修改nginx模块
    7
    vim nginx.yml
    8
    - module: nginx
    9
      access:
    10
        enabled: true
    11
        var.paths: ["/var/log/nginx/access.log"]
    12
    13
      error:
    14
        enabled: true
    15
        var.paths: ["/var/log/nginx/error.log"]
    16
    #修改filebeat的主配置文件
    17
    vim /etc/filebeat/filebeat.yml
    18
    output.elasticsearch:
    19
      hosts: ["10.0.0.240:9200"]
    20
      indices:
    21
      - index: "nginx-error-%{[beat.version]}-%{+yyyy.MM}"
    22
        when.contains:
    23
           source: "/var/log/nginx/error.log"
    24
      - index: "nginx-access-%{[beat.version]}-%{+yyyy.MM}"
    25
        when.contains:
    26
           source: "/var/log/nginx/access.log"
    27
    setup.template.name: "nginx"
    28
    setup.template.pattern: "nginx-*"
    29
    setup.template.enabled: false
    30
    setup.template.overwrite: true
    31
    filebeat.config.modules:
    32
      path: ${path.config}/modules.d/*.yml
    33
      reload.enabled: true
    34
      reload.period: 10s
    35
    #重启filebeat服务
    36
    systemctl restart filebeat
    37
    #发现es缺少相关的插件(如果是集群,每台服务器都要安装)
    38
    2020-03-20T12:56:11.819+0800    ERROR   pipeline/output.go:100  Failed to connect to backoff(elasticsearch(http://10.0.0.240:9200)): Connection marked as failed because the onConnect callback failed: Error loading pipeline for fileset nginx/access: This module requires the following Elasticsearch plugins: ingest-user-agent, ingest-geoip. You can install them by running the following commands on all the Elasticsearch nodes:
    39
        sudo bin/elasticsearch-plugin install ingest-user-agent
    40
        sudo bin/elasticsearch-plugin install ingest-geoip
    41
    #解决方法,需要在每中es服务器上面安装两个插件
    42
    cd /usr/share/elasticsearch/
    43
    ./bin/elasticsearch-plugin install file:///opt/es-software/ingest-geoip-6.6.0.zip
    44
    ./bin/elasticsearch-plugin install file:///opt/es-software/ingest-user-agent-6.6.0.zip
    45
    #重启es服务和filebeat服务
    46
    systemctl restart elasticsearch
    47
    systemctl restart filebeat
    48
    #在head界面验证日志是否正常收集
    49
    50
     
     

    1584622797239

    19.6、在kibana界面添加index-patterns

    1584622802700

    1584622807888做缓存来收集日志

    20.1、部署redis环境

     
     
     
     
     
     
    1
    #配置epel源
    2
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    3
    #安装redis
    4
    yum install redis -y
    5
    #修改redis配置文件
    6
    vim /etc/redis.conf
    7
    bind 10.0.0.240
    8
    #启动redis服务
    9
    systemctl start redis
    10
     
     

    20.2、修改filebeat的配置文件

     
     
     
     
     
     
    1
    #把nginx的日志格式改为json
    2
    #修改filebeat的主配置文件
    3
    vim /etc/filebeat/filebeat.yml
    4
    filebeat.inputs:
    5
    - type: log
    6
      enabled: true
    7
      paths:
    8
        - /var/log/nginx/access.log
    9
      json.keys_under_root: true
    10
      json.overwrite_keys: true
    11
      tags: ["access"]
    12
    13
    - type: log
    14
      enabled: true
    15
      paths:
    16
        - /var/log/nginx/error.log
    17
      tags: ["error"]
    18
    19
    output.redis:
    20
      hosts: ["10.0.0.240"]
    21
      keys:
    22
        - key: "nginx_access"
    23
          when.contains:
    24
            tags: "access"
    25
        - key: "nginx_error"
    26
          when.contains:
    27
            tags: "error"
    28
    29
    setup.template.name: "nginx"
    30
    setup.template.pattern: "nginx_*"
    31
    setup.template.enabled: false
    32
    setup.template.overwrite: true
    33
    #重启filebeat服务
    34
    systemctl restart filebeat
     
     

    20.3、检查redis能否收集日志

     
     
     
     
     
     
    1
    #连接redis
    2
    redis-cli -h 10.0.0.240
    3
    #使用命令检查
    4
    info
    5
    db0:keys=2,expires=0,avg_ttl=0
    6
     
     

    20.4、安装logstash工具

     
     
     
     
     
     
    1
    #进行目录
    2
    cd /opt/es-software
    3
    #上传logstash安装包,并安装
    4
    rpm -ivh logstash-6.6.0.rpm
    5
    #添加一个配置文件
    6
    cd /etc/logstash/con.d
    7
    vim nginx_log.conf
    8
    input {
    9
      redis {
    10
        host => "10.0.0.240"
    11
        port => "6379"
    12
        db => "0"
    13
        key => "nginx_access"
    14
        data_type => "list"
    15
      }
    16
      redis {
    17
        host => "10.0.0.240"
    18
        port => "6379"
    19
        db => "0"
    20
        key => "nginx_error"
    21
        data_type => "list"
    22
      }
    23
    }
    24
    25
    filter {
    26
      mutate {
    27
        convert => ["upstream_time", "float"]
    28
        convert => ["request_time", "float"]
    29
      }
    30
    }
    31
    32
    output {
    33
       stdout {}
    34
       if "access" in [tags] {
    35
          elasticsearch {
    36
            hosts => "http://10.0.0.240:9200"
    37
            manage_template => false
    38
            index => "nginx_access-%{+yyyy.MM}"
    39
          }
    40
        }
    41
        if "error" in [tags] {
    42
          elasticsearch {
    43
            hosts => "http://10.0.0.240:9200"
    44
            manage_template => false
    45
            index => "nginx_error-%{+yyyy.MM}"
    46
          }
    47
        }
    48
    }
    49
    #可以放到前台测试logstash能否正常取日志(如果正常,前台会打印日志信息)
    50
    /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_log.conf
    51
    #在head界面验证
     
     

    1584622823941

    • 在kibana界面添加index-patterns

    1584622836930

    21、使用kafka做缓存收集日志

    21.1配置hosts(三台都要配置)

     
     
     
     
     
     
    1
    10.0.0.240 es01
    2
    10.0.0.241 es02
    3
    10.0.0.242 es03
     
     

    架构图

    1584622965010

    21.2安装配置zookeeper(三台都要配置)

     
     
     
     
     
     
    1
    #把需要的软件包上传
    2
    cd /opt/es_software
    3
    #解压软件并做软件连接
    4
    tar zxf zookeeper-3.4.11.tar.gz -C /opt/
    5
    ln -s /opt/zookeeper-3.4.11/ /opt/zookeeper
    6
    #创建一个数据目录
    7
    mkdir -p /data/zookeeper
    8
    #把zoo_sample.cfg复制一个zoo.cfg文件
    9
    cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
    10
    #修改zoo.cfg配置文件
    11
    vim /opt/zookeeper/conf/zoo.cfg
    12
    # 服务器之间或客户端与服务器之间维持心跳的时间间隔
    13
    # tickTime以毫秒为单位
    14
    tickTime=2000
    15
    # 集群中的follower服务器(F)与leader服务器(L)之间的初始连接心跳数
    16
    initLimit=10
    17
    # 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数
    18
    syncLimit=5
    19
    dataDir=/data/zookeeper
    20
    # 客户端连接端口
    21
    clientPort=2181
    22
    # 三个接点配置,格式为: server.服务编号=服务地址、LF通信端口、选举端口
    23
    server.1=10.0.0.240:2888:3888
    24
    server.2=10.0.0.241:2888:3888
    25
    server.3=10.0.0.242:2888:3888
     
     

    21.3每台机器myid不一样(三台都要配置)

     
     
     
     
     
     
    1
    echo "1" > /data/zookeeper/myid
    2
    cat /data/zookeeper/myid
     
     

    21.4启动zookeeper所有节点都启动

     
     
     
     
     
     
    1
    /opt/zookeeper/bin/zkServer.sh start
     
     

    21.5检查每个节点运行状态

     
     
     
     
     
     
    1
    /opt/zookeeper/bin/zkServer.sh status
     
     

    21.6测试zookeeper

     
     
     
     
     
     
    1
    #在一个节点上执行,创建一个频道
    2
    /opt/zookeeper/bin/zkCli.sh -server 10.0.0.240:2181
    3
    create /test "hello"
    4
    5
    #在其他节点上看能否接收到
    6
    /opt/zookeeper/bin/zkCli.sh -server 10.0.0.240:2181
    7
    get /test
     
     

    21.7安装部署kafka,每个节点都配置,注意ip不同

     
     
     
     
     
     
    1
    #es01操作
    2
    #上传安装包
    3
    cd /opt/es_software/
    4
    #解压并创建软连接
    5
    tar zxf kafka_2.11-1.0.0.tgz -C /opt/
    6
    ln -s /opt/kafka_2.11-1.0.0/ /opt/kafka
    7
    #创建一个日志目录
    8
    mkdir /opt/kafka/logs
    9
    #修改配置文件
    10
    vim /opt/kafka/config/server.properties
    11
    # broker的id,值为整数,且必须唯一,在一个集群中不能重复
    12
    broker.id=1
    13
    listeners=PLAINTEXT://10.0.0.240:9092
    14
    # 处理网络请求的线程数量,默认为3个
    15
    num.network.threads=3
    16
    # 执行磁盘IO操作的线程数量,默认为8个
    17
    num.io.threads=8
    18
    # socket服务发送数据的缓冲区大小,默认100KB
    19
    socket.send.buffer.bytes=102400
    20
    # socket服务接受数据的缓冲区大小,默认100KB
    21
    socket.receive.buffer.bytes=102400
    22
    # socket服务所能接受的一个请求的最大大小,默认为100M
    23
    socket.request.max.bytes=104857600
    24
    # kafka存储消息数据的目录
    25
    log.dirs=/opt/kafka/logs
    26
    # 每个topic默认的partition数量
    27
    num.partitions=1
    28
    # 在启动时恢复数据和关闭时刷新数据时每个数据目录的线程数量
    29
    num.recovery.threads.per.data.dir=1
    30
    #topic的offset的备份份数。建议设置更高的数字保证更高的可用性
    31
    offsets.topic.replication.factor=1
    32
    transaction.state.log.replication.factor=1
    33
    transaction.state.log.min.isr=1
    34
    #每个日志文件删除之前保存的时间
    35
    log.retention.hours=24
    36
    #这个属性就是每个文件的最大尺寸;当尺寸达到这个数值时,就会创建新文件
    37
    log.segment.bytes=1073741824
    38
    #检查日志分段文件的间隔时间,以确定是否文件属性是否到达删除要求。
    39
    log.retention.check.interval.ms=300000
    40
    # Zookeeper连接信息,如果是zookeeper集群,则以逗号隔开
    41
    zookeeper.connect=10.0.0.240:2181,10.0.0.241:2181,10.0.0.242:2181
    42
    # 连接zookeeper的超时时间,6s
    43
    zookeeper.connection.timeout.ms=6000
    44
    group.initial.rebalance.delay.ms=0
     
     

    21.8前台启动测试能否正常启动

     
     
     
     
     
     
    1
    /opt/kafka/bin/kafka-server-start.sh  /opt/kafka/config/server.properties
     
     

    21.9测试创建topic

     
     
     
     
     
     
    1
    /opt/kafka/bin/kafka-topics.sh --create  --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --partitions 3 --replication-factor 3 --topic kafkatest
    2
      
     
     

    21.10测试获取toppid

     
     
     
     
     
     
    1
    /opt/kafka/bin/kafka-topics.sh --describe --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --topic kafkatest
     
     

    21.11测试删除topic

     
     
     
     
     
     
    1
    /opt/kafka/bin/kafka-topics.sh --delete --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --topic kafkatest
     
     

    21.12kafka测试命令发送消息

     
     
     
     
     
     
    1
    #创建命令
    2
    /opt/kafka/bin/kafka-topics.sh --create --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --partitions 3 --replication-factor 3 --topic  messagetest
    3
    #测试发送消息
    4
    /opt/kafka/bin/kafka-console-producer.sh --broker-list  10.0.0.241:9092,10.0.0.242:9092,10.0.0.240:9092 --topic  messagetest
    5
    #其他节点测试接收
    6
    /opt/kafka/bin/kafka-console-consumer.sh --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 --topic messagetest --from-beginning
    7
    #测试获取所有的频道
    8
    /opt/kafka/bin/kafka-topics.sh  --list --zookeeper 10.0.0.241:2181,10.0.0.242:2181,10.0.0.240:2181 
     
     

    21.13测试成功之后,可以放在后台启动

     
     
     
     
     
     
    1
    /opt/kafka/bin/kafka-server-start.sh  -daemon /opt/kafka/config/server.properties
     
     

    21.14修改filebeat配置文件并重启filebeat服务

     
     
     
     
     
     
    1
    vim /etc/filebeat/filebeat.yml
    2
    filebeat.inputs:
    3
    - type: log
    4
      enabled: true
    5
      paths:
    6
        - /var/log/nginx/access.log
    7
      json.keys_under_root: true
    8
      json.overwrite_keys: true
    9
      tags: ["access"]
    10
    11
    - type: log
    12
      enabled: true
    13
      paths:
    14
        - /var/log/nginx/error.log
    15
      tags: ["error"]
    16
    17
    output.kafka:
    18
      hosts: ["10.0.0.240:9092", "10.0.0.241:9092", "10.0.0.242:9092"]
    19
      topic: 'filebeat'
    20
    21
    setup.template.name: "nginx"
    22
    setup.template.pattern: "nginx_*"
    23
    setup.template.enabled: false
    24
    setup.template.overwrite: true
    25
    #重启filebeat服务
    26
    systemctl restart filebeat
     
     

    21.15修改logstash配置文件

     
     
     
     
     
     
    1
    vim /etc/logstash/conf.d/kafka.conf
    2
    input {
    3
      kafka{
    4
        bootstrap_servers=>"10.0.0.240:9092"
    5
        topics=>["filebeat"]
    6
        group_id=>"logstash"
    7
        codec => "json"
    8
      }
    9
    }
    10
    11
    filter {
    12
      mutate {
    13
        convert => ["upstream_time", "float"]
    14
        convert => ["request_time", "float"]
    15
      }
    16
    }
    17
    18
    output {
    19
       stdout {}
    20
       if "access" in [tags] {
    21
          elasticsearch {
    22
            hosts => "http://10.0.0.240:9200"
    23
            manage_template => false
    24
            index => "nginx_access-%{+yyyy.MM}"
    25
          }
    26
        }
    27
        if "error" in [tags] {
    28
          elasticsearch {
    29
            hosts => "http://10.0.0.240:9200"
    30
            manage_template => false
    31
            index => "nginx_error-%{+yyyy.MM}"
    32
          }
    33
        }
    34
    }
    35
    #重新启动logstash服务
     
     

    21.16、可以访问nginx可产生一些错误日志和正常日志

    21.17、在head界面检查日志是否收集成功

    1584622992562

    21.18、在kibana界面添加index-patterns

    1584622999954

    1584623006665

    22、如何使用kibana工具

    22.1、创建一个表格图形

    1584623011526

    1584623021588

    1584623026380

    1584623030381

    1584623034805

    22.2、创建一个饼状图

    1584623041397

    1584623046461

    1584623056593

    1584623063212

    22.3、创建一个markdown文档

    1584623068501

    1584623076177

    22.4、创建一个dashboard

    1584623081136

    1584623084853

    1584623088393

    1584623102265

  • 相关阅读:
    算法初步——贪心B1020/A1070月饼
    class.forname()用法 转
    java的几种连接池
    java中调用xml的方法:DocumentBuilderFactory
    java中HashMap详解
    使用J2SE API读取Properties文件的六种方法
    java读取properties配置文件信息
    学习(二)
    json入门(二)
    什么是序列化?在什么情况下将类序列化?
  • 原文地址:https://www.cnblogs.com/wx1899325/p/12923911.html
Copyright © 2011-2022 走看看