|
搭建ELK
: `4 H8 L9 x+ h% z2 BELK是由elasticsearch、logstash、kibana三个开源软件组成的一个组合体,ELK是elastic公司公司研发的一套完整的日志收集、分析和展示的企业级解决方案,在这三个软件当中,每个软件用于完成不同的功能,官方域名为elastic.io,ELK stack的主要优点:
6 A7 n. \) E8 ?- x; `- R
/ G, L- A/ I! A: |/ [9 M处理方式灵活:elasticsearch是实时全文索引,具有强大的搜索功能配置相当简单:elasticsearch的API全部使用JSON接口,logstash使用模块配置,kibana的配置文件部分更简单检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿数据的查询秒级响应。集群线性扩展:elasticsearch和logstash都可以灵活线性扩展前端操作绚丽:kibana的前端设计比较绚丽,而且操作简单
0 V8 l/ {, B0 D; _9 r" yElasticsearch - p4 k9 L/ }: C+ |7 a' O
elasticsearch是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索、支持分布式可实现高可用、提供API接口,可以处理大规模日志数据,比如nginx、tomcat、系统日志等功能。, [( H" q' b S2 V5 g
* R# h2 M Q. ]( Y1 f7 q9 Q! f" belasticsearch的特点:
/ v' D; r, `0 j, W8 F# W ; q6 V" C5 f5 C3 Q* H
实时收索、实时分析分布式架构、实时文件存储文档导向,所有对象都是文档高可用,易扩展,支持集群,分片与复制接口友好,支持json
9 C+ Y8 _( u+ E& f4 {部署elasticsearch
+ \0 A( m& |* N- F4 DGitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine,基于java开发- n( _/ x9 T& K" W' K
) A9 a; }1 S. u- c% t* u% Tcentos系统关闭服务器的防火墙和selinux,ubuntu关闭防火墙,保持各服务器时间同步
* F% D- x- X/ i) o8 [ C: W& U @
; E2 a) {, P" C7 @; x; i服务器1:172.20.22.24
- {: t4 P8 X" @9 g& z4 J, n5 p
* ~* Y1 [2 g% a2 I! `- B8 t服务器2:172.20.22.27
1 `4 r# B0 h, ?8 c$ G 4 k) u' b2 d, `. ^4 P
服务器3:172.20.22.28
7 A" V* R% C. V1 n" f7 `' ~3 J " o, m; J1 a- J7 p6 ?
###ubuntu! J, H* I$ b+ _# V. w
# apt install -y ntpdate
$ T/ j! Q, A/ e' y7 c/ B: h$ x# rm -f /etc/localtime
$ X) c, L# p' O! w" k4 B# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime% G) n1 j0 n) I
# hwclock --systohc. i8 Z" I* R- N
# ntpdate -u ntp1.aliyun.com
. v* \- G% t e( s5 @###设置内核参数
4 B! o- A: D' N& j* y' ]4 t# vim /etc/security/limits.conf
. s) m r7 @3 O" `, F7 F* soft nofile 500000
) `3 I8 I) a; p$ W; J* ?* hard nofile 500000* O" |$ R. S. f+ ~5 Q6 V# W6 \6 d
# vim /etc/security/limits.d/20-nproc.conf - Z: y! A% Z4 I3 S# D, N# O: m5 Z
* soft nproc 4096# d0 D- h2 E y# B( J
elasticsearch soft nproc unlimited
# p, v2 [+ I* r! O' k( wroot soft nproc unlimited
|4 }* }: w: f1 o2 q% B###安装jdk; v* s' M! U% K
# apt install -y openjdk-8-jdk
6 Y% y! R, u U: z
8 u9 K5 U6 u# u, U: w###每个节点都安装
4 g4 w0 J& o5 U3 L$ n4 V# ls -lrt elasticsearch-7.12.1-amd64.deb
) _+ Y% w( J5 r* m+ M5 W) c# dpkg -i elasticsearch-7.12.1-amd64.deb6 p) w6 I& b ]1 G8 X9 ~
###节点1配置文件4 D7 X% e% [$ \) g% l
# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml4 t) }6 C' {4 r0 L# R
cluster.name: m63-elastic #集群名称& u8 W$ N. H+ x1 B
node.name: node1 #当前节点在集群内的节点名称( O& q8 k1 H: b3 D9 r
path.data: /data/elasticsearch #数据保存目录
1 |) Z. Z/ g( n3 }9 fpath.logs: /data/elasticsearch #日志保存目录( J3 S9 O9 N5 c/ ~
bootstrap.memory_lock: true #服务启动的时候锁定足够的内存,防止数据写入swap
$ g) {7 I4 ~+ X) unetwork.host: 172.20.22.24 #监听IP3 S/ }3 s& W! j
http.port: 9200 #监听端口9 D5 o- ^1 F$ ?
###集群中node节点发现列表
; J& t [, o+ o1 `& b# T4 [2 ldiscovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]# i+ o8 {$ p8 }7 n
###集群初始化哪些节点可以被选举为master0 B: O* G. x& a: B- t6 e
cluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]1 ?" b$ z7 B8 D9 J
action.destructive_requires_name: true6 Q4 Z* @6 k3 O
# mkdir /data/elasticsearch -p
' \( ~. y1 }# m$ @# chown -R elasticsearch. /data/elasticsearch: @2 x) C$ ]0 R! F m0 y6 X
# systemctl start elasticsearch.service% Y3 {3 A3 m8 Q2 S0 z( b3 p: p
###节点2
' \( h' B) O# i; C# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml) f" ]' M9 y0 r& r1 @
cluster.name: m63-elastic
4 u" L) j% j# y, N/ }0 z2 P) V. inode.name: node2
2 v1 F, H9 {- I( bpath.data: /data/elasticsearch0 }/ a% \) K) z2 A& |
path.logs: /data/elasticsearch, T4 h p; D. F' Q; ], N
network.host: 172.20.22.27
9 I- Y, z, V jhttp.port: 9200
7 m' f: L0 `( y! y. t2 I7 k# h, ldiscovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
3 y) j& N3 q: _" ^! ]3 d' c) g9 Jcluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
3 _- j+ m! O! @action.destructive_requires_name: true
$ \4 l: Q0 \& ~+ w, b' R# mkdir /data/elasticsearch -p; r! e% v1 r% j. _$ G
# chown -R elasticsearch. /data/elasticsearch
0 H! D( n* _$ `) p! R5 e# systemctl start elasticsearch.service
5 I8 G$ z9 ~" {7 N###节点3$ Q8 p3 }2 T0 g) ~* z% K
# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml
: p2 x6 U. K* j: z. O7 F9 }cluster.name: m63-elastic
6 G# }5 H# z3 X% B# {node.name: node35 E8 r" a7 d( b6 e6 P, h
path.data: /data/elasticsearch
3 j% D; ~0 M% I+ C$ B, s3 z8 o$ kpath.logs: /data/elasticsearch& {; e' i6 z# w& z- `9 X
network.host: 172.20.22.28
3 r: c. U# _3 Q( r* Ghttp.port: 9200
+ R0 V9 r4 p( P% Z5 tdiscovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
# a M6 u% @1 O. ~& Q; M5 x, tcluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]6 v9 f; ?4 C; p3 {
action.destructive_requires_name: true9 X r/ x z4 p9 p
# mkdir /data/elasticsearch -p# u* }' q% d+ H, g* n9 H
# chown -R elasticsearch. /data/elasticsearch, R# ~& e0 u5 k3 k. F* H
# systemctl start elasticsearch.service
/ T. Z5 }" `! h9 p$ u; z浏览器访问验证 " E' W- \' x4 o& G
http://$IP:9200% O& E! S6 r' Z% N* R1 q6 R* d
$ G. O! K( R) X6 H' C1 c2 y 8 H7 \/ G5 b/ x8 j9 c9 q
1 Y/ v/ F/ g5 V" A' P/ @$ HLogstash
9 r* _5 e- O5 {4 `3 JLogstash是一个具有实时传输能力的数据收集引擎,其可以通过插件实现日志收集和转发,支持日志过滤,支持普通log、自定义json格式的日志解析,最终把经过处理的日志发送给elasticsearch。, j4 r# e ^8 F4 g
8 [/ i. O2 p e$ {1 I部署Logstash
$ d: o- X7 m2 m! {% z8 R# y4 S* u1 fLogstash是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中用于最多插件的一个组件,其可以接收来自不同来源的数据并统一输出到指定的且可以是多个不同目的地
, o( M/ L4 w- V$ d. A$ Q# ^
8 j9 l4 {& y* a4 Z$ qhttps://github.com/elastic/logstash #GitHub
4 j$ l- q, w4 F( u/ q% B9 x1 g5 ^ , V) S5 A( P% m7 j
Elastic Stack and Product Documentation | Elastic0 n }, t* U4 K" w
" ]4 Y7 r' g2 g* y, G' y& H6 h
环境准备:关闭防火墙和selinux,并且安装java环境
8 G- q8 s+ b T' v
K3 D6 b. `) {6 ~ w& ]# apt install -y openjdk-8-jdk
' X; R: o+ H0 g4 Q8 o# ls -lrt logstash-7.12.1-amd64.deb
2 C8 i; R/ p+ N2 j& _% E. d% p# dpkg -i logstash-7.12.1-amd64.deb3 A% z8 I) z$ p, V$ d3 | X
###启动测试
0 w& |& |) g$ @0 b9 Y# /usr/share/logstash/bin/logstash -e 'input { stdin {} } output { stdout {}}' ##标准输入和标准输出
* ~ j [, a4 c/ z4 V3 Ehello world!~% ?9 [& |- Y6 t" j
{
( T, S9 ]9 n- }+ t4 h "@version" => "1",
# a2 v1 T# p% W3 } q "@timestamp" => 2022-04-13T06:16:32.212Z,* r* v/ \3 W- M: U
"host" => "jenkins-slave",
. X' q; p+ ?7 N: K) h" R/ O2 J& G "message" => "hello world!~"4 n5 \- i0 I; @/ B0 ^
}
" e+ H6 T" G% z###通过配置文件启动
$ \) i. J( t7 s- v4 {# cd /etc/logstash/conf.d/5 n; H& F _, s+ X7 l6 ]7 j) f
# cat test.conf # Y( C0 Z4 N5 ]5 o: W
input { - P" @: x# S) W; S
stdin {}
0 b1 C/ Y, F+ \4 k8 V}' u+ G5 i. @7 ]# e5 L9 X
output {
$ F! e% k8 S, L" P2 X4 A stdout {}
) A% x( G, O8 J}8 J T* Q8 R& W; b1 ~! s7 V& F
/ ]) }! `% I+ A###通过指定配置文件启动
0 z3 ]$ w% Z: n* S! y6 B* K# /usr/share/logstash/bin/logstash -f test.conf -t ##检查配置文件语法
; ^: f# z, i H1 w5 C% @, k# /usr/share/logstash/bin/logstash -f test.conf
$ u2 i4 F: ?1 W# n. @
+ S7 I" E( d0 `! e####输出到elasticsearch' B- @) P8 N5 q* U& g& @
# cat test.conf
) N- e' f6 K" Yinput { $ M% r! x9 J' `! I
stdin {}
' H0 s8 X! \$ p1 N* a& n0 O5 J% X}' u( x- L6 J/ j1 N
output {7 M$ |3 G$ ~9 q
#stdout {}
. }7 @8 I, i9 D0 N) g8 [( T elasticsearch {
' S3 y5 _4 ~5 I$ j9 Z hosts => ["172.20.22.24:9200"]( D6 Z0 w M! z6 o( }2 p8 P5 j
index => "magedu-m63-test-%{+YYYY.MM.dd}"8 R2 O S! M5 n. C) U
}
3 r( o, B7 X+ w' \* k k3 F) f! w5 e}2 Q2 w- D: g% C* M- E b
# /usr/share/logstash/bin/logstash -f test.conf; K5 d7 s3 B8 u& W
version1
; n! b6 B+ g; s2 H- {2 ]* E2 Nversion28 X8 v3 W" z% x8 P1 b+ ?2 m3 E; ]
version3
4 _- z& _: } A- ]4 ^7 s) S. Ftest1: @/ h4 N( ^. c, x. D( m
test2
' [7 t# l# n Y$ W, ltest3( N% O4 P3 ?& H0 R
. I0 g3 |) T5 P% S
####elasticsearch服务器查看收集到的数据# R0 j2 E- t; r3 T8 K
# ls -lrt /data/elasticsearch/nodes/0/indices/
5 g- s8 A2 F) l" `# {total 4
# g) d, b! V1 z3 P; Rdrwxr-xr-x 4 elasticsearch elasticsearch 4096 Apr 13 14:36 DyCv8w7mTleuAvlItAJlWA $ P/ [" r* G7 o" y+ z& m& ^
kibana $ `# `" k' L" H
kibana为elasticsearch提供一个查看数据的web界面,其主要是通过elasticsearch的API接口进行数据查找,并进行前端数据可视化的展现,另外还可以针对特定格式的数据生成相应的表格、柱状图、饼图等, a5 q. P5 q7 A4 c) H7 Y
7 P( N7 X3 _2 a1 }) I! l+ c- n, x
部署kibana G, p. j' N _/ x. u
# ls -lrt kibana-7.12.1-amd64.deb
# [& h1 K9 c$ n* J6 P# N# dpkg -i kibana-7.12.1-amd64.deb) H' [8 @: u1 O! v* D3 s# p1 S
# grep "^[^$|#]" /etc/kibana/kibana.yml
1 V4 f: N4 X. J% z" x' J7 {/ rserver.port: 5601% I- S. U# z; Q9 _
server.host: "172.20.22.24"% G* r/ o) G' ~. s" a
elasticsearch.hosts: ["http://172.20.22.27:9200"]3 Q; S/ E! `- X$ E: }) B& }' G
i18n.locale: "zh-CN": g6 k% q. _% A
# systemctl restart kibana 9 p% G! y, C8 ~2 S3 t# ?( [
浏览器访问http://172.20.22.24:56016 i2 D' a* y! d7 m7 g6 i: x4 v
* X) `% F4 X. k. d/ t* g `Stack Management-->索引模式-->创建索引模式
; c. x( r/ M4 h% m0 Z) d ]9 Y! z- B% x4 t, |5 y
* A: I- E2 h, j选择时间字段
" J' {4 R: W* B' L) |$ K" s
# a' r2 T3 N+ y5 Z3 \查看对应创建的索引日志信息. h1 x5 M$ [# J, {: o7 r: q
, Z1 h; v# m+ P. O8 P; G4 x, U
' c# I* u8 b) Y( s& q4 p2 d" l. p 7 I" S" z; T- S+ _4 y: X7 s2 r
收集tomcat日志
# _0 t9 w6 L) I" [# k F+ A- r收集tomcat服务器的访问日志以及tomcat错误日志进行实时统计,在kibana页面进行搜索展现,每台tomcat服务器要安装logstash负责收集日志,然后将日志转发给elasticsearch进行分析,再通过kibana在前端展现, Z: T" l% R: k- o" V2 t7 F/ h
& p: S. v7 ^4 X, Y% ?# ?) I7 p/ b
部署tomcat
6 h6 H: e3 w( |6 F' c9 F/ S& b: i####tomcat1,172.20.22.30
- O: F6 M+ G( C' k, z4 K1 ~3 @# apt install -y openjdk-8-jdk# P* R- D* C' w! t% i; K
# ls -lrt apache-tomcat-8.5.77.tar.gz 8 E1 G: z6 _& Y% n5 Y o6 ?
-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz+ H2 _0 X. y: S* y
# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src/
+ S6 Y L/ O, i/ e, _# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat0 R3 b9 k# I8 L% A. @# B$ B) x
# cd /usr/local/tomcat
! E4 G$ S+ \- K% J###修改tomcat日志格式为json6 F8 Z# y: C+ N( C7 a0 t
# vim conf/server.xml, v2 q8 a. t7 s' f
...., R$ Y* O# q& S9 `
. Z1 t9 Z' k/ u; Z....
( V% {3 T, d) O3 F' P- A8 {6 g' {# mkdir /usr/local/tomcat/webapps/myapp
% Q# R0 W x1 C* j# echo "web1 172.20.22.30" > /usr/local/tomcat/webapps/myapp/index.html
+ m0 n* p- I# ` \7 ^, L0 T9 [# ./bin/catalina.sh start
! t: e6 K) t* k( F: _$ y' A
% w' |( x- G1 U) `# M###访问测试
: }/ X4 A9 e& e$ d4 u5 e0 L# curl http://172.20.22.30:8080/myapp/1 |% J8 B5 h( J# b" t
###查看访问日志' g! y6 G% A1 |. E) d$ t% \/ C+ f
# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-13.log: @7 p" w. }$ m* Z
Y* o; l% o/ }( p( q
####tomcat2,172.20.22.261 Y3 y$ m; m- J/ W6 _
# apt install -y openjdk-8-jdk- R3 J0 k' H4 G8 K& A
# ls -lrt apache-tomcat-8.5.77.tar.gz
5 ]' M& r1 H, ~' P* w6 K- g* o-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz0 A; H+ I7 m$ J- L9 s, V
# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src/$ [( T6 K7 b; b) B% x9 O
# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
% a/ X8 o( }0 ?9 {2 `( \. E# cd /usr/local/tomcat
4 T( @. @3 x' w4 [ R$ X###修改tomcat日志格式为json- x3 P* p0 ?, ^( c" }3 X
# vim conf/server.xml
: L/ F9 K( U* I) a....& V! m1 u d6 J
1 I0 t9 z6 r" q1 [* `
....0 ^5 J1 x0 S1 j. ?/ i$ K$ Z
# mkdir /usr/local/tomcat/webapps/myapp( w/ m3 [* x2 j3 T
# echo "web2 172.20.22.26" > /usr/local/tomcat/webapps/myapp/index.html
& L. [4 a C' j: X; I# ./bin/catalina.sh start# x9 [1 T5 m6 R: B8 N% B
. H L0 ^5 Z! j# _1 V###访问测试
k( T- f) B$ w( N( ^- H+ ?# curl http://172.20.22.26:8080/myapp/" g) v K3 w2 k7 R: K
###查看访问日志
r: d* D' ]# K# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-14.log 0 c0 T; d5 {" w
部署logstash 2 N: _2 ]6 \6 w4 h$ J! A+ k
在tomcat服务器安装logstash收集tomcat和系统日志
4 w. m- j' F/ _( I
2 i+ Z* r" B" _# q3 s: e####tomcat1,172.20.22.30, k. W: u, V4 o; o. g. t: e
# ls -lrt logstash-7.12.1-amd64.deb2 `- _1 S& J6 } k
# dpkg -i logstash-7.12.1-amd64.deb/ N9 f$ A& x( A/ q/ H
# vim /etc/systemd/system/logstash.service1 v3 V5 i+ k I; {- N
...5 ]" }3 ? \6 Z1 {- w4 F
User=root. R1 F+ H9 u* m2 v
Group=root7 B9 p0 q" I: B/ c% P# _6 _) ?
...
; E) j8 s+ j4 J( D- a# cd /etc/logstash/conf.d
2 ?/ D7 C' [/ k) g/ e2 S# cat tomcat.conf" U) t" y0 v$ `
input {
4 o# n8 H/ i, _2 J' x( W- w5 o file {# o8 n9 t0 ~0 ?. q9 E0 A
path => "/usr/local/tomcat/logs/tomcat_access_log*.log"
+ t' G$ Y& ~( w; Q3 j# |" X type => "tomcat-log"
3 r6 l" K4 M: n4 C start_position => "beginning"
5 W3 x7 u" x& v* P stat_interval => "3"
# ~" y) e1 y( ?$ u2 y- M }
2 X p& A' |! T* x file {- j* D8 X+ T; v$ ~; r8 N5 B
path => "/var/log/syslog"* D% N) N5 G( i& S
type => "systemlog"
9 X( M) k+ ^! q H start_position => "beginning": t# i! ?1 i: k, B3 }
stat_interval => "3"
8 }: f* o9 Y4 z }
?2 m2 ~ y% O$ r$ U}
7 ]% j: P# ` _output {
: f/ i) w9 I: D7 |! a if [type] == "tomcat-log" {
2 Q) d. h; G3 P! Z5 D' J ?+ z; Q) Q elasticsearch {
4 R: h; U! P l, B" q4 g hosts => ["172.20.22.24:9200","172.20.22.27:9200"]- A9 {: t2 Q+ U
index => "elk-tomcat-%{+YYYY.MM.dd}"
+ x, b" X* d: H1 D8 c! F }}9 ?: e; B9 H/ B+ w8 m' x, i
if [type] == "systemlog" {
* U1 r6 H1 `& {7 d y' S elasticsearch {- |) x( K; @! S
hosts => ["172.20.22.27:9200","172.20.22.27:9200"]! C+ a# u5 m3 @, k3 l. r3 x
index => "elk-syslog-%{+YYYY.MM.dd}"
0 @6 m+ w1 E' y m5 ]% L z }}
! e' C' ?( k" _. N, M1 M}
( Y* _/ y; a/ B! I' \5 | e3 Y5 `6 G! b3 M( ?( U* T# b: B
# /usr/share/logstash/bin/logstash -f tomcat.conf -t
& B4 |0 a' q2 W) T% g9 ?# systemctl daemon-reload- }2 ^- D& O. c
# systemctl start logstash.service. H' Q; m; y7 B: R. E9 @* g% L
# scp tomcat.conf root@3172.20.22.26
7 h- C; f. w) V, j5 ^2 ~! a% n
! p4 b$ S% M0 l. p! G- U& l3 X####tomcat2,172.20.22.266 J$ b7 X5 k* o- C ?
# ls -lrt logstash-7.12.1-amd64.deb
: o0 q5 F6 m4 l8 y; t4 H5 l# dpkg -i logstash-7.12.1-amd64.deb( [3 ~3 Z) O4 R/ X
# vim /etc/systemd/system/logstash.service
8 ~; ]4 \+ [% e) x7 R...
$ f" h/ `+ e0 u' }0 u. c; MUser=root1 Y6 F# a [) c3 Q2 d
Group=root
4 E# N% x) s0 A5 a7 f& }...
. p2 i: E6 X/ Q5 X# systemctl daemon-reload
4 m2 c' Q% W0 T/ s3 C* M& i/ T# systemctl daemon-reload& h9 m+ s+ s$ V q
# systemctl start logstash.service
& d/ H/ F+ b) c通过kibana展现
9 b& `% d; ]; @1 }5 t9 _
; \1 @3 t" e* o5 L& j3 a% s
4 i7 f# f- L7 Z7 }, F; ?( ?收集Java日志 3 d4 u. y4 X8 k% h. v
使用codec的multiline插件实现多行匹配,这是一个可以将多行进行合并的插件,而且可以使用what指定将匹配到的行与前面的行合并还是和后面的行合并7 }! `7 p, ^5 t$ G3 u' N
8 F& n- Z* ~, W9 ]( s& [Multiline codec plugin | Logstash Reference [8.1] | Elastic8 }: j6 z; G2 C& b+ k1 Z" ^; _
* A9 r& P, e* C5 p添加logstash配置文件
, y0 y P; P' a/ A i###收集logstash自身的日志,172.20.22.26, V/ T v6 k+ y" S- I5 ]
# cd /etc/logstash/conf.d% ~8 t( t/ r7 ^7 ]; c
# cat java.conf # F0 f+ U. u8 Z' m
input {) q! G g/ O, F) }$ P
file {
% k; @7 w) R3 @" \ path => "/var/log/logstash/logstash-plain.log"" Z* d* H1 c1 K- D" k& f
type => "logstash-log"
- I5 n4 ~% G- V# |4 i start_position => "beginning"$ {7 b6 U2 U+ j
stat_interval => "3"
& l2 g& ]- x4 g% {" M. S codec => multiline {5 o; ^; U* L. S3 X" W) Z: J0 U; w- Q
pattern => "^\["
4 l5 e/ A J- G: c( x0 s( D negate => true
$ ~% d) S; y' ~( B, `4 R) }3 w3 M2 t0 L what => "previous"
- d- }$ e+ a _5 d6 e" e# {; K8 l }}3 P, X# S0 d& @, J; T" w
}+ C/ ?4 I2 T" @% T8 g& [) F
output {
9 ~- s) p7 M( f) r, y if [type] == "logstash-log" {& Y3 P8 w$ Z) p( U E& H
elasticsearch {
0 `3 v1 n- ?& v hosts => ["172.20.22.24"]
* f: ^: W6 d8 e4 D index => "logstash-log-%{+YYYY.MM.dd}": @4 N, t$ C8 s$ f( K: I1 w3 d: l7 t
}}
7 H) R0 G/ m0 e+ ^}
# W9 _1 t$ v! I$ m5 s& v+ P5 u3 L# ^& d1 k3 q$ D( a7 n. H
# /usr/share/logstash/bin/logstash -f java.conf -t
0 N+ w, m2 T4 m5 v+ ~) z+ |# systemctl restart logstash.service
; }0 x1 z4 l4 X& v% a9 s0 q- B: l6 |* @6 G
###收集logstash自身的日志,172.20.22.30
' @. L! A/ [: T: U# cd /etc/logstash/conf.d- X; C+ }& j; P0 u2 N
# cat java.conf 0 C- h' Y( X1 O
input {, _0 p+ }0 l6 Z: F: h" K& r
file {2 d: {5 ~6 O; ?" q A
path => "/var/log/logstash/logstash-plain.log"; U) _. e; H, ]6 s
type => "logstash-log"& C" ~1 ~, M* u- P& ?# C
start_position => "beginning"
6 s' Q" N3 z. _, X- @5 ^' E. r stat_interval => "3"
! B, N7 }% c' g codec => multiline {
; Q. V1 c0 E' b% e. b1 u" n pattern => "^\["
# [, U. x4 {, L- k. q negate => true7 F+ O% ]. H! g X! m/ X
what => "previous" 1 e7 P& L+ l* P
}}, p6 d7 w* |- a& ?* q) p
}
! W* s) ^" r# p3 i' b" Woutput {* B Y5 ^! _: M* i# v( @& d( ~
if [type] == "logstash-log" {
, q4 {( Q. i' ^# L elasticsearch {, ?, R9 L8 M) ~' ^9 Y+ m/ S
hosts => ["172.20.22.24"]
G: P' i& R9 b8 n; e2 L* G index => "logstash-log-%{+YYYY.MM.dd}"
3 z4 C% {0 Q# o$ C }}& o4 B8 i6 C" Y3 I0 \/ r
}
1 M" w8 n" r3 S4 b {, G+ K# D8 |" h
# /usr/share/logstash/bin/logstash -f java.conf -t
1 a3 y5 a/ _2 a( U, O) \0 J# systemctl restart logstash.service $ ?6 r( b! J' x5 [* ~
查看kibana收集到的日志
0 O0 ]; i( E |$ }- y2 j; c3 s- q6 |$ d5 E9 e+ H
& I4 D7 D& u8 u- u# X6 t
9 g: G, [) K" Y8 v2 W7 _filebeat结合redis、logstash收集nginx日志 / w, G( x4 ]3 {5 k* J
使用filebeat收集日志发送到logstash1,再由logstash1发送到redis,最后再由logstash2发送到elasticsearch* B$ E$ a. i9 c' i
6 x- M2 V2 I3 R( f/ S. w- f2 N' Mweb1:172.20.22.30,部署好nginx、filebeat、llogstash8 k" X6 f7 W+ }
7 x; |6 _* w! `8 b4 g
web2:172.20.22.26,部署好nginx、filebeat、llogstash& h: | x" ~$ A5 q9 m) K
" l9 E/ [! j4 J8 `! m
logstash服务器2:172.20.22.23,redis服务器:172.20.23.1579 Y9 \. G; y" c* M: u
3 o' R5 I: q2 \
nginx服务器相关配置
5 O( L4 s3 i6 t9 [部署nginx + t `/ N6 T9 B; H) p
# wget http://nginx.org/download/nginx-1.18.0.tar.gz' U: U Y3 O, D$ Y
# tar xf nginx-1.18.0.tar.gz; ~7 J. ^5 ~/ _2 u9 G2 U. H
# cd nginx-1.18.0
4 K) S4 I% E1 B! G2 P, N# ./configure --prefix=/usr/local/nginx --with-http_ssl_module* D9 y' [% x0 k: X7 o' w
# make -j4 && make install
3 W4 y& D V+ @* }: W# /usr/local/nginx/sbin/nginx
* j$ a/ N; \5 z部署配置logstash
& o6 L2 N+ @# A ]把filebeat收集到的日志信息发送到redis
& W" h k- a5 J+ \( i0 y# ` ' s+ s2 W# t! \8 r+ k4 F; j
# apt install -y openjdk-8-jdk1 w3 L7 y/ d) \8 {
# dpkg -i logstash-7.12.1-amd64.deb: T- K& ~6 o0 }6 \# o
# cat /etc/logstash/conf.d/beats-to-redis.conf 0 k) }. C; X n3 K p1 c
input {
# T8 H4 Y) c2 n3 J/ \ beats {. h: |! ~' C O. r9 A+ y5 b- c6 E' |
port => 5044. p7 ~3 N, M- n6 j
codec => "json"
2 b1 l% r i# ~ }
0 x7 J2 d1 L8 L5 x. w beats {
9 N& C( A3 e5 r+ Z. s port => 5045! r7 w7 C* p+ J' _* y- U
codec => "json"
. g) w- v9 |; b6 @0 h }9 J; d$ q" T) z9 s8 U. Y
}
5 O: ]: t- `/ ^5 w2 Youtput {+ C2 Y& a3 D( | `; t- U% _
if [fields][project] == "filebeat-systemlog" {( n. W. D5 y# L8 t/ o
redis {
i* b% n# a3 g% K data_type => "list"
/ F C) k2 u$ H9 { key => "filebeat-redis-systemlog" f/ f+ N0 v$ J* O6 G! R9 F
host => "172.20.23.157"
: c4 d! ]- A7 \3 @0 n" ~% c- P- L H port => "6379"
1 D* G% h6 s' ~ z# f* c- ?2 T db => "0"1 |8 F, B) I8 I& _1 p: D, G v
password => "12345678"& X8 [9 J3 w) D2 ]' M0 \" f& o
}}6 z! H6 ^; v( } ]' Y$ [
if [fields][project] == "filebeat-nginx-accesslog" {) G/ t. X3 M7 \8 u
redis {
, P6 E0 h! L7 ^9 f- i! Y" `+ Z0 b# j6 F data_type => "list"
' V# ~6 [ J, x# _2 | key => "filebeat-redis-nginx-accesslog"
4 U5 b2 N( P/ U host => "172.20.23.157"3 I' L& {, p% G+ G, |
port => "6379"+ T9 x; k' q) o6 R
db => "1"
1 Y+ G) A* n4 T% l; @! q password => "12345678"
+ n& B5 ]- [3 Z& D& ~: E Q }}5 }+ Q% ?& W& p! u0 T% d- K
if [fields][project] == "filebeat-nginx-errorlog" {
( K* [5 P& `$ \" e redis {
1 i" h p4 t6 c( B) @" ^ data_type => "list"5 j/ F$ x+ |' {$ l
key => "filebeat-redis-nginx-errorlog"
/ {0 g, f% i- I( R! J3 P/ }) } host => "172.20.23.157"
9 Q( U8 r+ C* q port => "6379"
4 i$ z9 a! l& u8 e" i/ a db => "1"
3 @% U# D# V: \3 W" s! I password => "12345678"
- g! R4 V( x7 ~" A8 r3 V; H6 {. G, o }}
7 ?, B. Y$ A6 A5 O; z}
f/ |6 p. q$ R$ l7 x/ C3 G I, u K' i7 U# systemctl start logstash
: a( h# f( J Z% e) g& V# l& s# scp /etc/logstash/conf.d/beats-to-redis.conf root@172.20.22.26:/etc/logstash/conf.d/ ) o. h. j- Y7 g+ k* f
部署配置filebeat
, k% G- u ]- q+ R( v* ^通过filebeat收集日志信息发送到logstash8 z4 h6 H' E1 d9 F' C
) U$ T6 V9 k+ i) r1 Z$ {* X# dpkg -i filebeat-7.12.1-amd64.deb; ?- k3 R+ U- I. g
# grep -v "#" /etc/filebeat/filebeat.yml | grep "^[^$]"
, e# j& i% Q+ [8 k4 m2 Kfilebeat.inputs:
1 Y2 V2 A# E/ P0 d4 d- type: log% R4 M% ~+ L7 H( k
enabled: true
# R Z9 }, K- C9 ?3 H' T, b paths: q( x- [9 I6 k5 |4 P! c+ x$ j& Z9 ?
- /var/log/syslog6 E1 r4 d9 {( }5 c* C/ F. \
fields:$ E$ y; a( q$ {. c
project: filebeat-systemlog3 s8 }; y6 I# s- l. g
- type: log. X# N/ a/ m8 s3 P4 }( V8 p3 @
enabled: true
; `! Y5 k8 n' ? o paths:$ x. O; L% s9 @. W' ^
- /usr/local/nginx/logs/access.log) |( q* j* q7 C) @( C& S# C7 d
fields:
) j2 I; c- b8 y) m0 h: { project: filebeat-nginx-accesslog2 @4 a% S' _3 @$ S
- type: log e6 X, f( Y, t7 F1 q1 I
enabled: true
. a( v6 M @/ q! G, r/ I# T' J paths:
: w+ f* S( M" ]2 E' o - /usr/local/nginx/logs/error.log8 p8 ]. P( S( ]. F& u T) ]
fields:* x& c3 p P5 Q
project: filebeat-nginx-errorlog8 C2 i: ?: F& d9 r
filebeat.config.modules:& _- B8 ]4 ]2 y5 `$ o# s9 y$ w5 u
path: ${path.config}/modules.d/*.yml
# W% S, y: j# \. h+ o+ {% G9 T reload.enabled: false& i9 q6 j/ l+ b+ Y
setup.template.settings:
% t7 N: P- }6 t; A* n index.number_of_shards: 1$ u$ L! p; c; ]5 O: m) y2 S2 @& F
setup.kibana:; \2 i6 w8 O( P: H( i: o
processors:( d1 w. K7 V2 o- Z& |, V
- add_host_metadata:
9 }0 c3 ?4 X* h) e2 {9 I when.not.contains.tags: forwarded
: l$ E. e0 h/ `/ H% V& k. Y - add_cloud_metadata: ~
) g& M( @: S0 s! m, P/ o - add_docker_metadata: ~
9 F6 p T; p9 n* ] - add_kubernetes_metadata: ~
& i' a+ N2 x' v$ r7 Y6 `; u+ goutput.logstash:2 b h7 J% U2 T& m+ j2 s. ^
hosts: ["172.20.22.30:5044","172.20.22.30:5045"]) q2 _9 t+ q* g( n5 S* L
enabled: true
# ^# b: J% h/ u* A3 d worker: 21 f/ }4 h5 o% \1 T
compression_level: 3- U! S- W/ g# ~3 ]; R v
loadbalance: true
$ n$ U; B' {0 ^; @& i: k& H& S7 n) o0 B0 l+ {
# systemctl start filebeat$ I' k1 i1 Z. j% K& R
# scp /etc/filebeat/filebeat.yml root@172.20.22.26:/etc/filebeat/ : l2 f5 u! Q: |; _, |
logstash服务器配置
. O2 q5 F9 x4 ^' o' i1 `) I3 Alogstash服务器2:172.20.22.23,把redis缓存的日志发送到elasticsearch' P: v# f6 s1 k S W* `: h. B
& O# @3 ^: D' v5 L: K v7 t# apt install -y openjdk-8-jdk
% p6 v( B a e, [* R9 U) T) ~& l) T; @# dpkg -i logstash-7.12.1-amd64.deb/ ~! G% K8 [$ u# B: Z
# cat /etc/logstash/conf.d/redis-to-es.conf ; G( `* O" O, x+ O
input {
( Q. u) w$ [7 \2 } redis {
9 ~2 h! T6 X& H! a data_type => "list"( g. l5 @) P" q
key => "filebeat-redis-nginx-accesslog"
& C* V! T. {7 ~# E- J host => "172.20.23.157"
2 V. s) ^1 t+ D: H- @ port => "6379"' Q9 Q1 ?+ H K" G* K4 E3 g
db => "1"0 F3 j8 x1 j& N1 i3 W( y3 o
password => "12345678"
! x; g) ^- T8 d8 w F- W }
% I; P; l2 k- F% f9 `4 L9 m, }# E redis {: t! B1 k. N u9 j' n& W
data_type => "list"
8 @# W6 G4 c, d5 l key => "filebeat-redis-nginx-errorlog"
5 W: f4 m" M5 {- G: E0 y host => "172.20.23.157"$ i' _( ^( G3 d! V* d. X7 G
port => "6379"
! J( _5 w2 G5 S' b& D: T$ x db => "1"5 T* M0 h; ?* G$ `
password => "12345678"+ o) r E; A6 A- g: Q
}
( N& L) X$ f0 B, Q# } redis {% h4 ^! }0 x2 z1 G
data_type => "list"; P+ h3 a; X( t n* P
key => "filebeat-redis-systemlog"0 T" p- S2 K3 Q; o8 t/ Q) I
host => "172.20.23.157"! j2 U, Q Z" S: ~+ t% X
port => "6379"0 d4 @# B9 t; Q& d% I" M3 a/ B
db => "0" J; _4 O! c. `; H
password => "12345678"
) Q* @+ h3 A0 @ }
1 M C* G2 q3 Z% c; j& r* I! s* i}5 J9 v. o( A' ^, @6 K" l ]
output {
^/ I6 F6 G1 o; J if [fields][project] == "filebeat-systemlog" {
0 _$ j! x# S1 a; o4 L/ B elasticsearch {
* j/ D- R5 k9 V hosts => ["172.20.22.28:9200"]+ r: r0 Z' g0 p
index => "filebeat-systemlog-%{+YYYY.MM.dd}"3 s: ]& O) k! H% M# a5 z( d- I! I$ }
}}
/ o0 J! `9 N$ p1 V if [fields][project] == "filebeat-nginx-accesslog" {7 V+ E- {, D. Y, r, k% ^
elasticsearch {
. Q1 {/ n$ J- E( b hosts => ["172.20.22.28:9200"]
\' F4 K5 V! L& w1 R index => "filebeat-nginx-accesslog-%{+YYYY.MM.dd}"1 j& ^ }( u4 d. y
}}
7 q( Z3 a2 L- b; X: D if [fields][project] == "filebeat-nginx-errorlog" {
1 w8 u! R. T; ~( F elasticsearch {
2 X( v# @- Z, n# [ hosts => ["172.20.22.28:9200"]9 }% I, t* M- I& c5 Z/ u
index => "filebeat-nginx-errorlog-%{+YYYY.MM.dd}"
9 s! G: p" ^9 T. X9 v }}$ @- Z0 l9 ?- d8 l
}
% Q0 l$ }8 j3 R% J. G# systemctl restart logstash.service
7 ] a3 H4 d2 `% Sredis安装配置
3 l4 q- [4 f3 |) Fredis服务器:172.20.23.157,1 I" Y. I% [- A, V1 R
5 Q6 F2 l8 Z% D+ E( z1 X* p
# yum install -y redis+ w; F- h# S3 L
# vim /etc/redis.conf
; J& M3 }. v0 a! @/ g####修改以下配置项) v+ ?# [- p4 d( N' y* g
bind 0.0.0.0
- I0 N9 @2 X" F3 t/ l....3 c `" i9 J: H$ ?9 V
save ""
2 i( q( l: i8 [) D....1 v3 f5 t; p2 [# r P2 {3 t q
requirepass 12345678
( F! o' E a2 [. T....
9 S1 p0 g0 @* f. b4 w2 `) ?" A# systemctl start redis
+ Y, t0 o% e: H3 G###测试连接redis6 j. {3 H- r( D+ R+ A4 c6 O
# redis-cli 3 P1 _& z- a5 N$ |
127.0.0.1:6379> auth 12345678& Z* S* T' F6 C
OK
- _/ ~# m7 p/ J/ _1 R' |127.0.0.1:6379> ping$ i, c. X& o& E) N4 R! d
PONG' Y3 ]7 p/ k: t4 s8 ^. a& {
' v- h- A9 m$ b5 ]- }, A' ^: q J###验证收集到的日志信息
8 @5 z ?, t: p127.0.0.1:6379[1]> keys *
1 V- e/ y+ V; u2 Y1) "filebeat-redis-nginx-accesslog"
" C/ c i) ^ |) a" {2) "filebeat-redis-nginx-errorlog". Q* l+ C' z2 Q* [* @
127.0.0.1:6379[1]> select 0* z, j* t) A; j" w/ D' h: y6 J' J# i
OK
c1 p, x9 Q! I3 u; D& }: R0 z127.0.0.1:6379> keys *
* p8 }3 U+ E. V$ u7 z1) "filebeat-redis-systemlog" ' t- P+ d2 {1 r/ W: L: s9 B2 p- Q* N3 G
通过head插件验证生成的索引
+ R9 A) c2 j/ i3 V1 v+ J7 b/ n% ^ D, I8 c% a9 m: D
! ?. j m) M4 ]. Q
kibana验证收集到的日志信息 5 B- X4 F7 z2 P% p/ l- o* U
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|