window环境下搭建简单ELK日志收集

window环境下搭建简单ELK日志收集

前言
本文主要介绍如何在window环境下部署elk日志收集,网络上大部分是linux的,刚好这边服务需要用到window 环境,配置方式有点不同,大体是一样的。

部署环境
window server 2008 R2
依赖第三方组件
JDK8+
elasticsearch-5.6.14
logstash-5.6.14
kibana-5.6.14-windows-x86
redis-3.2

下载地址官网: https://www.elastic.co/

简单的流程示意图
redis跟elasticsearch 都支持集群部署,本文只简单描述如何单独部署
window环境下搭建简单ELK日志收集

部署步骤

elk三件套版本最好一致,elasticsearch小版本可以高,但是不可以低。
关于elasticsearch 的搭建和配置可以参考之前的一篇文章
DUBBO监控环境搭建

  • 启动redis

redis默认只允许本地访问,如果要redis可以远程需要修改redis.windows.conf
bind 127.0.0.1 注释掉,在redis3.2版本以上增加了一个保护模式,所以还需要修改protected-mode no

#bind 127.0.0.1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no
  • 配置logstash

logstash的作用是将redis中的日志信息收集并保存到elasticsearch中,redis只是作为缓存保存数据,logstash取完之后会删除redis中的数据信息。

1.指定启动JDK

指定JDK信息,如果有多个JDK需要指定,如果系统默认1.8可以跳过
在 logstash-5.6.14bin的setup.bat 中加入这句
set JAVA_HOME=C:Program FilesJavajdk1.8.0_181

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_181
rem ### 1: determine logstash home

rem  to do this, we strip from the path until we
rem find bin, and then strip bin (there is an assumption here that there is no
rem nested directory under bin also named bin)

2.配置输入输出源

在 logstash-5.6.14bin中新建 logback.config(名字任意) 配置文件,定义输入输出
input接受数据的输入,这边指定到redis;
output输出文件到指定的地方, elasticsearch;这边的index索引会在kibana那边配置用到
这个输入和输出的配置支持的类型比较多,有兴趣专研的可以参考官网配置
logstash输入配置
logstash输出配置
另外logstash还支持filter过滤器设置

input {

    redis {
        data_type => "list"
        key => "logstash"
        host => "127.0.0.1"
        port => 6379
        threads => 5
        codec => "json"
    }
}
filter {

}
output {

    elasticsearch {
        hosts => ["127.0.0.1:9200"]
        index => "logstash-%{type}-%{+YYYY.MM.dd}"
        document_type => "%{type}"
        workers => 1
        flush_size => 20
        idle_flush_time => 1
        template_overwrite => true
    }
    stdout{}
}

启动logstash命令 logstash.bat -f logback.config

  • 配置kibana

找到kibana-5.6.14-windows-x86config 中kibana.yml,1.配置host 0.0.0.0,运行外网访问,默认只能本地访问 2.配置es的地址。

server.host: "0.0.0.0"

# Enables you to specify a path to mount Kibana at if you are running behind a proxy. This only affects
# the URLs generated by Kibana, your proxy is expected to remove the basePath value before forwarding requests
# to Kibana. This setting cannot end in a slash.
#server.basePath: ""

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# The URL of the Elasticsearch instance to use for all your queries.
elasticsearch.url: "http://localhost:9200"

启动Kibana.bat

跟logback的结合

  • 添加maven支持,引入logback-redis-appender.jar包
<dependency>
            <groupId>com.cwbase</groupId>
            <artifactId>logback-redis-appender</artifactId>
            <version>1.1.3</version>
            <exclusions>
                <exclusion>
                    <groupId>redis.clients</groupId>
                    <artifactId>jedis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

配置logback.xml,这里的key需要跟上面配置的logback.config配置文件中配置的key相匹配

<appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender">
        <source>demo</source>
        <type>dubbo</type>
        <host>127.0.0.1</host>
        <key>logstash</key>
        <tags>dev</tags>
        <mdc>true</mdc>
        <location>true</location>
        <callerStackIndex>0</callerStackIndex>
    </appender>
    
        <!-- 语句监控 end -->
    <root level="INFO">
        <appender-ref ref="stdout" />
        <appender-ref ref="file-stdout" />
        <appender-ref ref="file-error" />
        <appender-ref ref="LOGSTASH" />
    </root>

启动服务工程,logback会将日志发送到redis,打开kibana的页面,第一次打开会提示创建索引规则,就是logback.config中es配置的索引logstash-*,创建好之后就可以看到日志已经被采集到elasticsearch中,我们在页面就可以查看日志信息。
kibana默认端口http://127.0.0.1:5601

window环境下搭建简单ELK日志收集

  • 结尾

最后说明下关于清除日志,ElasticSearch 5.x版本中删除过期ttl属性,所以我们需要定时清理es的索引,在服务中建一个bat定时执行,以下为删除30天前的全部索引。
http://{Elasticsearch IP}:9200/_cat/indices?v 查看es的所有索引

set DATA=`date -d "-30 days" +%Y.%m.%d`
curl -XDELETE http://127.0.0.1:9200/*-${DATA}

以上

参考文档
http://www.cnblogs.com/ASPNET...
https://blog.csdn.net/xuezhan...

相关推荐