ELK日誌平臺介紹
在搜索ELK資料的時候,發現這篇文章比較好,於是摘抄一小段:
日誌主要包括系統日誌、應用程式日誌和安全日誌。系統運維和開發人員可以通過日誌了解伺服器軟硬體信息、檢查配置過程中的錯誤及錯誤發生的原因。經常分析日誌可以了解伺服器的負荷,性能安全性,從而及時採取措施糾正錯誤。
通常,日誌被分散的儲存不同的設備上。如果你管理數十上百臺伺服器,你還在使用依次登錄每臺機器的傳統方法查閱日誌。這樣是不是感覺很繁瑣和效率低下。當務之急我們使用集中化的日誌管理,例如:開源的syslog,將所有伺服器上的日誌收集匯總。
集中化管理日誌後,日誌的統計和檢索又成為一件比較麻煩的事情,一般我們使用grep、awk和wc等Linux命令能實現檢索和統計,但是對於要求更高的查詢、排序和統計等要求和龐大的機器數量依然使用這樣的方法難免有點力不從心。
開源實時日誌分析ELK平臺能夠完美的解決我們上述的問題,ELK由ElasticSearch、Logstash和Kiabana三個開源工具組成。官方網站:https://www.elastic.co/cn/products/
---畫了一個ELK工作的原理圖-
如圖:Logstash收集AppServer產生的Log,並存放到ElasticSearch集群中,而Kibana則從ES集群中查詢數據生成圖表,再返回給Browser。
ELK平臺搭建
系統環境
System: Centos 7.3
ElasticSearch: 7.x
Logstash: 7.x
Kibana: 7.x
Filebeat: 7.x
Java: openjdk version "1.8"
ELK下載:https://www.elastic.co/downloads/
一、安裝JDK以及ELKyum源
下載JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html
本環境是下載的64位rpm包
#rpm -vih jdk-8u151-linux-x64.rpm
# 配置yum源,新建yum文件
Download and install the public signing key:
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/logstash.repo
# 加入以下內容
[logstash-7.x]
name=Elastic repository for 7.x packagesbaseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
# 執行指令
yum clean
二、先安裝Elasticsearch
yum安裝
yum install elasticsearch
配置ElasticSearch
vim /etc/elasticsearch/elasticsearch.yml
#這裡指定的是集群名稱,需要修改為對應的,開啟了自發現功能後,ES會按照此集群名稱進行集群發現
cluster.name: my-cluster
# 節點名稱
node.name: node-1
#數據目錄
path.data: /var/lib/elasticsearch
# log目錄
path.logs: /var/log/elasticsearch
#不在啟動的時候鎖定內存
bootstrap.memory_lock: false
#修改一下ES的監聽地址,這樣別的機器也可以訪問
network.host: 0.0.0.0
#默認的埠號
http.port: 9200
# - Discovery -
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
discovery.seed_hosts: ["node-1"]
# Bootstrap the cluster using an initial set of master-eligible nodes:
cluster.initial_master_nodes: ["node-1"]
#最後添加以下內容
#開啟跨域訪問支持,默認為 false
http.cors.enabled: true
#跨域訪問允許的域名地址
http.cors.allow-origin: "*"
*******************************************
在構建Elasticsearch(ES)多節點集群的時候,通常情況下只需要將elasticsearch.yml中的cluster.name設置成相同即可,ES會自動匹配並構成集群。但是很多時候可能由於不同的節點在不同的網段下,導致無法自動獲取集群。此時可以將啟用單播,顯示指定節點的發現。具體做法是在elasticsearch.yml文件中設置如下兩個參數:
discovery.zen.ping.multicast.enabled: false
discovery.zen.ping.unicast.hosts: ["192.168.0.11", "192.168.0.12"]
***********************************************
配置完成後啟動elasticsearch,並查看進程
systemctl start elasticsearch.service
ps -ef | grep elasticsearch
查看Elasticsearch的信息
#curl http://127.0.0.1:9200/
客戶端網頁訪問可能需要關掉防火牆 或 添加安全組策略:
Systemctl stop firewalld.service
注意事項:
修改系統參數,確保系統有足夠資源啟動Elasticsearch
設置內核參數
vim /etc/sysctl.conf
#增加以下參數
vm.max _map_count = 655360
執行以下命令,確保生效配置生效:
sysctl –p
設置資源參數
vim /etc/security/limits.conf
#修改
* soft nofile 65536
* hard nofile 131072
* soft nproc 65536
*hard nproc 131072
vim /etc/security/limits.d/20-nproc.conf
#設置elk用戶參數
elk soft nproc 65536
如何和elasticsearch交互
安裝elasticsearch-head插件,ElasticSearch-head是一個H5編寫的ElasticSearch集群操作和管理工具,可以對集群進行傻瓜式操作。
安裝docker鏡像或者通過github下載elasticsearch-head項目都是可以的,1或者2兩種方式選擇一種安裝使用即可
使用docker的集成好的elasticsearch-head
# docker run -p 9100:9100 mobz/elasticsearch-head:5
docker容器下載成功並啟動以後,運行瀏覽器打開http://localhost:9100/
2. 使用git安裝elasticsearch-head
# yum install -y npm
# git clone git://github.com/mobz/elasticsearch-head.git
#git 下載不了就用下面的地址下載
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
# 下載後解壓移動至此 /usr/local/elasticsearch-head
# cd elasticsearch-head
#npm install -g grunt
#由於國外鏡像太慢,使用的是國內鏡像代理安裝的
#npm install -g grunt-cli --registry=https://registry.npm.taobao.org --no-proxy
#npm install -g cnpm --registry=https://registry.npm.taobao.org
#cnpm install
# npm run start &
檢查埠是否起來
#netstat -antp | grep 9100
瀏覽器訪問測試是否正常
#http://IP:9100/
3. 增加自動啟動配置文件
vim /usr/local/elasticsearch-head/start.sh
#/bin/bash
sleep 2m # 開機後等待2分鐘,因為ELK啟動需要時間
cd /usr/local/elasticsearch-head
/usr/bin/npm instll # 正常是不需要再次安裝,不知為什麼有時需要重新安裝才可以用
sleep 20s
/usr/bin/npm run start & # 後臺啟動服務
exit
# 再把此腳本添加自動啟動, /etc/rc.local 文件最後增加一行
/bin/sh /usr/local/elasticsearch-head/start.sh > /root/es_head.log &
三、filebeat、winlogbeat簡介和安裝配置
filebeat是基於原先 logstash-forwarder 的源碼開發而來,無需JAVA環境,運行起來更輕便,無疑是業務伺服器端的日誌收集工具。filebeat支持各Linux環境,同時支持Window環境。
winlogbeat 是專門收集Window系統日誌的收集工具。
filebeat主要配置就是這個配置文件了,設定好之後啟動服務就會自動從源拉取數據發送到指定位置,當數據為普通行數據時,filebeat會自動為其添加欄位信息,其中一項欄位 @timestamp 為filebeat讀取到這條數據的時間,默認格式為UTC時間,比中國大陸時間早8小時。
如果數據為json格式,而數據中已包含@timestamp欄位,filebeat處理時會把@timestamp欄位值替換為filebeat讀取到該行數據的當前UTC時間。
安裝:
官方下載:https://www.elastic.co/cn/products/beats
yum安裝Filebeat
yum install filebeat
啟動服務
systemctl start filebeat.service
源碼安裝Filebeat
下載:https://www.elastic.co/downloads/
解壓:tar -zxvf filebeat-7…….tar.gz
啟動
./filebeat -e -c filebeat.yml
-c:配置文件位置
-path.logs:日誌位置
-path.data:數據位置
-path.home:家位置
-e:關閉日誌輸出
-d 選擇器:啟用對指定選擇器的調試。 對於選擇器,可以指定逗號分隔的組件列表,也可以使用-d「*」為所有組件啟用調試.例如,-d「publish」顯示所有「publish」相關的消息。
後臺啟動filebeat
./filebeat -e -c filebeat.yml >/dev/null 2>&1 & 將所有標準輸出及標準錯誤輸出到/dev/null空設備,即沒有任何輸出
./filebeat -e -c filebeat.yml > filebeat.log &
2、安裝winlogbeat,windows下filebeat安裝也類似,直接解壓即可
這裡 下載winlogbeat 解壓到 C:\Program Files
重新命名文件夾為winlogbeat
用管理員身份打開windows的 powershell
運行以下命令來安裝服務
PS C:\Users\Administrator> cd 'C:\Program Files\Winlogbeat'
PS C:\Program Files\Winlogbeat> .\install-service-winlogbeat.ps1
如果在系統上禁用了腳本執行,則需要為當前會話設置執行策略以允許腳本運行。
PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-winlogbeat.ps1.
或者可以使用以下命令來關閉一些安全防護,輸入命令後按Y確認
PS C:\Program Files\Winlogbeat> set-executionpolicy remotesigned
PS C:\Program Files\Winlogbeat> set-executionpolicy Bypass
官方文檔:
https://www.elastic.co/guide/en/beats/winlogbeat/current/winlogbeat-installation.html
3、配置文件說明
filebeat 配置:
filebeat.inputs:
- type: log
enabled: true
paths: - E:/DB_Backup/Logs/*.txt
fields: log_topic: system_log
name: "192.168.0.232"
tags: ["dbbackup_log"]
multiline.pattern: '(^NEW)' multiline.negate: true multiline.match: after multiline.max_lines: 15
- type: log enabled: true
paths: - E:/DB_Backup/WEBEIMS/*.txt
encoding: GB2312
fields: log_topic: system_log
name: "192.168.0.232"
tags: ["dbbackup_log"]
multiline.pattern: '(^RAR)' multiline.negate: true multiline.match: after multiline.max_lines: 15
filebeat.config.modules: path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings: index.number_of_shards: 1
output.logstash: hosts: ["192.168.0.77:5044"]
processors: - add_host_metadata: ~ - add_cloud_metadata: ~
- drop_fields: fields: ["agent.type","agent.version","agent.ephemeral_id","agent.id","agent.hostname","ecs.version","host.id","host.os.family","host.os.kernel","host.os.platform","host.os.version","host.architecture","host.containerized","host.os.codename","log.offset"]
logging.level: infowinlogbeat 配置:
winlogbeat.event_logs: - name: Application ignore_older: 72h - name: Security - name: System
fields: log_topic: system_log
setup.template.settings: index.number_of_shards: 1
name: "192.168.0.232"
tags: ["windows_system_log"]
output.logstash: hosts: ["192.168.0.77:5044"]
processors: - add_host_metadata: ~ - add_cloud_metadata: ~
- drop_fields: fields: ["agent.name","agent.type","agent.version","host.os.family","host.os.kernel","host.os.platform","host.os.version","host.architecture","host.containerized","host.os.codename","log.offset","agent.ephemeral_id","agent.id","host.id","agent.hostname","host.os.family","host.os.kernel","host.os.platform","host.os.version","host.architecture","host.containerized","host.os.codename","agent.ephemeral_id","agent.id","host.id","agent.hostname","ecs.version","winlog.api","winlog.computer_name","winlog.event_data.AccountDomain","winlog.event_data.LogonID","winlog.event_data.SessionName","winlog.provider_guid","winlog.provider_name","winlog.record_id","winlog.task","winlog.version","winlog.process.pid","winlog.process.thread.id"]
logging.level: info未完待續……