? 日志主要包括系统日志和应用程序日志,运维和开发人员能够经过日志了解服务器中软硬件的信息,检查应用程序或系统的故障,了解故障出现的缘由,以便解决问题。分析日志能够更清楚的了解服务器的状态和系统安全情况,从而能够维护服务器稳定运行。java
? 可是日志一般都是存储在各自的服务器中。若是管理数十台服务器, 查阅日志须要依次登录不一样的服务器,查看过程就会很繁琐从而致使工做效率低下。虽然可使用 rsyslog 服务将日志汇总。可是统计一些日志中的数据或者检索也是很麻烦的,通常使用grep、awk、wc、sort等Linux命令来统计和检索。若是对数量巨大的日志进行统计检索,人工的效率仍是十分低下。node
? 经过咱们对日志进行收集、汇总到一块儿,完整的日志数据具备很是重要的做用:linux
? ELK实时日志收集分析系统能够完美的解决以上问题。ELK做为一款开源软件能够无偿使用,也有强大的团队和社区对它实时更新。nginx
? ELK主要由ElasticSearch、Logstash和Kibana三个开源工具组成,还有其余专门因为收集数据的轻量型数据采集器Beats。web
Elasticsearch?:分布式搜索引擎。具备高可伸缩、高可靠、易管理等特色。能够用于全文检索、结构化检索和分析,并能将这三者结合起来。Elasticsearch 是用Java 基于 Lucene 开发,如今使用最广的开源搜索引擎之一,Wikipedia 、StackOverflow、Github 等都基于它来构建本身的搜索引擎。数据库
在elasticsearch中,全部节点的数据是均等的。bootstrap
Logstash?:数据收集处理引擎。支持动态的从各类数据源搜集数据,并对数据进行过滤、分析、丰富、统一格式等操做,而后存储以供后续使用。vim
Kibana?:可视化化平台。它可以搜索、展现存储在 Elasticsearch 中索引数据。使用它能够很方便的用图表、表格、地图展现和分析数据。安全
Filebeat:轻量级数据收集引擎。相对于Logstash所占用的系统资源来讲,Filebeat 所占用的系统资源几乎是微乎及微。它是基于原先 Logstash-fowarder 的源码改造出来。换句话说:Filebeat就是新版的 Logstash-fowarder,也会是 ELK Stack 在 Agent 的第一选择。ruby
Elasticsearch、Logstash、Kibana、Filebeat安装的版本号必须所有一致,否则会出现kibana没法显示web页面。
ELK工做演示图:
#自行下载jdk rpm -ivh jdk-8u144-linux-x64.rpm #或者 yum install java-1.8.0-openjdk* vim /etc/profile.d/java.sh export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64 export PATH=$JAVA_HOME/jre/bin:$PATH source /etc/profile.d/java.sh
vim /etc/yum.repo.d/ELK.repo [ELK] name=ELK-Elasticstack baseurl=https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-6.x/ gpgcheck=0 enabled=1 #关闭selinux setenforce 0 sed -i.bak 's@^SELINUX=\(.*\)@SELINUX=disabled@p' /etc/selinux/config #关闭防火墙 #Centos7 systemctl disable firewalld systemctl stop firewalld #CentOS6 service iptables stop service iptables disable
yum install elasticsearsh # 修改系统配置文件属性 # vim /etc/security/limits.conf elasticsearch soft memlock unlimited elasticsearch hard memlock unlimited elasticsearch soft nofile 65536 elasticsearch hard nofile 131072
# vim /etc/elasticsearch/elasticsearch.yml #集群名称 cluster.name: els #节点名称 node.name: els-1 #数据存放路径 path.data: /data/els_data #日志存放路径 path.logs: /data/log/els #锁定jvm.options指定的内存,不交换swap内存 bootstrap.memory_lock: true #绑定IP地址 network.host: 172.16.1.49 #端口号 http.port: 9200 #配置集群配置,填写集群节点,会自动发现节点 # discovery.zen.ping.unicast.hosts: ["host1", "host2"] # 集群配置 只须要修改节点名,和绑定ip地址便可 # vim /etc/elasticsearch/jvm.options -Xms1g #指定占用内存大小,两个数字要一致 都是1g -Xmx1g
#建立elasticsearch数据库目录,而且修改属主为elasticsearch· mkdir /data/els_data mkdir /data/log/els chown -R elasticsearch.elasticsearch /data/els_data chown -R elasticsearch.elasticsearch /data/log/els
systemctl start elasticsearch # 启动后自动关闭 #报错 [1] bootstrap checks failed [1]: memory locking requested for elasticsearch process but memory is not locked #将配置文件中的 bootstrap.memory_lock: true 注释掉,不使用;便可启动成功
报错
启动成功后
访问172.16.1.49:9200
Elasticsearch API
Kibana是node.js 编写的,不须要java环境。直接安装便可
yum install kibana # 版本号须要和Elasticsearch 相同
vim /etc/kibana/kibana.yml server.port: 5601 server.host: "172.16.1.50" elasticsearch.url: "http://172.16.1.49:9200" kibana.index: ".kibana" logging.dest: /data/log/kibana/kibana.log # 配置kibana日志输出到哪里 # 建立日志目录文件 mkdir -p /data/log/kibana/ touch /data/log/kibana/kibana.log chmod o+rw /data/log/kibana/kibana.log
访问172.16.1.50:5601
出现 Kibana server is not ready yet
说明kibana与Elasticsearch版本号不一致
# 查看版本号 rpm -qa elasticsearch kibana
若是版本号如出一辙,那就多刷新几回页面吧。。。。
启动成功后,访问页面能够看到:
配置与Elasticsearch相同的Java环境,版本为8以上的Java环境。
yum install logstash-"Version"
http.host: "172.16.1.229" http.port: 9600-9700
log_format main '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"request":"$request",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"status":"$status"}'; access_log /var/log/nginx/access_test.log main;
input { file { type =>"nginx-log" path => ["/var/log/nginx/access.log"] start_position => "beginning" sincedb_path => "/dev/null" } } output { elasticsearch { hosts => ["172.16.1.49:9200"] index => "nginx-log-%{+YYYY.MM}" } }
cd /usr/share/logstash/bin ./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf --config.test_and_exit OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N Sending Logstash logs to /var/log/logstash which is now configured via log4j2.properties [2019-02-20T17:34:29,949][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified Configuration OK # 配置文件OK 可使用 [2019-02-20T17:34:39,048][INFO ][logstash.runner ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
WARN报错不影响运行
直接使用命令
./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf
就能够直接运行logstash
? Logstash的数据都是从Beats中获取,Logstash已经不须要本身去数据源中获取数据了。
? 之前咱们使用的日志采集工具是logstash,可是logstash占用的资源比较大,没有beats轻量,因此官方也推荐使用beats来做为日志采集工具。并且beats可扩展,支持自定义构建。
yum install filebeat-6.6.0
vim /etc/filebeat/filebeat.yml - type: log paths: - /Log_File #/var/log/messages #output.elasticsearch: #注释掉输出到elasticsearch的配置 # hosts: ["localhost:9200"] output.console: #添加输出到当前终端的配置 enable: true
/usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml #运行filebeat,能够看见日志输出在当前终端
- type: log paths: - /Log_File #/var/log/messages output.elasticsearch: #注释掉输出到elasticsearch的配置 hosts: ["172.16.1.49:9200"]
启动 filebeat
systemctl start filebeat
运行
curl ‘172.16.1.49:9200/_cat/indices?v‘
,查看日志索引
便可查看到日志
#------------------- Logstash output ---------------------- output.logstash: #将输出到elasticsearch的配置注释 # The Logstash hosts hosts: ["172.16.1.229:5044"] # Optional SSL. By default is off. # List of root certificates for HTTPS server verifications #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"] # Certificate for SSL client authentication #ssl.certificate: "/etc/pki/client/cert.pem" # Client Certificate Key #ssl.key: "/etc/pki/client/cert.key"
vim /etc/logstash/conf.d/test.conf input { beats { port =>5044 } } #filter{} #过滤器,自定义过滤日志中的一些数据 output { stdout { codec => rubydebug } elasticsearch { hosts => "172.16.1.49:9200" index => "test-log-%{+YYYY.MM.dd}" } }
Elasticsearch上已经有了日志索引,在kibana上也能够查看到
yellow 表示没有副本节点可用,由于测试时没有搭建两台Elasticsearch
能够用filebeat监控个自定义文件,手动插入数据。