ELK日志收集


ELK學習

一、ELK介紹

1.什么是ELK

ELK是三個軟件
1.E:elasticsearch		java程序		存儲,查詢日志
2.L: logstash			java程序		收集、過濾日志
3.K: kibana				java程序		提供web服務,將數據頁面化

4.F: filebeat			go			收集、過濾日志

2.ELK作用

1.收集: 收集所有服務器的日志
2.傳輸: 把日志穩定的傳輸到ES或者其他地方
3.存儲: ES能有效快速的存儲日志數據
4.分析: 通過web頁面分析數據
5.監控: 監控集群架構

3.ELK優點

1.處理方式靈活:elasticsearch是實時全文索引,具有強大的搜索功能
2.配置相對簡單:elasticsearch全部使用JSON 接口,logstash使用模塊配置,kibana的配置文件部分更簡單。
3.檢索性能高效:基於優秀的設計,雖然每次查詢都是實時,但是也可以達到百億級數據的查詢秒級響應。
4.集群線性擴展:elasticsearch和logstash都可以靈活線性擴展
5.前端操作絢麗:kibana的前端設計比較絢麗,而且操作簡單

4.為什么使用ELK

#收集所有的日志
web服務日志
業務服務日志
系統日志

#統計、分析:
1.統計訪問量
2.統計訪問量前10的IP
3.站點訪問次數最多的URL
4.查詢一上午以上三個值
5.查詢一下午以上三個值
6.對比一下上下午用戶訪問量
7.對比這一周,每天用戶增長還是減少

二、ELK搭建

1.ES搭建

1)時間同步

[root@db01 ~]# yum install -y ntpdate
[root@db01 ~]# ntpdate time1.aliyun.com
  1. 安裝java環境
#上傳
[root@db01 ~]# rz jdk-8u181-linux-x64.rpm

#安裝
[root@db01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm
  1. es安裝
# 1.上傳或下載包
[root@db01 ~]# rz elasticsearch-6.6.0.rpm
下載地址:https://www.elastic.co/downloads/elasticsearch

# 2.安裝
[root@db01 ~]# rpm -ivh elasticsearch-6.6.0.rpm

# 3.根據提示繼續操作
[root@db01 ~]# systemctl daemon-reload
[root@db01 ~]# systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
[root@db01 ~]# systemctl start elasticsearch.service

# 4.啟動失敗,查看日志
[2020-08-10T10:38:56,170][ERROR][o.e.b.Bootstrap          ] [node-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
#說明內存未鎖定

# 5.修改內存限制,內存鎖定需要進行配置需要2g以上內存,否則會導致無法啟動elasticsearch。
[root@db01 ~]# vim /usr/lib/systemd/system/elasticsearch.service
[Service]
... ...
LimitMEMLOCK=infinity				                         # 添加此行在service中
[root@linux-elk1 ~]# vim /etc/elasticsearch/jvm.options                  # 一般設置內存大小為實際物理內存大小的一半即可
-Xms2g
-Xmx2g     #最小和最大內存限制,為什么最小和最大設置一樣大?參考:https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html

2.搭建 Logstash

  1. 說明
# 注意:
你要收集誰的日志,就在誰身上裝logstash

1)安裝java環境

1.上傳java包
2.安裝Java環境

2)時間同步

[root@web01 ~]# ntpdate time1.aliyun.com

3)安裝Logstash

1.上傳包
[root@web01 ~]# rz logstash-6.6.0.rpm

2.安裝
[root@web01 ~]# rpm -ivh logstash-6.6.0.rpm

3.授權
[root@web01 ~]# chown -R logstash.logstash /usr/share/logstash/

#啟動程序(無需啟動,后面調用啟動文件再一起使用)
/usr/share/logstash/bin/logstash

3.logstash介紹

1)輸入輸出插件介紹

INPUT、OUTPUT插件
INPUT:插件使Logstash收集指定源的日志
OUTPUT:插件將事件數據發送到特定的目的地
INPUT支持事件源 OUTPUT支持輸出源 CODEC編解碼器支持編碼
azure_event_hubs(微軟雲事件中心) elasticsearch(搜索引擎數據庫) avro(數據序列化)
beats(filebeat日志收集工具) email(郵件) CEF(嵌入式框架)
elasticsearch(搜索引擎數據庫) file(文件) es_bulk(ES中的bulk api)
file(文件) http(超文本傳輸協議) Json(數據序列化、格式化)
generator(生成器) kafka(基於java的消息隊列) Json_lines(便於存儲結構化)
heartbeat(高可用軟件) rabbitmq(消息隊列 OpenStack) line(行)
http_poller(http api) redis(緩存、消息隊列、NoSQL) multiline(多行匹配)
jdbc(java連接數據庫的驅動) s3*(存儲) plain(純文本,事件間無間隔)
kafka(基於java的消息隊列) stdout(標准輸出) rubydebug(ruby語法格式)
rabbitmq(消息隊列 OpenStack) tcp(傳輸控制協議)
redis(緩存、消息隊列、NoSQL) udp(用戶數據報協議)
s3*(存儲)
stdin(標准輸入)
syslog(系統日志)
tcp(傳輸控制協議)
udp(用戶數據報協議)

2)Logstash輸入輸出測試

#配置環境變量
[root@web01 ~]# vim /etc/profile.d/logstash.sh
export PATH=/usr/share/logstash/bin/:$PATH

#收集標准輸入到標准輸出測試
[root@web01 ~]# logstash -e 'input { stdin {} } output { stdout {} }'

#測試輸入
123456
{
	#時間戳
    "@timestamp" => 2020-08-13T01:34:24.430Z,
    	  #主機
          "host" => "web01",
      #版本
      "@version" => "1",
       #內容
       "message" => "123456"
}

#收集標准輸入到標准輸出指定格式
[root@web01 ~]# logstash -e 'input { stdin {} } output { stdout { codec => rubydebug } }'
123456
{
       "message" => "123456",
      "@version" => "1",
    "@timestamp" => 2020-08-13T01:39:40.837Z,
          "host" => "web01"
}

3)Logstash收集標准輸入到文件

#收集標准輸入到文件
[root@web01 ~]# logstash -e 'input { stdin {} } output { file { path => "/tmp/test.txt" } }'

#收集標准輸入到文件
[root@web01 ~]# logstash -e 'input { stdin {} } output { file { path => "/tmp/test_%{+YYYY-MM-dd}.txt" } }'

4)Logstash收集標准輸入到ES

#收集標准輸入到ES
[root@web01 ~]# logstash -e 'input { stdin {} } output { elasticsearch { hosts => ["10.0.0.51:9200"] index => "test_%{+YYYY-MM-dd}" } }'

#隨便輸入些內容

#查看頁面

4.kibana搭建

1)安裝

# 安裝包路徑
es官網: https://www.elastic.co/ 

#上傳代碼包
[root@db01 ~]# rz kibana-6.6.0-x86_64.rpm

#安裝
[root@db01 ~]# rpm -ivh kibana-6.6.0-x86_64.rpm

# 修改配置文件
[root@db01 ~]# vim /etc/kibana/kibana.yml
[root@db01 ~]# grep "^[a-z]" /etc/kibana/kibana.yml
#進程的端口
server.port: 5601
#監聽地址
server.host: "10.0.0.51"
#指定ES的地址
elasticsearch.hosts: ["http://10.0.0.51:9200"]
#kibana也會創建索引
kibana.index: ".kibana"

# 啟動kibana
[root@db01 ~]# systemctl start kibana.service

#驗證
[root@db01 ~]# netstat -lntp       
tcp        0      0 10.0.0.51:5601          0.0.0.0:*               LISTEN      88636/node

3)kibana頁面

1.時間區域
2.日志列表區域
3.搜索區域
4.數據展示區

三、Logstash使用

Logstash是一個開源的數據收集引擎,可以水平伸縮,而且logstash整個ELK當中擁有最多插件的一個組件,其可以接收來自不同來源的數據並統一輸出到指定的且可以是多個不同目的地。

1.logstash的配置文件

#默認的配置文件
/etc/logstash/logstash.yml

#一般不使用,只有用system管理時才使用

2.收集文件中的日志到文件

1)配置

[root@web01 ~]# vim /etc/logstash/conf.d/message_file.conf(文件名隨意寫,只需啟動時調用就行)
input {
  file {
    path => "/var/log/messages"
    start_position => "beginning"			# 這里的beginning是指定位置點從文件開始讀取
  }
}
output {
  file {
    path => "/tmp/message_file_%{+YYYY-MM-dd}.log"
  }
}

2)啟動

#檢測配置
[root@web01 ~]# logstash -f /etc/logstash/conf.d/message_file.conf -t

#啟動
[root@web01 ~]# logstash -f /etc/logstash/conf.d/message_file.conf

3)查看是否生成文件

[root@web01 tmp]# ll
total 4
-rw-r--r-- 1 root root 1050 Aug 13 11:24 message_file_2020-08-13.log

3.收集文件中的日志到ES

1)配置

# 在輸出時可以只寫集群中的一個ip,但是以防只寫一個ip容易出現單點服務器故障無法和集群其他機器通訊,所以這里寫多個ip,視情況而定。
[root@web01 ~]# vim /etc/logstash/conf.d/message_es.conf
input {
  file {
    path => "/var/log/messages"
    start_position => "beginning"
  }
}
output {
  elasticsearch {
    hosts => ["10.0.0.51:9200","10.0.0.52:9200","10.0.0.53:9200"]
    index => "message_es_%{+YYYY-MM-dd}"
  }
}

2)啟動

[root@web01 ~]# logstash -f /etc/logstash/conf.d/message_es.conf

4.啟動多個logstash收集日志

1)創建多個數據目錄

# 默認在啟動logstash時會產生數據文件,當不指定數據文件時,會在指定路徑生成數據,
當啟動多個logstash收集日志時會發現第一個logstash可以正常啟動,當第二個logstash啟動時卻無法啟動,所以需要為不同的logstash啟動時指定不同的數據目錄。

# 創建數據目錄
[root@web01 ~]# mkdir /data/logstash/{message_file,message_es} -p

#授權
[root@web01 ~]# chown -R logstash.logstash /data/

2)啟動時指定數據目錄

[root@web01 ~]# logstash -f /etc/logstash/conf.d/message_es.conf --path.data=/data/logstash/message_es &
[1] 18693
[root@web01 ~]# logstash -f /etc/logstash/conf.d/message_file.conf --path.data=/data/logstash/message_file &
[2] 18747

3)驗證

查看文件和ES頁面

5.一個logstash收集多個日志

1)配置

# 這里為收集多個日志文件到不同文件中
[root@web01 ~]# vim /etc/logstash/conf.d/more_file.conf
input {
  file {
    type => "messages_log"
    path => "/var/log/messages"
    start_position => "beginning"
  }
  file {
    type => "secure_log"
    path => "/var/log/secure"
    start_position => "beginning"
  }
}
output {
  if [type] == "messages_log" {
    file {
      path => "/tmp/messages_%{+YYYY-MM-dd}"
    }
  }
  if [type] == "secure_log" {
    file {
      path => "/tmp/secure_%{+YYYY-MM-dd}"
    }
  }
}

6.收集nginx和tomcat的日志

[root@web01 tmp]# vim /etc/logstash/conf.d/message_file.conf 
input {										# 輸入
  file {									# 事件源為文件
    type => "nginx_log"						                # 因為有收集兩個文件日志,通過這里打個標簽,名字隨意定
    path => "/var/log/nginx/access.log"		                                # 收集nginx的日志路徑
    start_position => "beginning"			                        # 監控文件為持續監控	
    sincedb_path => "/dev/null"				                        # 配合beginning使用,從日志文件開頭開始監控
  }
  file {									# 這里也是一樣,監控的是tomcat日志
    type => "tomcat_log"
    path => "/usr/local/tomcat/logs/localhost_access_log*.txt"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}
output {									# 輸出
  if [type] == "nginx_log" {				                        # 和input的type配合,如果是nginx日志文件就執行如下
    elasticsearch {							        # 指定輸出源為es
      hosts => ["10.0.0.51:9200"]			                        # es集群中任意地址
      index => "message_nginx_%{+YYYY-MM-dd}"	                                # 數據文件格式
    }
  }
  if [type] == "tomcat_log" {				                        # 和上面nginx一樣,這里是tomcat的輸出信息
    elasticsearch {
      hosts => ["10.0.0.51:9200"]
      index => "message_tomcat_%{+YYYY-MM-dd}"
    }
  }
}

7.kibana展示數據

1)kibana中根據索引名,和es索引建立綁定

2) 搜索已經存在的索引名

3)配置選擇時間戳

4) 在發現中,找到已經綁定的索引

5) 可對最近的日志按不同時間進行查看

6) 可將左側中的字段添加至右側進行顯示


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM