本文共 6711 字,大约阅读时间需要 22 分钟。
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。新增了一个FileBeat,它是一个轻量级的日志收集处理工具(Agent),Filebeat占用资源少,适合于在各个服务器上搜集日志后传输给Logstash,官方也推荐此工具。
Elasticsearch 是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。
Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。
Filebeat 隶属于Beats。目前Beats包含四种工具:
官方文档:
Filebeat:Logstash:
Kibana:
Elasticsearch:
elasticsearch中文社区:
一般我们需要进行日志分析场景:直接在日志文件中 grep、awk 就可以获得自己想要的信息。但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档、文本搜索太慢怎么办、如何多维度查询。需要集中化的日志管理,所有服务器上的日志收集汇总。常见解决思路是建立集中式日志收集系统,将所有节点上的日志统一收集,管理,访问。
一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以提高定位问题的效率。
一个完整的集中式日志系统,需要包含以下几个主要特点:
ELK提供了一整套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用。目前主流的一种日志系统。
主机名 | 操作系统 | IP地址 | 服务名 |
---|---|---|---|
es | centos7.4 | 192.168.96.85 | elasticsearch 6.4.0、kibana 6.4.0、rsyslog |
nginx | centos7.4 | 192.168.96.60 | elasticsearch 6.4.0、logstash-6.4.0 |
httpd | centos7.4 | 192.168.96.86 | elasticsearch 6.4.0、filebeat-6.4 |
客户机 | windows 10 | 192.168.96.2 | 网页浏览器 |
以上所有服务器均关闭防火墙及SElinux功能
setenforece 0systemctl stop firewalld
这里分别使用了3种收集日志的方法,官网建议选用filebeat,因为轻量、高效
vim /etc/hosts
192.168.96.85 es192.168.96.86 httpd192.168.96.60 nginx
# 导入keyrpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch# 创建es仓库源vim /etc/yum.repos.d/elasticsearch.repo[elasticsearch-6.x]name=Elasticsearch repository for 6.x packagesbaseurl=https://artifacts.elastic.co/packages/6.x/yumgpgcheck=1gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearchenabled=1autorefresh=1type=rpm-md# 安装es软件包yum -y install elasticsearch
vim /etc/elasticsearch/elasticsearch.yml
17 cluster.name: es-server23 node.name: master24 node.master: true25 node.data: true58 network.host: 0.0.0.062 http.port: 920071 discovery.zen.ping.unicast.hosts: ["192.168.96.85", "192.168.96.86","192.168.96.60"]
scp /etc/elasticsearch/elasticsearch.yml httpd:/etc/elasticsearch/scp /etc/elasticsearch/elasticsearch.yml nginx:/etc/elasticsearch/
# httpd服务器:vim /etc/elasticsearch/elasticsearch.ymlnode.name: httpdnode.master: false# nginx服务器:vim /etc/elasticsearch/elasticsearch.ymlnode.name: nginxnode.master: false
# es服务器:(先启动master,在启动其他es)systemctl enable elasticsearch.servicesystemctl start elasticsearch.service# nginx服务器:systemctl enable elasticsearch.servicesystemctl start elasticsearch.service# httpd服务器:systemctl enable elasticsearch.servicesystemctl start elasticsearch.service
至此,es集群已经部署完成了
yum -y install kibana
vim /etc/kibana/kibana.yml
2 server.port: 56017 server.host: "192.168.96.85"28 elasticsearch.url: "http://192.168.96.85:9200"96 logging.dest: /var/log/kibana.log
touch /var/log/kibana.logchmod 777 /var/log/kibana.log
systemctl enable kibanasystemctl start kibana
[root@es ~]# netstat -tunlp | grep 5601tcp 0 0 192.168.96.85:5601 0.0.0.0:* LISTEN 2597/node
yum install logstash -y
vim /etc/rsyslog.conf
第91行*.* @@192.168.96.85:10514
systemctl restart rsyslog
vim /etc/logstash/conf.d/syslog.conf
input { syslog { type => "system-syslog" port => 10514 }}output { elasticsearch { hosts => ["192.168.96.85:9200"] //es服务器ip地址 index => "system-syslog-%{+YYYY.MM}" //索引 }}
./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.4.0-x86_64.rpmrpm -ivh filebeat-6.4.0-x86_64.rpm
vim /etc/filebeat/filebeat.yml
#注释掉下一行#enabled: false paths: - /var/log/messages //指定日志路径output.elasticsearch: # Array of hosts to connect to. hosts: ["192.168.96.85:9200"] //指向es服务器
systemctl enable filebeatsystemctl start filebeat
ps aux | grep filebeat
curl '192.168.96.85:9200/_cat/indices?v'
vim /etc/logstash/conf.d/nginx.conf
input { file { path => "/var/log/logstash/elk_access.log" start_position => "beginning" type => "nginx" }}filter { grok { match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"} } geoip { source => "clientip" }}output { stdout { codec => rubydebug } elasticsearch { hosts => ["192.168.96.85:9200"] //es服务器ip index => "nginx-test-%{+YYYY.MM.dd}" }}
/usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf --config.test_and_exit
vim /etc/nginx/conf.d/elk.conf
server { listen 80; server_name www.test.com; location / { proxy_pass http://192.168.96.85:5601; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log /var/log/logstash/elk_access.log main2;}
log_format main2 '$http_host $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$upstream_addr" $request_time';
systemctl enable logstashsystemctl start logstash
重启nginx服务
systemctl restart nginx
开启logstash收集nginx日志
/usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/nginx.conf
转载于:https://blog.51cto.com/10316297/2171415