ELK套件(ELK stack)是指ElasticSearch、Logstash和Kibana三件套。这三个软件可以组成一套日志分析和监控工具。
由于三个软件各自的版本号太多,建议采用ElasticSearch官网推荐的搭配组合:http://www.elasticsearch.org/overview/elkdownloads/
本文把ELK套件部署在一台CentOS单机上。
具体的版本要求如下:
操作系统版本:CentOS 6.4;
JDK版本:1.7.0;
Logstash版本:1.4.2;
ElasticSearch版本:1.4.2;
Kibana版本:3.1.2;
为了正常使用HTTP服务等,需要关闭防火墙:
# service iptables stop
# service iptables stop
或者可以不关闭防火墙,但是要在iptables中打开相关的端口:
# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9292 -j ACCEPT
# service iptables restart
# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 9292 -j ACCEPT # service iptables restart
ElasticSearch和Logstash依赖于JDK,所以需要安装JDK:
# yum -y install java-1.7.0-openjdk*
# java -version
# yum -y install java-1.7.0-openjdk* # java -version
ElasticSearch默认的对外服务的HTTP端口是9200,节点间交互的TCP端口是9300。
下载ElasticSearch:
# mkdir -p /opt/software && cd /opt/software
# sudo wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.tar.gz
# sudo tar -zxvf elasticsearch-1.4.2.tar.gz -C /usr/local/
# ln -s /usr/local/elasticsearch-1.4.2 /usr/local/elasticsearch
# mkdir -p /opt/software && cd /opt/software # sudo wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.tar.gz # sudo tar -zxvf elasticsearch-1.4.2.tar.gz -C /usr/local/ # ln -s /usr/local/elasticsearch-1.4.2 /usr/local/elasticsearch
安装elasticsearch-servicewrapper,并启动ElasticSearch服务:
# sudo wget https://github.com/elasticsearch/elasticsearch-servicewrapper/archive/master.tar.gz
# sudo tar -zxvf master
# mv /opt/software/elasticsearch-servicewrapper-master/service /usr/local/elasticsearch/bin/
# /usr/local/elasticsearch/bin/service/elasticsearch start
# sudo wget https://github.com/elasticsearch/elasticsearch-servicewrapper/archive/master.tar.gz # sudo tar -zxvf master # mv /opt/software/elasticsearch-servicewrapper-master/service /usr/local/elasticsearch/bin/ # /usr/local/elasticsearch/bin/service/elasticsearch start
测试ElasticSearch服务是否正常,预期返回200的状态码:
# curl -X GET http://localhost:9200
# curl -X GET http://localhost:9200
Logstash默认的对外服务的端口是9292
下载Logstash:
# sudo wget https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz
# sudo tar -zxvf logstash-1.4.2.tar.gz -C /usr/local/
# ln -s /usr/local/logstash-1.4.2 /usr/local/logstash
# sudo wget https://download.elasticsearch.org/logstash/logstash/logstash-1.4.2.tar.gz # sudo tar -zxvf logstash-1.4.2.tar.gz -C /usr/local/ # ln -s /usr/local/logstash-1.4.2 /usr/local/logstash
简单测试Logstash服务是否正常,预期可以将输入内容以简单的日志形式打印在界面上:
# /usr/local/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'
# /usr/local/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'
创建Logstash配置文件,并再次测试Logstash服务是否正常,预期可以将输入内容以结构化的日志形式打印在界面上:
# mkdir -p /usr/local/logstash/etc
# vim /usr/local/logstash/etc/hello_search.conf
input {
stdin {
type => "human"
}
}
output {
stdout {
codec => rubydebug
}
elasticsearch {
host => "10.111.121.22"
port => 9300
}
}
# /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/hello_search.conf
# mkdir -p /usr/local/logstash/etc # vim /usr/local/logstash/etc/hello_search.conf input { stdin { type => "human" } } output { stdout { codec => rubydebug } elasticsearch { host => "10.111.121.22" port => 9300 } } # /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/hello_search.conf
CentOS默认预装了Apache,所以将Kibana的代码直接拷贝到Apache可以访问的目录下即可
# sudo wget https://download.elasticsearch.org/kibana/kibana/kibana-3.1.2.tar.gz
# sudo tar -zxvf kibana-3.1.2.tar.gz
# mv kibana-3.1.2 /var/www/html/kibana
# sudo wget https://download.elasticsearch.org/kibana/kibana/kibana-3.1.2.tar.gz # sudo tar -zxvf kibana-3.1.2.tar.gz # mv kibana-3.1.2 /var/www/html/kibana
修改Kibana的配置文件,把elasticsearch所在行的内容替换成如下:
# vim /var/www/html/kibana/config.js
elasticsearch: "http://10.111.121.22:9200",
# vim /var/www/html/kibana/config.js elasticsearch: "http://10.111.121.22:9200",
启动一下HTTP服务:
# service httpd start
# service httpd start
修改ElasticSearch的配置文件,追加一行内容,并重启ElasticSearch服务:
# vim /usr/local/elasticsearch/config/elasticsearch.yml
http.cors.enabled: true
# /usr/local/elasticsearch/bin/service/elasticsearch restart
# vim /usr/local/elasticsearch/config/elasticsearch.yml http.cors.enabled: true # /usr/local/elasticsearch/bin/service/elasticsearch restart
然后就可以通过浏览器访问Kibana了:
http://10.111.121.22/kibana
http://10.111.121.22/kibana
现在,在之前的Logstash会话中输入任意字符,就可以在Kibana中查看到日志情况。
再次创建Logstash配置文件,这里将HTTP日志和文件系统日志作为输入,输出直接传给ElasticSearch,不再打印在界面上:
# vim /usr/local/logstash/etc/logstash_agent.conf
input {
file {
type => "http.access"
path => ["/var/log/httpd/access_log"]
}
file {
type => "http.error"
path => ["/var/log/httpd/error_log"]
}
file {
type => "messages"
path => ["/var/log/messages"]
}
}
output {
elasticsearch {
host => "10.111.121.22"
port => 9300
}
}
# nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/logstash_agent.conf &
# vim /usr/local/logstash/etc/logstash_agent.conf input { file { type => "http.access" path => ["/var/log/httpd/access_log"] } file { type => "http.error" path => ["/var/log/httpd/error_log"] } file { type => "messages" path => ["/var/log/messages"] } } output { elasticsearch { host => "10.111.121.22" port => 9300 } } # nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/etc/logstash_agent.conf &