ELK Stack是軟件集合Elasticsearch、Logstash、Kibana的簡稱,由這三個軟件及其相關的組件能夠打造大規模日誌實時處理系統。java
其中,Elasticsearch 是一個基於 Lucene 的、支持全文索引的分佈式存儲和索引引擎,主要負責將日誌索引並存儲起來,方便業務方檢索查詢。node
Logstash是一個日誌收集、過濾、轉發的中間件,主要負責將各條業務線的各種日誌統一收集、過濾後,轉發給 Elasticsearch 進行下一步處理。linux
Kibana是一個可視化工具,主要負責查詢 Elasticsearch 的數據並以可視化的方式展示給業務方,好比各種餅圖、直方圖、區域圖等。nginx
所謂「大規模」,指的是 ELK Stack 組成的系統以一種水平擴展的方式支持天天收集、過濾、索引和存儲 TB 規模以上的各種日誌。c++
一般,各種文本形式的日誌都在處理範圍,包括但不限於 Web 訪問日誌,如 Nginx/Apache Access Log 。web
基於對日誌的實時分析,能夠隨時掌握服務的運行情況、統計 PV/UV、發現異常流量、分析用戶行爲、查看熱門站內搜索關鍵詞等。redis
上圖是ELK Stack實際應用中典型的一種架構,其中:json
1)filebeat:部署在具體的業務機器上,經過定時監控的方式獲取增量的日誌,並轉發到Kafka消息系統暫存。bootstrap
2)Kafka:以高吞吐量的特徵,做爲一個消息系統的角色,接收從filebeat收集轉發過來的日誌,一般以集羣的形式提供服務。vim
3)logstash:而後,Logstash從Kafka中獲取日誌,並經過Input-Filter-Output三個階段的處理,更改或過濾日誌,最終輸出咱們感興趣的數據。一般,根據Kafka集羣上分區(Partition)的數量,1:1肯定Logstash實例的數量,組成Consumer Group進行日誌消費。
4)elasticsearch:最後,Elasticsearch存儲並索引Logstash轉發過來的數據,並經過Kibana查詢和可視化展現,達到實時分析日誌的目的。
Elasticsearch/Kibana還能夠經過安裝x-pack插件實現擴展功能,好比監控Elasticsearch集羣狀態、數據訪問受權等。
咱們一步步安裝部署Elastic Stack系統的各個組件,而後以網站訪問日誌爲例進行數據實時分析。
首先,到ELK 官網下載須要用到的Filebeat/Logstash/Elasticsearch/Kibana軟件安裝包。(推薦下載編譯好的二進制可執行文件,直接解壓執行就能夠部署)
System: Centos release 6.8
ElasticSearch: 5.4.1
Logstash: 5.4.1
Kibana: 5.4.1
Java: openjdk version "1.8.0_161"
redis:3.05
Nginx: 1.10.1
1..下載軟件包:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz
wget http://download.redis.io/releases/redis-3.0.5.tar.gz
yum install java-1.8.0-openjdk
2.準備
1)下面的命令實現永久關閉SELinux
[root@elk ~]# sed -i 's/^SELINUX=.*/#&/;s/^SELINUXTYPE=.*/#&/;/SELINUX=.*/a SELINUX=disabled' /etc/sysconfig/selinux
#永久修改下主機名,須要重啓系統以後生效
#下面的命令實現臨時關閉SELinux
[root@elk ~]# setenforce 0
setenforce: SELinux is disabled
2) 修改主機名
#臨時修改
[root@elk ~]#hostname elk.server.com
#永久修改
[root@elk ~]# vi /etc/sysconfig/network
localhost.localdomain ------》把這行修改爲下面的
elk.server.com #修改爲你本身的主機名
#添加域名
[root@elk ~]#cat /etc/hosts
192.168.10.243 elk.server.com
3)關閉防火牆
#臨時關閉
[root@elk ~]# iptables -F
或者
[root@elk ~]# service iptables stop
4) 同步時間
ntpdate -u ntp.api.bz
#建立安裝目錄
[root@elk yum.repos.d]#mkdir -pv /data/application/
#編譯並進行安裝
[root@elk ~]# tar zxf redis-3.0.5.tar.gz && cd redis-3.0.5
[root@elk redis-3.0.5]# make PREFIX=/data/application/redis-3.0.5 install
#建立配置文件目錄
[root@elk redis-3.0.5]#mkdir /data/application/redis-3.0.5/{etc,run,log}
#修改redis.conf
[root@elk redis-3.0.5]#cp /data/application/redis-3.0.5/redis.conf etc/
[root@elk redis-3.0.5]#vi /data/application/redis-3.0.5/redis.conf
修改如下幾項:
daemonize yes #後臺模式運行
pidfile /data/application/redis-3.0.5/run/redis.pid #redis的pid
bind 0.0.0.0 #這裏根據本身的ip填寫
port 6379#端口
logfile "/data/application/redis-3.0.5/log/redis.log" #log存放位置
dir /data/application/redis-3.0.5
#啓動redis
執行下面的命令
[root@elk~]#/ data/application/redis-3.0.5/bin/redis-server /data/application/redis-3.0.5/etc/redis.conf
#查看是否啓動成功
[root@elk ~]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 2736 root 4u IPv4 26198 0t0 TCP 192.168.10.243:6379 (LISTEN)
#測試redis
[root@localhost ~]# /data/application/redis-3.0.5/bin/redis-cli -h 192.168.10.243
192.168.10.243:6379> ping
PONG
#出現PONG,證實可使用
注意:
es(elasticsearch)版本2.x以上須要JDK 1.8以上
運行es不能使用root用來來運行
es目錄必須指定一個普通用戶和組(受權)
es對內存和CPU的消耗比較高
es使用的端口看開放iptables:9200,9300等
es配置其餘插件實現資源等可視化監控
es的版本和插件之間版本要匹配
es集羣配置,第一節點配置好scp到其餘節點便可(修改配置文件)
#建立elk用戶
[root@localhost application]# adduser -s /bin/bash -c 'elk' -m -d /home/elk elk
注:
從2.0開始不能用root用戶啓動須要elk用戶啓動
#解壓
[root@elk elk_pack]# tar zxvf elasticsearch-6.01.tar.gz -C /data/application/
注:
Elasticsearch是不須要編譯的,解壓就可使用
備份配置文件
[root@elk ~]# cp /data/application/elasticsearch-6.01/config/elasticsearch.yml{,.ori}
#找到如下幾行修改
[root@elk config]# vi elasticsearch.yml
path.data: /data/shuju ----》存放數據路徑
path.logs: /data/logs -----》日誌路徑
network.host: 0.0.0.0 -----》根據本身的ip修改
http.port: 9200
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#建立los,shuju
[root@elk config]#mkdir /data/{shuju,logs}
#修改elasticsearch權限
[root@elk ~]#chown -R elk.elk /data/application/elasticsearch-6.0.1./
[root@elk ~]#chown -R elk.elk /data/{shuju,logs}
[root@elk ~]# su – elk
#在前臺顯示下效果
[elk@elk ~]$/data/application/elasticsearch-6.0.1/bin/elasticsearch
#測試是否成功
[root@elk ~]# curl 192.168.10.243:9200
{
"name" : "z8htm2J",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "wEbF7BwgSe-0vFyHb1titQ",
"version" : {
"number" : "6.0.1",
"build_hash" : "3adb13b",
"build_date" : "2017-03-23T03:31:50.652Z",
"build_snapshot" : false,
"lucene_version" : "6.4.1"
},
"tagline" : "You Know, for Search"
}
啓動elasticsearch出現以下錯誤
1.問題:最大線程數,打開的過低,須要增長線程數
max number of threads [1024] for user [elasticsearch] likely toolow, increase to at least [2048]
解決:
vi /etc/security/limits.d/90-nproc.conf
* soft nproc 2048
2.問題:打開虛擬內存的個數太少須要增長
max virtual memory areas vm.max_map_count [65530] likely toolow, increase to at least [262144]
解決:
[root@elk ~]#vi /etc/sysctl.conf
vm.max_map_count=655360
[root@elk ~]#sysctl -p
注:
vm.max_map_count文件容許max_map_count限制虛擬內存的數量
3.max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
#臨時修改
[root@elk ~]# ulimit -SHn 65536
注:
-S 設置軟件資源限制
-H 設置硬件資源限制
-n 設置內核能夠同時能夠打開文件描述符
[root@elk ~]# ulimit -n
65536
注:
修改這個緣由,啓動elasticsearch 會出現這個狀況too many open files,致使啓動失敗
#永久修改
#在文件最後添加
[root@elk ~]# vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
注:
文件格式:username|@groupname type resource limit
分爲3中類型type(有 soft,hard 和 -)
soft是指當前系統生效的設置值
hard 系統設置的最大值
[if !supportLists]- [endif]同時設置了soft和hard的值
nofile - 打開文件的最大數目
noproc - 進程的最大數目
soft<=hard soft的限制不能比hard限制高
#須要重啓系統纔會生效
#解壓
[root@elk elk_pack]# tar zxvf logstash-6.0.1tar.gz -C /data/application/
注:
Logstash是不須要編譯的,解壓就可使用
#測試可否使用
[root@elk ~]# /data/application/logstash-6.0.1/bin/logstash -e 'input { stdin { } } output {stdout {} }'
Sending Logstash's logs to /data/application/logstash-5.2.0/logs which is now configured via log4j2.properties
The stdin plugin is now waiting for input:
[2017-04-12T11:54:10,457][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>125}
[2017-04-12T11:54:10,481][INFO ][logstash.pipeline ] Pipeline main started
[2017-04-12T11:54:10,563][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
hello world ---->輸入hell world,隨便輸入什麼,能輸出就證實可使用
2017-04-12T03:54:40.278Z localhost.localdomain hello world ---->輸出hello world
/data/application/logstash-5.2.0/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.0.2/patterns
#解壓
[root@elk elk_pack]# tar zxvf kibana-6.0.1-linux-x86_64.tar.gz -C /data/application/
注:
Kibana是不須要編譯的,解壓就可使用
#修改配置kibana.yml文件
#cd kibana這個目錄
[root@elk ~]# cd /data/application/kibana-6.0.1/config/
#找到如下幾行修改
[root@elk config]# egrep -v "^$|^[#]" kibana.yml
server.port: 5601 #kibana的端口
server.host: "0.0.0.0" #訪問kibana的ip地址
elasticsearch.url: "http://192.168.10.243:9200" #elasticsearch的ip地址
kibana.index: ".kibana" #建立索引
#測試是否啓動成功
[root@192 ~]# /data/application/kibana-6.0.1/bin/kibana
log [06:22:02.940] [info][status][plugin:kibana@5.2.0] Status changed from uninitialized to green - Ready
log [06:22:03.106] [info][status][plugin:elasticsearch@5.2.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch
log [06:22:03.145] [info][status][plugin:console@5.2.0] Status changed from uninitialized to green - Ready
log [06:22:03.193] [warning] You're running Kibana 5.2.0 with some different versions of Elasticsearch. Update Kibana or Elasticsearch to the same version to prevent compatibility issues: v5.3.0 @ 192.168.10.243:9200 (192.168.201.135)
log [06:22:05.728] [info][status][plugin:timelion@5.2.0] Status changed from uninitialized to green - Ready
log [06:22:05.744] [info][listening] Server running at http://192.168.10.243:5601
log [06:22:05.746] [info][status][ui settings] Status changed from uninitialized to yellow - Elasticsearch plugin is yellow
log [06:22:08.263] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to yellow - No existing Kibana index found
log [06:22:09.446] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to green - Kibana index ready
log [06:22:09.447] [info][status][ui settings] Status changed from yellow to green – Ready
#證實啓動成功
#查看port
[root@elk shuju]# lsof -i:5601
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 4690 root 13u IPv4 40663 0t0 TCP 192.168.10.243:esmagent (LISTEN)
經過web訪問
http://192.168.10.243:5601
1)安裝nginx
安裝依賴包
[root@www ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
下載nginx的源碼包:http://nginx.org/download
[root@www ~]# tar zxf nginx-1.10.2.tar.gz
[root@www ~]# cd nginx-1.10.2/
[root@www ~]# groupadd www#添加www組
[root@www ~]# useradd -g www www -s /sbin/nologin#建立nginx運行帳戶www並加入到www組,不容許www用戶直接登陸系統
[root@www nginx-1.10.2]# ./configure --prefix=/usr/local/nginx1.10 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www
[root@www nginx-1.10.2]# make&& make install
修改日誌類型爲json
[root@rocketmq-nameserver2 soft]# vim nginx/conf/nginx.conf
#添加以下內容
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log logs/access.log json;
2)安裝部署Filebeat
tar xf filebeat-6.0.1-linux-x86_64.tar.gz
cd filebeat-6.0.1-linux-x86_64
編寫收集文件:
filebeat.prospectors:
- input_type: log
paths:
- /var/log/*.log
- input_type: log
paths:
- /aebiz/soft/nginx/logs/*.log
encoding: utf-8
document_type: my-nginx-log
scan_frequency: 10s
harvester_buffer_size: 16384
max_bytes: 10485760
tail_files: true
output.redis:
enabled: true
hosts: ["192.168.10.243"]
port: 6379
key: filebeat
db: 0
worker: 1
timeout: 5s
max_retries: 3
啓動
[root@~ filebeat-6.0.1-linux-x86_64]# ./filebeat -c filebeat2.yml
後臺啓動
[root@~ filebeat-6.0.1-linux-x86_64]# nohup ./filebeat -c filebeat2.yml &
[root@bogon config]# vim 02-logstash.conf
input {
redis {
host => "192.168.10.243"
port => "6379"
data_type => "list"
key => "filebeat"
type => "redis-input"
}
}
filter {
json {
source => "message"
remove_field => "message"
}
}
output {
elasticsearch {
hosts => ["192.168.10.243:9200"]
index => "logstash-nginx-%{+YYYY.MM.dd}"
document_type => "nginx"
# template => "/usr/local/logstash-2.3.2/etc/elasticsearch-template.json"
workers => 1
}
}
啓動前檢查 -t 參數
[root@bogon config]# /data/application/logstash-6.0.1/bin/logstash -t -f /data/application/logstash-6.0.1/config/02-logstash.conf
Configuration OK[2018-05-06T14:11:26,442][INFO ][logstash.runner ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
啓動
[root@bogon config]# /data/application/logstash-6.0.1/bin/logstash-f /data/application/logstash-6.0.1/config/02-logstash.conf
[2018-05-06T14:12:06,694][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}[2018-05-06T14:12:07,317][WARN ][logstash.outputs.elasticsearch] You are using a deprecated config setting "document_type" set in elasticsearch. Deprecated settings will continue to work, but are scheduled for removal from logstash in the future. Document types are being deprecated in Elasticsearch 6.0, and removed entirely in 7.0. You should avoid this feature If you have any questions about this, please visit the #logstash channel on freenode irc. {:name=>"document_type", :plugin=>["192.168.10.243:9200"], index=>"logstash-nginx-%{+YYYY.MM.dd}", document_type=>"nginx", workers=>1, id=>"a8773a7416ee72eecc397e30d8399a5da417b6c3dd2359fc706229ad8186d12b">} ..........
後臺啓動
[root@bogon config]# nohup /data/application/logstash-6.0.1/bin/logstash -t -f /data/application/logstash-6.0.1/config/02-logstash.conf &
查看redis是否收到filebeat發送key爲filebeat的數據
[root@elk config]# /data/application/redis-3.0.5/bin/redis-cli
127.0.0.1:6379> keys *
(empty list or ) #個人爲空,數據是被logstash取走了
啓動順序由左到右Elasticsearch-àKibana--àLogstash
啓動es
[root@elk config]# su - elk
[elk@elk ~]$ /data/application/elasticsearch-6.0.1/bin/elasticsearch
啓動kibana
[root@elk etc]# /data/application/kibana-6.0.1-linux-x86_64/bin/kibana
啓動logstah
[root@bogon config]# /dahta/application/logstash-6.0.1/bin/logstash -f /data/application/logstash-6.0.1/config/02-logstash.conf
客戶端啓動filebeat
[root@rocketmq-nameserver2 filebeat-6.0.1-linux-x86_64]# ./filebeat -c filebeat2.yml