一、ELK搭建篇
官網位址:https://www.elastic.co/cn/
官網權威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
安裝指南:https://www.elastic.co/guide/en/elasticsearch/reference/2.x/rpm.html
ELK是Elasticsearch、Logstash、Kibana的簡稱,這三者是核心套件,但并非全部。
Elasticsearch是實時全文搜尋和分析引擎,提供搜集、分析、存儲資料三大功能;是一套開放REST和JAVA API等結構提供高效搜尋功能,可擴充的分布式系統。它建構于Apache Lucene搜尋引擎庫之上。
Logstash是一個用來搜集、分析、過濾日志的工具。它支援幾乎任何類型的日志,包括系統日志、錯誤日志和自定義應用程式日志。它可以從許多來源接收日志,這些來源包括 syslog、消息傳遞(例如 RabbitMQ)和JMX,它能夠以多種方式輸出資料,包括電子郵件、websockets和Elasticsearch。
Kibana是一個基于Web的圖形界面,用于搜尋、分析和可視化存儲在 Elasticsearch名額中的日志資料。它利用Elasticsearch的REST接口來檢索資料,不僅允許使用者建立他們自己的資料的定制儀表闆視圖,還允許他們以特殊的方式查詢和過濾資料

環境
Centos6.5 兩台IP:192.168.1.224 安裝: elasticsearch、logstash、Kibana、Nginx、Redis
192.168.1.157 安裝: elasticsearch (兩台都關閉iptables和SElinux)
安裝
安裝elasticsearch的yum源的密鑰(這個需要在所有伺服器上都配置)
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
配置elasticsearch的yum源,在elasticsearch.repo檔案中添加如下内容
vim /etc/yum.repos.d/elasticsearch.repo
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
安裝elasticsearch的環境
安裝elasticsearch
# yum install -y elasticsearch
安裝java環境(java環境必須是1.8版本以上的)
1.tar zxvf ./jdk-8u151-linux-x64.tar.gz -C /usr/lib/jvm
2.用指令vim /etc/bashrc檔案,在檔案最後面加上:
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_151
export JRE_HOME=${JAVA_HOME}/jre
exportCLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
3.更新環境變量
source /etc/bashrc
4.驗證是否安裝成功
java -version
echo $PATH
建立elasticsearch data的存放目錄,并修改該目錄的屬主屬組
# mkdir -p /data/es-data (自定義用于存放data資料的目錄)
# chown -R elasticsearch:elasticsearch /data/es-data
修改elasticsearch的日志屬主屬組
# chown -R elasticsearch:elasticsearch /var/log/elasticsearch/
修改elasticsearch的配置檔案
# vim /etc/elasticsearch/elasticsearch.yml
#找到配置檔案中的cluster.name,打開該配置并設定叢集名稱
cluster.name: oldboy
#找到配置檔案中的node.name,打開該配置并設定節點名稱
node.name: ywxi-1
#修改data存放的路徑
path.data: /data/es-data
#修改logs日志的路徑
path.logs: /var/log/elasticsearch/
#配置記憶體使用用交換分區
bootstrap.memory_lock: true
#監聽的網絡位址
network.host: 0.0.0.0
#開啟監聽的端口
http.port: 9200
#關掉廣播位址傳播
discovery.zen.ping.multicast.enabled: false
#指定區域網路IP,讓他們找到叢集
discovery.zen.ping.unicast.hosts: ["192.168.1.224","192.168.1.157"]
啟動服務
[root@al7 elasticsearch]# /etc/init.d/elasticsearch start
Starting elasticsearch: Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /tmp/hs_err_pid2616.log
[FAILED]
這個報錯是因為預設使用的記憶體大小為2G,虛拟機沒有那麼多的空間
修改參數:
vim /etc/elasticsearch/jvm.options
-Xms512m
-Xmx512m
再次啟動
/etc/init.d/elasticsearch start檢視服務狀态,如果有報錯可以去看錯誤日志 less /var/log/elasticsearch/demon.log(日志的名稱是以叢集名稱命名的)
建立開機自啟動服務
# chkconfig elasticsearch on
注意事項
vim /etc/security/limits.conf #開啟elasticsearch使用者鎖住記憶體
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
[root@227 elasticsearch]# /etc/init.d/elasticsearch restart
Stopping elasticsearch: [FAILED]
Starting elasticsearch: Exception in thread "main" java.lang.IllegalStateException: marvel plugin requires the license plugin to be installed
at org.elasticsearch.marvel.license.LicenseModule.verifyLicensePlugin(LicenseModule.java:37)
at org.elasticsearch.marvel.license.LicenseModule.<init>(LicenseModule.java:25)
at org.elasticsearch.marvel.MarvelPlugin.nodeModules(MarvelPlugin.java:89)
at org.elasticsearch.plugins.PluginsService.nodeModules(PluginsService.java:263)
at org.elasticsearch.node.Node.<init>(Node.java:179)
at org.elasticsearch.node.Node.<init>(Node.java:140)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:143)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:194)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:286)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:45)
Refer to the log for complete error details.
[FAILED]
報錯解決: /usr/share/elasticsearch/bin/plugin install license #安裝下插件解決
通過浏覽器請求下9200的端口,看下是否成功
先檢查9200端口是否起來
[root@al7 elasticsearch]# netstat -tnlp|grep 9200
tcp 0 0 :::9200 :::* LISTEN 4760/java
如何和elasticsearch互動
JavaAPI
RESTful API
Javascript,.Net,PHP,Perl,Python
利用API檢視狀态# curl -i -XGET 'localhost:9200/_count?pretty'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 95
{ "count" : 0, "_shards" : { "total" : 0, "successful" : 0, "failed" : 0
}
}
安裝插件
安裝elasticsearch-head插件1 /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
#通路 http://192.168.1.224:9200/_plugin/head
安裝插件2 ES監控 /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
#通路 http://192.168.1.224:9200/_plugin/kopf
浏覽器通路:http://192.168.1.224:9200/_plugin/head/ #ywxi-1和ywxi-2是我這裡搭的節點,另外兩個是同僚的節點
LogStash的使用
安裝Logstash環境:
官方安裝手冊:
https://www.elastic.co/guide/en/logstash/current/installing-logstash.html
下載下傳yum源的密鑰認證:
# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
#配置logstash的yum源,在logstash.repo檔案中添加如下内容
vim logstash.repo
[logstash-2.4]
name=Logstash repository for 2.4.x packages
baseurl=https://packages.elastic.co/logstash/2.4/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
# yum install -y logstash
檢視下logstash的安裝目錄
# rpm -ql logstash
#建立一個軟連接配接,每次執行指令的時候不用在寫安裝路勁(預設安裝在/usr/share下)ln -s /opt/logstash/bin/logstash /bin/
執行logstash的指令
# logstash -e 'input { stdin { } } output { stdout {} }'運作成功以後輸入:
nihao
stdout傳回的結果:
注:
-e 執行操作 input 标準輸入
{ input } 插件 output 标準輸出
{ stdout } 插件
通過rubydebug來輸出下更詳細的資訊
# logstash -e 'input { stdin { } } output { stdout {codec => rubydebug} }'
執行成功輸入:
nihao
stdout輸出的結果:
如果标準輸出還有elasticsearch中都需要保留應該怎麼玩,看下面
# logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["192.168.1.224:9200"] } stdout { codec => rubydebug }}'
運作成功以後輸入:
I am elk
傳回的結果(标準輸出中的結果):
logstash使用配置檔案
官方指南:https://www.elastic.co/guide/en/logstash/current/configuration.html
建立配置檔案01-logstash.conf
# vim /etc/logstash/conf.d/elk.conf 檔案中添加以下内容input { stdin { } }output { elasticsearch { hosts => ["192.168.1.224:9200"] } stdout { codec => rubydebug }
}
使用配置檔案運作logstash# logstash -f ./elk.conf運作成功以後輸入以及标準輸出結果
logstash的資料庫類型
1. Input插件
權威指南:https://www.elastic.co/guide/en/logstash/current/input-plugins.html
file插件的使用
# vim /etc/logstash/conf.d/elk.conf
# 添加如下配置
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
運作logstash指定elk.conf配置檔案,進行過濾比對#logstash -f /etc/logstash/conf.d/elk.conf
來一發配置安全日志的并且把日志的索引按類型做存放,繼續編輯elk.conf檔案
# vim /etc/logstash/conf.d/elk.conf添加secure日志的路徑
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
}
}
output { if [type] == "system" {
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "system-%{+YYYY.MM.dd}"
}
} if [type] == "secure" {
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "nagios-secure-%{+YYYY.MM.dd}"
}
}
}
運作logstash指定elk.conf配置檔案,進行過濾比對# logstash -f ./elk.conf
這些設定都沒有問題之後,接下來安裝下kibana,可以讓在前台展示
Kibana的安裝及使用
安裝kibana環境
官方安裝手冊:https://www.elastic.co/guide/en/kibana/current/install.html
下載下傳kibana的tar.gz的軟體包
[root@al7 conf.d]# cd /usr/local/src/
[root@al7 src]# wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
解壓kibana的tar包
[root@al7 src]# tar zxvf kibana-4.3.1-linux-x64.tar.gz
進入解壓好的kibana
[root@al7 src]# mv kibana-4.3.1-linux-x64 /usr/local/ #在/usr/local建立kibana的軟連接配接
[root@al7 src]# ln -s /usr/local/kibana-4.3.1-linux-x64/ /usr/local/kibana
編輯kibana的配置檔案
[root@al7 src]# vim /usr/local/kibana/config/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.1.224:9200"
kibana.index: ".kibana"
#安裝screen,以便于kibana在背景運作(當然也可以不用安裝,用其他方式進行背景啟動)
[root@al7 src]# yum -y install screen
[root@al7 src]# screen
/usr/local/kibana/bin/kibana
Crtl a+d 退出
[root@al224 ~]# screen -ls
There is a screen on:
2257.pts-0.al224 (Detached)
1 Socket in /var/run/screen/S-root.
打開浏覽器并設定對應的index http://IP:5601
添加ES的索引到kibana裡
二、ELK實戰篇
好,現在索引也可以建立了,現在可以來輸出nginx、message、secrue的日志到前台展示(Nginx有的話直接修改,沒有自行安裝)
編輯nginx配置檔案,修改以下内容(在http子產品下添加)
log_format json '{"@timestamp":"$time_iso8601",'
'"@version":"1",'
'"client":"$remote_addr",'
'"url":"$uri",'
'"status":"$status",'
'"domian":"$host",'
'"host":"$server_addr",'
'"size":"$body_bytes_sent",'
'"responsetime":"$request_time",'
'"referer":"$http_referer",'
'"ua":"$http_user_agent"'
'}';
修改access_log的輸出格式為剛才定義的json
access_log logs/elk.access.log json;
編輯logstash配置檔案,進行日志收集
vim /etc/logstash/conf.d/full.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
}
file {
path => "/usr/local/nginx/logs/elk.access.log"
type => "nginx"
start_position => "beginning"
}
}
output { if [type] == "system" {
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "nagios-system-%{+YYYY.MM.dd}"
}
}
if [type] == "secure" {
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "nagios-secure-%{+YYYY.MM.dd}"
}
}
if [type] == "nginx" {
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "nginx-log-%{+YYYY.MM.dd}"
}
}
}
運作看看效果如何
logstash -f /etc/logstash/conf.d/full.conf
可以發現所有建立日志的索引都已存在,接下來就去Kibana建立日志索引,進行展示(按照上面的方法進行建立索引即可),看下展示的效果
ab壓測下: ab -n 20000 -c 1000 http://192.168.1.224/
具體的日志輸出需求,進行具體的分析
三:ELK終極篇
安裝reids
# yum install -y redis
修改redis的配置檔案
# vim /etc/redis.conf
修改内容如下
daemonize yes
bind 192.168.1.224啟動redis服務
# /etc/init.d/redis restart
測試redis的是否啟用成功
# redis-cli -h 192.168.1.224輸入info如果有不報錯即可
redis 192.168.1.224:6379> info
編輯配置redis-out.conf配置檔案,把标準輸入的資料存儲到redis中
# vim /etc/logstash/conf.d/redis-out.conf
添加如下内容
input {
stdin {}
}
output {
redis {
host => "192.168.1.224"
port => "6379"
db => '6'
data_type => "list"
key => 'demo'
}
}
運作logstash指定redis-in.conf的配置檔案# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf
編輯配置redis-in.conf配置檔案,把reids的存儲的資料輸出到elasticsearch中
# vim /etc/logstash/conf.d/redis-out.conf添加如下内容
input{
redis {
host => "192.168.1.224"
port => "6379"
db => '6'
data_type => "list"
key => 'demo'
}
}
output {
elasticsearch {
hosts => ['192.168.1.224:9200']
index => 'redis-test-%{+YYYY.MM.dd}'
}
}
運作logstash指定redis-in.conf的配置檔案# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf
把之前的配置檔案修改一下,變成所有的日志監控的來源檔案都存放到redis中,然後通過redis在輸出到elasticsearch中
更改為如下,編輯full.conf
input {
file {
path => "/var/log/nginx/access_json.log"
type => "nginx"
start_position => "beginning"
}
file {
path => "/var/log/secure"
type => "secure"
start_position => "beginning"
}
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
if [type] == "nginx" {
redis {
host => "192.168.1.224"
port => "6379"
db => "6"
data_type => "list"
key => 'nginx'
}
}
if [type] == "secure" {
redis {
host => "192.168.1.224"
port => "6379"
db => "6"
data_type => "list"
key => 'secure'
}
}
if [type] == "system" {
redis {
host => "192.168.1.224"
port => "6379"
db => "6"
data_type => "list"
key => 'system'
}
}
}
運作logstash指定shipper.conf的配置檔案# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/full.conf在redis中
(檢視是否已經将資料寫到裡面(有時候輸入的日志檔案不産生日志,會導緻redis裡面也沒有寫入日志)
億級日志平台之——ELK Stack實踐
把redis中的資料讀取出來,寫入到elasticsearch中(需要另外一台主機做實驗)
編輯配置檔案
# vim /etc/logstash/conf.d/redis-out.conf
添加如下内容
input {
redis {
type => "system"
host => "192.168.1.224"
port => "6379"
db => "6"
data_type => "list"
key => 'system'
batch_count => 1
}
redis {
type => "nginx"
host => "192.168.1.224"
port => "6379"
db => "6"
data_type => "list"
key => 'nginx'
batch_count => 1
}
redis {
type => "secure"
host => "192.168.1.224"
port => "6379"
db => "6"
data_type => "list"
key => 'secure'
batch_count => 1
}
}
output {
if[type] == "system"
{
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if[type] == "nginx"
{
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "nginx-%{+YYYY.MM.dd}"
}
}
if[type] == "secure"
{
elasticsearch {
hosts => ["192.168.1.224:9200"]
index => "secure-%{+YYYY.MM.dd}"
}
}
}
注意:
input是從用戶端收集的
output是同樣也儲存到192.168.1.224中的elasticsearch中,如果要儲存到目前的主機上,可以把output中的hosts修改成localhost,如果還需要在kibana中顯示,需要在本機上部署kabana,為何要這樣做,起到一個松耦合的目的。說白了,就是在用戶端收集日志,寫到服務端的redis裡或是本地的redis裡面,輸出的時候對接ES伺服器即可
運作
指令看看效果# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf
上線ELK
1.日志分類
系統日志 rsyslog logstash syslog插件
通路日志 nginx logstash codec json
錯誤日志 file logstash mulitline
運作日志 file logstash codec json
裝置日志 syslog logstash syslog插件
Debug日志 file logstash json或者mulitline
2.日志标準化
路徑 固定
格式 盡量json3. 系統個日志開始-->錯誤日志-->運作日志-->通路日志
因為ES儲存日志是永久儲存,是以需要定期删除一下日志,下面指令為删除指定時間前的日志
curl -X DELETE http://xx.xx.com:9200/logstash-*-`date +%Y-%m-%d -d "-$n days"`