需求背景前端
因爲公司的後臺服務有三臺,每當後臺服務運行異常,須要看日誌排查錯誤的時候,都必須開啓3個ssh窗口進行查看,研發們以爲很不方便,因而便有了統一日誌收集與查看的需求。 java
這裏,我用ELK集羣,經過收集三臺後臺服務的日誌,再統一進行日誌展現,實現了這一需求。node
固然,當前只是進行了簡單的日誌採集,若是後期相對某些日誌字段進行分析,則能夠經過logstash以及Kibana來實現。linux
部署環境nginx
系統:CentOS 7web
軟件:redis
elasticsearch-6.1.1數據庫
logstash-6.1.1bootstrap
kibana-6.1.1bash
下載地址:https://www.elastic.co/cn/products
搭建步驟
一:elasticsearch:
elasticsearch是用於存儲日誌的數據庫。
下載elasticsearch軟件,解壓:
1
2
|
# tar -zxvf elasticsearch-6.1.1.tar.gz
# mv elasticsearch-6.1.1 /opt/apps/elasticsearch
|
因爲elasticsearch建議使用非root用戶啓動,使用root啓動會報錯,故需建立一個普通用戶,並進行一些簡單配置:
1
2
3
4
5
6
|
# useradd elk
# vi /opt/apps/elasticsearch/config/elasticsearch.yml
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled:
true
http.cors.allow-origin:
"*"
|
啓動,並驗證:
1
2
3
4
5
6
7
|
# su - elk
$
nohup
/opt/apps/elasticsearch/bin/elasticsearch
&
# netstat -ntpl | grep 9200
tcp 0 0 0.0.0.0:9200 0.0.0.0:* LISTEN 6637
/java
#curl 'localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1514858033 09:53:53 elasticsearch yellow 1 1 241 241 0 0 241 0 - 50.0%
|
若是報錯:OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N 說明須要加CPU和內存
bootstrap checks failed
max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解決方案
一、vi /etc/sysctl.conf
設置fs.file-max=655350
vm.max_map_count=262144
保存以後sysctl -p使設置生效
二、vi /etc/security/limits.conf 新增
* soft nofile 655350
* hard nofile 655350
三、從新使用SSH登陸,再次啓動elasticsearch便可。
二:logstash
logstash用於收集各服務器上的日誌,而後把收集到的日誌,存儲進elasticsearch。收集日誌的方式有不少種,例如結合redis或者filebeat,這裏咱們使用redis收集的方式。
安裝logstash:
1
2
3
|
在全部服務器上:
# tar -zxvf logstash-6.1.1.tar.gz
# mv logstash-6.1.1 /opt/apps/logstash/
|
配置後臺服務器,收集相關的日誌:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
在三臺後臺服務器上新建logstash文件,配置日誌收集:
# vi /opt/conf/logstash/logstash.conf
input {
file
{
#指定type
type
=>
"web_stderr"
#匹配多行的日誌
codec => multiline {
pattern =>
"^[^0-9]"
what =>
"previous"
}
#指定本地的日誌路徑
path => [
"/opt/logs/web-stderr.log"
]
sincedb_path =>
"/opt/logs/logstash/sincedb-access"
}
file
{
type
=>
"web_stdout"
codec => multiline {
pattern =>
"^[^0-9]"
what =>
"previous"
}
path => [
"/opt/logs/web-stdout.log"
]
sincedb_path =>
"/opt/logs/logstash/sincedb-access"
}
#收集nginx日誌
file
{
type
=>
"nginx"
path => [
"/opt/logs/nginx/*.log"
]
sincedb_path =>
"/opt/logs/logstash/sincedb-access"
}
}
output {
#指定輸出的目標redis
redis {
host =>
"xx.xx.xx.xx"
port =>
"6379"
data_type =>
"list"
key =>
"logstash"
}
}
|
配置elk日誌服務器上的logstash,從redis隊列中讀取日誌,並存儲到elasticsearch中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# vi /opt/conf/logstash/logstash-server.conf
#配置從redis隊列中讀取收集的日誌
input {
redis {
host =>
"xx.xx.xx.xx"
port =>
"6379"
type
=>
"redis-input"
data_type =>
"list"
key =>
"logstash"
threads => 10
}
}
#把日誌輸出到elasticsearch中
output {
elasticsearch {
hosts =>
"localhost:9200"
index =>
"logstash-%{type}.%{+YYYY.MM.dd}"
}
#這裏把日誌收集到本地文件
file
{
path =>
"/opt/logs/logstash/%{type}.%{+yyyy-MM-dd}"
codec => line {
format
=>
"%{message}"
}
}
}
|
啓動logstash進程:
1
2
3
4
|
後臺服務器:
# nohup /opt/apps/logstash/bin/logstash -f /opt/conf/logstash/logstash.conf --path.data=/opt/data/logstash/logstash &
elk日誌服務器:
# nohup /opt/apps/logstash/bin/logstash -f /opt/conf/logstash/logstash-server.conf --path.data=/opt/data/logstash/logstash-server &
|
三:kibana
kibana用於日誌的前端展現。
安裝、配置kibana:
1
2
3
4
5
6
7
8
|
# tar -zxvf kibana-6.1.1-linux-x86_64.tar.gz
# mv kibana-6.1.1-linux-x86_64 /opt/apps/kibana
配置elasticsearch連接:
# vi /opt/apps/kibana/config/kibana.yml
server.port: 5601
server.host:
"0.0.0.0"
#配置elasticsearch連接:
elasticsearch.url:
"http://localhost:9200"
|
啓動kibana:
1
|
nohup
/opt/apps/kibana/bin/kibana
&
|
訪問kibana:
能夠根據咱們在logstash中配置的type,建立索引:
能夠根據咱們建立的索引,進行查看(這裏查看nginx日誌):
後記:
固然了,結合logstash和kibana不僅僅僅能實現收集日誌的功能,經過對字段的匹配、篩選以及結合kibana的圖標功能,能對咱們想要的字段進行分析,實現相應的數據報表等。