為什麼使用ELK日志分析:
一般我們需要進行日志分析場景:直接在日志檔案中 grep、awk 就可以獲得自己想要的資訊。但在規模較大的場景中,此方法效率低下,面臨問題包括日志量太大如何歸檔、文本搜尋太慢怎麼辦、如何多元度查詢。需要集中化的日志管理,所有伺服器上的日志收集彙總。常見解決思路是建立集中式日志收集系統,将所有節點上的日志統一收集,管理,通路。
準備兩台測試伺服器:
Centos7(1),Centos(2)
運作記憶體:最少2G以上把
一,配置環境
Centos(1):操作 →
添加本地DNS解析:
vim /etc/hosts
192.168.xxx.111 aaa
192.168.xxx.110 bbb
修改檔案描述符:
vim /etc/systemd/system.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535
vim /etc/systemd/user.conf
配置時間同步:
vim /etc/chrony.conf
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.xxx.111
開啟所有同一網段來我這裡同步時間:
allow 192.168.xxx.0/24
開啟共享
local stratum 10
儲存重新開機服務,開機自啟:
systemctl restart chronyd
systemctl enable chronyd
Centos(2):操作 →
安裝同步時間插件:
yum -y install ntpdate
同步時間(最少兩次)
[root@localhost ~]# ntpdate 192.168.xxx.111
23 Apr 09:40:35 ntpdate[3299]: adjust time server 192.168.xxx.111 offset -0.009580 sec
23 Apr 09:40:48 ntpdate[3300]: adjust time server 192.168.xxx.111 offset -0.006129 sec
檢視同步路徑
[root@localhost ~]# which ntpdate
/usr/sbin/ntpdate
編寫計劃任務,實作自動化同步時間(一分鐘執行一次)
crontab -e
/usr/sbin/ntpdate 192.168.xxx.111
檢視是否成功
[root@localhost ~]# tail -f /var/log/cron
Apr 23 09:41:01 localhost run-parts(/etc/cron.daily)[3313]: finished logrotate
Apr 23 09:41:01 localhost run-parts(/etc/cron.daily)[3301]: starting man-db.cron
Apr 23 09:41:05 localhost run-parts(/etc/cron.daily)[5888]: finished man-db.cron
Apr 23 09:41:05 localhost anacron[3285]: Job `cron.daily' terminated
Apr 23 09:45:14 localhost crontab[5892]: (root) BEGIN EDIT (root)
Apr 23 09:47:14 localhost crontab[5892]: (root) REPLACE (root)
Apr 23 09:47:14 localhost crontab[5892]: (root) END EDIT (root)
Apr 23 09:48:01 localhost CROND[5906]: (root) CMD (/usr/sbin/ntpdate 192.168.xxx.111)
這樣我們的ELK環境搭建完成!(下面就是ELK部署了)
Centos(1):操作 →
安裝java環境:
yum -y install java-1.8.0-openjdk
下載下傳elasticsearch安裝包并安裝
elasticsearch-6.6.0.rpm
rpm -ivh elasticsearch-6.6.0.rpm
修改elasticsearch配置檔案
vim /etc/elasticsearch/elasticsearch.yml
cluster.name: ccc
node.name: aaa
network.host: 192.168.xxx.111
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.xxx.111"]
啟動elasticsearch服務并設定開機自起:
[root@localhost ~]# systemctl restart elasticsearch
[root@localhost ~]# systemctl enable elasticsearch
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
等待 9200,9300端口啟動
tcp6 0 0 192.168.xxx.111:9200 ::: LISTEN 6535/java
tcp6 0 0 192.168.xxx.111:9300 ::: LISTEN 6535/java
通路頁面是否成功
http://192.168.xxx.111:9200/
ELK通路日志
Centos(2):操作 →
下載下傳logstash安裝包并安裝
rpm -ivh logstash-6.6.0.rpm
配置搜集系統核心日志
vim /etc/logstash/conf.d/syslog.conf
input {
file {
path => "/var/log/messages"
type => "systemlog"
start_position => "beginning"
stat_interval => "2"
}
output {
elasticsearch {
hosts => ["192.168.xxx.111:9200"]
index => "logstash-systemlog-%{+YYYY.MM.dd}"
啟動logstash并設定開機自起
[root@localhost ~]# systemctl restart logstash
[root@localhost ~]# systemctl enable logstash
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.
等待9600端口啟動
[root@localhost ~]# ss -tnl
LISTEN 0 50 ::ffff:127.0.0.1:9600 :::*
測試是否成功
curl -XGET 'localhost:9600/?pretty'
{
"host" : "localhost.localdomain",
"version" : "6.6.0",
"http_address" : "127.0.0.1:9600",
"id" : "8df16d18-b09d-4ccb-a2fc-f470bb48b1e0",
"name" : "localhost.localdomain",
"build_date" : "2019-01-24T12:13:56+00:00",
"build_sha" : "e4390be7e4d511af9d48bc503c9dcc15b03d3bce",
"build_snapshot" : false
下載下傳kibana安裝包并安裝
rpm -ivh kibana-6.6.0-x86_64.rpm
修改kibana配置檔案
vim /etc/kibana/kibana.yml
server.port: 5601
server.host: "192.168.xxx.111"
elasticsearch.hosts: ["http://192.168.xxx.111:9200"]
啟動kibana并設定開機自啟
[root@aaa ~]# systemctl restart kibana
[root@aaa ~]# systemctl enable kibana
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.
等待5601端口啟動
[root@aaa ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 192.168.40.111:5601 :
http://192.168.xxx.111:5601/
發現沒有索引
解決:
在Centos(2)添權重限
chmod 644 /var/log/messages
這樣我們再次重新整理頁面
我們可以添加索引
這樣呢我們的ELK監控系統日志就完成了!