扫一扫,微信登陆

 青浦修电脑 青浦笔记本维修 青浦手机维修 青浦电器维修

搜索
查看: 558|回复: 0

ELK日志收集

[复制链接]

1万

主题

1万

帖子

5万

积分

论坛元老

Rank: 8Rank: 8

积分
56206
发表于 2022-9-5 08:10:05 | 显示全部楼层 |阅读模式
搭建ELK ( z9 m( \$ C5 o  c
ELK是由elasticsearch、logstash、kibana三个开源软件组成的一个组合体,ELK是elastic公司公司研发的一套完整的日志收集、分析和展示的企业级解决方案,在这三个软件当中,每个软件用于完成不同的功能,官方域名为elastic.io,ELK stack的主要优点:
% h) F9 s5 H# L
/ R, u& a, y, z- I8 |. O处理方式灵活:elasticsearch是实时全文索引,具有强大的搜索功能配置相当简单:elasticsearch的API全部使用JSON接口,logstash使用模块配置,kibana的配置文件部分更简单检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿数据的查询秒级响应。集群线性扩展:elasticsearch和logstash都可以灵活线性扩展前端操作绚丽:kibana的前端设计比较绚丽,而且操作简单3 a2 Q3 F6 G4 n( y; o" z
Elasticsearch
8 C2 n+ u  G* I) X1 r% Delasticsearch是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索、支持分布式可实现高可用、提供API接口,可以处理大规模日志数据,比如nginx、tomcat、系统日志等功能。
/ c- G; A& U5 Q0 Y& _. s
' p/ s; @8 O0 b# R/ h! f. velasticsearch的特点:
4 P1 Z. c' h- I( L ( C; @/ R- f, s8 g$ F, a
实时收索、实时分析分布式架构、实时文件存储文档导向,所有对象都是文档高可用,易扩展,支持集群,分片与复制接口友好,支持json
: E; U4 D9 }" a9 [/ Q0 b  B部署elasticsearch 1 e5 M% W! B+ B! j; D9 M& \
GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine,基于java开发1 N6 c* d1 n+ c7 \; ~- c

- j' Q! W  O0 S0 M' |6 tcentos系统关闭服务器的防火墙和selinux,ubuntu关闭防火墙,保持各服务器时间同步) y9 }/ q1 Z3 d- f% N5 `
  k/ B7 ]% i8 k' [3 B, o
服务器1:172.20.22.24
" O+ x7 T& C' V( u& u
$ D: ^8 ?' O; L4 b% w( M服务器2:172.20.22.27
, }6 r0 k3 I4 M! _6 e/ P ' I* L* Y' Y) e4 x2 W
服务器3:172.20.22.282 S- |# y, J9 z2 i/ e% O0 C9 `2 l

- {  M2 ^) u4 w###ubuntu. ?5 _) G; w( S. x  c
# apt install -y ntpdate/ }  R% h/ ^) ]2 J
# rm -f /etc/localtime
# m% Y& b$ r5 K  x# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; T. y- q5 S. N
# hwclock --systohc4 ]2 t: O& Q7 Y( _% s
# ntpdate -u ntp1.aliyun.com
. Q1 ]; X: z  N' P) p###设置内核参数$ ^8 f- H* X: `
# vim /etc/security/limits.conf3 j( [3 [* }' b0 D
*                soft        nofile                5000000 \* [  t& P$ |  J$ T4 f
*                hard        nofile                500000
  d* o( z+ r) q0 L# vim /etc/security/limits.d/20-nproc.conf / O0 P% N! k' h$ U2 k8 Y# n5 ?7 ]
*          soft    nproc     4096/ k) j' [( S9 N6 ?& P
elasticsearch soft    nproc     unlimited0 I* g0 b5 [/ |
root       soft    nproc     unlimited- Q; _% c9 Z% Y3 U; O
###安装jdk1 y! `2 d& S1 L
# apt install -y openjdk-8-jdk  h" M# r5 s* [" Z5 Z

: ~: u' n, Y* \% a1 M###每个节点都安装
8 d" I& `! s; d3 l: D! c9 e' A' U' g/ \# ls -lrt elasticsearch-7.12.1-amd64.deb
5 P, x3 K3 g, c- {( o2 D9 Q; O: l4 b# dpkg -i elasticsearch-7.12.1-amd64.deb
5 V+ @8 i# a+ Z1 m' j' {2 C) M###节点1配置文件) V& R: Z: Y, f! H) W% t
# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml
( I! d* y4 [. b0 k. ^cluster.name: m63-elastic        #集群名称6 u2 x  |: T8 d* ?- d
node.name: node1                 #当前节点在集群内的节点名称
1 v7 P# u: k' m. L# @! d0 b: F5 _path.data: /data/elasticsearch   #数据保存目录4 n1 Z) ~( \/ R. m' _$ l+ `/ }
path.logs: /data/elasticsearch   #日志保存目录1 g  c1 b, b, }' w
bootstrap.memory_lock: true      #服务启动的时候锁定足够的内存,防止数据写入swap, ^$ `' D; q8 x# X
network.host: 172.20.22.24       #监听IP
% H. L4 Y0 j% |# ehttp.port: 9200                  #监听端口
/ b7 U7 C% B9 F3 Z; S###集群中node节点发现列表/ F! m1 F9 b6 {6 g5 o
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]- U& _$ e) l$ U& f
###集群初始化哪些节点可以被选举为master
0 |9 f) d, Q5 e4 \* k* P  i- Dcluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
; J, R/ Z( k! D  S$ b9 o* taction.destructive_requires_name: true
  L6 `$ h. j7 Q" G! K  w# mkdir /data/elasticsearch -p3 S# d* m: W6 Q5 P
# chown -R elasticsearch. /data/elasticsearch2 ?5 P  F8 l" Q- h
# systemctl start elasticsearch.service
2 Z5 u3 y4 D7 }- L###节点2
. w: ]9 @6 H1 X$ v  D1 l* c) t$ Y# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml7 T7 z  @: G2 f- j5 }
cluster.name: m63-elastic
8 X+ B- B  P8 t" ^3 {; Q; o& tnode.name: node2- ^3 D1 |7 B: g4 l1 l- `9 q
path.data: /data/elasticsearch7 K; l: {4 n, [: W, e- p4 G
path.logs: /data/elasticsearch( K: P& \5 V& j  y' M( n
network.host: 172.20.22.27
' ~+ s0 j% ?; w: z- @5 }1 u/ qhttp.port: 9200
. h2 j$ u5 r- Z1 N, S& ldiscovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]2 n& ~; L0 D: v/ |& L! \
cluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
2 ^. e  s6 z+ ?- Q  Zaction.destructive_requires_name: true
+ ?6 o0 B7 [" x5 u7 X  m6 }# mkdir /data/elasticsearch -p
1 w8 G7 n! H" c. z" ?# chown -R elasticsearch. /data/elasticsearch/ Z! [/ A. {; {5 A
# systemctl start elasticsearch.service2 N  h+ H5 R; Q4 d# a2 l3 l
###节点3* B; j- C! {5 `8 x/ w( N) Y
# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml
9 J( J8 P7 L. k( o; Wcluster.name: m63-elastic0 y" j/ \+ I$ Y. e
node.name: node3! I9 F, S2 I, o9 E
path.data: /data/elasticsearch0 ?" S. W3 Y# G% y1 {* q, O% d
path.logs: /data/elasticsearch9 g- W) M2 `) F) P9 }' {
network.host: 172.20.22.28
1 K; t9 ~7 [0 E/ P0 B1 rhttp.port: 9200% ?8 U( ]1 J9 t) {
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]/ W" L, r0 N$ w8 O) _7 i
cluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]1 v2 ?. c( l' a" U7 M
action.destructive_requires_name: true2 V4 C' [# s) X9 }  L  s# m( r
# mkdir /data/elasticsearch -p  h: A4 D' C6 W% v( ~$ T" `. v
# chown -R elasticsearch. /data/elasticsearch
% p- p1 Z4 f1 B: p8 T2 M# systemctl start elasticsearch.service
  i: S% r: W+ q8 f2 P5 {浏览器访问验证 1 S3 B) x: s9 g  Q: p. G
http://$IP:92002 Z/ w' z5 \+ g% `
) y& Y8 d6 `! z" U; f
8 n/ z" ?' x% U. N4 ~% U$ _

6 U0 C, f- c! w& V# ?Logstash 1 K( A' Y, x( {0 j
Logstash是一个具有实时传输能力的数据收集引擎,其可以通过插件实现日志收集和转发,支持日志过滤,支持普通log、自定义json格式的日志解析,最终把经过处理的日志发送给elasticsearch。: b: M: @* F' b: _
1 |6 @- J5 ^/ ]5 W$ C
部署Logstash # b2 H, Y9 X/ F9 S. B/ S7 m7 S
Logstash是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中用于最多插件的一个组件,其可以接收来自不同来源的数据并统一输出到指定的且可以是多个不同目的地
2 R/ H. M& L! I3 ~/ j; A$ h ( U1 m0 z2 p1 T9 _! ?9 F* A2 m, q
https://github.com/elastic/logstash #GitHub5 I. m7 o2 u9 S; u1 t+ y: [" y/ W
/ z: x5 N7 j+ C9 b2 L
Elastic Stack and Product Documentation | Elastic" ^) {' N4 b: C: y6 c$ U" @3 ?

  Y+ h7 d7 I! B! P: J环境准备:关闭防火墙和selinux,并且安装java环境5 f+ E' F, M8 H6 h

# N% {5 t6 |. }0 }- J' x# apt install -y openjdk-8-jdk
/ |4 Q, i2 k% |) t6 c! m# ls -lrt logstash-7.12.1-amd64.deb
5 ]- ^) I) @! X4 }% a# dpkg -i logstash-7.12.1-amd64.deb
; Z' o4 w, n- ^$ X- H###启动测试! i! d) f2 ^* [0 f6 N! a
# /usr/share/logstash/bin/logstash -e  'input { stdin {} } output { stdout {}}'   ##标准输入和标准输出$ w5 i3 x% b3 [1 b: w9 F8 N1 R- c: v
hello world!~! ]+ H* i6 y# g; M
{. q5 K  h5 V0 k4 \
      "@version" => "1",( K# f; k0 C; D# \! G6 p* F
    "@timestamp" => 2022-04-13T06:16:32.212Z,
! G. W% ?! s+ B( ^' S          "host" => "jenkins-slave",2 k8 r. p9 p  m, n% l0 m
       "message" => "hello world!~"
* V- {1 t% J5 B6 K' }}4 q5 G6 X, Y/ p  W% n& A! I
###通过配置文件启动0 S* ^' y' _, j: M
# cd /etc/logstash/conf.d/
2 k" R6 ^" K# x& Q% a# cat test.conf
8 d6 ^; @, F- @+ p- j0 _- Ginput { # T1 D1 p$ ~  m& o2 J  Z" G
  stdin {}2 q  n- k! u$ w
}
" R' i9 M( e4 }1 m. m! Youtput {' m0 ?4 \% g- z$ R
  stdout {}
6 z% U9 A3 u3 z, |}
" C3 K0 f% G& }3 m/ Y2 F' l. K9 Q! }4 C
###通过指定配置文件启动. T: g/ ~8 S2 B& e7 i% J! j3 R
# /usr/share/logstash/bin/logstash -f test.conf -t   ##检查配置文件语法# \1 g: W$ X8 Y6 _
# /usr/share/logstash/bin/logstash -f test.conf
% g/ N# d: k# ]9 E# @+ |! x2 g* p; R. l+ d- x; s
####输出到elasticsearch( T% @  ~$ K) {0 T' c% B
# cat test.conf
5 {( l" M; Y5 i# ainput {
. ~( X* p2 m0 E& K  stdin {}
" F2 _; @( _4 Y# K) N}
8 \- i6 \9 q$ V# [1 q; [8 youtput {
* X" X+ r2 W: c8 N  #stdout {}
2 [5 _) N) O& J% u  elasticsearch {4 _+ f. ?% S4 f% B
    hosts => ["172.20.22.24:9200"]
+ H& e* F( L0 q5 [0 w9 r0 t    index => "magedu-m63-test-%{+YYYY.MM.dd}"$ K( l! O3 V* X6 k. }# j) G
  }' A# M' O5 S' k: R! h: M
}8 Q; Y5 U8 z/ z0 N
# /usr/share/logstash/bin/logstash -f test.conf; [2 ~" t  v. U! k$ l
version1* J% q- M6 J' ~) m" d: v9 s8 t! u( c+ k
version2
* Y; |2 B# z1 w' Pversion3) P+ G6 K& p7 c4 V. }# b( a
test1% l/ I4 A/ H6 a" E+ u, O/ b% J
test2
0 q. D+ m: J! h+ K/ ?) ltest3
2 G. s8 a% O8 _& L
/ s9 H# T: }* m# x####elasticsearch服务器查看收集到的数据2 K; h7 p. E. {2 o9 v
# ls -lrt /data/elasticsearch/nodes/0/indices/
9 l7 L3 z& J5 G! ]! t* C. p& `total 4
! f) g  z! }4 o6 ^- \drwxr-xr-x 4 elasticsearch elasticsearch 4096 Apr 13 14:36 DyCv8w7mTleuAvlItAJlWA
( M, s. s5 v9 P+ G0 K% ckibana
3 v. n0 m: v  Z8 S1 h, Akibana为elasticsearch提供一个查看数据的web界面,其主要是通过elasticsearch的API接口进行数据查找,并进行前端数据可视化的展现,另外还可以针对特定格式的数据生成相应的表格、柱状图、饼图等( k' h1 g" B, F7 l+ G" U" B# c( t/ V

0 ]; e/ n0 U. n/ B$ ?6 }3 i部署kibana
8 ?" `5 c# s( D. {: G; B# ls -lrt kibana-7.12.1-amd64.deb/ e3 w" F0 s" H* W
# dpkg -i kibana-7.12.1-amd64.deb
9 U" ~+ h3 V  |" R% F+ @. m- c7 ^# grep "^[^$|#]" /etc/kibana/kibana.yml
* P) g  T3 i$ T% ^* z8 E4 k2 k- s7 Cserver.port: 56012 x8 A" `: I/ V; U) m% t" j) f
server.host: "172.20.22.24". u3 ]. k" o  O$ M- X  K+ u" h
elasticsearch.hosts: ["http://172.20.22.27:9200"]5 D# z3 F1 ~3 L. f
i18n.locale: "zh-CN"
/ A" p7 i' ^5 l+ S1 W: P) i8 z# systemctl restart kibana ' v* }7 \& b" H# C, H
浏览器访问http://172.20.22.24:56016 H; C2 k) Z& r2 y3 O. l, t+ b
/ U5 M) x' S' d6 Q% U2 q
Stack Management-->索引模式-->创建索引模式# V3 i+ T% E3 P' f2 `7 H

* h! P( ~! ~, y6 ]* l
! P) c6 `% m  ?- T' L选择时间字段
1 R. R' J2 w8 V, G" o
- }) j! ]% u8 r' i" h1 y查看对应创建的索引日志信息8 ^* D1 v( N: N. p+ j3 U9 t9 ]
* S( ~1 R# W  b$ Z' V

; ~9 ~: Z- C4 X3 A& j8 a 5 @1 }* a  |. w. s" Z/ f: j5 C
收集tomcat日志
( u. G# T" ?. ]收集tomcat服务器的访问日志以及tomcat错误日志进行实时统计,在kibana页面进行搜索展现,每台tomcat服务器要安装logstash负责收集日志,然后将日志转发给elasticsearch进行分析,再通过kibana在前端展现
. A1 u% f0 o, ]' m# P2 ~) {
  k4 U, Z8 l6 E7 j% o' r# e, P8 @部署tomcat ! b' L4 G* q1 m7 E& R3 q
####tomcat1,172.20.22.30" v5 R2 ]3 `: D6 t( s# @0 U% ?
# apt install -y openjdk-8-jdk
2 A# Q5 |9 H, P" @# ls -lrt apache-tomcat-8.5.77.tar.gz
. Z: I* q2 @% N. }/ m( P-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz
8 |9 p) P/ W. W# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src/7 r3 Y9 ~1 n7 h9 f/ y6 m
# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
2 E% G5 z, k0 r9 @# cd /usr/local/tomcat
+ a1 Z- k) t; v9 @2 h. S###修改tomcat日志格式为json
+ f9 ~% d4 ^2 ~% s7 k: W# x/ L, V# vim conf/server.xml
, B5 e7 M- `+ I- N6 T8 K. e....$ Y9 r) t! {0 r1 i* A7 [

8 V; F1 O$ T$ c( Q5 ~....
3 @( {- x% Q! w# \3 h5 ?' _# mkdir /usr/local/tomcat/webapps/myapp& \8 @/ s# d' ^) |. y! S
# echo "web1 172.20.22.30" > /usr/local/tomcat/webapps/myapp/index.html
3 _! g+ q5 m$ _6 y7 X  X/ {# ./bin/catalina.sh start
2 \) F1 D) e& S# `9 J% F$ j
5 r1 O! o. i" o( t2 u###访问测试
3 U3 {* p2 D- b# Z( R3 b0 d# curl http://172.20.22.30:8080/myapp/, w. u( W1 i' r# ~5 m( ^2 f: }
###查看访问日志. {1 x2 e* K8 b8 R- U2 ?6 [
# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-13.log' [+ Y- |+ R2 d# f

; c) c. V/ I7 t; Z; c8 E####tomcat2,172.20.22.261 ?' q: E9 j4 X8 C0 {' D
# apt install -y openjdk-8-jdk
$ w9 D& V# X7 Z2 S. v6 c  [# ls -lrt apache-tomcat-8.5.77.tar.gz : P( n) f. t# V& m9 I
-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz8 m2 C0 y% U/ H1 {
# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src/
- r1 C5 i+ ?) k# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
: ]7 I" }/ R( ?, v# cd /usr/local/tomcat4 [7 I. X+ _" p# u' Q9 l
###修改tomcat日志格式为json* i- f; @/ U5 H+ V" \9 N* R
# vim conf/server.xml
1 O3 @) L, f) h% o8 J....4 ~9 W- B: p& f" t- p

! c5 W! E5 s, G& _: m4 S....5 o8 T0 `6 R5 B) M0 U" m7 v: e
# mkdir /usr/local/tomcat/webapps/myapp
4 F. t6 v$ {! f3 k9 b, |3 Q0 F# echo "web2 172.20.22.26" > /usr/local/tomcat/webapps/myapp/index.html, P3 k8 P8 B1 ?6 \1 h
# ./bin/catalina.sh start
$ {' B7 d$ P( E6 U
3 n, T3 T6 _5 U  Y0 v###访问测试
; i: h4 ]" w- s6 q8 j6 M4 `# curl http://172.20.22.26:8080/myapp/1 A- p9 ^6 N( O4 C
###查看访问日志
$ _% ^" @* P; b+ y- \" U# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-14.log
+ s1 Z2 A" E) w: b3 U; w' M9 ?部署logstash
* G; d* |' I/ n在tomcat服务器安装logstash收集tomcat和系统日志, l2 t# o0 T8 q" T7 x+ w
* f/ ^( b7 `" g, V
####tomcat1,172.20.22.30
' D, `/ g* x) ~8 Q" U9 c0 I# ls -lrt logstash-7.12.1-amd64.deb* ?6 a# d8 Y, D3 d: Q
# dpkg -i logstash-7.12.1-amd64.deb1 {! M: D7 s, z! U3 ?
# vim /etc/systemd/system/logstash.service
0 C# l! C% f3 z: O/ ]  s...* f+ ?+ O: {  S6 s& B/ A/ ~
User=root) T0 V. Z# Q7 w% ?! `7 h
Group=root
( }6 X0 k) e, _& J0 o( ^...
1 v- y% H0 L6 F) U6 m# cd /etc/logstash/conf.d
/ I! n/ N- b9 C+ E6 M# cat tomcat.conf
- c4 i! y; u" A: Rinput {
* V+ Z3 n! ?. b* q8 L+ `* L  file {5 c8 Q5 q  J, Y6 I
    path => "/usr/local/tomcat/logs/tomcat_access_log*.log"6 I0 h  ?! o. I$ Z. S% x( Q7 I
    type => "tomcat-log"4 d! N4 H8 V1 `) H7 y% u3 |
    start_position => "beginning"
* m. Q1 [8 I! k: B. S' U& P$ ^    stat_interval => "3"
' [0 A8 K% Z% D; R0 @  }
* r: c5 ?' @- {& A  file {
' A  Y: @! K4 B& i    path => "/var/log/syslog"9 o" F4 o% v- X! ^5 a& `0 r6 v* x
    type => "systemlog"  G2 ~: q% b. H/ A/ n- s0 A
    start_position => "beginning", e& f) `+ I1 o% T$ C& s
    stat_interval => "3"
/ K) M/ ~# Z7 i4 m% m+ u4 z  }  }1 I* Y7 V: m/ F. Y% m
}
) a* N' V0 Z' }- Coutput {
; t+ o+ [5 f: P6 G- Y4 t7 ]# e2 b  if [type] == "tomcat-log" {- z1 `9 l, O; T! n: z7 B4 o
  elasticsearch {% _4 L  {! g4 [
    hosts => ["172.20.22.24:9200","172.20.22.27:9200"]7 `, N& \% i% q: c4 B& I% b% J# Q5 g- Q
    index => "elk-tomcat-%{+YYYY.MM.dd}"6 m' y$ G9 ]% y8 \
  }}: k* _2 j7 W. v  Z8 u
  if [type] == "systemlog" {
: X# j6 r" r, S  |" s8 O$ I! t' w9 x  elasticsearch {; ]% U% a* m/ K. V
    hosts => ["172.20.22.27:9200","172.20.22.27:9200"]
4 e0 S! a! q* \+ U$ T0 g: H2 i    index => "elk-syslog-%{+YYYY.MM.dd}"2 E+ v1 N0 x2 O2 j* A! Q2 l8 Z
  }}
6 p' u8 g; |: i# u}
( |" g8 [# r9 ^9 p. H: [
# x  [/ ~5 a, l5 v" p* Z# /usr/share/logstash/bin/logstash -f tomcat.conf -t( s1 h. r1 Y9 \; T. f3 o8 E+ w( {
# systemctl daemon-reload
1 X2 a6 {5 Q1 C9 u5 O# c1 r# systemctl start logstash.service) g. _* l5 Z4 p, F3 `
# scp tomcat.conf root@3172.20.22.26
" E9 k  J' ~- e( D6 F2 j
8 z. M* ?' Y* A3 j7 x: w####tomcat2,172.20.22.26& Q+ \2 U, Y0 {# J2 F; M2 r5 k
# ls -lrt logstash-7.12.1-amd64.deb
1 s2 a4 W+ ~: x) O3 Q* U# dpkg -i logstash-7.12.1-amd64.deb0 U8 \/ M! j0 o! [, t7 ~* |
# vim /etc/systemd/system/logstash.service
0 M. Y- a! A2 s4 N...0 J" |! w$ z: [* O" b9 i) V, {
User=root
; U9 H' t2 o9 OGroup=root
& [' S  K* B5 j$ {, I...
3 O3 I8 W2 S0 J7 i+ e, f# g9 L! H' a/ E# systemctl daemon-reload4 P5 Y3 A! K( Y3 }4 ~
# systemctl daemon-reload+ [* J& _! L6 q4 ~
# systemctl start logstash.service % O. }0 N/ Z3 t7 Y2 p
通过kibana展现
+ r7 y2 b7 s5 [! o) _
& E& c; \' c0 m& E! D
, t9 W' ^9 x6 x  H5 n收集Java日志 1 }" @2 B# I1 B
使用codec的multiline插件实现多行匹配,这是一个可以将多行进行合并的插件,而且可以使用what指定将匹配到的行与前面的行合并还是和后面的行合并3 x" z  L8 g4 u8 K6 z9 s" t
1 l8 D* w, P8 @: C7 n
Multiline codec plugin | Logstash Reference [8.1] | Elastic
' o9 ~2 }( Y0 q* o3 T ! g# d6 q: s. l7 V+ u' e
添加logstash配置文件
9 M) ]# x% B, B1 N; k###收集logstash自身的日志,172.20.22.26
4 ?  ^" b# g  r& O' @  l) m# j# cd /etc/logstash/conf.d
& v: z9 Q. o5 Q0 o% m% I# cat java.conf
$ g$ H, Y0 [# U4 F: I, ^! Ainput {
7 I5 o8 u% u( M& a+ w( l* ^  file {8 w% t. N- @$ K/ {5 i% m
    path => "/var/log/logstash/logstash-plain.log"9 ?8 Y: s8 n9 C- v1 q2 b0 Y
    type => "logstash-log"8 n) N2 V  l/ p( H
    start_position => "beginning"
( M, T/ U% r* G, }; k3 U    stat_interval => "3"1 m3 V8 _3 J( O6 }# R8 u; z: [% U7 Y
    codec => multiline {
- U9 m& k( E8 O4 q, w      pattern => "^\["' v5 [, E# ~7 `8 f- J  I) a( p
      negate => true  W8 B* ]$ O. D: J. D/ P
      what => "previous"
& L3 K  n8 A9 o; J0 m3 v   }}
- u. n0 W* ~: Z/ s& `1 t}& M2 s* R! y5 P2 A& j9 _! i
output {8 _7 F2 Z% p! k# Z& Q  ]' w
  if [type] == "logstash-log" {
. i6 S7 L- w/ W  elasticsearch {
4 `: o- K* F% k9 V8 ]9 t# Y    hosts => ["172.20.22.24"]
4 B- w$ J7 O& \7 E    index => "logstash-log-%{+YYYY.MM.dd}"
' y! ~' ?* Q* O" y2 K$ p  }}
  n0 K7 N7 d, _, ?' t( O/ v% p+ V4 ?}+ |; ]! \1 }# d5 k8 c& L  m

7 f+ c8 U( Q' H0 Y2 |5 O) m3 r& b# /usr/share/logstash/bin/logstash -f java.conf -t
* B; O& V% q; [% r# systemctl restart logstash.service
/ v/ o; l; a* Q4 @' X3 {( W
+ p9 O8 X: o: X###收集logstash自身的日志,172.20.22.30
2 v% R& l/ j+ P' P% ~# cd /etc/logstash/conf.d
1 Y( X+ x9 o0 G0 N# cat java.conf # j" R9 q# `& f* p+ ^! ^
input {/ \/ u1 l+ ]7 @
  file {" g, ^% |7 C* s/ U+ s1 n
    path => "/var/log/logstash/logstash-plain.log"4 q7 x9 v! P2 G  t
    type => "logstash-log"
; T, e' m( G& o# i4 }- q7 K+ {    start_position => "beginning"
) o- c6 o/ y% ^; V. i( G  H    stat_interval => "3"
( C2 I7 e: g9 M6 |4 M    codec => multiline {
6 r1 R, X$ F( `7 K% ~1 S1 |6 [+ o      pattern => "^\["
9 B) X+ Z* o7 _      negate => true* Z; ^' M- R2 o" `5 B+ F+ ?
      what => "previous" 2 }3 ^: ?7 ^  ~- n3 ~
   }}; k6 w" K7 e7 J, i. s3 Z0 T$ r; g! t
}
% r) F) `9 B/ k+ ~. Y0 ooutput {
+ ~( \+ _# b8 R0 v4 x2 o, a0 F  if [type] == "logstash-log" {
  V, ~! y* r. m$ q1 \! C  elasticsearch {, _. z+ I" ~- S( p
    hosts => ["172.20.22.24"]
- E; [9 ~% l0 G- ~9 u/ d2 ?5 l. d9 Q    index => "logstash-log-%{+YYYY.MM.dd}"
# y3 L2 v( C1 l+ \  }}
2 P& N+ f2 l' c+ }3 D/ w" b6 S- F# h}
+ p8 r7 x4 L" D/ Z2 F3 Q+ l0 q+ U* t* M7 e. O* ?# X% _
# /usr/share/logstash/bin/logstash -f java.conf -t
& b. B* K: z9 V+ p/ s# systemctl restart logstash.service : y6 f# e3 b: Y$ d* r3 i
查看kibana收集到的日志
4 t$ c! ~+ ]$ I
$ v3 F& \, P1 t5 b
+ K0 [9 }; k: ^/ a6 m! ^8 j : K& l4 Y2 a( Q( X7 F3 [
filebeat结合redis、logstash收集nginx日志
7 K7 [1 F' Q% ~; M( ~7 ^使用filebeat收集日志发送到logstash1,再由logstash1发送到redis,最后再由logstash2发送到elasticsearch
4 ^5 i7 \1 ]1 C2 n, h5 u / T; |# [# [) @9 R3 T6 K; Z
web1:172.20.22.30,部署好nginx、filebeat、llogstash3 Q  T, L' K) u2 {$ n1 ]
/ [- b% a) _" x5 G8 y
web2:172.20.22.26,部署好nginx、filebeat、llogstash
6 i, p! y" S; H
$ [/ q5 d# y5 Flogstash服务器2:172.20.22.23,redis服务器:172.20.23.1575 V/ b. B" f, W: c' v

9 S9 v9 j! {. Enginx服务器相关配置
6 a- k; m2 V$ ]" M  l  z: `% v部署nginx 2 u# Z6 {! x% F6 z( {/ y
# wget http://nginx.org/download/nginx-1.18.0.tar.gz
7 b9 S/ J! T* {# tar xf nginx-1.18.0.tar.gz
: R3 I7 @% z: A" a9 {$ |8 z: g$ R# cd nginx-1.18.0; U4 e& U$ y2 J7 A) {1 h
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module1 X# a, U' ?8 M$ d; X
# make -j4 && make install0 q2 e# A1 H/ x' t- d4 X& v% g7 _
# /usr/local/nginx/sbin/nginx
) f$ y+ i6 |8 F9 ^! G3 N( n: [部署配置logstash " m  y0 ]4 g% Z/ N4 \+ j
把filebeat收集到的日志信息发送到redis/ s& B  c& I4 ~6 j! ]9 O

- e( E% G3 `! ?# apt install -y openjdk-8-jdk
- J4 v& O3 K" z6 {2 i# dpkg -i logstash-7.12.1-amd64.deb
- R! g* G. L) V* E% P# cat /etc/logstash/conf.d/beats-to-redis.conf 6 }7 t# G; J: N8 C' @
input {) C$ {8 G. l8 M* u6 N0 h9 O
  beats {  P  r! h% E' s! m  W
    port => 50448 l4 `0 z# M5 m% W
    codec => "json"2 y( S  a0 Q7 p! o% M: B
  }
3 `/ p2 A+ e7 @  beats {
; T4 j. ]' s/ W( {9 y    port => 5045
, F* h: v% `2 Q; h) c/ t    codec => "json"
4 ?2 u  I3 }6 d- U6 L  }0 w8 m+ V7 L- D' X8 m
}
+ N! r6 Y, H. V  ?. {4 Youtput {
8 [% i5 \% `. `0 {. n& I; M& J  if [fields][project] == "filebeat-systemlog" {
& U: M# Y9 e; K- _    redis {: m, x4 y! E7 ^+ ^. a6 Y
      data_type => "list"
. S1 d* V  }1 ?      key => "filebeat-redis-systemlog"& b" ]+ T" n, ]/ m6 V5 z9 X/ R; N
      host => "172.20.23.157"
* `7 ?3 D: y5 w5 ~! k. o7 ]      port => "6379"6 I% O; I' y' q
      db => "0"
6 q. s# O4 c) B) b9 k# |      password => "12345678"
! c) x5 f2 {4 a3 e- w0 c$ J  }}
: t2 A7 F7 n9 h( x  if [fields][project] == "filebeat-nginx-accesslog" {
* ^( h# h2 c9 `' ]4 H9 M    redis {
1 y3 j0 O& c! r4 _9 B. B' i      data_type => "list"* Y- O* p7 Y. ]& G+ l+ F$ [1 t8 }
      key => "filebeat-redis-nginx-accesslog") l$ j# x) l. w3 @
      host => "172.20.23.157"
6 F6 t: z4 b# U2 J! C      port => "6379". V# C9 o4 a3 d3 j2 @
      db => "1"
: Y4 Q7 a5 X9 R( k' s$ I1 P      password => "12345678"; x( u8 M9 Y0 T; U* G- A
  }}5 f, H4 t, \4 ^% x. G' |. Q
  if [fields][project] == "filebeat-nginx-errorlog" {2 M9 j) Z& _; q2 T1 a
    redis {* O+ r' ^& _- }4 U0 n' L
      data_type => "list"
- _" O- \4 t. ?0 y      key => "filebeat-redis-nginx-errorlog"
- o2 s5 G# ]( T7 a      host => "172.20.23.157"% M4 T: a( p+ w5 w/ T- {: ~1 z0 I. c& c
      port => "6379"
1 M. Z; d# i& e. [  g. Z, n      db => "1"
; V" s% o$ K* Z2 w      password => "12345678"
6 Z5 H+ E' N) y+ |+ Z  }}& o1 k0 ~& X8 A) s  @2 Z2 K0 s
}
8 ^' W# R& Z8 `# systemctl start logstash3 Q; ~; }; @0 j: M; U
# scp /etc/logstash/conf.d/beats-to-redis.conf root@172.20.22.26:/etc/logstash/conf.d/ 1 O5 l& y: t, k
部署配置filebeat 0 \) D! H# j/ a
通过filebeat收集日志信息发送到logstash
. H6 e$ u3 t1 [, Z
% H! B1 H  r# T; j, F# dpkg -i filebeat-7.12.1-amd64.deb- {/ Y- L# p5 F6 A* j2 M" o
# grep -v "#" /etc/filebeat/filebeat.yml | grep "^[^$]"
# `) p" z$ n  @; ^) jfilebeat.inputs:3 M- Z' i' J$ ?2 B7 i8 Y
- type: log  ~2 y" M/ @- Y
  enabled: true  p+ `, C' E6 Y8 [, m1 n
  paths:% T$ j# c9 k8 ~! Z" }+ p# ~
    - /var/log/syslog
& S+ o$ p. }" T4 q+ V) w  fields:
1 A/ \7 H" \$ ~* c! L. @2 d& e  Z    project: filebeat-systemlog
6 J. T5 v1 j+ f9 j; A- type: log
. Y1 Y9 W0 Q; `" V  enabled: true7 {( X. j7 ?, f" u  \9 T$ u4 v
  paths:  V+ C8 {  n4 u7 V( y" [9 n
    - /usr/local/nginx/logs/access.log
9 M! ?: `( D& P5 P4 n2 |2 n. s9 d* _  fields:
' i* |1 Y( e( L9 t* N  q    project: filebeat-nginx-accesslog- @: w" g! n. |" i
- type: log7 [2 g2 X" p0 k  w0 A. p
  enabled: true& {' r8 h. J, C  Z- W- Y6 c
  paths:' g' A2 D- e# `8 M6 O1 F  V
    - /usr/local/nginx/logs/error.log
* c6 n2 q- C6 [% j: R) P& q0 Q  fields:2 \7 ]8 Q! [; _5 j/ U. j$ `, V2 m
    project: filebeat-nginx-errorlog
7 P- W! {/ w: w: L# Efilebeat.config.modules:
5 C2 V% P4 Y$ d# d  path: ${path.config}/modules.d/*.yml
8 {& @/ B# z. Q' Y. K' P8 s  reload.enabled: false
, a9 s, H" Z8 I7 }5 h; C1 Zsetup.template.settings:% i. H" l1 I* o1 K  ]
  index.number_of_shards: 1, U/ v; p8 d2 o
setup.kibana:, i1 v3 T5 }$ S& u0 p" K
processors:
( c5 w5 N" B  x: p! G# b. {  - add_host_metadata:
  d( o: e: B. |+ |/ l      when.not.contains.tags: forwarded
* P; e2 Q/ P, w6 m7 n- T  - add_cloud_metadata: ~% ?8 c+ n0 Q# R. C+ U5 o( c& b
  - add_docker_metadata: ~
, u9 o4 E7 y. W6 l! X+ f  - add_kubernetes_metadata: ~
9 m: D) P1 v% X. o6 N9 G6 C# Poutput.logstash:
$ Y9 y  ~' u4 ?  hosts: ["172.20.22.30:5044","172.20.22.30:5045"]
: J: d, r" {4 |2 Y& s  enabled: true# J3 }5 c  u# R. B& A* M
  worker: 2
6 x& s7 [) H8 B! b( E7 r  compression_level: 39 Z7 w: F% a# C) \
  loadbalance: true
( D" i$ q, `  [( h# z+ d
  a  G' `5 i& u# j$ A7 F/ n: G# systemctl start filebeat
1 W- g& u; P7 m. z# scp /etc/filebeat/filebeat.yml root@172.20.22.26:/etc/filebeat/ 6 H8 h. b$ M3 t  }% y+ l; G
logstash服务器配置 - \+ M; c$ Q9 f$ P: Y
logstash服务器2:172.20.22.23,把redis缓存的日志发送到elasticsearch/ {, y( w* J+ L6 _1 i6 n; d( T
8 B: e9 ]/ O5 B+ |/ B- ]
# apt install -y openjdk-8-jdk: b' F- ]2 }2 K  j2 U/ `
# dpkg -i logstash-7.12.1-amd64.deb5 \3 E# G! s) d9 f- B" t
# cat /etc/logstash/conf.d/redis-to-es.conf
. {+ u. i4 k! s" K- O4 Binput {
- k' h2 \& G& C4 h. I  redis {5 Y5 O# s  ?/ ^: E
    data_type => "list"
" L: E/ @& H( A) [# v. v3 r    key => "filebeat-redis-nginx-accesslog"
: i  C( H$ t5 S! h5 i    host => "172.20.23.157"9 D' T# m0 w3 W' ^# _! p
    port => "6379"
4 T+ o" g5 m( m: O  c' M    db => "1"1 t: R6 c9 m7 y5 X4 M
    password => "12345678"6 ]  |% u8 T* K" K# h* ]
  }
& D) U: E2 `& ^0 B9 \4 X! E* \  redis {
; [, q/ s5 \, ~4 M# e    data_type => "list"9 m( D5 P5 P" n! ?8 r% H
    key => "filebeat-redis-nginx-errorlog"
4 H. C: W4 {; A* \9 M9 ~1 r    host => "172.20.23.157"
# g( E6 v% O) m! B; L0 C    port => "6379"3 V- N* e' m8 Y/ o9 `6 j0 J
    db => "1"- I* F2 [' f3 r; v, d
    password => "12345678"
1 v' Z7 |8 K7 c8 l  }9 f8 b' g) \  X* T  Q
  redis {6 W, G5 {* Y5 N: x. s7 S. s
    data_type => "list"
8 v' R+ h. F7 e2 _( S    key => "filebeat-redis-systemlog"+ v$ G1 j0 u2 e+ ~# N
    host => "172.20.23.157"
2 j  S9 x1 ]' W" V: g/ Q5 X    port => "6379"  A9 B( ~- a- r% f
    db => "0"
6 {$ x6 X; |) z, h1 D    password => "12345678"
" T( B* c0 J+ S9 T( n' v. q6 \  }" ~  J4 r3 R" d* F# S0 ]7 ?
}
5 |( L& ?* r; Y2 K2 Koutput {
( h. ?" v  |+ L( v2 ~8 F  if [fields][project] == "filebeat-systemlog" {
/ ^( P+ W5 }% ?" U0 B; Y  f    elasticsearch {) J7 N" J. \: E0 @6 J
      hosts => ["172.20.22.28:9200"]
1 D6 o: r- Q5 g1 }: X) ^      index => "filebeat-systemlog-%{+YYYY.MM.dd}"4 ~- ?# Y$ S! f# ^% R  o1 J: N
  }}* g- i) C) q. \: b% r) G1 w
  if [fields][project] == "filebeat-nginx-accesslog" {7 r- ]1 K% ], X6 b7 W, t
    elasticsearch {! n0 X9 X! v$ i2 @3 B
      hosts => ["172.20.22.28:9200"]9 b" r% l# A9 L, F8 Q
      index => "filebeat-nginx-accesslog-%{+YYYY.MM.dd}"
: `/ j3 C- Q5 B9 c" M  }}3 W6 X( @* \3 d. [
  if [fields][project] == "filebeat-nginx-errorlog" {: t2 {& j1 ^/ r, Z# h
    elasticsearch {
1 W* S) z4 E& e8 ?6 D+ a      hosts => ["172.20.22.28:9200"]
% a, c8 N* e5 z5 d' ]      index => "filebeat-nginx-errorlog-%{+YYYY.MM.dd}"9 Z  V6 }, i: L  e, u' _0 J
  }}
% ]9 a0 C% F; J: [9 G}# M$ E1 s0 S- M, b0 i
# systemctl restart logstash.service 7 z- W9 K7 g5 m7 {# X
redis安装配置
8 _% @9 f# j1 D5 M( @+ i( Xredis服务器:172.20.23.157,$ m7 _" {) p# x6 V' e- G
* U+ E+ L4 g8 U$ d/ P* A
# yum install -y redis) y% V9 k# M6 T& O2 a# f
# vim /etc/redis.conf; G' A- R# G6 X+ {+ w( ]( [) c
####修改以下配置项
) @; B, J  G) X. tbind 0.0.0.0
: J4 [3 g  g  I. u2 g1 L/ Y....
# i: W4 V% h( ?) T! b' j& gsave ""
! X5 j1 V7 N6 k' I* q....2 i# t9 |7 w' S" s1 ^
requirepass 12345678
9 N* R  D5 u% O  C* p* r! h. ^& V....! ?, i) ~& E: f% h: p
# systemctl start redis
  ]& c, X, }+ Y###测试连接redis
$ o/ t- ~/ K1 C5 p# redis-cli 5 f8 a# i- V5 z, k$ p( R( J6 p
127.0.0.1:6379> auth 123456786 G3 r9 f. O& C/ N; \
OK
6 R: s, B% v5 d127.0.0.1:6379> ping0 a  d9 f& Z+ X/ ]; x
PONG
. U: M5 Z% w( }+ }! |! G. k* B' z8 a0 f0 d# [: T
###验证收集到的日志信息- d8 y# e  }( B0 v- A) F) E
127.0.0.1:6379[1]> keys *: f2 ~' d! G& v: N$ w5 G
1) "filebeat-redis-nginx-accesslog", v2 s! m' j% T! Z7 l5 a& i
2) "filebeat-redis-nginx-errorlog"
- p$ E# {/ x6 Q127.0.0.1:6379[1]> select 0
5 ~# U" U2 o& {' XOK7 ?' W, u' {, n7 o
127.0.0.1:6379> keys *1 l0 A) q4 W! O- E0 G+ u
1) "filebeat-redis-systemlog" 4 L- j0 e+ u: t( |) d6 `
通过head插件验证生成的索引: @+ b5 t) u8 D& l# z4 X

/ p. \( n) C. f/ F( H% c & _, J/ L8 y" n+ n& k8 b
kibana验证收集到的日志信息   ]% J" v8 B' N" U% c3 ?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2001-2013 Comsenz Inc.Powered by Discuz!X3.4( 沪ICP备18024137号 )
快速回复 返回顶部 返回列表