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

7次阅读

共计 3718 个字符,预计需要花费 10 分钟才能阅读完成。

window 环境下搭建简单 ELK 日志收集
前言本文主要介绍如何在 window 环境下部署 elk 日志收集,网络上大部分是 linux 的,刚好这边服务需要用到 window 环境,配置方式有点不同,大体是一样的。
部署环境 window server 2008 R2 依赖第三方组件 JDK8+elasticsearch-5.6.14logstash-5.6.14kibana-5.6.14-windows-x86redis-3.2
下载地址官网: https://www.elastic.co/
简单的流程示意图 redis 跟 elasticsearch 都支持集群部署,本文只简单描述如何单独部署
部署步骤
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
配置 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

结尾
最后说明下关于清除日志,ElasticSearch 5.x 版本中删除过期 ttl 属性,所以我们需要定时清理 es 的索引,在服务中建一个 bat 定时执行,以下为删除 30 天前的全部索引。
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…

正文完
 0