內容要點:
一、ELK日誌分析系統簡介
二、搭建ELK日誌分析系統
一、ELK 日誌分析系統簡介:
日誌服務器:
提高安全性;
集中存放日誌;
缺陷:對日誌的分析困難
ELK日誌處理步驟:
將日誌進行集中格式化;
將日誌格式化(logstash)並輸出到 Elasticsearch;
對格式化後的數據進行索引和存儲(Elasticsearch);
前端數據的展示(Kibana)
E:Elasticsearch,提供了一個分佈式多用戶能力的全文搜索引擎
L:Logstash,一款強大的數據處理工具,可以實現數據傳輸、格式處理、格式化輸出
數據輸入、數據加工(如過濾,改寫等)以及數據輸出
K:Kibana,一個針對Elasticsearch的開源分析及可視化平臺
搜索、查看存儲在Elasticsearch索引中的數據
通過各種圖表進行高級數據分析及展示
二、搭建 ELK 日誌分析系統:
主機 | 操作系統 | 主機名 | IP地址 | 主要軟件 |
---|---|---|---|---|
服務器 | Centos7.4 | node1 | 192.168.50.142 | Elasticsearch、Kibana |
服務器 | Centos7.4 | node2 | 192.168.50.139 | Elasticsearch |
服務器 | Centos7.4 | apache | 192.168.50.141 | Logstash Apache |
第一步:先配置 elasticsearch 環境
(1)修改兩臺主機名,分別是:node1 和 node2
(2)修改 hosts 文件:
vim /etc/hosts
添加以下主機名和主機IP地址(兩臺node都需要):
192.168.50.142 node1
192.168.50.139 node2
(3)防火牆都關閉
systemctl stop firewalld.service
setenforce 0
第二步:部署安裝 elasticsearch 軟件(兩臺節點都需要)
(1)安裝:
rpm -ivh elasticsearch-5.5.0.rpm //安裝
systemctl daemon-reload //重新加載服務配置文件
systemctl enable elasticsearch.service //設置爲開機自啓動
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
(2)修改配置文件:
注意:第二臺節點服務器配置和第一臺一樣,注意修改一下節點名和ip地址即可。
vim /etc/elasticsearch/elasticsearch.yml
17/ cluster.name: my-elk-cluster //集羣名字(自定義)
23/ node.name: node-1 //節點名字
33/ path.data: /data/elk_data //數據存放路徑
37/ path.logs: /var/log/elasticsearch/ //日誌存放路徑
43/ bootstrap.memory_lock: false //不在啓動的時候鎖定內存
55/ network.host: 0.0.0.0 //提供服務綁定的IP地址(本機地址)
59/ http.port: 9200 //端口
68/ discovery.zen.ping.unicast.hosts: ["node1", "node2"] //集羣發現通過單播實現
(3)創建數據存放路徑並授權:
mkdir -p /data/elk_data
chown elasticsearch:elasticsearch /data/elk_data/
(4)開啓服務:
systemctl start elasticsearch.service
netstat -natp | grep 9200
1、使用瀏覽器打開 http://192.168.50.142:9200 下面是節點信息
{
"name" : "node1",
"cluster_name" : "my-elk-cluster",
"cluster_uuid" : "47bm_xHBSfSvB-mg5qaLWg",
"version" : {
"number" : "5.5.0",
"build_hash" : "260387d",
"build_date" : "2017-06-30T23:16:05.735Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
2、使用瀏覽器 http://192.168.50.142:9200/_cluster/health?pretty ###檢查集羣健康情況
{
"cluster_name" : "my-elk-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
3、使用瀏覽器 http://192.168.50.142:9200/_cluster/state?pretty ###檢查羣集狀態信息
{
"cluster_name" : "my-elk-cluster",
"version" : 3,
"state_uuid" : "vU5I5cttQiiedAu38QwWEQ",
"master_node" : "3wHS1VEBQ_q0FxZs2T5IiA",
"blocks" : { },
"nodes" : {
"V0n8BHRfS1CA75dbo312HA" : {
"name" : "node2",
"ephemeral_id" : "2rC6LV1qQcaZNEFRc8uIBg",
"transport_address" : "192.168.50.139:9300",
"attributes" : { }
},
"3wHS1VEBQ_q0FxZs2T5IiA" : {
"name" : "node1",
"ephemeral_id" : "d2nLeY3RSYaI7g7jpzNkMA",
"transport_address" : "192.168.50.142:9300",
"attributes" : { }
}
},
"metadata" : {
"cluster_uuid" : "47bm_xHBSfSvB-mg5qaLWg",
"templates" : { },
"indices" : { },
"index-graveyard" : {
"tombstones" : [ ]
}
},
"routing_table" : {
"indices" : { }
},
"routing_nodes" : {
"unassigned" : [ ],
"nodes" : {
"3wHS1VEBQ_q0FxZs2T5IiA" : [ ],
"V0n8BHRfS1CA75dbo312HA" : [ ]
}
}
}
第三步:安裝 elasticsearch-head 插件
(1)安裝依賴包:
yum install gcc gcc-c++ make -y
(2)編譯安裝 node 組件:
tar zvxf node-v8.2.1.tar.gz -C /opt/
cd /opt/node-v8.2.1/
./configure
make -j3 //這步耗時較長,耐心等待
make install
(3)安裝 phantomjs 前端框架:
tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /opt/
cd phantomjs-2.1.1-linux-x86_64/bin
cp phantomjs /usr/local/bin/
(4)安裝 elasticsearch-head 數據可視化工具:
tar zvxf elasticsearch-head.tar.gz -C /opt/
cd /opt/elasticsearch-head/
npm install
(5)修改主配置文件:
vim /etc/elasticsearch/elasticsearch.yml
末尾插入以下兩行代碼:
http.cors.enabled: true
http.cors.allow-origin: "*"
(6)啓動 elasticsearch-head
cd /opt/elasticsearch-head/
npm run start & //放在後臺運行
此時,可以檢查一下 9100和9200 這個兩個端口的狀態:
netstat -lnupt |grep 9100
netstat -lnupt |grep 9200
(7)使用瀏覽器輸入 http://192.168.50.142:9100/ 可以看到羣集很健康是綠色
在Elasticsearch 後面的欄目中輸入http://192.168.50.142:9200
然後點連接會發現: 集羣健康值: green (0 of 0)
◉node1信息動作
★node2信息動作
(8)打開瀏覽器輸入 http://192.168.50.139:9100/ 可以看見羣集很健康是綠色
在Elasticsearch後面的欄目中輸入http://192.168.50.139:9200
然後點連接會發現:集羣健康值: green (0 of 0)
◉node1信 息動作
★node2信息動作
第四步:創建索引
可以直接新建索引:
也可以輸入以下命令創建索引:
curl -XPUT '192.168.50.142:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
//索引名爲 index-demo,類型爲test
瀏覽器刷新一下,就會看到剛剛創建的索引信息,可以看出索引默認被分片5個,並且有一個副本
第五步:安裝 logstash 並做一些日誌蒐集輸出到 elasticsearch中
(1)修改主機名
hostnamectl set-hostname apache
(2)安裝Apache服務:
systemctl stop firewalld.service
setenforce 0
yum install httpd -y
systemctl start httpd.service
(3)安裝 logstash
rpm -ivh logstash-5.5.1.rpm
systemctl start logstash
systemctl enable logstash
ln -s /usr/share/logstash/bin/logstash /usr/local/bin/ //創建軟連接到bin目錄下
(4)logstash(Apache)與 elasticsearch(node)功能是否正常,做對接測試:
可以用 logstash 這個命令測試:
[root@apache bin]# logstash
-f:可以指定 logstash的配置文件,根據配置文件配置 logstash
-e:後面跟着字符串,該字符串可以被當做 logstash 的配置(如果是“”,則默認使用stdin作爲輸入、stdout作爲輸出)
-t:測試配置文件是否正確,然後退出
(5)輸入採用標準輸入、輸出採用標準輸出:
logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.220.136:9200"] } }'
此時,瀏覽器訪問 http://192.168.50.142:9200/ 查看索引信息,就會多出 logstash-2019.12.17
(6)登錄Apache主機,做對接配置:
logstash 配置文件主要由三部分組成:input、output、filter(這個視情況做不做)
給日誌文件授權:
chmod o+r /var/log/messages
ll /var/log/messages
-rw----r--. 1 root root 488359 12月 17 14:52 /var/log/messages
創建並編輯配置文件:
vim /etc/logstash/conf.d/system.conf
input {
file {
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["192.168.50.142:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
重啓服務:
systemctl restart logstash.service
(7)瀏覽器查看索引信息:會多出 system-2019.12.17
第六步:node1主機安裝 kibana
rpm -ivh kibana-5.5.1-x86_64.rpm
cd /etc/kibana/
cp kibana.yml kibana.yml.bak
vim kibana.yml
修改,開放以下功能:
server.port: 5601 //開發端口
server.host: "0.0.0.0" //監聽所有
elasticsearch.url: "http://192.168.50.142:9200"
kibana.index: ".kibana"
重啓服務:
systemctl start kibana.service
(1)瀏覽器訪問:192.168.50.142:5601
接下來在可視化界面中創建一個索引名:system-* (對接系統日誌文件)
(2)對接 Apache 主機的 Apache 日誌文件 (包括正常訪問的、錯誤的)
cd /etc/logstash/conf.d/
vim 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 => ["192.168.220.136:9200"]
index => "apache_access-%{+YYYY.MM.dd}"
}
}
if [type] == "error"{
elasticsearch {
hosts => ["192.168.220.136:9200"]
index => "apache_error-%{+YYYY.MM.dd}"
}
}
}
重啓服務:
/usr/share/logstash/bin/logstash -f apache_log.conf
再在可視化界面上, 創建兩個索引:
1、apache_access-
2、apache_error-