摘要
日志分析是運維工程師解決系統故障,發現問題的主要手段。日志主要包括系統日志、應用程序日志和安全日志。系統運維和開發人員可以通過日志了解服務器軟硬件信息、檢查配置過程中的錯誤及錯誤發生的原因,經常分析日志可以了解服務器的負荷,性能安全性,從而及時采取措施糾正錯誤。
日志是一個非常龐大的數據,並且常常被分散在不同的設備上,這樣排查問題的時候找日志就非常繁瑣困難。
這時,一個專門處理日志的系統就非常必要,這里介紹其中的一種,ELK日志分析系統(ELasticsearch+Logstash+Kibana)
日志服務器
提高安全性、集中存放日志、缺陷:對日志分析困難
ELK日志分析系統的日志處理步驟
- 首先在集群的每台服務器上都需要安裝日記收集工具logstash用於日志的收集工作
- 每個服務器節點會將本地的日志文件輸入到LogStash中去。
- LogStash會將日志文件以json的形式輸出到ES集群中去。
- 最后可以在可視化界面kibana中對日志進行展示查詢操作。
一、Elasticsearch簡介
Elasticsearch的概述
提供了一個分布式多用戶能力的全文搜索引擎
Elasticsearch核心概念
- 接近實時
- 集群
- 節點
- 索引
- 索引(庫)> 類型(表)> 文檔(記錄)
- 分片和副本
二、Logstash簡介
Logstash介紹
- 一款強大的數據處理工具
- 可實現數據傳輸、格式處理、格式化輸出
- 數據輸入、數據加工(如過濾,改寫等)以及數據輸出
LogStash主要組件· - Shipper: 日志收集者。負責監控本地日志文件的變化,及時收集最新的日志文件內容。
- Indexer:日志存儲者。負責接收日志並寫入到本地文件。
- Broker: 日志Hub。 負責連接多個Shipper 和多個Indexer.
- Search and Storage: 允許對事件進行搜索和存儲。
- Web Interface: 基於Web 的展示界面。
在Logstash 中,包括了三個階段,分別是輸入(Input )、處理(Filter, 非必需)和輸出(Output) 。
三、Kibana簡介
一個針對ELasticsearch的開源分析及可視化平台
搜索、查看存儲在Elasticsearch索引中的數據
通過各種圖標進行高級數據分析和展示
功能
- Elasticsearch無縫與之集成
- 整合數據,進行復雜數據的分析
- 讓團隊內的人員更加受益
- 接口靈活,易於分享
- 配置簡單,可視化展示多數據源
- 簡單數據導出
四、ELK實戰分析
4.1、環境部署
部署兩台Elasticsearch做集群,node1做ELK日志分析系統
apache |
20.0.0.30 |
httpd |
Logstash |
||
node1 |
20.0.0.10 |
Elasticsearch |
Kinaba |
||
node2 |
20.0.0.20 |
Elasticsearch |
4.2、在node1和node2上進行Elasticsearch配置
只在node1上進行演示
[root@node1 ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch [root@node1 ~]# cd /etc/yum.repos.d/ [root@node1 yum.repos.d]# vi elasticsearch.repo [elasticsearch-2.x] name=Elasticsearch repository for 2.x packages baseurl=http://packages.elastic.co/elasticsearch/2.x/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enable=1 [root@node1 ~]# yum list #查看yum清單 [root@node1 ~]# yum install elasticsearch -y #安裝elasticsearch [root@node1 ~]# yum install java -y #安裝Java [root@node1 ~]# java -version #查看Java版本 openjdk version "1.8.0_131" OpenJDK Runtime Environment (build 1.8.0_131-b12) OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
4.3、修改配置文件
[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml cluster.name: elastic #17行 集群名稱 node.name: node1 #第23行 節點名稱 path.data: /data/es-data #第33行 工作目錄 path.logs: /var/log/elasticsearch bootstrap.memory_lock: true #第43行 防止交換swap分區刪除#無需修改 network.host: 0.0.0.0 #第54行 監聽網絡 http.port: 9200 # 第58 行 端口
4.4、創建目錄並開啟服務
[root@node1 ~]# mkdir -p /data/es-data [root@node1 ~]# chown -R elasticsearch:elasticsearch /data/es-data/ [root@node1 ~]# systemctl start elasticsearch.service [root@node1 ~]# netstat -anpt | grep 9200 tcp6 0 0 :::9200 :::* LISTEN 56017/java
4.4、測試
第二種方法測試
[root@node1 ~]# curl -i -XGET 'http://20.0.0.10:9200/_count?pretty' -d '{ > "query" { > "match_all"; {} > } > }' HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 95 { "count" : 0, "_shards" : { "total" : 0, "successful" : 0, "failed" : 0 } }
4.5。安裝elasticsearch插件
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head -> Installing mobz/elasticsearch-head... Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ... Downloading ...................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................DONE Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ... NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify) Installed head into /usr/share/elasticsearch/plugins/head
測試
4.6 、復合查詢
- 提交請求
刪除后查詢
4.7 elasticsearch 集群部署
[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml discovery.zen.ping.unicast.hosts: ["20.0.0.10", "20.0.0.20"] #69 行 單播列表自動發現機制 重啟服務 [root@node1 ~]# systemctl restart elasticsearch.service
4.8 測試
4.9、node1和node2日志提示不讓鎖內存
[root@node1 ~]# less /var/log/elasticsearch/elastic.log [2020-11-30 18:33:39,167][INFO ][cluster.metadata ] [node1] [index-demo] creating index, cause [auto(index api)], templates [], shards [5]/[1], mappings [] [2020-11-30 18:33:39,422][INFO ][cluster.routing.allocation] [node1] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[index-demo][2], [index-demo][3], [index-demo][3], [index-demo][2], [index-demo][0], [index-demo][0], [index-demo][4]] ...]). [2020-11-30 18:33:39,485][INFO ][cluster.metadata ] [node1] [index-demo] create_mapping [test] [2020-11-30 18:47:09,875][INFO ][node ] [node1] stopping ... [2020-11-30 18:47:09,907][INFO ][node ] [node1] stopped [2020-11-30 18:47:09,907][INFO ][node ] [node1] closing ... [2020-11-30 18:47:09,911][INFO ][node ] [node1] clo
修改配置文件
[root@node1 ~]# vi /etc/security/limits.conf # allow user 'elasticsearch' mlockall elasticsearch soft memlock unlimited elasticsearch hard memlock unlimited #在行尾插入
4.10 安裝監控組件
[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
測試
4.11 Logstash部署 (apache上部署)
1、安裝httpd 並開啟服務
yum -y install httpd [root@apache ~]# systemctl start httpd [root@apache ~]# netstat -anpt | grep httpd tcp6 0 0 :::80 :::* LISTEN 61700/httpd
[root@apache ~]# echo '<h1>this is my web!!!!!</h1>' > /var/www/html/index.html [root@apache ~]# curl http://localhost <h1>this is my web!!!!!</h1>
2、安裝秘鑰及logstash源
[root@apache ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch [root@apache ~]# vi /etc/yum.repos.d/logstash.repo [logstash-2.1] name=Logstash repository for 2.1.x packages baseurl=http://packages.elastic.co/logstash/2.1/centos gpgcheck=1 gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch enable=1 [root@apache ~]# yum -y install logstash
3、安裝java環境。
[root@apache ~]# yum -y install java [root@apache ~]# java -version openjdk version "1.8.0_272" OpenJDK Runtime Environment (build 1.8.0_272-b10) OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
ln -s /opt/logstash/bin/logstash /usr/local/bin/
4、logstash(apache)與elasticsearch(node)功能是否正常,做對接測試
[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{} }' Settings: Default filter workers: 1 Logstash startup completed www.aliyun.com 2020-11-30T14:27:35.958Z apache www.aliyun.com www.baidu.com 2020-11-30T14:27:41.062Z apache www.baidu.com
1 logstash命令選項解釋: 2 -f:指定logstash的配置文件,根據配置文件配置logstash 3 -e:后面跟着字符串,該字符串可以被當做logstash的配置(如果是“ ”,則默認使用stdin做輸入,stdout為輸出) 4 -t:測試配置文件是否正確,然后退出 5 #輸入采用標准輸入,輸出采用標准輸出
5、使用rubydebug顯示詳細輸出,codec為一種解碼器
[root@apache ~]# logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }' Settings: Default filter workers: 1 Logstash startup completed www.baidu.com { "message" => "www.baidu.com", "@version" => "1", "@timestamp" => "2020-11-30T14:29:58.952Z", "host" => "apache" }
6、使用logstash講信息輸出給elasticsearch
[root@apache ~]
# logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["20.0.0.10:9200"] } }' Settings: Default filter workers: 1 Logstash startup completed aaa bbb ccc
.7、打開瀏覽器輸入http://20.0.0.10:9200/_plugin/head/,查看索引信息,顯示新的數據信息說明輸出到elasticsearch成功
8、在apache主機中做對接配置,logstash收集系統日志
[root@apache ~]# chmod o+r /var/log/messages [root@apache ~]# ll /var/log/messages -rw----r--. 1 root root 1292470 11月 30 22:30 /var/log/messages [root@apache ~]# vi /etc/logstash/conf.d/system.conf input { #logstash輸入:從/var/log/messages輸入,類型為system,起始位 置為從頭開始 file { path => "/var/log/messages" type => "system" start_position => "beginning" } } output { #logstash輸出:輸出給elasticsearch(以IP地址指定位置) elasticsearch { hosts => ["20.0.0.10:9200"] index => "system-%{+YYY.MM.dd}" } } [root@apache ~]# systemctl restart logstash.service
9、打開瀏覽器輸入http://20.0.0.10:9200/_plugin/head/,查看索引信息,顯示新的索引信息代表logstash搜集到的日志信息成功輸出給elasticsearch
5、在node1主機上安裝kibana
[root@node1 ~]# tar zxf kibana-4.3.1-linux-x64.tar.gz [root@node1 ~]# mv kibana-4.3.1-linux-x64 kibana [root@node1 ~]# mv kibana /usr/local/ [root@node1 ~]# cd /usr/l lib/ lib64/ libexec/ local/ [root@node1 ~]# cd /usr/local/kibana/ vi config/kibana.yml //2行 kibana打開的端口 server.port: 5601 //5行 kibana偵聽的地址 server.host: "0.0.0.0" //12行 和elasticsearch建立聯系 elasticsearch.url: "http://20.0.0.10:9200" //20行 在elasticsearch中添加.kibana索引 kibana.index: ".kibana" [root@node1 kibana]# yum -y install screen [root@node1 kibana]# yum -y install screen 啟動kibana [root@node1 kibana]# /usr/local/kibana/bin/kibana log [22:41:10.966] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready log [22:41:10.998] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch log [22:41:11.018] [info][status][plugin:kbn_vislib_vis_types] Status changed from uninitialized to green - Ready log [22:41:11.041] [info][status][plugin:markdown_vis] Status changed from uninitialized to green - Ready log [22:41:11.050] [info][status][plugin:metric_vis] Status changed from uninitialized to green - Ready log [22:41:11.065] [info][status][plugin:spyModes] Status changed from uninitialized to green - Ready log [22:41:11.069] [info][status][plugin:statusPage] Status changed from uninitialized to green - Ready log [22:41:11.072] [info][status][plugin:table_vis] Status changed from uninitialized to green - Ready log [22:41:11.095] [info][listening] Server running at http://0.0.0.0:5601 log [22:41:16.085] [info][status][plugin:elasticsearch] Status changed from yellow to yellow - No existing Kibana index found log [22:41:18.763] [info][status][plugin:elasticsearch] Status changed from yellow to green - Kibana index ready
5.2 瀏覽器中登錄20.0.0.10:5601,首次登錄提示創建一個索引名字:
填入system-*,即對接系統日志文件名
5.3同時也能看到剛才在kibana.yml中添加的在elasticsearch中添加.kibana索引配置生效了(用於驗證kibana和elasticsearch是否成功連接)
5.4/ 添加logstash配置,收集apache服務的訪問日志和錯誤日志
中的數據
[root@apache ~]# cd /etc/logstash/conf.d/ [root@apache conf.d]# vi apache_log.conf input { file { path => "/etc/httpd/logs/access_log" type => "access" start_position => "beginning" } file { path => "/etc/httpd/logs/error_log" type => "error" start_position => "beginning" } } output { if [type] == "access" { elasticsearch { hosts => ["20.0.0.10:9200"] index => "apache_access-%{+YYY.MM.dd}" } } if [type] == "error" { elasticsearch { hosts => ["20.0.0.10:9200"] index => "apache_error-%{+YYY.MM.dd}" } } } [root@apache conf.d]# logstash -f apache_log.conf #指定logstash的配置文件,根據apache_log.conf配置logstash(-f可以不重啟加載)
5.5/ 瀏覽器訪問http://20.0.0.10:9200/_plugin/head/,查看apache日志文件的索引是否建立成功