搭建分布式日志管理ELK
所需环境:1.两台CentOS7系统虚拟机(内存不够先用两台搭建,先做两机间的时间同步,这里就略过了)2.elasticsearch、kibana、logstash、filebeat等软件的rpm包 3.关闭SELinux、4.搭建好网络环境(两台主机要ping通)
部署基本交代:192.168.100.196 当做ELK服务器 会在这台上安装els、kibana、logstash软件
192.168.100.197 当做客户端 安装filebeat
filebeat会给服务器logstash传日志
logstash负责收集、过滤、存储日志
elasticsearch负责搜索
kibana 提供web界面
一、安装elasticsearch
rpm -ivh elasticsearch-5.3.2.rpm (注:此版本依赖Java1.8以上的版本)
vim /etc/elasticsearch/elasticsearch.yml
更改:network.host:0.0.0.0 (注:0.0.0.0表示所有主机)
systemctl start elasticsearch
netstat -pant | grep java (验证服务启动没,会有Java服务,监听端口是9200)
二、安装kibana
rpm -ivh kibana-5.3.2-x86_64.rpm
vim /etc/kibana/kibana.yml
server.host: "192.168.100.196"
systemctl start kibana
systemctl status kibana (查看kibana服务状态)
(这时可以在浏览器看下kibana页面,在浏览器输入:192.168.100.196:5601)
三、安装logstash
(logstash因为要与filebeat通信,所以最好安装配置ssl加密证书来确保传输安全)
vim /etc/pki/tls/openssl.cnf
(找到[v3 ca] 注意书写空客)
[ v3 ca ]
subjectAltName = IP: 192.168.100.196
cd /etc/pki/tls/
openssl req -config /etc/pki/tls/openssl.cnf -x509 -days 3650 -batch -nodes -newkey rsa:2048 -keyout logstash-forwarder.key -out logstash-forwarder.crt
rpm -ivh logstash-5.3.2.rpm
vim /etc/logstash/conf.d/01-beats-input.conf (01-baats-input.conf为接收日志的配置文件,需要自己编写,名字可自定义)
input {
beats {
port => 5044
ssl => true
ssl_certificate => "/etc/pki/tls/logstash-forwarder.crt"
ssl_key => "/etc/pki/tls/logstash-forwarder.key"
}
}
vim /etc/ logstash/conf.d/01-syslog-filter.conf (配置filter过滤文件,收集的是syslog系统日志)
filter {
if [type] == "syslog" {
grok {
match => {"message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
add_field => ["received_at","%{@timestamp}"]
add_field => ["received_from","%{host}"]
}
syslog_pri {}
date {
match => ["syslog_timestamp","MMM d HH:mm:ss","MMM dd HH:mm:ss"]
}
}
}
vim /etc/logstash/conf.d/01-elasticsearch-output.conf
output {
elasticsearch {
hosts => ["localhost:9200"]
sniffing => true
manage_template => false
index => "%{[@metadata][beat]}-%{+YYY.MM.dd}"
document_type => "%{[@metadata][type]}"
}
}
systemctl start logstash
scp /etc/pki/tls/logstash-forwarder.crt 192.168.100.197:/etc/pki/tls/ (把证书传给客户端)
四、配置客户端 filebeat
rpm -ivh filebeat-5.3.2-x86_64.rpm
vim /etc/filebeat/filebeat.yml
更改部分有三处
(1)
output.elasticsearch;
# Array of hosts to connect to.
hosts: ["192.168.100.196:9200"]
(2)
output.logstash:
# The Logstash hosts
hosts: ["192.168.100.196:5044"]
(3)
ssl.certificate_authorities: ["/etc/pki/tls/logstash-forwarder.crt"]
systemctl start filebeat
curl 192.168.100.196:9200 (验证)
{
"name" : "8kIDYaG",
"cluster_name" : "elasticsearch"
……
}
以上是验证成功的效果
然后就可以登录浏览器配置啦 (192.168.100.196:5601)