扫一扫,微信登陆

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

搜索
查看: 420|回复: 0

ELK日志收集

[复制链接]

1万

主题

1万

帖子

5万

积分

论坛元老

Rank: 8Rank: 8

积分
56206
发表于 2022-9-5 08:10:05 | 显示全部楼层 |阅读模式
搭建ELK 6 x. k! \5 y5 r/ m6 N6 G$ O" k, ?. Z
ELK是由elasticsearch、logstash、kibana三个开源软件组成的一个组合体,ELK是elastic公司公司研发的一套完整的日志收集、分析和展示的企业级解决方案,在这三个软件当中,每个软件用于完成不同的功能,官方域名为elastic.io,ELK stack的主要优点:
9 T, v& X! V  g8 e% e
) h6 h4 ]& Z' a# ]3 [% L2 G3 H处理方式灵活:elasticsearch是实时全文索引,具有强大的搜索功能配置相当简单:elasticsearch的API全部使用JSON接口,logstash使用模块配置,kibana的配置文件部分更简单检索性能高效:基于优秀的设计,虽然每次查询都是实时,但是也可以达到百亿数据的查询秒级响应。集群线性扩展:elasticsearch和logstash都可以灵活线性扩展前端操作绚丽:kibana的前端设计比较绚丽,而且操作简单
+ m. j6 |, X7 g6 u! ^* ~/ k! v0 rElasticsearch $ I1 s/ g! ]( j) U
elasticsearch是一个高度可扩展的开源全文搜索和分析引擎,它可实现数据的实时全文搜索、支持分布式可实现高可用、提供API接口,可以处理大规模日志数据,比如nginx、tomcat、系统日志等功能。
* D8 Z- t- i1 N , j) {; I1 m# Z4 w
elasticsearch的特点:
9 r3 X! I8 c3 E  v1 ? ' u, P1 D5 J3 e& |+ e% Y
实时收索、实时分析分布式架构、实时文件存储文档导向,所有对象都是文档高可用,易扩展,支持集群,分片与复制接口友好,支持json) [) A* @- ^. C& F( W
部署elasticsearch
4 |  j4 s# s) S" b2 xGitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine,基于java开发
5 i0 c! z0 e7 Q" q; z* L7 C/ U& W$ X 1 Z6 Y4 `7 ?0 i& U- [
centos系统关闭服务器的防火墙和selinux,ubuntu关闭防火墙,保持各服务器时间同步
% n* A2 x) k/ i+ r
$ f0 E6 E$ H  s3 w( ]2 f服务器1:172.20.22.242 u- y* E4 k: v0 C: q0 n5 a. X

' b& j- X* Z- p服务器2:172.20.22.27# }( h8 t$ {+ @
! i' `0 R$ R. x9 X
服务器3:172.20.22.28
' H( N% f3 i( V1 q" L6 B" ~
# L. l/ T4 p! x/ u/ k  l1 x' \###ubuntu5 c# f7 k' g+ o- M$ L7 p
# apt install -y ntpdate/ S% u+ }/ g3 O- f8 L3 Q
# rm -f /etc/localtime) K- t0 D% `( M; B& k
# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; `% B. s9 E( }8 B
# hwclock --systohc
, Y- q" R3 j8 F8 i* Q# ntpdate -u ntp1.aliyun.com! w2 @6 B) m! k- ]3 z
###设置内核参数
0 H; R' C- p: N6 C$ q# vim /etc/security/limits.conf
- G% B1 J' Q$ B; n5 M*                soft        nofile                500000% \  B  S9 ?: R2 e8 \. L
*                hard        nofile                5000007 s) _  w# i% D$ r* t" X; H
# vim /etc/security/limits.d/20-nproc.conf , d- ^% [1 `9 \( Z' C( x
*          soft    nproc     4096' ]. C0 q. z% g4 b6 T4 B1 v2 l1 d6 \
elasticsearch soft    nproc     unlimited
; W6 d& S+ F2 S: t/ O5 b  Aroot       soft    nproc     unlimited, [5 D' u/ S* C& `
###安装jdk
4 g  {" ^/ s8 r# apt install -y openjdk-8-jdk
- W: E! J7 W3 c% ~& K* D- X$ n1 _3 a& b
###每个节点都安装
/ ]) Y7 C* c5 P1 g. w* ~( e, r# ls -lrt elasticsearch-7.12.1-amd64.deb
8 B  Z5 f5 ?5 f; B% r6 m& ?: |# dpkg -i elasticsearch-7.12.1-amd64.deb2 k( S5 k1 A4 K# R% S9 s9 i0 E
###节点1配置文件
* J$ W+ \9 B! {7 ^* I- F# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml
- q) G$ j; t; U4 x) v6 hcluster.name: m63-elastic        #集群名称
9 ^( n, Q# R8 ^) M/ T+ m7 }node.name: node1                 #当前节点在集群内的节点名称
% U" }" _6 H: b; l4 C+ ^/ E% rpath.data: /data/elasticsearch   #数据保存目录
) y, q1 c, b8 {! S9 n3 @, H  y% kpath.logs: /data/elasticsearch   #日志保存目录+ k# }. ^9 L1 o$ E8 d! q, h5 J/ p
bootstrap.memory_lock: true      #服务启动的时候锁定足够的内存,防止数据写入swap
" F' m' g) l" K& _- Y! Tnetwork.host: 172.20.22.24       #监听IP" W+ f4 O8 g! B) Q& T% y' z' h
http.port: 9200                  #监听端口5 P( B# V4 `( w4 Q0 i: W
###集群中node节点发现列表" c4 K- Y7 Z/ B; C1 n! R" M5 w
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"], d+ O/ M% C. W5 u
###集群初始化哪些节点可以被选举为master
1 P& s; e4 ~5 ?: b2 j2 ycluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]% e5 z2 E: X; v& G
action.destructive_requires_name: true9 ]; ]$ G3 \* e4 o
# mkdir /data/elasticsearch -p) e) r8 G  q2 ^2 ~/ E
# chown -R elasticsearch. /data/elasticsearch
) x3 ~( l7 F7 {, [7 @, K# systemctl start elasticsearch.service
; p* x: P% V! g! T8 p, L###节点2
: G; e3 ^& `* b5 a- }2 v# q: y5 b# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml. d6 S: q9 [4 m. ]$ Y+ h
cluster.name: m63-elastic  ?3 M# @4 |3 c/ S* e1 H
node.name: node23 I: |+ x2 {& w- ^' @
path.data: /data/elasticsearch
/ W1 s7 s6 S$ n4 f; T+ dpath.logs: /data/elasticsearch
9 m1 e6 g( p" @+ \9 ]network.host: 172.20.22.27( d8 }  E% s* ^% G2 n! l
http.port: 9200% G5 }" @$ g2 |4 m
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]0 X3 {. R( g1 J  O
cluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
+ b- [3 F# e1 ]6 q$ Paction.destructive_requires_name: true
0 @" g' @1 K# V9 F+ \7 j# j' l# mkdir /data/elasticsearch -p
5 \! M# K5 u5 u' f- h# chown -R elasticsearch. /data/elasticsearch6 _9 ^4 E* F! V% |8 \1 s- ?
# systemctl start elasticsearch.service
$ q. e* t( O( T9 T' E###节点3% _; y! L" r& _# c6 \
# grep '^[^#]' /etc/elasticsearch/elasticsearch.yml4 u( H, J* }4 [6 d4 Y7 V) a) O
cluster.name: m63-elastic1 h/ m  n7 r1 _% ?. v
node.name: node3- K2 Q; f6 O% B5 T
path.data: /data/elasticsearch
9 n/ q0 e) w( C# v6 Wpath.logs: /data/elasticsearch9 |, V) U& O0 C3 C  w( C, d
network.host: 172.20.22.28: q; D' L$ @1 O' K
http.port: 9200/ a) ]) Q, N1 w5 ]
discovery.seed_hosts: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
% Y1 x# z  ]& `cluster.initial_master_nodes: ["172.20.22.24", "172.20.22.27","172.20.22.28"]
  J( j: `1 j+ V5 Z3 s5 J" L- Paction.destructive_requires_name: true8 ?# n) L& N/ Z; c
# mkdir /data/elasticsearch -p3 L- y+ q& X, N2 ~. x9 @, \$ }! I
# chown -R elasticsearch. /data/elasticsearch; v3 ?* J4 w. R  }
# systemctl start elasticsearch.service
* o* ~! ^0 S8 h% `0 K浏览器访问验证 * u4 G/ Y3 y7 b0 f* c7 J
http://$IP:9200
- l% V# J+ D; x& ~1 s' |9 T% T4 N5 [# C, I! Z8 T# i1 T
2 a! i* i4 ]" u9 F; l' S

" o9 E" ]2 Q7 F0 bLogstash " V. }; y( t5 s/ k
Logstash是一个具有实时传输能力的数据收集引擎,其可以通过插件实现日志收集和转发,支持日志过滤,支持普通log、自定义json格式的日志解析,最终把经过处理的日志发送给elasticsearch。
% x( f, Q; F+ m) r, G) }9 `$ `
6 H/ m' L0 W9 Z+ W部署Logstash : E; ?9 i. o# M, m3 X
Logstash是一个开源的数据收集引擎,可以水平伸缩,而且logstash是整个ELK当中用于最多插件的一个组件,其可以接收来自不同来源的数据并统一输出到指定的且可以是多个不同目的地- V% s/ {3 o: D8 S, H% T" H

( x, I- v, f- B! h+ F: \$ S# Ihttps://github.com/elastic/logstash #GitHub6 ?$ z5 q) f& E

6 b; }* n+ r/ u, b/ pElastic Stack and Product Documentation | Elastic
, J9 d1 c! q) d/ D3 [
: p" c: B" }0 P环境准备:关闭防火墙和selinux,并且安装java环境) N" H7 F) d8 ^) F3 s( `

- j2 h6 Q9 g& G; l8 _  H# apt install -y openjdk-8-jdk  X# O( T# {8 _  T3 y# A: x
# ls -lrt logstash-7.12.1-amd64.deb% O* T8 a- e' ^) J3 h
# dpkg -i logstash-7.12.1-amd64.deb
6 a' m9 ~: U- Z, R###启动测试( d" G  K$ S) L) \) e$ {
# /usr/share/logstash/bin/logstash -e  'input { stdin {} } output { stdout {}}'   ##标准输入和标准输出2 O, T" Q) l2 ^4 }4 t
hello world!~
0 `  o$ R" `* y' P' g& j{0 x2 e! A. k4 N% u
      "@version" => "1",
2 \* j! F3 [7 E9 j1 w9 p    "@timestamp" => 2022-04-13T06:16:32.212Z,
* u* V. c! _* A) f0 c* e% h7 ?          "host" => "jenkins-slave",) g& X( q# [0 t9 q
       "message" => "hello world!~"& k9 N) b2 T! W- H% e- j8 B+ f( m
}
; S; g- S1 I4 a& z& |###通过配置文件启动) u( B* I- @3 e5 A) m' {) f) h; q
# cd /etc/logstash/conf.d/
5 z7 ~; b+ w; X2 P6 h$ A# cat test.conf 3 ]: f8 f! w# z6 M4 X
input {
" {* K3 z( k1 j- l  stdin {}6 g, [: q3 U) u+ X
}: ^$ a* f+ X* K3 j# s  E+ v. F
output {9 h+ y$ p8 T- @- t1 k
  stdout {}
7 L, g  \2 G: p. ^; R. O}
0 a1 @" j  v+ |
: {% q: ?  h, R* {###通过指定配置文件启动2 H. ^3 e& ]1 z
# /usr/share/logstash/bin/logstash -f test.conf -t   ##检查配置文件语法
, ]* z" @/ b8 l' ^# /usr/share/logstash/bin/logstash -f test.conf& P4 S0 x' G! M( ]! T7 i1 D4 ?

5 z& a. h, R9 ^$ S) @. x, I" _####输出到elasticsearch, N  l/ Z( e. k3 H9 `
# cat test.conf
7 l' R0 E! m9 T0 [+ o  A! pinput {
' y  R9 w3 K$ S  stdin {}! I1 \, a6 ~. `) D  |
}
# b  a- {! }/ Joutput {
9 V; g* ~% s6 _; q  #stdout {}6 y0 ]$ y4 O  ^9 V! T/ @$ _
  elasticsearch {
9 @6 l) p7 Z( h# |# [    hosts => ["172.20.22.24:9200"]. k; q9 ^- x5 \8 w4 V! I+ D( E5 \
    index => "magedu-m63-test-%{+YYYY.MM.dd}"
, A: \2 p- v7 K9 y  |  }
3 A# n) e$ g7 K' A* Z' a6 {, m$ B}
$ a4 q1 |& d  V# /usr/share/logstash/bin/logstash -f test.conf( [2 g; t) P/ O: P& v. K
version1
/ n' |. c5 Z$ \5 E2 j9 W! zversion2
9 i% v" D8 B3 {% M6 Y' gversion3
) _+ L! h' ^% Y6 Ltest1
' i7 `6 H" F; b9 X& _3 X% ?! i: x/ y! Dtest2* c! E$ n3 E* X& U: A
test3
0 P( l5 v/ h" t, f7 W+ J0 I5 S4 p  h8 k; ]5 X: d. Z
####elasticsearch服务器查看收集到的数据0 j* r  U. a; H1 l
# ls -lrt /data/elasticsearch/nodes/0/indices/
( g  S# Q2 D& F, L& utotal 4; E7 C/ O) I4 `+ Y8 }8 \2 ~' J
drwxr-xr-x 4 elasticsearch elasticsearch 4096 Apr 13 14:36 DyCv8w7mTleuAvlItAJlWA
3 q8 T( r$ r7 M& I7 P; Bkibana " p. z9 v1 [+ P# n" \7 d2 B2 |: L
kibana为elasticsearch提供一个查看数据的web界面,其主要是通过elasticsearch的API接口进行数据查找,并进行前端数据可视化的展现,另外还可以针对特定格式的数据生成相应的表格、柱状图、饼图等
  w7 j) B. ]0 R( B/ ~) a5 F 1 e+ d1 o3 z8 @  u# A9 C
部署kibana
+ i2 B( F: c% |% V) Q# ls -lrt kibana-7.12.1-amd64.deb5 J* Z; V* B  F7 I# A; D
# dpkg -i kibana-7.12.1-amd64.deb; w2 ?& t  E2 H
# grep "^[^$|#]" /etc/kibana/kibana.yml
; {/ y/ L- _% @server.port: 56019 s. I" H5 }' w8 K
server.host: "172.20.22.24"
1 H) O+ L9 |# L6 Q  \2 K  M( p# [elasticsearch.hosts: ["http://172.20.22.27:9200"]% o% ^1 s/ }6 a- M
i18n.locale: "zh-CN"
# P  Y" d* U  K+ J# systemctl restart kibana & g, w; e% E: j
浏览器访问http://172.20.22.24:5601
* R9 t& H7 l1 a8 j; H : b8 n0 a, H+ N1 E1 |
Stack Management-->索引模式-->创建索引模式/ o/ R0 P8 Z) @$ b# M
) V# r& ]7 ]  e1 U$ S8 P
4 ^4 @% s0 ^+ V- l
选择时间字段
& C: g* T1 E5 y% p% m5 z, N5 E, Q! E8 Q; H: ^
查看对应创建的索引日志信息. f$ l% o0 I  `8 ~* D0 p

5 Q' c# s5 D( P* d- P6 w
  t1 r" ?* D/ G' z7 P- y
: o) g  l+ H) M. i  o1 [) ]6 [" ]4 O& u收集tomcat日志 ' v1 ~* E$ p. v" T
收集tomcat服务器的访问日志以及tomcat错误日志进行实时统计,在kibana页面进行搜索展现,每台tomcat服务器要安装logstash负责收集日志,然后将日志转发给elasticsearch进行分析,再通过kibana在前端展现* r& h  E, Z8 Y$ j
9 Z4 d# c# s) O
部署tomcat & m: s7 Y! U  Z+ @1 U9 M
####tomcat1,172.20.22.30
& N% P$ a( U5 X. T# apt install -y openjdk-8-jdk7 Q7 I+ z, \. x* n$ K7 t1 J
# ls -lrt apache-tomcat-8.5.77.tar.gz
" r: U$ A: g; ^  i, n/ l, d9 U-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz
$ l+ e- e3 v' y6 L7 m# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src/
, T9 ~) ^* {- R4 q7 n' _# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
# i% |, _2 w$ q: m& y- l# cd /usr/local/tomcat
% B2 |2 G) W7 D$ @, [  W% B###修改tomcat日志格式为json* S. p2 O! u* p% M
# vim conf/server.xml$ S5 ]& L4 |' N( V: {' \% i! a
....: ^9 _; ?+ k/ x& Q
1 A3 S6 U% h$ x7 t: g
....
  ]) H- c/ J, R/ e9 k6 Q0 H$ y. t# mkdir /usr/local/tomcat/webapps/myapp$ A9 _* ~! C% r! k4 I! u$ J8 S7 r
# echo "web1 172.20.22.30" > /usr/local/tomcat/webapps/myapp/index.html
8 y1 h: ^/ h- o  a- q% ]# ./bin/catalina.sh start
/ v0 j& @# [3 k4 q& |7 X
/ F. [3 E5 k: D& k& G9 R% X- I) l7 E###访问测试
: P4 f5 h! m! y0 @/ e2 ^# curl http://172.20.22.30:8080/myapp/
) x' g: ?3 Z9 l# O###查看访问日志- t; B9 _2 Y8 _4 E- e* j0 e/ x
# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-13.log' w; i$ ?) Y/ J' N+ \# i0 a0 W; E
  P% Z# R) G& N/ t, d, T4 T3 P
####tomcat2,172.20.22.26
$ v% h8 U$ [% h8 @1 s# apt install -y openjdk-8-jdk
& Q# R5 ^4 x# \" y# ls -lrt apache-tomcat-8.5.77.tar.gz
' ]3 I2 U( M) }6 K; p% i9 `! a; g-rw-r--r-- 1 root root 10559655 Apr 13 21:44 apache-tomcat-8.5.77.tar.gz
3 b7 e6 F1 Q3 o' w. m# tar xf apache-tomcat-8.5.77.tar.gz -C /usr/local/src/; U+ }4 i  d9 q' q
# ln -s /usr/local/src/apache-tomcat-8.5.77 /usr/local/tomcat
9 G& G, j; u9 j3 Q$ r3 s# cd /usr/local/tomcat
- ~4 S  W9 v- S###修改tomcat日志格式为json' d3 P7 e4 m7 \: R
# vim conf/server.xml2 F: V5 c# Y; e1 D3 C
....
2 Q# {) o" ?9 a% d, U- e' s
% f; |; d% A8 m+ m! X3 X. E....
6 d  q3 l( ^4 e5 x* k# mkdir /usr/local/tomcat/webapps/myapp
! h# v/ E2 `2 v1 Q" w2 M# echo "web2 172.20.22.26" > /usr/local/tomcat/webapps/myapp/index.html
. p5 I' ?2 `: _" R: d5 n! u  ~$ l# ./bin/catalina.sh start
% w8 A7 \; E5 o* \7 z3 S/ g2 B1 H
###访问测试
3 f/ n9 ]- L3 D9 `2 P' l; T7 c# curl http://172.20.22.26:8080/myapp/8 @/ D) ]$ h$ ^" o! P* |5 W( P
###查看访问日志
; \( q/ C# P* \' J( [1 b6 a# tail -f /usr/local/tomcat/logs/tomcat_access_log.2022-04-14.log
" k  ?3 k$ p7 }# i5 L部署logstash
) Z7 X' z0 ^; d3 \- s在tomcat服务器安装logstash收集tomcat和系统日志0 q4 A+ Q, f* U8 u7 Y
1 w4 k$ d, L" Q3 I
####tomcat1,172.20.22.30  y0 {3 k9 K# ~$ B
# ls -lrt logstash-7.12.1-amd64.deb
8 M, W7 H- v  n5 k/ R& _# dpkg -i logstash-7.12.1-amd64.deb8 ]: r6 X4 n( _" A
# vim /etc/systemd/system/logstash.service% B. x& ?( z3 b
..." {% C  y" ]- c. A' ^( a7 Z
User=root
4 l( o8 l, \& r. E' [Group=root
& v7 J' u" c/ s/ h...; s7 J2 g( r" e
# cd /etc/logstash/conf.d
9 t. u5 ^0 w) |" b' S# cat tomcat.conf
9 f4 x- P0 W0 b5 z0 v: tinput {
2 P+ S" I) m3 n3 x  file {
0 h% E9 ^. J$ x- z1 T    path => "/usr/local/tomcat/logs/tomcat_access_log*.log"
; p' B" {: D; |" b8 N' q    type => "tomcat-log": u+ R* g1 V8 ]3 E+ z
    start_position => "beginning"
  ~, t3 _- \7 ^    stat_interval => "3"
# X' c; I% _2 u( E  }" Y: y* m. d5 D5 _* h7 S
  file {
$ M( e( X* J3 r. x2 o4 o    path => "/var/log/syslog"8 O9 t9 a* C( V  b9 ~. P) _
    type => "systemlog"9 o# C8 l% d4 l: w
    start_position => "beginning"- a) Y' U4 \; w& P: |2 B
    stat_interval => "3"
( U! A; N  ?  N7 V2 L  }- L( B, V+ t* h$ Q! A1 c) Y
}
) s5 T& a* o( g4 ^5 _; |output {/ e* Y$ u  f1 l5 x) N; H, R
  if [type] == "tomcat-log" {2 f( v( p2 J/ A4 z# z
  elasticsearch {
3 X4 [0 k! A! w    hosts => ["172.20.22.24:9200","172.20.22.27:9200"]
& B; ~/ \7 A3 |5 l    index => "elk-tomcat-%{+YYYY.MM.dd}"/ x0 E$ S' V8 ~+ a
  }}5 z! n$ z8 e0 X7 e
  if [type] == "systemlog" {  u5 ^5 n4 f/ ~8 s" `. b
  elasticsearch {; K; v. b4 b6 S, G/ X, v% w" h
    hosts => ["172.20.22.27:9200","172.20.22.27:9200"]
! C# M3 g5 w- R0 l1 M% `/ ^# N    index => "elk-syslog-%{+YYYY.MM.dd}"! k+ X5 W& ]8 O. ]! H/ Q
  }}
; `. c! V) ]9 |5 ]& ~, P9 @  a% S}
$ _3 a! n" @+ N, z$ n: M7 B; V# N1 H. H; ]3 [4 B) o
# /usr/share/logstash/bin/logstash -f tomcat.conf -t
8 p% V- i9 x6 x4 ]/ e- n8 d0 |# systemctl daemon-reload. t, F! {1 H. ?* W( f" y
# systemctl start logstash.service) x2 A) p) c6 @& G$ f; b" }
# scp tomcat.conf root@3172.20.22.26, ~2 U& R1 D5 S
% A+ Y( m3 L" v- I. r
####tomcat2,172.20.22.26
6 S3 w8 \1 D- I( X0 ^, e6 {# ls -lrt logstash-7.12.1-amd64.deb; L9 w% e: X( \1 _
# dpkg -i logstash-7.12.1-amd64.deb
7 R: G3 g3 {" p. o# vim /etc/systemd/system/logstash.service
. m1 A3 U6 N8 B8 p% B8 m...% t& J4 S) O9 q( Y4 e5 M+ X6 V
User=root* s5 r8 d: s7 {% o) `  |
Group=root6 g( @3 m2 x: A( r4 I% v5 K1 d
...
" C  t1 `1 q: Z! o; z  Z# systemctl daemon-reload
' f' e6 \! n  f3 I% A1 _/ N# systemctl daemon-reload7 O0 |8 O0 H' |
# systemctl start logstash.service 7 Z* q# w: u0 f- N& Y! X: F
通过kibana展现8 R+ N8 j4 _( K; w) X& Z
' [3 s2 g7 s2 k' m

3 p) @+ A! a8 N2 F  S6 x收集Java日志 / ?4 B* Z& J4 P( _  G& E3 p5 U
使用codec的multiline插件实现多行匹配,这是一个可以将多行进行合并的插件,而且可以使用what指定将匹配到的行与前面的行合并还是和后面的行合并- l& m0 r" Z* G& V

3 w  |7 @6 h" D7 l# x; Y. WMultiline codec plugin | Logstash Reference [8.1] | Elastic) e' F' b& |  ^1 f# z/ W

4 c0 h. N7 m! }! i% @) y添加logstash配置文件 : J8 Q- I$ q- m
###收集logstash自身的日志,172.20.22.26
# d* v& y; O5 G# cd /etc/logstash/conf.d: j5 E- T7 r. N4 o
# cat java.conf 4 D3 ^3 H; j2 }: o- N; Z# C
input {
$ k2 I. w8 g4 X5 h0 h& u- V  file {
' r2 q1 g( D1 F) ^( h' V& }( ~    path => "/var/log/logstash/logstash-plain.log"
, x$ r' g" l7 B; {    type => "logstash-log"
, T$ N3 |- h7 ]% _4 S# W" _% r6 F    start_position => "beginning". C3 [% [: k1 @' w; P
    stat_interval => "3"
; a8 d, n! y7 j$ g    codec => multiline {
5 n4 u% v, `$ h0 n  d      pattern => "^\[") i8 e3 V# D! @+ R% _- [
      negate => true
' Q- m8 B4 B/ E; K      what => "previous" 3 {1 u* l2 d1 b( F% W( d0 n
   }}
4 \7 g* n% A' T}
+ `! p* y/ ]1 e& @: y+ ioutput {7 W- M) G2 S7 l! G4 I6 Q
  if [type] == "logstash-log" {
* K3 d$ Z) D+ M9 d3 G# q4 I5 e- A  elasticsearch {
0 E% m' N/ u7 o& k8 _4 e    hosts => ["172.20.22.24"]2 H- g$ q* H. P' Y; g# L& U
    index => "logstash-log-%{+YYYY.MM.dd}"4 {3 h8 Y' t" D( p. t; z
  }}: D1 p* B* d7 K
}8 A" [+ Z9 D. `8 r

% L; [5 g; m7 m7 l7 y# /usr/share/logstash/bin/logstash -f java.conf -t
$ T+ \2 {( J3 d6 g' U# systemctl restart logstash.service
, ]9 a2 S  V( p) V9 i5 w$ g6 n' z
###收集logstash自身的日志,172.20.22.30  k3 S) u; c" k* q/ |
# cd /etc/logstash/conf.d
5 P# f( U6 C- `7 V! }# cat java.conf
' k( l9 B! t6 ]' H" F3 y4 r) Kinput {5 I; U: S4 i: P. O2 |/ D6 g
  file {/ P3 d8 V& p* G
    path => "/var/log/logstash/logstash-plain.log"
' n* S9 Z# r* }    type => "logstash-log"3 A# ]8 _6 [4 n1 @
    start_position => "beginning": u2 {. b# d& D5 {1 o6 Y
    stat_interval => "3"" C( O- u1 ^& L2 Q" P
    codec => multiline {% z  |5 l( V" r5 O1 l- w
      pattern => "^\["" d9 ^/ H, Q) M4 ~0 t
      negate => true8 \* U% U; _3 G8 h. L, }, S( _
      what => "previous"
/ g& U1 S, I% ~5 Y   }}
1 B/ a$ W5 I- v# t  E4 a1 V}
9 W: \4 n7 u  a3 K. H8 _8 Joutput {# ]1 f8 u4 m0 J9 D+ W) c' o
  if [type] == "logstash-log" {
* h4 l- l, s& |' z- {, z  elasticsearch {
9 z$ @! X* ]; c8 g# y2 t0 e5 p& |    hosts => ["172.20.22.24"]
' i& E' i( `3 w7 ~    index => "logstash-log-%{+YYYY.MM.dd}"( l2 h4 n) m/ U: A2 h. a
  }}
/ b; G' n7 k* C7 `5 r- s- g}
- P7 @* w* e" [& t4 C; E' W( S, y
  w, M& k' `. g5 y' J. h# /usr/share/logstash/bin/logstash -f java.conf -t8 ^6 V& q* {; ?+ b9 t; l4 O
# systemctl restart logstash.service
" D) s; G$ j1 ]9 Q查看kibana收集到的日志( T/ Y5 o  ~" F. f0 o4 N. j

4 A; o2 z, ^" _
5 \* |& z6 E3 F* ^+ j& D
( N5 b, Y2 G& m5 e' _0 k8 Ifilebeat结合redis、logstash收集nginx日志
  ^% C8 \' `" m% e8 U使用filebeat收集日志发送到logstash1,再由logstash1发送到redis,最后再由logstash2发送到elasticsearch
5 i, Y6 O* u- v3 o% R# g
/ T# z% c" F2 Hweb1:172.20.22.30,部署好nginx、filebeat、llogstash& M9 c+ P0 ~) f& ^, X

. m- e6 @; d; {1 @web2:172.20.22.26,部署好nginx、filebeat、llogstash
7 P# d, g2 J4 X4 `8 {$ ` - w0 @7 s+ j& Z0 `+ H; P# }
logstash服务器2:172.20.22.23,redis服务器:172.20.23.157
* |. d6 Q! _% F 4 K8 }! F# B6 M- r, @  t2 g5 Q4 V4 p
nginx服务器相关配置   E9 x1 I; P+ P1 g1 L0 }8 @8 ?
部署nginx
) a' u6 S, r: q' g( \/ n" ~4 G3 o( D# wget http://nginx.org/download/nginx-1.18.0.tar.gz
' `/ D/ ]& N* x0 ?. D# tar xf nginx-1.18.0.tar.gz1 H8 h' w, Y5 n2 N- b
# cd nginx-1.18.0
( t5 p, [/ O6 e0 V& o6 D* a# ./configure --prefix=/usr/local/nginx --with-http_ssl_module- ~" s/ A: u* d' j. z) p
# make -j4 && make install
* }# P# y$ v9 \6 E" _# /usr/local/nginx/sbin/nginx : L" R# @$ y+ ^
部署配置logstash : Y. L4 ?) E: S& k  z
把filebeat收集到的日志信息发送到redis$ J  B% n- U+ B- w" G% g
+ p+ f) p/ r9 K" E7 {, w: }
# apt install -y openjdk-8-jdk( J. s+ G/ T( h7 \- v( a; ^
# dpkg -i logstash-7.12.1-amd64.deb8 H% t* i6 Z1 H
# cat /etc/logstash/conf.d/beats-to-redis.conf
/ K& d" Y! p( a7 h' vinput {
+ @; B* _1 r/ ?  beats {
; Z$ b6 _* A1 u9 R  o    port => 5044
: f* i, B" f% Z; h  F- ]    codec => "json"4 S# T& X! d8 ^: m8 n# d# U* w
  }
* y4 s3 n% d- s  beats {
1 M  s& `1 p! N, [( K' R% _* i# A* M    port => 5045
; ^' V" M7 X! e0 [$ l$ b8 _& x6 S    codec => "json"
6 W- S) s% T1 b2 b0 _9 {  }: v# \: O1 e* [# H- K1 G9 s
}
# ?9 r! T& v! Z7 q5 voutput {' G" _2 H& B" N$ V& k
  if [fields][project] == "filebeat-systemlog" {
) P; c' L9 X/ H$ o- k5 Z    redis {% h) W# Q  y! B% @4 U
      data_type => "list"' F8 V# x" S0 U7 Y
      key => "filebeat-redis-systemlog"
6 M) z1 a3 B/ Q3 ^0 B      host => "172.20.23.157"+ r9 p+ B0 j+ Y. x
      port => "6379"( y3 K# `, x; H) P* K/ @; A
      db => "0"
+ n0 m/ E" j  e: Z5 P      password => "12345678"( b, ?* y, K- l# t) E- d+ l
  }}! ]+ H7 M+ b) V/ t# B
  if [fields][project] == "filebeat-nginx-accesslog" {" |1 T2 V. K0 _4 w+ C8 U
    redis {
* b+ m/ S" G* _% L& W      data_type => "list") ]2 g5 j- O' l( R/ A
      key => "filebeat-redis-nginx-accesslog"
9 q1 n3 s3 H5 K      host => "172.20.23.157"/ I) }2 j7 h/ K6 q
      port => "6379"
$ _3 ~9 J. X- b8 d& ~- l      db => "1"$ k: q& F  A3 i$ H, B- r# w
      password => "12345678"7 E: y) J$ ]7 K5 d7 m6 s5 X/ q
  }}" X# b1 T2 q6 M" p, m
  if [fields][project] == "filebeat-nginx-errorlog" {
! V3 f5 O" U, g$ E( p# {! ?" b    redis {
4 z: m# V1 y0 S: e      data_type => "list"9 h( X, S) H* N. H) H" y
      key => "filebeat-redis-nginx-errorlog"9 P/ F$ \2 I: a6 J# G
      host => "172.20.23.157"2 j) S: `2 C+ A4 q: j
      port => "6379"
: r" [4 |) Y  [+ Q; g% D) i      db => "1"" j9 e6 ]8 g# U: g/ Q
      password => "12345678"! o# g: y- n& v, q, F& p9 p! b  Z
  }}
2 ?6 G8 @/ A$ N2 z: b0 h( E. F}
: t* |9 _8 L2 j4 {# systemctl start logstash7 r5 o/ V! `6 y# _9 j7 @
# scp /etc/logstash/conf.d/beats-to-redis.conf root@172.20.22.26:/etc/logstash/conf.d/
/ n9 K5 a2 \" n) d部署配置filebeat 3 s) ^0 v; q- C7 @% q7 N8 U
通过filebeat收集日志信息发送到logstash5 t  @: s. ~- V# z  d
# ~& f# H, H+ |1 H; c
# dpkg -i filebeat-7.12.1-amd64.deb
2 q) D! B* k  s, z. b# grep -v "#" /etc/filebeat/filebeat.yml | grep "^[^$]"
* ?1 B4 _. o, zfilebeat.inputs:
' `; z! u. ]0 L- type: log( ^$ |& ^3 c0 e% c3 x
  enabled: true8 q' L! f9 {) g3 `( g/ H  E5 F4 e
  paths:  A8 G( U) T  i  {  y# j
    - /var/log/syslog6 U# `8 X. n( J# U4 y0 x  ]
  fields:
& S, M% r  \& U8 c4 x    project: filebeat-systemlog3 e( v0 v3 [, i
- type: log  B* }3 T& B8 o& y" ^% a* E
  enabled: true
; {7 ?; T* o  g+ L  paths:
  f8 y3 N0 J* d" |& o; R4 c+ ]    - /usr/local/nginx/logs/access.log* ?; U' Y( R+ ~3 C5 m  Y. p/ e& ?. s
  fields:! G( W7 V' i9 q* m
    project: filebeat-nginx-accesslog
, O& a7 P& y+ H3 {1 E+ k& \0 n- type: log
( @6 e2 g' r; S$ u5 |  enabled: true1 k) s, U+ y+ _6 {5 P6 [, F
  paths:
! F! u" w# ]% M8 c    - /usr/local/nginx/logs/error.log
7 B) U4 a+ f" W; g2 s  fields:
7 l' ~# C5 _2 k$ r    project: filebeat-nginx-errorlog, b- F9 B/ ?& ]
filebeat.config.modules:
) k- S- }5 [% w' Y9 n# ?  path: ${path.config}/modules.d/*.yml
2 X' Z/ g; l) w8 {  reload.enabled: false
$ C5 ?6 w; `/ u" d7 Rsetup.template.settings:0 w9 v7 V( W$ k  X( v4 ]# p& W
  index.number_of_shards: 1
' K* _- x6 q2 b/ S9 h. `setup.kibana:# g/ J5 I1 U* j2 H  @
processors:
+ s! P9 V% |$ l! I. v7 Y- t  - add_host_metadata:
1 |! f. A* z2 r+ I' a) ?      when.not.contains.tags: forwarded5 e; ?! r  f: D/ m5 R
  - add_cloud_metadata: ~
5 w& {" Y7 e% E% c" t  - add_docker_metadata: ~2 i- V8 }$ ~' O/ @
  - add_kubernetes_metadata: ~  p& c* c+ i. n, B' n. d
output.logstash:
. \/ w' r! f; T, _  hosts: ["172.20.22.30:5044","172.20.22.30:5045"]" A) E* z4 M6 g3 s% i' O4 z( H& m3 L
  enabled: true
1 W+ ^2 A; o- i& o. m: B, R( q) M  worker: 2
* Q; ]6 h/ D. {# E* V+ }, Z& y0 C  compression_level: 3
- _: K+ \& n% m  loadbalance: true
2 q) X: w+ k' z1 @/ u4 G% H" P7 }( Z
# systemctl start filebeat
; i) c& b5 v, u# scp /etc/filebeat/filebeat.yml root@172.20.22.26:/etc/filebeat/ ' [3 {2 z, P# X% q. L0 g
logstash服务器配置
, i% c, `' }6 v( [  k+ W, v, |3 b& Zlogstash服务器2:172.20.22.23,把redis缓存的日志发送到elasticsearch6 H9 Y0 d+ Q1 }; {% }1 a
+ L! W* k* p3 d$ x) Q
# apt install -y openjdk-8-jdk. n' Y) ^/ W- t3 Y, g& a; Q
# dpkg -i logstash-7.12.1-amd64.deb! \% T; [8 \. b5 d7 a! U2 ~
# cat /etc/logstash/conf.d/redis-to-es.conf
/ {& d7 c  u- x% B& h$ cinput {
/ L8 O% `4 G' j2 `; P2 D  redis {7 m2 `5 z" v) K$ r) \. u" h
    data_type => "list"
! j  ]# O* w% Z* M) r4 z* D5 A    key => "filebeat-redis-nginx-accesslog"  `: m# n& y( Q- K# P) Z
    host => "172.20.23.157"9 X7 `1 o) {$ x1 f% z5 K
    port => "6379"" Z- T7 m2 v1 v0 i% ~' f" b
    db => "1"
# Z! l. J9 e* ~' k    password => "12345678"
* _3 A* `/ V. X5 F% k  }' Z* S: c" Q5 l2 y% I7 V
  redis {* E/ Q! X7 n( ]& i
    data_type => "list"2 Y0 y3 J1 _; X4 s0 V
    key => "filebeat-redis-nginx-errorlog"
8 \* }: b8 a' D1 [. E, @% i    host => "172.20.23.157"
4 x  g7 |! N' m: I& b    port => "6379"$ n& D% r0 ?' M1 n" Q0 [: O; g
    db => "1"
' Z+ S1 s" l, Q2 b2 T4 Q    password => "12345678"2 M5 W5 S  P* n. a
  }
% T1 O0 T. B, s2 u/ K  redis {
8 S  U( f1 `+ @/ x7 q6 c& Y    data_type => "list"
* ^7 C& z+ c2 a: o5 K: _    key => "filebeat-redis-systemlog"  D1 e1 }- v0 l, a$ S
    host => "172.20.23.157"
# N/ s4 m2 m+ \" O5 ?- l    port => "6379": |" O! n5 x6 R. Z$ ?1 s
    db => "0"
3 Y1 G7 i) a! P' j    password => "12345678"
. ]4 `+ d9 R5 d7 G  }
' s) ~9 W) Q9 J0 u# O}. v) C9 Y9 n+ M5 T
output {
0 c) @: S; \& ]1 P; q  if [fields][project] == "filebeat-systemlog" {
% Q1 N: _% ]! Z. ^8 @+ q    elasticsearch {
- ~1 u3 ?3 ]% K  r6 |' t# H      hosts => ["172.20.22.28:9200"]
: u, x6 I# P# Z5 U& z. T      index => "filebeat-systemlog-%{+YYYY.MM.dd}"4 x" u% T  Q/ Q4 F3 ^0 Y
  }}
+ Z, ~# K/ Q! \+ w, X2 u# n7 s) z  if [fields][project] == "filebeat-nginx-accesslog" {
. W  M# F4 J7 C. W3 R    elasticsearch {/ c( i$ J7 e2 Q3 r; t- `
      hosts => ["172.20.22.28:9200"]
5 h1 ^# }3 b1 y/ c( R      index => "filebeat-nginx-accesslog-%{+YYYY.MM.dd}"- T) N2 v1 u! i& S
  }}. R1 C3 y. Z" p5 |
  if [fields][project] == "filebeat-nginx-errorlog" {
4 T! S2 F/ \4 R) K$ z2 E4 q8 u    elasticsearch {
9 y' P. w, ^6 N( P% e, l' K      hosts => ["172.20.22.28:9200"]
" c, ?' I: b+ M( N* G" D! l      index => "filebeat-nginx-errorlog-%{+YYYY.MM.dd}"- |0 C* e# R9 H& h- i9 S. ^
  }}) g4 k( p6 U/ a/ U% E9 G
}/ f9 t) s* x7 `& y4 x) ^
# systemctl restart logstash.service
4 Z2 a$ S9 z, X% j; H+ fredis安装配置 & D" h2 n3 j: c  H5 `* i
redis服务器:172.20.23.157,
9 r) p5 w3 K3 ~: W $ Q# d) |6 G, s3 t1 _
# yum install -y redis7 B  |( |2 ]3 d" v) L  i4 v
# vim /etc/redis.conf  D9 `: ?  {" Y! b  o, D
####修改以下配置项5 z% d+ u, P( F# m* f
bind 0.0.0.0
8 A$ R6 `/ i% j....
( t3 L" w0 R6 p9 h5 W$ Zsave """ P" L$ \7 q, b9 K8 L
....
1 X+ ~& C$ |5 r$ h$ W3 ?- |% b, }requirepass 12345678
4 q- v; v: b6 e, K....9 p& e+ e3 B+ m) m) Q
# systemctl start redis
) r, q+ d8 ~& T/ n###测试连接redis
0 s* [% e& M, Y, k: K# redis-cli
4 H" i% H) }! }127.0.0.1:6379> auth 12345678
$ G6 V: _, O. y8 \OK
2 o/ S" v3 q6 p3 K/ v127.0.0.1:6379> ping# {1 B. c- W# o
PONG
/ a& N+ O( b& I7 w9 q( D* G0 B# @) v% p8 r
###验证收集到的日志信息- c" {3 E  A3 Z, F, Z4 x
127.0.0.1:6379[1]> keys *. Q# V) d1 }; f* q9 ~  g1 \
1) "filebeat-redis-nginx-accesslog"; o6 _/ U/ T+ u+ ?8 ~& }
2) "filebeat-redis-nginx-errorlog"$ s8 N: F1 q. R
127.0.0.1:6379[1]> select 0
" F$ _9 N, ~1 v/ \3 R# }- \OK
2 ]! n" B( j4 A" ?' d3 \* z127.0.0.1:6379> keys *6 M+ w- V# F! ]) {
1) "filebeat-redis-systemlog"
+ x, f$ N" ?  s% D6 d& l通过head插件验证生成的索引. _/ E3 H8 D- Z& `+ c$ P5 ]
0 N3 b/ O5 w- n2 I' B2 f  t

4 e/ m5 }6 w7 l: b9 Gkibana验证收集到的日志信息
4 \* D- G$ J0 `/ C8 G

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

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