https://www.unixhot.com/article/59html
对于日志来讲,最多见的需求就是收集、存储、查询、展现,开源社区正好有相对应的开源项目:logstash(收集)、elasticsearch(存储+搜索)、kibana(展现),咱们将这三个组合起来的技术称之为ELKStack,因此说ELKStack指的是Elasticsearch、Logstash、Kibana技术栈的结合。通用的架构图:java
最佳实践:yum安装的方式。
node
因yum源都是国外的,须要×××才能使用。本文不作详细安装步骤了,请参考上面的连接。
我是将要安装的软件都下载到本地,而后使用yum localinstall “package name‘的方式安装。python
elasticsearch 依赖 java环境,在这里咱们用yum安装便可。linux
[root@elk01-node2 ~]# yum -y install java
[root@elk01-node2 tools]# yum localinstall elasticsearch-2.4.4.rpm
Logstash也依赖java环境,这里咱们也使用yum安装java环境便可。nginx
[root@elk01-node2 tools]# yum localinstall logstash-2.3.4-1.noarch.rpm
Kibana 是为 Elasticsearch 设计的开源分析和可视化平台。你可使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互。你能够很容易实现高级的数据分析和可视化,以图表的形式展示出来。git
[root@elk01-node2 tools]# yum localinstall kibana-4.5.4-1.x86_64.rpm
学习软件的方法:安装 - 配置 - 启动 - 测试github
[root@elk01-node2 elk]# vim /etc/elasticsearch/elasticsearch.yml 修改内容 以下: 17 cluster.name: myes # 集群名,集群的时候须要用到。 23 node.name: linux-node1 # 节点名,不能重复 33 path.data: /data/es-data # 数据存放的位置 37 path.logs: /var/log/elasticsearch/ # 日志存放的位置 43 bootstrap.memory_lock: true # 此配置的意思是,锁住es占用的内存分区,防止被交换到swap分区,影响性能。 54 network.host: 10.0.0.204 # 监听的接口地址,默认监听的端口是9200 58 http.port: 9200 # 监听的端口
新建对应的目录。web
[root@elk01-node2 elk]# mkdir /data/es-data -p [root@elk01-node2 elk]# chown -R elasticsearch. /data/
若是不能正常启动,看日志。redis
[root@elk01-node2 elk]# /etc/init.d/elasticsearch start [root@elk01-node2 elk]# netstat -tnlpua|grep 9200 tcp6 0 0 10.0.0.204:9200 :::* LISTEN 2943/java
[root@elk01-node2 elk]# curl http://10.0.0.204:9200 { "name" : "linux-node1", "cluster_name" : "myes", "cluster_uuid" : "CRLwDyWsSX-6q4RC4wRcqA", "version" : { "number" : "2.4.4", "build_hash" : "fcbb46dfd45562a9cf00c604b30849a6dec6b017", "build_timestamp" : "2017-01-03T11:33:16Z", "build_snapshot" : false, "lucene_version" : "5.5.2" }, "tagline" : "You Know, for Search" } [root@elk01-node2 elk]# curl -i XGET 'http://10.0.0.204:9200/_count?' curl: (6) Could not resolve host: XGET; Name or service not known HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 59 {"count":1,"_shards":{"total":5,"successful":5,"failed":0}}
因安装插件须要×××。从github下载的插件能够用。
es下载的插件放在/usr/share/elasticsearch/plugins/目录下面。
安装方法
/usr/share/elasticsearch/bin/plugin install marvel-agent /usr/share/elasticsearch/bin/plugin install head
git下载的插件安装
[root@elk01-node2 plugins]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head ( this plugin is to github download.) [root@elk01-node2 plugins]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
测试安装的插件
{ "user": "wf", "mesg": "hehe" }
http://10.0.0.204:9200/_plugin/kopf/#!/cluster
(ps:图是补的)
elk01-node2.damaiche.org-204 配置
[root@elk01-node2 elk]# grep ^[a-z] /etc/elasticsearch/elasticsearch.yml cluster.name: myes node.name: linux-node1 path.data: /data/es-data path.logs: /var/log/elasticsearch/ bootstrap.memory_lock: true network.host: 10.0.0.204 http.port: 9200 discovery.zen.ping.unicast.hosts: ["10.0.0.203", "10.0.0.204"] # 如不能正常加入到集群中去。10.0.0.204 须要将组播改为单播。(204 知道203 是它的兄弟,203 不知道无所谓)
10.0.0.203配置
ps: es 安装方法相同
[root@web01-node1 ~]# grep ^[a-z] /etc/elasticsearch/elasticsearch.yml cluster.name: myes node.name: linux-node2 path.data: /data/es-data path.logs: /var/log/elasticsearch/ bootstrap.memory_lock: true network.host: 10.0.0.203 http.port: 9200
etc/init.d/elasticsearch restart
10.0.0.204的日志,以今天加入到集群里去了。
/var/log/elasticsearch/my-es.log
说明:
监测集群健康状态
例子:
https://www.elastic.co/guide/en/elasticsearch/guide/current/_cluster_health.html
[root@web01-node1 ~]# curl -i XGET http://10.0.0.204:9200/_cluster/health?pretty=True curl: (6) Could not resolve host: XGET; Name or service not known HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 458 { "cluster_name" : "myes", "status" : "green", "timed_out" : false, "number_of_nodes" : 2, "number_of_data_nodes" : 2, "active_primary_shards" : 5, "active_shards" : 10, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 }
https://www.elastic.co/guide/en/logstash/2.3/index.html
手动的输入内容,而且打印出来。
/opt/logstash/bin/logstash -e \ 'input { stdin {} } output { stdout{} }'
手动的输入内容,而且打印出来,格式更加的好看。
/opt/logstash/bin/logstash -e \ 'input { stdin {} } output { stdout{codec => rubydebug} }'
将输出的内容打印出来同时存入到es里。
input插件
官方参考连接 https://www.elastic.co/guide/en/logstash/current/input-plugins.html
logstash
官方参考连接 https://www.elastic.co/guide/en/logstash/current/index.htm
output插件
官方参考连接 https://www.elastic.co/guide/en/logstash/current/output-plugins.html
/opt/logstash/bin/logstash -e \ 'input { stdin {} } output { stdout { codec => rubydebug } elasticsearch { hosts => ["http://10.0.0.204:9200"] index => "test-log-%{+YYYY.MM.dd}" } }'
https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html
写一个输出到前台,而且将日志存储到es里面的配置文件,启动在前台。
[root@elk01-node2 ~]# cd /etc/logstash/conf.d/ [root@elk01-node2 conf.d]# cat demo.conf input{ stdin {} } filter{ } output{ stdout { codec => rubydebug } elasticsearch { hosts => ["10.0.0.204:9200"] index => "demo-log-%{+YYYY.MM.dd}" } } [root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f demo.conf
写一个收集系统日志的配置文件
[root@elk01-node2 conf.d]# cat file.conf input{ file { path => ["/var/log/messages", "/var/log/secure"] # path 指定日志收集的位置 start_position => "beginning" # 指定从日志文件什么地方开始读。 type => "system-log" # 指定日志的类型,这个是自定义的。用来作if判断。 } } filter { } output{ if [type] == "system-log" { # 这里的等于不是=> 而是== , 须要注意一下。 elasticsearch { hosts => ["10.0.0.204:9200"] index => "system-log-%{+YYYY.MM}" } } } [root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f file.conf
这里须要注意下:若是使用了type来作if判断,那么在日志里就不能出现type字段了。
input{ file { path => ["/var/log/messages", "/var/log/secure"] start_position => "beginning" type => "system-log" } file { path => ["/var/log/elasticsearch/myes.log"] start_position => "beginning" type => "myes-log" } } filter { } output{ if [type] == "system-log" { elasticsearch { hosts => ["10.0.0.204:9200"] index => "system-log-%{+YYYY.MM}" } } if [type] == "myes-log" { elasticsearch { hosts => ["10.0.0.204:9200"] index => "myes-log-%{+YYYY.MM.dd}" } } } [root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f file.conf
在上面配置文件基础上,添加对java日志文件的收集。须要注意java日志的日志格式。java日志内容 一行会有不少的内容。须要用到multiline模块
https://www.elastic.co/guide/en/logstash/2.3/plugins-codecs-multiline.html
[root@elk01-node2 conf.d]# cat codec.conf input{ file { path => ["/var/log/elasticsearch/myes.log"] start_position => "beginning" type => "myes" codec => multiline { # 下面这三行的意思是,匹配到以[开头的文件以前的内容,就合并到上一行。 pattern => "^\[" # 匹配的表达式 negate => true # 匹配的结果,这是一个bool类型。 what => "previous" # 动做,合并到上一行。previous合并到上一行,next是合并到下一行。 } } } filter { } output{ if [type] == 'myes' { elasticsearch { hosts => ["10.0.0.204:9200"] index => "myes_log-%{+YYYY.MM.dd}" } } }
能够将其添加到kibana里面去,能清楚看到效果。
注:
https://www.elastic.co/guide/en/logstash/2.3/plugins-codecs-json.html
为何要用json来收集日志?
获取日志里面的参数信息,es不能直接作到的(若是你会ruby就能够本身写了),用json就能够轻松的获取到。(用app客户端的访问日志来举例,客户端,uid等等。)
获取方式
ps: nginx能够将日志写成json格式的。
修改nginx配置文件
[root@elk01-node2 nginx]# vim nginx.conf log_format access_log_json'{"user_ip":"$http_x_real_ip","lan_ip":"$remote_addr","log_time":"$time_iso8601","user_req":"$request","http_code":"$status","body_bytes_sent":"$body_bytes_sent","req_time":"$request_time","user_ua":"$http_user_agent"}'; access_log /var/log/nginx/access_json.log access_log_json; [root@elk01-node2 nginx]# systemctl restart nginx
编写收集nginx日志的配置文件,启动在前台进行测试。
[root@elk01-node2 conf.d]# cat nginx.conf input{ file { path => ["/var/log/nginx/access_json.log"] type => "nginx-log" codec => "json" } } filter { } output{ if [type] == 'nginx-log' { stdout { codec => rubydebug} } } [root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f nginx.conf # 模拟数据 [root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/aaa/ [root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/
效果
确认没问题后写入到es里
[root@elk01-node2 conf.d]# cat nginx.conf input{ file { path => "/var/log/nginx/access_json.log" codec => "json" type => "nginx-log" } } filter{ } output{ if [type] == 'nginx-log' { elasticsearch { hosts => ["10.0.0.204:9200"] index => "nginxlog-%{+YYYY.MM.dd}" } } } [root@elk01-node2 conf.d]# /opt/logstash/bin/logstash -f nginx.conf # 模拟数据 [root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/aaa/ [root@web01-node1 ~]# ab -n 100 -c 2 http://10.0.0.204/
遇到的问题: