ELK
概述
日志处理步骤
第一步:将日志进行集中化管理(beats)
第二步:将日志格式化(Logstash),然后将格式化后的数据输出到Elasticsearch
第三步:对格式化后的数据进行索引和存储(Elasticsearch)
第四步:前端数据的展示(Kibana)
Elasticsearch
概述
Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene™ 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 Lucene 那么简单,它不仅包括了全文搜索功能,还可以进行以下工作:
分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。
实时分析的分布式搜索引擎。
可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。
Elasticsearch的特性
接近实时的搜索
集群
节点
索引
索引(库)→类型(表)→文档(记录)
分片和副本
LogStash概述
Logstash 是一个功能强大的工具,可与各种部署集成。 它提供了大量插件,可帮助你解析,丰富,转换和缓冲来自各种来源的数据。 如果你的数据需要 Beats 中没有的其他处理,则需要将 Logstash 添加到部署中。
Kibana概述
部署elk
配置ELK日志分析集群
使用Logstash收集日志
使用Kibana查看分析日志
案例环境
配置和安装ELK日志分析系统,安装集群方式,2个elasticsearch节点,并监控apache服务器日志
环境
node1 CentOS7 192.168.21.20 Elasticsearch/Kibana
node2 CentOS7 192.168.21.30 Elasticsearch
apache CentOS7 192.168.21.100 httpd / Logstash
客户机(宿主机) Windows10 192.168.21.188
关防火墙和系统安全机制
环境
node1(192.168.21.20)
node2(192.168.21.30)
echo '192.168.21.20 node1' >> /etc/hosts
echo '192.168.21.30 node2' >> /etc/hosts
java -version #如果没有安装,yum -y install java
部署elasticsearch软件
安装包放在/opt目录下
cd /opt
rpm -ivh elasticsearch-5.5.0.rpm
加载系统服务
systemctl daemon-reload
systemctl enable elasticsearch.service
更改服务配置文件
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
vim /etc/elasticsearch/elasticsearch.yml
#17行;
cluster.name: my-elk-cluster
#23行;
node.name: node1
#33行;
path.data: /data/elk_data
#37行;
path.logs: /var/log/elasticsearch
#43行;
bootstrap.memory_lock: false
#55行;
network.host: 0.0.0.0
#59行;
http.port: 9200
#68行;
discovery.zen.ping.unicast.hosts: ["node1", "node2"]
检验配置
grep -v "^#" /etc/elasticsearch/elasticsearch.yml
创建数据存放路径并授权
mkdir -p /data/elk_data
chown elasticsearch:elasticsearch /data/elk_data/
启动elasticsearch是否成功开启
systemctl start elasticsearch
netstat -antp |grep 9200
安装elasticsearch-head插件
解压
编译安装‘
安装phantomjs(前端框架)
包放opt
node1(192.168.21.20)
node2(192.168.21.30)
cd /opt
tar jxf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/src/
cd /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin
cp phantomjs /usr/local/bin
安装elasticsearch-head(数据可视化工具)
包放opt
node1(192.168.21.20)
node2(192.168.21.30)
cd /opt
tar zxf elasticsearch-head.tar.gz -C /usr/local/src/
cd /usr/local/src/elasticsearch-head/
npm install
修改主配置文件
node1(192.168.21.20)
node2(192.168.21.30)
vim /etc/elasticsearch/elasticsearch.yml
......
#-------末尾;添加以下内容--------
http.cors.enabled: true
http.cors.allow-origin: "*"
#-----------参数解释-----------------------------
http.cors.enabled: true #开启跨域访问支持,默认为 false
http.cors.allow-origin: "*" #指定跨域访问允许的域名地址为所有
systemctl restart elasticsearch.service
启动elasticsearch-head
node1(192.168.21.20)
node2(192.168.21.30)
必须在解压后的 elasticsearch-head 目录下启动服务,进程会读取该目录下的 gruntfile.js 文件,否则可能启动失败。
cd /usr/local/src/elasticsearch-head/
npm run start &
> elasticsearch-head@0.0.0 start /usr/local/src/elasticsearch-head
> grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
elasticsearch-head 监听的端口是 9100
netstat -natp |grep 9100
使用elasticsearch-head插件查看集群状态
创建索引
node1(192.168.21.20)
创建索引为index-demo,类型为test
curl -XPUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"srs","mesg":"hello world"}'
回到宿主机192.168.163.1
打开浏览器输入地址,查看索引信息
http://192.168.163.11:9100
安装logstash
环境
apache(192.168.21.70)
yum -y install httpd
systemctl start httpd
java -version
安装logstash
包放在opt下
apache(192.168.21.70)
cd /opt
rpm -ivh logstash-5.5.1.rpm
systemctl start logstash.service
systemctl enable logstash.service
#建立logstash软连接
ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
测试logstash
在宿主机192.168.221.188上访问
Apache主机上做对接配置
apache(192.168.163.13)
Logstash配置文件主要由三部分组成:input、output、filter(根据需要)
chmod o+r /var/log/messages
ll /var/log/messages
vim /etc/logstash/conf.d/system.conf
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.21.20:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
systemctl restart logstash.service
访问
安装kibana
访问
对接Apache主机的Apache 日志文件(访问日志、错误日志)
apache(192.168.21.70)
cd /etc/logstash/conf.d/
vim apache_log.conf
input {
file{
path => "/etc/httpd/logs/access_log"
type => "access"
start_position => "beginning"
}
file{
path => "/etc/httpd/logs/error_log"
type => "error"
start_position => "beginning"
}
}
output {
if [type] == "access" {
elasticsearch {
hosts => ["192.168.21.20:9200"]
index => "apache_access-%{+YYYY.MM.dd}"
}
}
if [type] == "error" {
elasticsearch {
hosts => ["192.168.21.20:9200"]
index => "apache_error-%{+YYYY.MM.dd}"
}
}
}
/usr/share/logstash/bin/logstash -f apache_log.conf
访问