elk部署配置,收集nginx和tomcat日志

部署流程:
登陆官网下载三个安装包: https://www.elastic.co/products
Elasticsearch 2.4.1     ###版本更新较快,各个版本之间依赖性较强,尽量使用官网推荐下载的
Logstash 2.4.0
Kibana 4.6.1
( 1 )安装 Logstash 依赖包 JDK
Logstash 的运行依赖于 Java 运行环境, Logstash 1.5 以上版本不低于 java 7 推荐使用最新版本的 Java 。由于我们只是运行 Java 程序,而不是开发,下载 JRE 即可。首先,在 Oracle 官方下载新版 jre ,再次我使用的是centos6.5的64位操作系统所以下载的是java1.8的64位版本
wget http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz    ###下载java1.8
JDK 的安装方式比较简单,只需将下载回来的程序包解压到相应的目录即可。
# mkdir /usr/local/java# tar -zxf jdk-8u45-linux-x64.tar.gz -C /usr/local/java/
设置 JDK 的环境变量,如下:
# tail -3 ~/.bash_profileexport JAVA_HOME=/usr/local/java/jdk1.8.0_45export PATH=$PATH:$JAVA_HOME/binexportCLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
安装 Elasticsearch
下载 Elasticsearch 后,解压到对应的目录就完成 Elasticsearch 的安装。
tar -xf elasticsearch-2.4.0.tar.gz -C /usr/local/

ElasticSearch
配置ElasticSearch:
安装两个所需的插件,head和kopf
/usr/local/elasticsearch/bin/plugin install mobz/elasticsearch-head     查看集群状态
/usr/localelasticsearch/bin/plugin install lmenezes/elasticsearch-kopf    配置集群工具

安装完成后,修改下配置文件,内容如下:
vim config/elasticsearch.xml

写入如下内容:
cluster .name=es_cluster node .name=node0 path . data =/tmp/elasticsearch/ data path .logs=/tmp/elasticsearch/logs#当前hostname或IP,我这里是centos2 network .host=0.0.0.0 network .port=9200
其他的选项保持默认,然后启动ES:
./bin/elasticsearch

启动时可能会报错:
启动可能会报错:按照如下解决:
一、ES
1、启动
[root@elasticseach1 bin]# ./elasticsearch start
ERROR: Parameter [start]does not start with --
 
[root@elasticseach1 bin]# ./elasticsearch
Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:270)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35)
Refer to the log for complete error details.
 
解决方法1:
在执行elasticSearch时加上参数-Des.insecure.allow.root=true,完整命令如下:
./elasticsearch -Des.insecure.allow.root=true  
 
解决办法2:
用vi打开elaticsearch执行文件,在变量ES_JAVA_OPTS使用前添加以下命令:
ES_JAVA_OPTS="-Des.insecure.allow.root=true"  
 
修改后,./elasticsearch 启动成功
 
2、修改elasticsearch.yml
cluster.name 和 node.name   
以及 network.host 为服务器ip         ###修改绑定的ip,修改成0.0.0.0就绑定机器所有ip
 
修改完成后启动elasticsearch 
然后可以打开页面localhost:9200,将会看到以下内容:
返回展示了配置的cluster_name和name,以及安装的ES的版本等信息。
刚刚安装的head插件,它是一个用浏览器跟ES集群交互的插件,可以查看集群状态、集群的doc内容、执行搜索和普通的Rest请求等。现在也可以使用它打开  localhost:9200/_plugin/head  页面来查看ES集群状态:
可以看到,现在,ES集群中没有index,也没有type,因此这两条是空的。

启动 Elasticsearch
/usr/local/elasticsearch/bin/elasticsearch  &
如果使用远程连接的 Linux 的方式并想后台运行 elasticsearch 执行如下命令:
nohup /usr/local/elasticsearch/bin/elasticsearch >nohup &

确认 elasticsearch 的 9200 以及9300端口已监听,说明 elasticsearch 已成功运行
netstat -anp |grep :9200 tcp        0      0 :::9200                     :::*                        LISTEN      3362/java

安装logstash

下载并安装 Logstash ,安装 logstash 只需将它解压的对应目录即可,例如: /usr/local 下:# tar -xf  logstash-2.4.0.tar.gz -C /usr/local/
安装完成后运行如下命令:
/usr/local/logstash-2.4.0/bin/logstash -e 'input { stdin { } } output { stdout {} }'
Hello World!   ###这个是自己输入2015-07-15T03:28:56.938Z noc.vfast.com Hello World!    ###这个是自动输出

我们可以看到,我们输入什么内容logstash按照某种格式输出,其中-e参数参数允许Logstash直接通过命令行接受设置。这点尤其快速的帮助我们反复的测试配置是否正确而不用写配置文件。使用CTRL-C命令可以退出之前运行的Logstash


安装 Kibana

下载 kibana 后,解压到对应的目录就完成 kibana 的安装
tar -zxf kibana-4.6.1-linux-x64.tar.gz -C /usr/local/
启动 kibana
#  /usr/local/kibana-4.6.1-linux-x64/bin/kibana
使用  http://kibanaServerIP: 5601  访问 Kibana ,登录后,首先,配置一个索引,默认, Kibana 的数据被指向 Elasticsearch ,使用默认的 logstash-* 的索引名称,并且是基于时间的,点击“ Create ”即可
想要使用这个还得先安装一些工具才行,个人实际安装经验,参考如下:
先下载如下三个连接的安装包。 然后分别在elasticsearch的路径下和kibana的路径下安装上,然后启动在登陆web界面就行,下面的这三个可以安装也可以不安装(还是安装上吧,不确定是啥用)

1. Manually download the License and Marvel binaries:
  1. Transfer the archive files to a temporary directory on the offline machine. (Do NOT put the archive files in the Elasticsearch plugins directory.)
  2. Specify the archive files when you run bin/plugin and bin/kibana. For example:
bin / plugin install file :/// path / to / file / license - 2.4 . 0.zip bin / plugin install file :/// path / to / file / marvel - agent - 2.4 . 0.zip bin / kibana plugin  -- install marvel  -- url file :/// path / to / file / marvel - 2.4 . 0.tar . gz




看到如下界面说明索引创建完成。
点击“ Discover ”,可以搜索和浏览 Elasticsearch 中的数据,默认搜索的是最近 15 分钟的数据。可以自定义选择时间。
到此,说明你的 ELK 平台安装部署完成。


最后收集nginx的日志还有tomcat的日志

首先,修改nginx的日志格式为json格式,将如下配置写入到nginx中:
注意使用access格式的日志
log_format  access  '{"@timestamp":"$time_iso8601",'                     
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"request":"$request",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
 
写入后,测试下没有语法错误即可使用

然后,配置下logstash的配置文件,写入如下配置:
vim /usr/local/logstash/config/nginx_acces.log
input {
     file {
     #制定nginx日志文件
        path => ["/usr/aochu/nginx/logs/access.log"]
        type => "nginx_log"
        start_position => "beginning"
        codec => json
     }
}
filter {
        date {
                match => [ "timestamp" , "YYYY-MM-dd HH:mm:ss" ]
        }
        }
output {
        elasticsearch {
     #制定elasticsearch的ip
        hosts => ["192.168.1.41:9200"]
     #制定默认创建的索引的名称
        index => "logstash-nginx-access-%{+YYYY.MM.dd}"
    }
##输出到标准显示,貌似可省略
    stdout {
codec => rubydebug
    }
}

然后测试下是否有语法错误:
/usr/local/logstash//bin/logstash -t -f ./config/nginx_test.conf         ###切记 -t和-f 不能位置写反
测试没有报错后,直接启动即可:
/usr/local/logstash//bin/logstash  -f ./config/nginx_test.conf   &   ###后台运行

然后,在前台界面就可以访问,查看是否有日志记录了,打开kibana的web界面,创建新的索引就可以查看日志了


收集tomcat的日志

首先,修改下tomcat的日志格式因为json格式,这样方便logstash得处理
在tomcat的配置文件中的<Context> </Context>段后加入下内容,
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
          prefix="localhost_access_log." suffix=".log"
          pattern="{"clientip":"%h","realip":"%{X-FORWARDED-FOR}i","ClientUser"
          :"%l","authenticated":"%u","
          AccessTime":"%t","method":"
          %r","status":"%s","SendBytes"
          :"%b","Query?string":"%q","partner
          ":"%{Referer}i",
          "AgentVersion":"%{User-Agent}i"}"/>

然后重新启动tomcat,访问日志会在logs/local_access_log.2016-10-25.log中,

配置logstash的语法规则,来收集tomcat日志:
vim /usr/local/logstash/config/tomcat_test.conf

input {
     file {
        path => ["/usr/aochu/pc/tomcat_pc/logs/localhost_access_log.2016-10-25.log"]
        type => "tomcat_log"
        start_position => "beginning"
codec => json
     }
}
filter {
date {
match => [ "timestamp" , "YYYY-MM-dd HH:mm:ss" ]
}
}
output {
elasticsearch {
        hosts => ["192.168.1.41:9200"]
        index => "tomcat-pc-%{+YYYY.MM.dd}"
    }
    stdout {
codec => rubydebug
    }
}

然后测试下没有语法错误就可以启动了
/usr/local/logstash//bin/logstash -t -f ./config/tomcat_test.conf         ###切记 -t和-f 不能位置写反
测试后,没有报错即可,然后启动:
/usr/local/logstash//bin/logstash -t -f ./config/tomcat_test.conf   

然后访问kibana的web界面重新添加个索引,即可

如果为了使应大多数的日志,不进行格式输出处理,将全部日志输出即可的话,使用如下的配置:
input {
     file {
        path => ["/usr/local/logstash-2.4.0/config/info.*.log"]
        type => "info_log"
        start_position => "beginning"
     }
}
filter {
     ###定义message的内容,可以用grok来定义格式,但是还不会语法
grok { 
match => { "message" => "%{COMBINEDAPACHELOG"}
}
date {
match => [ "timestamp" , "YYYY-MM-dd HH:mm:ss Z" ]
}
}
output {
elasticsearch {
        hosts => ["192.168.1.41:9200"]
        index => "info-pc-%{+YYYY.MM.dd}"
    }
    stdout {
codec => rubydebug
    }
}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值