title: ELK系列之一:安裝和基本使用
categories: Linux
tags:
- ELK
timezone: Asia/Shanghai
date: 2019-02-02
ELK介紹
需求背景:
業(yè)務(wù)發(fā)展越來越龐大,服務(wù)器越來越多
各種訪問日志、應(yīng)用日志、錯誤日志量越來越多,導(dǎo)致運維人員無法很好的去管理日志
開發(fā)人員排查問題,需要到服務(wù)器上查日志,不方便
運營人員需要一些數(shù)據(jù),需要我們運維到服務(wù)器上分析日志
為什么要用到ELK:
一般我們需要進行日志分析場景:直接在日志文件中 grep、awk 就可以獲得自己想要的信息。
但在規(guī)模較大也就是日志量多而復(fù)雜的場景中,此方法效率低下,
面臨問題包括日志量太大如何歸檔、文本搜索太慢怎么辦、如何多維度查詢。
需要集中化的日志管理,所有服務(wù)器上的日志收集匯總。
常見解決思路是建立集中式日志收集系統(tǒng),將所有節(jié)點上的日志統(tǒng)一收集,管理,訪問。
大型系統(tǒng)通常都是一個分布式部署的架構(gòu),不同的服務(wù)模塊部署在不同的服務(wù)器上,問題出現(xiàn)時,
大部分情況需要根據(jù)問題暴露的關(guān)鍵信息,定位到具體的服務(wù)器和服務(wù)模塊,
構(gòu)建一套集中式日志系統(tǒng),可以提高定位問題的效率。
一個完整的集中式日志系統(tǒng),需要包含以下幾個主要特點:
收集-能夠采集多種來源的日志數(shù)據(jù)
傳輸-能夠穩(wěn)定的把日志數(shù)據(jù)傳輸?shù)街醒胂到y(tǒng)
存儲-如何存儲日志數(shù)據(jù)
分析-可以支持 UI 分析
警告-能夠提供錯誤報告,監(jiān)控機制
ELK簡介:
ELK是三個開源軟件的縮寫,分別為:Elasticsearch 、 Logstash以及Kibana , 它們都是開源軟件。
不過現(xiàn)在還新增了一個Beats,它是一個輕量級的日志收集處理工具(Agent)
Beats占用資源少,適合于在各個服務(wù)器上搜集日志后傳輸給Logstash,官方也推薦此工具,
目前由于原本的ELK Stack成員中加入了 Beats 工具所以已改名為Elastic Stack。
為什么被采集端使用 Filebeat ,而不用原來的 Logstash 呢?
原因很簡單,資源消耗比較大。
由于 Logstash 是跑在 JVM 上面,資源消耗比較大
后來作者用 GO 寫了一個功能較少但是資源消耗也小的輕量級的 Agent 叫 Logstash-forwarder。
后來作者加入 elastic.co 公司, Logstash-forwarder 的開發(fā)工作給公司內(nèi)部 GO 團隊來搞,最后命名為 Filebeat。
Filebeat 需要部署在每臺應(yīng)用服務(wù)器上,可以通過 Salt 來推送并安裝配置。
整體架構(gòu)
Elasticsearch :分布式搜索引擎。具有高可伸縮、高可靠、易管理等特點??梢杂糜谌臋z索、結(jié)構(gòu)化檢索和分析,并能將這三者結(jié)合起來。Elasticsearch 基于 Lucene 開發(fā),現(xiàn)在使用最廣的開源搜索引擎之一,Wikipedia 、StackOverflow、Github 等都基于它來構(gòu)建自己的搜索引擎。
Kibana :可視化化平臺。它能夠搜索、展示存儲在 Elasticsearch 中索引數(shù)據(jù)。使用它可以很方便的用圖表、表格、地圖展示和分析數(shù)據(jù)。
Logstash :數(shù)據(jù)收集處理引擎。支持動態(tài)的從各種數(shù)據(jù)源搜集數(shù)據(jù),并對數(shù)據(jù)進行過濾、分析、豐富、統(tǒng)一格式等操作,然后存儲以供后續(xù)使用。
Filebeat:輕量級數(shù)據(jù)收集引擎?;谠?Logstash-fowarder 的源碼改造出來。換句話說:Filebeat就是新版的 Logstash-fowarder,也會是 ELK Stack 在 Agent 的第一選擇。
環(huán)境
[root@centos181002 httpd]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@centos181001 home]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)
Elastic Stack 6.6.0
第一步:系統(tǒng)初始化設(shè)置
1.關(guān)閉系統(tǒng)默認(rèn)防火墻(支持REHL\CentOS6和7)
setenforce 0
sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/config
which systemctl && systemctl stop firewalld
which systemctl && systemctl disable firewalld
which systemctl && systemctl stop iptables || service iptables stop
which systemctl && systemctl disable iptables || chkconfig iptables off
2.安裝JDK環(huán)境
Elasticsearch requires Java 8 or later. Use the official Oracle distribution or an open-source distribution such as OpenJDK.
官方建議使用Oracle JDK1.8.0.131或更高,這里我們使用的是1.8.0.192
# 解壓并重命名
tar vxf jdk-8u192-linux-x64.tar.gz
mv jdk1.8.0_192 jdk
# 修改環(huán)境變量并提前備份原始文件
/bin/grep 666666 /etc/profile && /bin/cp /etc/profile.666666 /etc/profile || /bin/cp /etc/profile /etc/profile.666666
cat <<EOF >>/etc/profile
export JAVA_HOME=/home/jdk
export CLASSPATH=.:\${JAVA_HOME}/jre/lib/rt.jar:\${JAVA_HOME}/lib/dt.jar:\${JAVA_HOME}/lib/tools.jar
export PATH=\$PATH:\${JAVA_HOME}/bin
EOF
source /etc/profile
第二步:安裝Elasticsearch
配置文件:/etc/elasticsearch/
啟動日志:/var/log/messages
默認(rèn)日志:/var/log/elasticsearch
官方安裝說明:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
1.下載并安裝
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.0.rpm
rpm -ivh elasticsearch-6.6.0.rpm
2.修改配置文件
# 1.修改監(jiān)聽IP和監(jiān)聽端口
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
vim /etc/elasticsearch/elasticsearch.yml
network.host: 11.11.11.61
http.port: 9200
# 2.為elasticsearch設(shè)置jdk路徑,否則啟動時候會報錯
cp /etc/sysconfig/elasticsearch /etc/sysconfig/elasticsearch.bak
vim /etc/sysconfig/elasticsearch
JAVA_HOME=/home/jdk
3.啟動服務(wù)
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service
4.檢查程序是否正常運行。
# 1.查看服務(wù)狀態(tài)是否正常
systemctl status elasticsearch.service
# 2.查看監(jiān)聽端口是否正常(elasticsearch會監(jiān)聽9200和9300兩個端口)
# 雖然我們這里是單機模式,不過9300一樣會正常監(jiān)聽
# 9200則是數(shù)據(jù)傳輸時用的
# 9300端口是集群通信用的
netstat -lntp |grep java
# 3.查看進程是否正常
ps aux | grep elasticsearch
# 4.檢查9200端口返回狀態(tài)
# 也可以通過瀏覽器此地址測試:http://11.11.11.61:9200/
curl -X GET "11.11.11.61:9200/"
[root@centos181001 elasticsearch]# curl -X GET "11.11.11.61:9200/"
{
"name" : "iI1f5lo",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "1QFZh0mDQHWmW5uJJnXixw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
使用瀏覽器顯示出來的也是一堆字符串,我們希望這些信息能以圖形化的方式顯示出來,那就需要安裝kibana來為我們展示這些數(shù)據(jù)了。
第三步:安裝Kibana
配置文件:/etc/kibana/
官方安裝說明:https://www.elastic.co/guide/en/kibana/6.6/index.html
1.下載并安裝
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.6.0-x86_64.rpm
rpm -ivh kibana-6.6.0-x86_64.rpm
2.修改配置文件
# 1.修改Kibana服務(wù)監(jiān)聽IP、監(jiān)聽端口、配置elasticsearch地址
# elasticsearch為es服務(wù)器的ip,如果是集群則配置該集群中主節(jié)點的ip
cp /etc/kibana/kibana.yml /etc/kibana/kibana.yml.bak
vim /etc/kibana/kibana.yml
server.port: 5601
server.host: "11.11.11.61"
elasticsearch.hosts: ["http://11.11.11.61:9200"]
3.啟動并設(shè)置開機自動啟動
systemctl enable kibana
systemctl stop kibana
systemctl start kibana
systemctl status kibana
4.檢查程序是否正常
# 1.檢查默認(rèn)監(jiān)聽端口
# 由于kibana是使用node.js開發(fā)的,所以進程名稱為node
netstat -lntp |grep 5601
# 2.查看進程
ps aux | grep kibana
5.打開瀏覽器訪問管理頁面
打開瀏覽器里進行訪問,由于我們并沒有安裝x-pack,所以此時是沒有用戶名和密碼的,可以直接訪問的。
到此我們的kibana就安裝完成了,很簡單,接下來就是安裝logstash,不然kibana是沒法用的。
http://11.11.11.61:5601
到此我們的kibana就安裝完成了,很簡單,接下來就是安裝logstash,不然kibana是沒法用的。
第四步:安裝Logstash
程序如果啟動錯誤或者端口沒有正常監(jiān)聽,可以到/var/log/message查看日志
1.下載安裝
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.6.0.rpm
rpm -ivh logstash-6.6.0.rpm
2.安裝以后先不要啟動服務(wù),先配置Logstash收集syslog日志并測試
1.創(chuàng)建文件并編輯
cat <<EOF >/etc/logstash/conf.d/syslog.conf
input { # 定義日志源
syslog {
type => "system-syslog" # 定義類型
port => 10514 # 定義監(jiān)聽端口
}
}
output { # 定義日志輸出
stdout {
codec => rubydebug # 將日志輸出到當(dāng)前的終端上顯示
}
}
EOF
cat /etc/logstash/conf.d/syslog.conf
2.檢查配置文件是否有錯誤
/usr/share/logstash/bin/logstash \
--path.settings /etc/logstash/ \
-f /etc/logstash/conf.d/syslog.conf \
--config.test_and_exit
--path.settings 用于指定logstash的配置文件所在的目錄
-f 指定需要被檢測的配置文件的路徑
--config.test_and_exit 指定檢測完之后就退出,不然就會直接啟動了
# 顯示Configuration OK代表配置沒問題
[root@centos181001 conf.d]# /usr/share/logstash/bin/logstash \
> --path.settings /etc/logstash/ \
> -f /etc/logstash/conf.d/syslog.conf \
> --config.test_and_exit
Sending Logstash logs to /var/log/logstash which is now configured via log4j2.properties
[2019-02-02T03:06:08,558][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.queue", :path=>"/var/lib/logstash/queue"}
[2019-02-02T03:06:08,582][INFO ][logstash.setting.writabledirectory] Creating directory {:setting=>"path.dead_letter_queue", :path=>"/var/lib/logstash/dead_letter_queue"}
[2019-02-02T03:06:09,714][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
Configuration OK
[2019-02-02T03:06:21,137][INFO ][logstash.runner ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
3.配置kibana服務(wù)器的ip以及配置的監(jiān)聽端口:
# 1.編輯并在#### RULES ####位置增加以下行(IP地址為本機監(jiān)聽IP地址和端口)
vim /etc/rsyslog.conf
*.* @@11.11.11.61:10514
# 2.重啟rsyslog,讓配置生效
systemctl restart rsyslog
4.指定配置文件,啟動logstash測試
/usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf
# 等待出現(xiàn)以下信息
[root@centos181001 conf.d]# /usr/share/logstash/bin/logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf
Sending Logstash logs to /var/log/logstash which is now configured via log4j2.properties
[2019-02-02T03:16:57,098][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
[2019-02-02T03:16:57,192][INFO ][logstash.runner ] Starting Logstash {"logstash.version"=>"6.6.0"}
[2019-02-02T03:16:57,259][INFO ][logstash.agent ] No persistent UUID file found. Generating new UUID {:uuid=>"b98c03ce-ca73-4b08-a392-653913d80883", :path=>"/var/lib/logstash/uuid"}
[2019-02-02T03:17:11,739][INFO ][logstash.pipeline ] Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
[2019-02-02T03:17:12,794][INFO ][logstash.pipeline ] Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x1b532bc9 run>"}
[2019-02-02T03:17:12,892][INFO ][logstash.inputs.syslog ] Starting syslog udp listener {:address=>"0.0.0.0:10514"}
[2019-02-02T03:17:12,913][INFO ][logstash.inputs.syslog ] Starting syslog tcp listener {:address=>"0.0.0.0:10514"}
[2019-02-02T03:17:13,011][INFO ][logstash.agent ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
[2019-02-02T03:17:13,751][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
5.檢查10514端口是否已被監(jiān)聽
netstat -antp | grep 10514
6.用另外機器ssh登錄到這臺服務(wù)器,看看是否有日志輸出
應(yīng)該可以看到很多以JSON的格式打印的收集到的日志
3.配置Logstash
以上只是測試的配置,這一步我們需要重新改一下配置文件,讓收集的日志信息輸出到es服務(wù)器中,而不是當(dāng)前終端
1.編輯配置文件
cat <<EOF >/etc/logstash/conf.d/syslog.conf
input {
syslog {
type => "system-syslog"
port => 10514
}
}
output {
elasticsearch {
hosts => ["11.11.11.61:9200"] # 定義es服務(wù)器的ip
index => "system-syslog-11.11.11.61-%{+YYYY.MM}" # 定義索引
}
}
EOF
cat /etc/logstash/conf.d/syslog.conf
2.為Logstash手動指定JAVA_HOME,否則啟動程序會報錯
cp /usr/share/logstash/bin/logstash.lib.sh /usr/share/logstash/bin/logstash.lib.sh.bak
vim /usr/share/logstash/bin/logstash.lib.sh
JAVA_HOME=/home/jdk
3.檢測配置文件是否有錯誤
/usr/share/logstash/bin/logstash \
--path.settings /etc/logstash/ \
-f /etc/logstash/conf.d/syslog.conf \
--config.test_and_exit
4.將以下兩個文件夾所有者改為logstash,否則啟動的時候會因為沒有權(quán)限報錯
chown -R logstash:logstash /var/log/logstash/
chown -R logstash:logstash /var/lib/logstash/
5.修改Logstash默認(rèn)監(jiān)聽IP
cp /etc/logstash/logstash.yml /etc/logstash/logstash.yml.bak
vim /etc/logstash/logstash.yml
http.host: "11.11.11.61"
4.測試OK后啟動并設(shè)置開機自動啟動
systemctl enable logstash
systemctl stop logstash
systemctl start logstash
systemctl status logstash
5.檢查是否是否正常啟動
# 1.檢查進程
ps aux |grep logstash
# 2.查看服務(wù)狀態(tài)
systemctl status logstash
# 3.查看監(jiān)聽端口
netstat -antp | grep -E '9600|10514'
6.可以使用以下命令查看索引
curl '11.11.11.61:9200/_cat/indices?v'
# 這里可以看到上邊我們配置的:
# index => "system-syslog-11.11.11.61-%{+YYYY.MM}" # 定義索引
第五步:登錄Kibana查看
到這里我們已經(jīng)可以實現(xiàn)了將本地的message日志歸檔并可使用Kibana查看
1.登錄Kibana
http://11.11.11.61:5601
2.需要設(shè)置索引才能夠查看。
- Management
- Create index pattern
- 選擇剛才添加索引名稱,可以使用通配符,比如:system-syslog*,然后點擊【Next step】
- 選擇【@timestamp】后點擊【Create index pattern】
3.查看日志
- Discover
在這里可以跟在搜索框里搜索任意想要查找的日志內(nèi)容。
第六步:使用Filebeat采集日志
之前也介紹過beats是ELK體系中新增的一個工具,它屬于一個輕量的日志采集器,以上我們使用的日志采集工具是logstash,但是logstash占用的資源比較大,沒有beats輕量,所以官方也推薦使用beats來作為日志采集工具。而且beats可擴展,支持自定義構(gòu)建。
這里我們新增一臺測試服務(wù)器11.11.11.62來安裝Filebeat并將日志發(fā)送到ES服務(wù)器
1.關(guān)閉系統(tǒng)默認(rèn)防火墻(支持REHL\CentOS6和7)
setenforce 0
sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/config
which systemctl && systemctl stop firewalld
which systemctl && systemctl disable firewalld
which systemctl && systemctl stop iptables || service iptables stop
which systemctl && systemctl disable iptables || chkconfig iptables off
2.下載安裝
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.6.0-x86_64.rpm
rpm -ivh filebeat-6.6.0-x86_64.rpm
3.測試配置
1.先將配置文件備份
cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak
2.過濾掉#開頭的備注內(nèi)容和空行,方便修改查看
cat /etc/filebeat/filebeat.yml.bak | grep -v '^#' | grep -v '^ #' | grep -v '^$' > /etc/filebeat/filebeat.yml
3.修改配置文件
[root@centos181002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
# 本行注釋掉
# enabled: false
# 這里是日志路徑可以有多條
paths:
# - /var/log/*.log
- /var/log/messages
#- c:\programdata\elasticsearch\logs\*
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
# 這里先注釋掉,設(shè)定輸出到控制臺先測試
#output.elasticsearch:
# hosts: ["localhost:9200"]
# 新增這里,讓日志輸出到控制臺,以測試
output.console: # 指定在終端上輸出日志信息
enable: true
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
4.修改完以上配置后執(zhí)行以下命令,查看是否有日志輸出,終端內(nèi)有打印日志說明配置正常
/usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml
4.修改為正式配置文件并啟動程序
1.修給配置文件為以下內(nèi)容
[root@centos181002 ~]# cat /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
# 本行注釋掉
# enabled: false
# 這里是日志路徑可以有多條
paths:
# - /var/log/*.log
- /var/log/messages
#- c:\programdata\elasticsearch\logs\*
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
# 這里設(shè)定ES服務(wù)器地址
output.elasticsearch:
hosts: ["11.11.11.61:9200"]
# 新增這里,讓日志輸出到控制臺,以測試,測試部分注釋掉
#output.console: # 指定在終端上輸出日志信息
# enable: true
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
2.啟動Filebeat服務(wù)并設(shè)置開機自動啟動
systemctl enable filebeat
systemctl stop filebeat
systemctl start filebeat
systemctl status filebeat
3.查看服務(wù)是否正常
# 1.查看服務(wù)狀態(tài)
systemctl status filebeat
# 2.查看進程
ps aux | grep filebeat
# 3.查看ES服務(wù)器上是否新增了一個以filebeat-6.0.0開頭的索引
# 這就代表filesbeat和es能夠正常通信了
curl '11.11.11.61:9200/_cat/indices?v'
4.登錄ES管理頁面,配置索引,并查看日志。
- 登錄頁面:http://11.11.11.61:5601
- Management
- Index Patterns
- Create index pattern
- 填寫Index pattern(索引模式,支持通配符,比如:filebeat-6.6.0*)后【Next step】
- Time Filter field name選擇【@timestamp】后點擊【Create index pattern】
- 到Discover可以查看系統(tǒng)收集到的日志。
5.在以上基礎(chǔ)上增加httpd日志搜集
因為filebeat日志路徑是支持多條的,所以這里只需要將需要采集的日志路徑填入配置文件即可。
1.安裝httpd
yum install -y httpd
2.修改filebeat配置文件以搜集httpd日志
httpd默認(rèn)日志路徑:/var/log/httpd/
vim /etc/filebeat/filebeat.yml
在paths:處增加以下內(nèi)容:
- /var/log/httpd/
3.重啟Filebeat服務(wù)并啟動httpd服務(wù)
systemctl stop filebeat
systemctl start filebeat
systemctl start httpd
systemctl status filebeat httpd
4.打開瀏覽器訪問httpd服務(wù),并到ES服務(wù)器查看是否有httpd日志被采集。
第六步:漢化
作者主頁:https://github.com/anbai-inc
結(jié)語:到這里ELK的架設(shè)和簡單使用基本OK,更深入的使用和其他內(nèi)容將抽時間繼續(xù)整理。
附錄:
所用到離線包下載:https://pan.baidu.com/s/16uVQL-d5HdgjajzCpY2e3g
官方安裝:https://www.elastic.co/guide/en/elastic-stack/current/installing-elastic-stack.html
中文官網(wǎng):https://www.elastic.co/cn/products
ELKstack中文指南:https://elkguide.elasticsearch.cn/logstash/examples/nginx-access.html
不錯的文章(億級 ELK 日志平臺構(gòu)建實踐):http://blog.51cto.com/13527416/2117141
搭建ELK日志分析平臺(上)—— ELK介紹及搭建 Elasticsearch 分布式集群:http://blog.51cto.com/zero01/2079879
搭建ELK日志分析平臺(下)—— 搭建kibana和logstash服務(wù)器:http://blog.51cto.com/zero01/2082794