扫一扫,微信登陆

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

搜索
查看: 559|回复: 0

ELK日志收集

[复制链接]

1万

主题

1万

帖子

5万

积分

论坛元老

Rank: 8Rank: 8

积分
56206
发表于 2022-9-5 08:10:05 | 显示全部楼层 |阅读模式
搭建ELK
: G* b# ]& R9 P  `+ n9 J+ C( ]4 f$ kELK是由elasticsearch、logstash、kibana三个开源软件组成的一个组合体,ELK是elastic公司公司研发的一套完整的日志收集、分析和展示的企业级解决方案,在这三个软件当中,每个软件用于完成不同的功能,官方域名为elastic.io,ELK stack的主要优点:
; {! P6 X) f1 d$ x& v' H+ Y( j  m9 F- g
$ C1 @& t  E( _' ^9 w处理方式灵活:elasticsearch是实时全文索引,具有强大的搜索功能配置相当简单:elasticsearch的API全部使用JSON接口,logstash使用模块配置,kibana的配置文件部分更简单检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿数据的查询秒级响应。集群线性扩展:elasticsearch和logstash都可以灵活线性扩展前端操作绚丽:kibana的前端设计比较绚丽,而且操作简单
+ P$ v2 K, u( V( T' mElasticsearch 7 u8 l$ m0 ?, U$ p
elasticsearch是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索、支持分布式可实现高可用、提供API接口,可以处理大规模日志数据,比如nginx、tomcat、系统日志等功能。+ d3 s* H5 f/ o; |' t- I- x

/ h3 c8 a! ^, }) p# X) Kelasticsearch的特点:  I3 H$ D3 v0 l
) Y$ Q: [; {* ?4 Q
实时收索、实时分析分布式架构、实时文件存储文档导向,所有对象都是文档高可用,易扩展,支持集群,分片与复制接口友好,支持json, t  M1 p! ]& E
部署elasticsearch
. w$ I; B+ [% |, a* W' IGitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine,基于java开发, Z3 ~( x* a  q2 X# P# s$ E
1 d' x& }( g( U. l* z
centos系统关闭服务器的防火墙和selinux,ubuntu关闭防火墙,保持各服务器时间同步
6 n- y* j, C- U# L1 b ; b% `: r) P) l' b+ R. |
服务器1:172.20.22.24
2 I- L, d, |; T $ R  ?6 q+ Z* q: R- o4 t, T
服务器2:172.20.22.27
. O+ P. j* V! H! {
" ]  c8 x' f; C; O' @  n5 l服务器3:172.20.22.28
& D; O" e4 L. I! a. d ! T3 Y" O9 U5 b- M
###ubuntu$ D" v- j1 `7 J4 _( A
# apt install -y ntpdate  n! z/ o# M! {' C
# rm -f /etc/localtime
  F" r& n2 U! Q# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime6 [/ Z( L5 q2 t3 e2 E
# hwclock --systohc4 v9 ]# \7 i7 `' D8 G: i- I! C
# ntpdate -u ntp1.aliyun.com
- W* w0 @( U- X/ I  [! r###设置内核参数
4 V9 F+ ^# m# A, G# vim /etc/security/limits.conf
1 q( S1 j) T8 m+ q*                soft        nofile                5000006 k% ]% c; `4 O% T
*                hard        nofile                500000# `# t, z0 t( Y+ p6 P# f
# vim /etc/security/limits.d/20-nproc.conf . ^+ Z/ G7 T% ]3 X. v
*          soft    nproc     40960 ]2 ~" t. e) v
elasticsearch soft    nproc     unlimited
# V$ }% F1 b' j  |7 r: q4 Croot       soft    nproc     unlimited
, H/ J/ o! x; ?; E9 E, c9 X###安装jdk
3 o$ T0 I2 U* }* W' Y* ]# apt install -y openjdk-8-jdk
% K8 E. |! o  R! s( p" X3 K* p; k( A+ R
###每个节点都安装2 s8 V3 ?8 N) c# d8 M% R" Z) |6 j& V
# ls -lrt elasticsearch-7.12.1-amd64.deb
( \0 N, y7 ^5 J6 R# dpkg -i elasticsearch-7.12.1-amd64.deb1 @+ G! K% k* J, i' w8 h1 o# l
###节点1配置文件& z8 O6 ^7 l& g5 B$ K
# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml
6 j/ n% `3 l6 h7 Y- N& E- j* R! @cluster.name: m63-elastic        #集群名称: k) H/ C5 l) j
node.name: node1                 #当前节点在集群内的节点名称
5 d2 s+ ?+ R/ F) }% t$ u" E+ fpath.data: /data/elasticsearch   #数据保存目录1 U9 Q1 g. |8 {0 }
path.logs: /data/elasticsearch   #日志保存目录  Y2 R' _6 V, t, L
bootstrap.memory_lock: true      #服务启动的时候锁定足够的内存,防止数据写入swap- x6 g. V# s5 ]+ i
network.host: 172.20.22.24       #监听IP
0 B0 l( B2 Z4 b0 ~' x; Ohttp.port: 9200                  #监听端口
! w' |" X* T! |! D6 V! O* w0 B###集群中node节点发现列表. v/ U& c) f4 e0 K4 c5 h/ Z. R
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]9 ^4 I/ ~1 w  ~1 A; [/ [
###集群初始化哪些节点可以被选举为master
+ p  W4 F/ T. L: ~cluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
, p% `7 }6 k" `) D# M4 j# d# ]# R2 gaction.destructive_requires_name: true
; v/ `1 I9 L, o, ]# mkdir /data/elasticsearch -p
4 U, A" O: b0 Q: Q7 p; I. e) L# chown -R elasticsearch. /data/elasticsearch3 |& A. x2 D8 Z
# systemctl start elasticsearch.service, r1 n' ]# J0 x. R
###节点2
/ r( K1 t0 K/ o. i0 Y. l  t# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml8 c; r- m% k; p
cluster.name: m63-elastic# d0 L( R+ F$ d6 f. j: b2 t2 G' A
node.name: node2
! [' {  D% I. _' o- D9 u1 |' r1 zpath.data: /data/elasticsearch
2 e8 b% Z' R6 ?: V# Jpath.logs: /data/elasticsearch
! F# y" Q" Y3 A9 p: u) A1 o, Vnetwork.host: 172.20.22.273 l3 f/ m2 i1 k9 c& I6 m+ U$ ]2 L
http.port: 92000 L# G' @5 f7 Q: ~  X
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
* p6 f0 }- g- Y) qcluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]7 X( P1 T5 r: b7 N3 X. }( D
action.destructive_requires_name: true9 H2 A. s& O* Q2 G. \& i
# mkdir /data/elasticsearch -p
) [) P1 p) z' r) j1 o# chown -R elasticsearch. /data/elasticsearch
9 ]' \: O1 s4 t: O, {" c# systemctl start elasticsearch.service7 X# T" T) T' S- {7 L" e7 T
###节点3
7 F/ N7 M& y& `5 a$ X# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml
5 |7 Q/ d3 {" M. `  Ncluster.name: m63-elastic
3 N- m/ y3 T. g5 [node.name: node3* F; B% c/ e4 {, N3 P* U3 a5 K" q7 j
path.data: /data/elasticsearch; }4 o- |( X* E2 d1 f# m6 H9 R$ q
path.logs: /data/elasticsearch2 E$ w1 Q4 ?* \) l
network.host: 172.20.22.28( X2 Q3 {6 E  f0 C
http.port: 9200* L8 u' u, J$ C/ H
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
# t/ L, k) V1 h9 v( ?0 t: ocluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
$ m& d( ?8 o8 Y0 i" waction.destructive_requires_name: true2 D5 P, S( g; t: D
# mkdir /data/elasticsearch -p5 Q' l' Z. w* N6 x) z" t
# chown -R elasticsearch. /data/elasticsearch9 |+ \% k1 C% S9 V5 i3 n1 h
# systemctl start elasticsearch.service
( U, m, e! a7 ~! d& R1 z浏览器访问验证 9 y1 u' m) [/ I* j; z: H
http://$IP:9200
1 i6 W! q0 y  ?9 S; ~5 z/ s
3 t  S* ^, s( e+ S* x8 k+ w
5 P; I' b3 I: h! k; ]' N" l$ W
2 ^3 ]  R7 Q& F, |0 F8 |Logstash % d& B" x, z8 o4 Z% [- C4 q
Logstash是一个具有实时传输能力的数据收集引擎,其可以通过插件实现日志收集和转发,支持日志过滤,支持普通log、自定义json格式的日志解析,最终把经过处理的日志发送给elasticsearch。; \. s% y9 K: _8 l! Z- m

) Z0 `* l3 ^* P9 Y1 @, p5 t部署Logstash
: _! ?) e# [6 I9 E* kLogstash是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中用于最多插件的一个组件,其可以接收来自不同来源的数据并统一输出到指定的且可以是多个不同目的地
2 ^6 X. E0 ]( @& k9 a& J. m ' i( H9 H- w( V% V: Y  f: `
https://github.com/elastic/logstash #GitHub
: s# h- Z: q* @/ m5 t. n
2 r4 m6 A! P; f, cElastic Stack and Product Documentation | Elastic
8 B# m7 {8 S- e9 N: `7 P% n 8 Z# B6 l- I! v5 ^5 h0 `4 Q3 U' D
环境准备:关闭防火墙和selinux,并且安装java环境
) K* r- J1 l8 [: k9 d/ b% ?7 {' x$ j
% c1 E6 e# U- W0 T# apt install -y openjdk-8-jdk! ^. N1 X; Z0 d
# ls -lrt logstash-7.12.1-amd64.deb" a6 H* x+ R) J( j* X
# dpkg -i logstash-7.12.1-amd64.deb8 C/ O+ {/ `7 t3 J) {1 _$ w  u
###启动测试4 J* V. n' e2 ^8 U8 H  z
# /usr/share/logstash/bin/logstash -e  'input { stdin {} } output { stdout {}}'   ##标准输入和标准输出
3 l! D* V+ k9 u4 {4 v" g- F7 r8 w+ Dhello world!~
7 x0 p( n$ W& ]7 q, _( X{' `/ k9 k3 N$ i3 x5 B# W
      "@version" => "1",
0 r5 R5 c) `3 }8 ?2 I* e2 u" `    "@timestamp" => 2022-04-13T06:16:32.212Z,
- B* l3 n6 r+ i; b2 R' T          "host" => "jenkins-slave",
% V- K# m+ K$ d3 a/ N       "message" => "hello world!~"- p( h1 D6 M6 S" I* D
}2 u9 G: B# |9 G5 Y) d' Y7 Y4 R( Y
###通过配置文件启动  U# D# i$ N, R9 F1 u, C
# cd /etc/logstash/conf.d// Q3 {6 V3 _2 f& b4 I2 A' r/ z) j# s
# cat test.conf 0 b  o0 I! M  {
input {
0 |/ Q( I- c8 S5 S  stdin {}5 Q2 @1 G1 ^0 [" m7 ?
}% K' O* f; t- {5 A2 q6 Q
output {$ g! V" M1 p5 W* E/ [: F+ D# z
  stdout {}( I0 a( N+ q0 T& b3 _1 e7 E8 @
}
( C3 J) H$ e* K0 s1 G- o+ ~  z" c. [8 r1 o+ u4 {5 S9 n$ D% u
###通过指定配置文件启动( j; i  G' s- W0 s' S/ Q/ J
# /usr/share/logstash/bin/logstash -f test.conf -t   ##检查配置文件语法
) U. B; |3 v, Y' _# /usr/share/logstash/bin/logstash -f test.conf
% f4 E4 K+ f3 v8 d' W; T  R; I: d  K
####输出到elasticsearch
8 C5 q& c2 }( T$ U7 X$ I# cat test.conf
1 c0 ]# _- H: p& h  Einput { & F1 m! E, \% Z5 D
  stdin {}
3 \- ]& F0 F2 ~4 u/ f5 |5 r9 |}! j- v& p+ Z& J+ f( q+ Z
output {
. \5 D  {5 @) A4 o# Q  #stdout {}
9 R9 P$ U- A! o2 B$ N$ V! {  elasticsearch {- f0 ]# F# v' |5 Y4 F6 U8 A
    hosts => ["172.20.22.24:9200"]9 a5 D* ?' s' J. G, H7 {+ b% `
    index => "magedu-m63-test-%{+YYYY.MM.dd}"
( N- s) b# p  q  }8 ^' h# _* B4 S6 J
}! o2 b5 ^  m. S0 r' `+ `: v
# /usr/share/logstash/bin/logstash -f test.conf
4 [$ @3 e+ x, l/ W# V% y/ Nversion1
5 x4 G9 _* ~9 m5 ?( [; M* tversion29 V+ |& Y+ M- v$ I: H; f+ r2 ~8 z5 l
version3" S- e6 B( ~6 z- X
test1
9 b5 ^$ k/ b3 u3 Y( y# B& j# R3 P4 atest2+ E" }6 L, x) |8 s2 W0 j% S5 Y" c8 I
test3
% L7 ?  I' l& M3 `
3 A1 r7 K5 C" N####elasticsearch服务器查看收集到的数据- W0 K! z, S, M& j1 _! e7 ^
# ls -lrt /data/elasticsearch/nodes/0/indices/) S/ G/ K- l+ T
total 4, ~) U3 `2 \! X8 h2 m8 Z
drwxr-xr-x 4 elasticsearch elasticsearch 4096 Apr 13 14:36 DyCv8w7mTleuAvlItAJlWA
* x3 J: W6 V" G4 K3 C4 |kibana 0 ]4 {) W* i3 \9 R$ h3 r
kibana为elasticsearch提供一个查看数据的web界面,其主要是通过elasticsearch的API接口进行数据查找,并进行前端数据可视化的展现,另外还可以针对特定格式的数据生成相应的表格、柱状图、饼图等
& j2 [7 q+ O% \# ~ ) M  ~9 U. G8 @. }/ W) _
部署kibana
7 n1 R& f! |% F. F0 `* I& T# ls -lrt kibana-7.12.1-amd64.deb
- P: g- o) a- \# w. |0 {# dpkg -i kibana-7.12.1-amd64.deb3 Y6 `8 g0 P  j1 A
# grep "^[^$|#]" /etc/kibana/kibana.yml' T" u& V3 a& c* |; Y% W
server.port: 5601
- r" k& X# r$ kserver.host: "172.20.22.24"
' P- x% A" x5 O' o  melasticsearch.hosts: ["http://172.20.22.27:9200"]! O/ O, i, j8 v0 J3 }& h9 A
i18n.locale: "zh-CN"
1 w1 A2 \! {3 f, l# systemctl restart kibana % a+ g: {0 s6 [% B
浏览器访问http://172.20.22.24:5601
) N! o, y& u# |" d/ E+ |$ r9 R
- k  H$ h9 w9 B6 qStack Management-->索引模式-->创建索引模式+ W1 T1 M- j2 O& s* q5 C

+ S9 s! Y2 }' I% Y: z1 u
. V0 \6 S) \& y4 M! o选择时间字段
" A' L6 x& d% U
$ e- ]3 u6 U* h, h1 l/ w( M) s( D查看对应创建的索引日志信息, a" ~: h" s: K+ I% U# [
7 [9 y- u( p/ l: i' M7 X! U! i, e
6 c1 t. |9 @& i+ b, s0 F
2 j: p0 f  N, U( I2 h# |) W
收集tomcat日志
6 P! d- w- k$ t收集tomcat服务器的访问日志以及tomcat错误日志进行实时统计,在kibana页面进行搜索展现,每台tomcat服务器要安装logstash负责收集日志,然后将日志转发给elasticsearch进行分析,再通过kibana在前端展现4 K! ^2 M/ S8 {4 L1 P4 N
- q5 J% X! Y1 k
部署tomcat
! V# {; i* A' p####tomcat1,172.20.22.30
, i# n# G4 G, K& [# apt install -y openjdk-8-jdk
2 X. j' ]$ g* O2 ^4 f# ls -lrt apache-tomcat-8.5.77.tar.gz
5 w1 Y* w- Z% S- ^, W0 v8 A-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz
! O. i' Z% @4 R& Y, |% f0 T# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src// k# c+ L6 i4 ^5 F
# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
6 N$ ^! m! G. s7 q/ D' [7 _+ F# cd /usr/local/tomcat& z% C8 p! x0 b/ U1 w! s2 `
###修改tomcat日志格式为json
" ~& B  E0 `# \% U% z' D# vim conf/server.xml' a8 `: i% ?0 ~
...." ?: q$ S9 [  Q5 o; v5 ^

3 T' i3 I5 y% T  ]- `5 u....
2 T8 r6 x% {+ ]4 W7 E) `# mkdir /usr/local/tomcat/webapps/myapp1 i5 O# W( p( }5 I" F, R
# echo "web1 172.20.22.30" > /usr/local/tomcat/webapps/myapp/index.html
4 m7 ~. N0 x4 ~6 i' @# ./bin/catalina.sh start% K5 ]$ }  `1 Z; U1 c3 u. s8 Q; D
' v& T) z. @1 i/ L2 J
###访问测试
& ~# ]5 X2 [1 a  P0 ]# curl http://172.20.22.30:8080/myapp/
7 B; e9 h7 u9 [/ G###查看访问日志
, E0 `  v$ h7 _# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-13.log
0 I# h( u3 P7 ~- K0 F' D" K" L4 t; k$ w0 {
####tomcat2,172.20.22.26. e3 A: E1 n& p6 U" ~: J
# apt install -y openjdk-8-jdk+ f2 f( [, W. C# v% H4 E
# ls -lrt apache-tomcat-8.5.77.tar.gz 6 B4 X$ K8 f8 M: C3 x. P+ ]1 _
-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz
6 h' z# ]3 A' L( [. \1 Q& l# j% _# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src// I( B5 I7 ?# f0 z, Y2 q* J3 z
# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
6 o( X8 Y& j0 m1 N# cd /usr/local/tomcat7 x# `  ^: B" v4 O
###修改tomcat日志格式为json8 F" X7 X/ B$ h! V$ L( l% ?  V9 [
# vim conf/server.xml
  A! _# l& e- L....
, _! H' U# c. C4 }! P 9 p6 M% \/ k" P" l, o+ E
....0 `: e4 G5 f- v- Y3 ~
# mkdir /usr/local/tomcat/webapps/myapp7 m& M% @' V; p7 l6 p
# echo "web2 172.20.22.26" > /usr/local/tomcat/webapps/myapp/index.html3 J+ u$ z* Z& Y0 h; A
# ./bin/catalina.sh start
2 p! q- Y& p+ f6 \6 V" V. F5 W1 t" L' O' o1 R5 p- k* a
###访问测试
- D# B$ t( x2 l. b# curl http://172.20.22.26:8080/myapp/
9 u8 C: P* E% I. Z7 D###查看访问日志/ O. h# y1 T) ]  T
# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-14.log
2 o% X0 Y1 d) X: i* U部署logstash , M" R' m. `8 S
在tomcat服务器安装logstash收集tomcat和系统日志
3 b* p" {+ ?  r( A2 y- K! K) C 5 M7 H0 F, W) R+ U
####tomcat1,172.20.22.307 I, u/ w0 }" O3 C- M
# ls -lrt logstash-7.12.1-amd64.deb4 z2 u2 X3 q* c
# dpkg -i logstash-7.12.1-amd64.deb
  J  n$ y0 C0 B) r: \3 q5 v) A# vim /etc/systemd/system/logstash.service* j# t  |0 S5 {; T1 `- R* e
...
4 L( M1 Z- ^( l6 |User=root: V2 {. W) l* b
Group=root$ k1 u5 q9 m9 P0 p7 u( _- K
...
8 ^& h: p& ?  {( T& U- w# cd /etc/logstash/conf.d# _) `  d) a$ e' v& c5 i6 _& _
# cat tomcat.conf
, U! K: f/ H& s5 {) uinput { ) `0 C. c# U% p* a" ?& B) `7 M
  file {
2 r$ o5 t9 K- a( ?. R    path => "/usr/local/tomcat/logs/tomcat_access_log*.log"
9 C3 r2 T7 ~1 M3 I, m    type => "tomcat-log"# x4 U8 _3 Z2 q  u8 k7 j8 U! W
    start_position => "beginning"
! i: }# E7 N8 x0 M: ?    stat_interval => "3": Q: N$ e* q, B" V5 z2 `
  }' P1 m5 O, k0 A
  file {
1 [; h6 g6 V1 [7 F    path => "/var/log/syslog"
" E  T7 r* t4 [    type => "systemlog"8 _9 M9 X$ |/ V8 n5 `- `9 |- l
    start_position => "beginning"% E6 b3 F" d% V2 ~2 \+ V
    stat_interval => "3"
- M# B9 l: E' x8 |$ ]4 N  }; o6 t8 L. u; E% U7 b. c
}
7 Y! T) j9 Z( e8 K- h3 \1 K* |3 R# r2 Houtput {; @; C, Q1 L, t  P- ?; ^
  if [type] == "tomcat-log" {$ \9 Z* X* Y. ?( }% q
  elasticsearch {
. B0 S- M! a( g    hosts => ["172.20.22.24:9200","172.20.22.27:9200"]
& d3 L( a6 v2 @1 [# E' `+ }    index => "elk-tomcat-%{+YYYY.MM.dd}"
8 g; u& h" D" M; j# @( r9 t8 a  }}, S+ s: f! @1 t. ]2 Q5 e
  if [type] == "systemlog" {
) n6 X1 F: E7 ~$ _6 a8 r7 J1 Q  elasticsearch {; \! M8 {6 s5 U* L. n" S
    hosts => ["172.20.22.27:9200","172.20.22.27:9200"]  q% a! L; ^4 N7 Y2 x( p
    index => "elk-syslog-%{+YYYY.MM.dd}"
) v  l# ~; x0 o, ~1 N, ]9 ~  }}
7 A/ r, D: \5 P# t- G. E1 E}* F2 o0 I5 k$ c; o) C+ L, h

  K& N& D3 P4 |% t: B/ H# /usr/share/logstash/bin/logstash -f tomcat.conf -t
0 q6 j) S; f* P# systemctl daemon-reload' b7 L( f  F+ \& W5 F
# systemctl start logstash.service. D! \6 z* {* K8 n1 W) `
# scp tomcat.conf root@3172.20.22.26
; M3 T& t, }0 Q) W# ]/ j3 J/ x1 G  ?$ l* a- a' G/ H& L! ]
####tomcat2,172.20.22.26: F! a. X9 J: P( g  b
# ls -lrt logstash-7.12.1-amd64.deb
5 f, g( q+ {- Z$ E6 g0 l1 L# dpkg -i logstash-7.12.1-amd64.deb
: C; {2 K% v! k0 S) X1 Z# vim /etc/systemd/system/logstash.service
; ?) B1 x! V- j6 z8 O2 `% d& @...# H2 J, ?, X5 L
User=root
9 W' t# Y0 g" h! a5 w4 V$ dGroup=root
* g! S- z& V, K4 R1 ?1 f' O& A...
# n4 C" K. C2 Y0 G1 p9 {; k2 Z9 |# systemctl daemon-reload
) x6 @4 g/ Y' t% _' G" I. A0 [" V) d# systemctl daemon-reload
* g+ M. F- s/ O7 z9 M- j# systemctl start logstash.service 4 X3 n/ ~. Q2 s
通过kibana展现
7 S2 ?) r4 @# I, q7 ]5 m- F. _! \9 A, _; \4 e" J

+ s! o! z3 `0 x, _5 U8 \9 T! @收集Java日志
+ ~1 H, ?9 ?: Y使用codec的multiline插件实现多行匹配,这是一个可以将多行进行合并的插件,而且可以使用what指定将匹配到的行与前面的行合并还是和后面的行合并
0 e0 M( }  C0 K4 h ) p) C; u  q+ ~, r
Multiline codec plugin | Logstash Reference [8.1] | Elastic% i( K2 G0 f  Q

! o& `' o" K  G. |7 ?1 H% l2 z+ P添加logstash配置文件 8 Q- R  i/ S8 b0 p, e
###收集logstash自身的日志,172.20.22.269 J% P4 j" Z+ q  t, [( z: i
# cd /etc/logstash/conf.d
, l, E* y  w% t0 y+ ^0 x5 o% o4 E# cat java.conf
! p8 E/ t: X+ ?, \- T, E* jinput {
: ^) E" b% D6 ^3 n! x& ]8 q  file {
* m' A2 k6 L) ?/ u    path => "/var/log/logstash/logstash-plain.log"/ l' c: e; q7 Y- ?6 K: t: Y/ d7 _
    type => "logstash-log") a3 w7 q8 Q0 D  k/ p. q
    start_position => "beginning"
0 y# X' \! J+ ]    stat_interval => "3", C" r$ B: e5 N
    codec => multiline {
3 E, c- u, S) R( S      pattern => "^\["
  f& Y) i/ J  K3 E; z0 T5 G+ D      negate => true
- Z3 C' F8 T" {( T& `8 X6 l4 C- v      what => "previous"
% |, C9 `+ B% u( d3 B/ @( r/ ]   }}
: |6 ~5 }; O8 \+ D" K0 X# P* ^}
; l0 t& u$ J" ]6 E( h3 H" @output {
4 W* }  [" w/ M6 r4 B  if [type] == "logstash-log" {2 h( r" H6 v- Q0 O7 Z
  elasticsearch {- F9 s& f7 n' _8 _& I
    hosts => ["172.20.22.24"]2 v* E- I7 T1 i. h4 A
    index => "logstash-log-%{+YYYY.MM.dd}"
0 K! |% F" M$ P% q  h  }}: L) [5 L: v# E
}7 O5 w$ l' E5 [) x- {1 [. t

8 Y3 n( M9 Q" L; U6 u# /usr/share/logstash/bin/logstash -f java.conf -t
% N  m9 \$ B) ~6 A8 s# systemctl restart logstash.service& S& w2 Q- U, G6 P8 o- I
, [4 f# f$ C+ ~/ p% Y
###收集logstash自身的日志,172.20.22.30
5 ?: x* @% |# Z" C' v- Q# cd /etc/logstash/conf.d
8 J# q8 X5 A0 f& ]# cat java.conf & T& D& g4 `6 ?0 |
input {
  \; J! s; A5 A9 g$ U, u( p( y6 F  file {
" P9 q  b  I/ Y' D    path => "/var/log/logstash/logstash-plain.log"; V* G1 V- H. |/ [+ Z
    type => "logstash-log"
( i/ h5 X- ~4 b) [# \4 W5 |( F( H    start_position => "beginning"9 T3 I6 t* u. A4 ^' F9 |
    stat_interval => "3"& ]+ C4 S* @8 y  }4 I1 v
    codec => multiline {
8 p" N" s5 v7 q2 d" c  S6 z      pattern => "^\["1 l* d$ Z2 W3 E; I3 }/ j9 g
      negate => true. @9 m- h7 e1 O4 T( S
      what => "previous"
5 j8 f% w  z/ o- \5 H$ J   }}. c: t, ?% q7 V3 j+ Q& c$ Y$ X
}5 [. M& x4 p5 Z7 f; b
output {
- }) N+ b" P+ h3 z8 M. W% o( q  if [type] == "logstash-log" {& s5 @6 |0 H! J9 `# y# H6 o7 @4 b
  elasticsearch {. K1 P! A  {  H1 K3 }! p
    hosts => ["172.20.22.24"]
: f* K1 X/ v! d/ C3 U    index => "logstash-log-%{+YYYY.MM.dd}"
4 R, r- A0 b# S; ^( U' S  }}' @. z' R! y  w. W5 W
}
. N" e6 [6 s7 w& Y4 X7 O7 s; J  @# M
# /usr/share/logstash/bin/logstash -f java.conf -t9 b$ F0 i9 C0 U/ m0 {
# systemctl restart logstash.service
8 _- k1 [, g% i" Q查看kibana收集到的日志+ y$ I, ~4 C& w# {: K  H/ V
: I+ @4 @. w! r. b& _) Y' S
: J0 W0 L  C& Q* D3 F

; q/ |1 }7 R' T+ y4 v! F& B3 V7 dfilebeat结合redis、logstash收集nginx日志 & \4 P/ L/ u& l( G: A# V* a  r0 B
使用filebeat收集日志发送到logstash1,再由logstash1发送到redis,最后再由logstash2发送到elasticsearch
0 q2 ]7 U  D$ e- q: F: j   e' ^) t! ?/ Q$ M4 G; L
web1:172.20.22.30,部署好nginx、filebeat、llogstash
1 O. ^( _9 J6 Y, _1 i# S2 S & C' n2 `$ [8 ^( i2 C
web2:172.20.22.26,部署好nginx、filebeat、llogstash
4 N% O/ d* v5 P& H4 d* Y( \4 r 7 K) P$ |! H" a" L
logstash服务器2:172.20.22.23,redis服务器:172.20.23.157' y- J$ ]( i% X" H6 T- P/ n

0 B- `% O( H3 X# I! m* p; {nginx服务器相关配置 1 `# n. j. T+ q5 l8 \' |
部署nginx , B& P& O/ z- R) V4 d
# wget http://nginx.org/download/nginx-1.18.0.tar.gz
6 k2 u$ K3 Z/ p9 w5 p+ T" r# tar xf nginx-1.18.0.tar.gz
9 Q' f' y* z* w5 O* t1 R. P" |# cd nginx-1.18.0
7 O( I! D1 Y" U" d- r0 O2 L4 L# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  p- U0 E2 }) ^8 G9 _& c# make -j4 && make install
& t7 O0 U0 C9 m7 X# /usr/local/nginx/sbin/nginx
7 c  |" o- e# ?3 n部署配置logstash : H7 E8 U1 W0 \, }( c
把filebeat收集到的日志信息发送到redis
' F: w; ?2 b( ?; C! K, [7 ^ . K( ~: V7 z$ H1 g+ U9 ~( K
# apt install -y openjdk-8-jdk
4 Q" }* l5 j8 ^% H# o( j9 D1 I# dpkg -i logstash-7.12.1-amd64.deb
  n! y% m7 P: ?$ _% d# cat /etc/logstash/conf.d/beats-to-redis.conf
* o4 G& T" c3 H2 a3 W1 rinput {
& j0 r0 O' U0 u9 ~% {+ m9 \9 I" y$ D- A  beats {3 F* q2 X4 f2 s4 m- x2 A: \6 }- X
    port => 5044% w. ?4 y. B$ W5 [
    codec => "json"& i# H( k" ?, `, Y, z. \
  }
( o- b' ~" G: ]8 ~  beats {: e0 U1 l& E' ?* x( L
    port => 50450 [: [5 y' `$ `) s* s: l# W
    codec => "json"
; j" Y( Z* U' F  l2 i3 g* g  }+ s1 @5 V, O0 E( p
}
- x8 a" c6 V9 X. s  ^* m, Youtput {
  N) B) c7 e" v* [: O7 h  if [fields][project] == "filebeat-systemlog" {# J: L" W) ?3 K" }$ F
    redis {
, o4 R9 S2 S+ R3 n$ C  I9 {- Y      data_type => "list"
, T- I/ x7 O& \5 f      key => "filebeat-redis-systemlog"
4 S  A$ t  p6 X      host => "172.20.23.157"
1 d7 _! L/ T7 ]5 q      port => "6379"" z+ O& y* q( Z. F% l1 H
      db => "0"
, k7 m, t5 A3 K& x; e% h- }      password => "12345678"1 k& Q$ F+ R& y7 P" S$ E0 g
  }}# J6 c/ f. P, Z; y/ n- Y7 S% v
  if [fields][project] == "filebeat-nginx-accesslog" {
. m; ]$ K& ~: R6 s2 s    redis {( R3 @5 K2 A* _- ^: x. K' v. x% ]" o3 d
      data_type => "list"* p5 Q2 h1 c( V# k2 l) s8 H4 l2 T3 Q
      key => "filebeat-redis-nginx-accesslog"
" }9 _/ r* z, t$ i% x, @      host => "172.20.23.157"8 C- ~" H1 d0 k' \, N; s3 ^
      port => "6379"# O& Q- B( o+ f9 K  d
      db => "1"- G4 }" [) Z! p& B2 r0 I, T0 N2 l. u
      password => "12345678"' Z  d$ A  R% t$ g# K# X
  }}
* m# |9 x( B' X$ Q- x- F1 F0 ?" [  if [fields][project] == "filebeat-nginx-errorlog" {# j. S2 x' R! E/ A9 V- c2 V. U
    redis {
6 Z/ N) w/ y9 x      data_type => "list"
$ F+ U+ Q7 f. _      key => "filebeat-redis-nginx-errorlog"
0 O8 x0 t% T# Z: q  c, a      host => "172.20.23.157"
( q! O7 c4 C1 O5 \# c. q& Q2 @      port => "6379") ^" j0 A! ?3 ~
      db => "1"8 V+ H7 O( n! ^) L
      password => "12345678"
1 f, u% E! {( S' I* r$ _4 T3 `  }}* k# `' i6 w; b
}
$ w& @8 M% T0 w1 C# systemctl start logstash
( v' D! N$ N" _* [# scp /etc/logstash/conf.d/beats-to-redis.conf root@172.20.22.26:/etc/logstash/conf.d/
2 C0 ~! R. G$ i7 m2 J' G$ w部署配置filebeat
: X1 g& A! m: R5 ~. w+ q通过filebeat收集日志信息发送到logstash
3 G( C: t% |* q! \; X ( j2 ]( i6 D6 f. ~: C
# dpkg -i filebeat-7.12.1-amd64.deb
& y3 @/ ~% k' a) y2 x/ W. k& K8 u5 F# grep -v "#" /etc/filebeat/filebeat.yml | grep "^[^$]"! U9 a2 g3 N4 H( `% U- J9 l
filebeat.inputs:3 @! z/ d/ d/ a5 @5 s
- type: log
- C: d0 J5 x% @- q  enabled: true
: s1 e& I4 N# ~! O6 [; g  paths:: U8 n4 X+ G: E3 k; P$ d3 N0 R/ o( w
    - /var/log/syslog
& f: d  e1 w& X2 M0 k. V  fields:
! A7 r* W2 D3 C" {    project: filebeat-systemlog8 B, t+ Q+ O1 o
- type: log9 {2 R5 b. Q0 a5 a' i
  enabled: true+ c4 K  X5 D+ c5 h3 O+ }, q
  paths:) q' d" j  ~; t" J4 L
    - /usr/local/nginx/logs/access.log
$ W/ M  p2 \& c+ A2 c  x  fields:4 H! B- K' {8 w, B5 g
    project: filebeat-nginx-accesslog
- c+ C- b+ G) l9 h- type: log( L" C) d1 S" P4 {: P6 L" k  L
  enabled: true
8 k% j0 A: \( q6 c0 E9 S  paths:
1 g% T3 j, {9 T    - /usr/local/nginx/logs/error.log7 B8 z6 Z$ ^/ p
  fields:' y4 z+ e# |1 c9 d# }  I# n3 S5 j
    project: filebeat-nginx-errorlog
! I% {5 S* l4 x& w2 W$ K: wfilebeat.config.modules:
5 A- p) i# z9 t% K4 x% |& P$ ~0 t. E  path: ${path.config}/modules.d/*.yml9 J0 D5 T7 J, E: @5 u- K
  reload.enabled: false2 i2 Q4 n- X% s4 b  k; O
setup.template.settings:
3 n: x: j8 {$ o/ s% x9 ]6 c  index.number_of_shards: 1
6 u3 ~5 y* u; Lsetup.kibana:
% `1 v0 U( F/ {$ s  r$ ?" |processors:. c. I5 V- e$ Q% H; X+ b
  - add_host_metadata:( ^2 R6 P3 Z. D( D: c
      when.not.contains.tags: forwarded1 j' g; H1 @& ~0 [5 L* s% t
  - add_cloud_metadata: ~0 H$ ]) B% n/ P+ q
  - add_docker_metadata: ~" |$ H1 m3 `4 o: A6 [) w
  - add_kubernetes_metadata: ~- s% h6 F  M! ?2 L9 f
output.logstash:; @- b; K5 t* @, m
  hosts: ["172.20.22.30:5044","172.20.22.30:5045"]
# @5 C1 s( {6 v5 S  enabled: true* F9 r+ D8 w; Q5 [3 e
  worker: 2+ Q! Y8 S; b. d% \
  compression_level: 3
; Z$ v+ P  |; K6 m8 Z: J- P. f' R  loadbalance: true
& ]4 v: v! p+ T- A$ ?6 a" W2 i# Q
: [: N; _/ y& v1 [4 U* i# systemctl start filebeat
$ q: T; k8 _3 O( B* t2 n# scp /etc/filebeat/filebeat.yml root@172.20.22.26:/etc/filebeat/ 5 v8 l: B6 V2 A9 Z- |  X
logstash服务器配置 + E. @% A/ D/ E- r8 u; b
logstash服务器2:172.20.22.23,把redis缓存的日志发送到elasticsearch
( c0 E/ W5 v2 N; Q! A $ j) u: }; N' V7 |* o+ K+ E" d
# apt install -y openjdk-8-jdk
$ U% F! B+ m: g5 c: b# dpkg -i logstash-7.12.1-amd64.deb3 N# u' k+ J* ]4 Z
# cat /etc/logstash/conf.d/redis-to-es.conf ) \( J/ R/ W, y: M
input {2 ~2 Y, r2 i% o* m) Q# w
  redis {
1 p7 D) _) b- x! V; [! G4 R6 q) n. q    data_type => "list"9 X7 b: ]5 i7 W* d
    key => "filebeat-redis-nginx-accesslog"& t; r7 g9 F4 N" L2 W& G
    host => "172.20.23.157"" `0 b: W2 f# V5 c% A% P: \
    port => "6379"
- g$ V& H8 O/ d    db => "1"
, l4 B9 M8 d. @( t    password => "12345678"0 s) ~9 W' ?# M& U3 n
  }
1 K0 M  o" w1 |2 `  l. F& Q  redis {& Q# M& y) G' Z/ Z
    data_type => "list"
( E! G% S" V- O4 s% S2 S" ^6 b) G    key => "filebeat-redis-nginx-errorlog"2 O* U7 E8 @& n$ I2 `! c& Q
    host => "172.20.23.157"4 A" ~( p$ X( Y- @" e- b
    port => "6379"# c  A% ^$ K% Q
    db => "1"+ v7 v* p5 M% j& V6 [
    password => "12345678", r2 P# @! X5 [
  }% K5 p# j  Z$ P+ X1 y* U
  redis {$ _! z! A+ G8 i% z) {3 b
    data_type => "list"
% t# i: t% I! y' Z  I6 t0 I    key => "filebeat-redis-systemlog"
1 j0 {$ o/ e$ O    host => "172.20.23.157"
: z- m% `& l8 k6 b: M' N    port => "6379"3 ~2 c3 P% E; v2 O# p# K' e
    db => "0"1 Z; _, _  ^3 f8 ~6 L) e8 t
    password => "12345678": g6 ]* c! U* v3 i: a6 T! a, j" @
  }7 b* V0 L* l6 \% }7 ?  K1 Y
}
3 |  E. P  C4 ~5 {% \output {) Q0 L$ \# d" H: g
  if [fields][project] == "filebeat-systemlog" {0 k: Z1 m% k, \6 u; O$ j9 O( A
    elasticsearch {8 I' X1 O; [8 O: X7 s3 R; P
      hosts => ["172.20.22.28:9200"]
! y2 `" m# q$ P/ b4 I* Y- O      index => "filebeat-systemlog-%{+YYYY.MM.dd}"6 Q3 |! m  W$ h) g
  }}
/ a7 k, Q/ e4 B9 m* [* o! b4 V3 o  if [fields][project] == "filebeat-nginx-accesslog" {# j6 U% s, a9 _/ M7 z7 z
    elasticsearch {' q  i$ i5 d) x( @4 l/ h& C% C
      hosts => ["172.20.22.28:9200"]" l5 x  ]) g' n- D1 Y
      index => "filebeat-nginx-accesslog-%{+YYYY.MM.dd}"
% V* |- c3 u5 U1 J: c9 X  }}
( F$ j1 C' i3 h) ~; e# ?  if [fields][project] == "filebeat-nginx-errorlog" {! |, E# ~: I, Z* E4 b" |
    elasticsearch {
0 l6 X( r8 ~  k( P$ ?7 \3 ^      hosts => ["172.20.22.28:9200"]9 ]' }5 a( s# p# J$ S  @. [. W+ t4 j
      index => "filebeat-nginx-errorlog-%{+YYYY.MM.dd}"
* P2 J3 u6 }4 U4 p, [  }}
5 F9 X( n& h1 O2 A3 e) H+ A- q}( p; ?- N. E: }
# systemctl restart logstash.service 9 p" O6 F; l) U  l* a
redis安装配置
* Z; U, b6 M: bredis服务器:172.20.23.157,( ?- V% u( w. [6 z, c% `

7 y2 K. b( x8 K3 ]  D; _# yum install -y redis
9 T: g, r1 C6 R2 w: j# vim /etc/redis.conf
! m% K4 {5 t$ E####修改以下配置项9 x  g2 z, Z$ l; R; U% S" z
bind 0.0.0.0: Z: W% e5 k; R. T% I- q! x, D
....
8 i/ b1 W8 D; {. h6 hsave ""
/ J- P; L: h. m% [4 w/ O. a- W....
) d  i9 a- s/ |/ C) H" Jrequirepass 123456789 l' |/ F" I+ n, p
....
' m) m8 A( w7 a- F% r+ j: m# systemctl start redis
/ f# Q! I* j0 u- ~# E###测试连接redis
: F! x: N% i' e8 y+ R2 T2 a5 ?# redis-cli . n$ ?: o8 L. G; U# x3 K/ c
127.0.0.1:6379> auth 123456789 n2 X: s6 d5 y( M9 G# C
OK2 Z5 u1 `7 L6 O
127.0.0.1:6379> ping; o2 X$ J. r8 i3 u' [0 f
PONG5 [) @/ h; _+ D/ \

# V# i5 q3 {: [4 f  L###验证收集到的日志信息
: |7 C- ]: o; w% P+ T127.0.0.1:6379[1]> keys *8 x7 T# z; s7 o$ r7 p0 j! n
1) "filebeat-redis-nginx-accesslog"+ g% {0 O( \$ l* {
2) "filebeat-redis-nginx-errorlog", x  z6 ?4 g/ W9 N3 a. J9 O4 t
127.0.0.1:6379[1]> select 0
2 W+ z7 i- |9 |; pOK
* q) c! |: C7 D# F/ O& r) ^127.0.0.1:6379> keys ** \- c4 s% u5 ]  Y0 D+ b( Q
1) "filebeat-redis-systemlog"
/ o% b% @; S( o: o& O通过head插件验证生成的索引' \, U( A% w$ J1 Z" A5 Q

9 x! J' c6 o7 c$ z1 I8 z3 o$ K ) z$ J- p( _. P6 h7 f
kibana验证收集到的日志信息 ; T" z" N7 Z! L

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

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