一.簡介
ELK Stack是軟件集合Elasticsearch、Logstash、Kibana的簡稱,由這三個軟件及其相關的組件可以打造大規模日志實時處理系統。
其中,Elasticsearch 是一個基于 Lucene 的、支持全文索引的分布式存儲和索引引擎,主要負責將日志索引并存儲起來,方便業務方檢索查詢。
Logstash是一個日志收集、過濾、轉發的中間件,主要負責將各條業務線的各類日志統一收集、過濾后,轉發給 Elasticsearch 進行下一步處理。
Kibana是一個可視化工具,主要負責查詢 Elasticsearch 的數據并以可視化的方式展現給業務方,比如各類餅圖、直方圖、區域圖等。
所謂“大規模”,指的是 ELK Stack 組成的系統以一種水平擴展的方式支持每天收集、過濾、索引和存儲 TB 規模以上的各類日志。
通常,各類文本形式的日志都在處理范圍,包括但不限于 Web 訪問日志,如 Nginx/Apache Access Log 。
基于對日志的實時分析,可以隨時掌握服務的運行狀況、統計 PV/UV、發現異常流量、分析用戶行為、查看熱門站內搜索關鍵詞等。
上圖是ELK Stack實際應用中典型的一種架構,其中:
1)filebeat:部署在具體的業務機器上,通過定時監控的方式獲取增量的日志,并轉發到Kafka消息系統暫存。
2)Kafka:以高吞吐量的特征,作為一個消息系統的角色,接收從filebeat收集轉發過來的日志,通常以集群的形式提供服務。
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
三.安裝redis
#創建安裝目錄
[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,證明可以使用
四. 安裝elasticsearch
注意:
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限制高
#需要重啟系統才會生效
五.安裝logstash
#解壓
[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
六、安裝Kibana
#解壓
[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
七.客戶端安裝filebeat收集nginx日志
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 &
八.收集端編寫logstash文件
[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
十.訪問kibana添加索引
參考:
http://www.ywnds.com/?p=9776
下篇
x-pack、head、ksof 組件