技术小计
写在前面,这篇博客的目的主要是总结我在公司实习的这两个月里面所接触的一些新技术栈,因为时间的关系今天就只能跟新我自己弄的ELK监控平台,以后肯定会慢慢把我这两个月学习的补充上去。
ELK 日志监控平台
缘起: 因为实习公司的主要业务就是爬虫,所以所有的服务都是以分布式的方式存在。所以公司就使用ELK搭建了一个日志监控平台。
首先ELK看似是存在三个服务的,就我简单理解。分别为:
- Elasticsearch 处理格式化日志
- Logstash 收集日志
- Kinaba 前台展示
那我现在就一一讲一下我搭建这个平台的时候碰到的坑。首先这三个服务的所有下载包都可以到Elk下载。
Elasticsearch
- 下载安装包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.zip
使用非root用户登陆
- 因为Elasticsearch的安全策略,禁止root用户启动,所以需要创建一个非root用户来运行Elasticsearch程序。
useradd simon
- 更换Elasticsearh的工作目录的所有者和组都为’simon’所有。
- 使用’simon’用户
su simon
- 因为Elasticsearch的安全策略,禁止root用户启动,所以需要创建一个非root用户来运行Elasticsearch程序。
修改配置文件
- 开启服务之前还会遇到一个坑,就是Elasticsearch默认的内存为2g而一般的服务器都不会得到那么大的内存,所以需要到
/config/log4j2.properties
修改内存为能顾接受的范围。
- 开启服务之前还会遇到一个坑,就是Elasticsearch默认的内存为2g而一般的服务器都不会得到那么大的内存,所以需要到
运行
好了到这里Elasticsearch的配置就差不多了,只需要让它默默的跑在后台给我们处理日志就可以了。
nohup ./bin/elasticsearch >& nohup.out &
验证
如何看自己的Elasticsearch是否真的运行在后台呢?
只需要访问Elasticsearch的服务测试一下就可以
curl localhost:9200
Logstash
- 下载安装包
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.5.2.tar.gz
tar -zxvf
- 创建输入输出模板
前面说过我的浅显理解Logstas就是用来对日志的收集,所以必定存在对日志输入和输出设定。我这里使用的log4j的日志输入,而且Logstash存在log4j的模板,所以我的日志设置为(需要自建文件):
# Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
input {
# For detail config for log4j as input,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html
log4j {
mode => "server"
host => "0.0.0.0"
port => 4500
}
}
filter {
#Only matched data are send to output.
}
output {
# For detail config for elasticsearch as output,
# See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
elasticsearch {
action => "index" #The operation on ES
hosts => "localhost:9200" #ElasticSearch host, can be array.
index => "applog" #The index to write data to.
}
}
其中各个参数的含义如下:
名称 | 类型 | 含义 |
---|---|---|
mode | server/client | 服务器/客户端 |
host | string | 服务地址 |
port | int | 服务端口 |
antion | index | |
hosts | string | 服务的地址 |
index | Stirng | parent 的名称 |
其中,加入你是把你的ELK放在服务器端的话,就需要把hosy设置成为允许访问的ip即 0.0.0.0
SpringBoot日志输送
由于SpringBoot的成熟,所以已经集成了Logstash的日志传输服务,只需要在application.properties中添加:
log4j.appender.logstash=org.apache.log4j.net.SocketAppender log4j.appender.logstash.RemoteHost=your_server_host log4j.appender.logstash.port=4500 log4j.appender.logstash.ReconnectionDelay=60000 log4j.appender.logstash.LocationInfo=true
当然你事先得导入log4j的maven依赖库
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency>
运行
当一切准备结束之后,我们就可以让Logstash也默默热运行在后台了
nohup ./bin/logstash -f log4j.conf *& nohup.out &
Kinaba
- 下载安装包
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.5.2-linux-x86_64.tar.gz
tar -zxvf
- 配置Kinaba允许外网访问
进入Kinaba的config/
目录下vi Kinaba.yml
其中将:
- server.port :port
- server.host :0.0.0.0
- elasticsearch.url: “http://localhost:9200”
设置完毕,看起来Kinaba的设置相比前面起来简单很多。
- 运行
啦啦啦,只需要简单的配置Kinaba的服务可以运行起来了。
nohup Kinaba >& nohup.out &
- 测试
我们可以打开浏览器输入:localhost:port
,等一会,我们就可以看到Kinaba的界面了。
然后在:
中我们在配置Logstash设置的index属性:applog
,然后我们就可以看到我们的日志文件了。
最后说一下,三个的服务启动时存在先后顺序的,处理日志的Elasticsearch必须第一个启动,原因是Logstash中设置了文件输出的位置,假设Elasticsearch的服务没有启动的话,就会存在找不到输出文件的错误。同理在Knaba中也设置了读取日志文件的位置。
好了,到这里一个简单的日志监控平台就搭建完毕了,因为我写的这篇博客完全都是出于个人的理解,所以其中个肯定存在很多问题。仅供参考。