Contents
elkb安装
ELK是比较经典的集中式日志管理方式, 现在又多了个B.
E-Elasticsearch, 全文索引数据库, 这里用于存储日志
L-Logstash, 日志采集或解析
K-Kibana, 日志可视化
B-filebeat, 轻量级的日志采集工具
参考:https://www.cnblogs.com/yylyhl/p/17283794.html
参考: https://blog.51cto.com/zhanjq/5576459
参考: https://www.cnblogs.com/jiangcong/p/14683318.html
参考: windows下elk安装配置-elasticsearch/kibana/filebeat
TL; DR
使用ELKB套件,实现微策云日志的集中式管理.
-
解压elkb压缩包 (20mins左右, 解压无法使用多核特性)
-
cmd安装elasticsearch服务:
elkb\elasticsearch-8.7.1\bin\elasticsearch-service.bat install
-
cmd安装kibana服务:
elkb\kibana-8.7.1\elkb-kibana.exe install
-
cmd安装logstash服务:
elkb\logstash-8.7.1\bin\elkb-logstash.exe install
-
powershell安装filebeat服务:
elkb\filebeat-8.7.1-windows-x86_64\install-service-filebeat.ps1
-
到“服务”界面,依次开启elsticsearch-xxx服务,ELK-kibana服务, ELK-logstash服务,filebeat服务
-
filebeat日志收集: 自动监测
D:\vivachekcloud\server\logs\*.log
微策云日志的更新, 并传输到logstash中. -
logstash日志解析: 接收filebeat的输入, 使用grok正则工具等,解析日志内容(比如traceId,logLevel等), 并传输到elasticsearch中.
-
elasticsearch日志索引: 接收logstash的输入, 建立index(类似MySQL中database的概念). 用于快速查询
-
kibana日志可视化: 提供可视化界面, 查询elasticsearch中的数据.
-
登录kibana:
http://localhost:5601/
,账号
elastic
,密码
123456
,点击图标“三”->Observability->Logs->Streams.
如果
D:\vivachekcloud\server\logs\*.log
已有日志,会在右侧显示时序的日志信息.使用kql进行日志过滤和筛查.kql 查询语法: https://developer.aliyun.com/article/1135069, https://www.elastic.co/guide/en/kibana/7.7/lucene-query.html

提供一个demo演示地址:
ELK安装地址: 120.26.94.79 administrator Nova2019
filebeat地址: 47.96.7.96
中安装了filebeat服务, 进行日志收集(收集这个路径下的日志: /cjdata/vivachek_test/10*-*9802/logs/*.log
)
kibana登录地址: http://120.26.94.79:5601/
elastic 123456
三天后关闭.
Windows下elasticsearch安装
下载解压
下载压缩包,解压
这里是指定目录
cd D:\vivachekcloud\elkb\elasticsearch-8.7.1\bin
安装为服务
安装为服务,并启动:
.\elasticsearch-service.bat install
.\elasticsearch-service.bat start
windows的服务面板中,可以看到一个Elasticsearch 8.7.1 (elasticsearch-service-x64)的服务。
如果只想手动启动一次elasticsearch, 也可不安装为服务,直接执行:
# 可以先直接命令行启动一遍,elastic账户的初始密码和kibana的entrollment token会输出在控制台
# 也可以稍后重设密码(自定义)
.\elasticsearch.bat
设置密码
参考: https://www.jianshu.com/p/9f33028fa65a
elasticsearch-setup-passwords.bat interactive --url http://127.0.0.1:9200
或者也可以使用https
D:\vivachekcloud\elkb\elasticsearch-8.7.1\bin>.\elasticsearch-setup-passwords.bat interactive --url http://127.0.0.1:9200
******************************************************************************
Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This command will be removed in a future release.
******************************************************************************
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
passwords must be at least [6] characters long
Try again.
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
这里默认会自动创建几个账户, 需要为其设置密码.这里都设为了 123456
浏览器访问
#浏览器地址
http://127.0.0.1:9200
#账号
elastic
#密码
123456
会弹出账号密码输入框, 输入账号密码,返回json结果.
附录: 重设密码
elasticsearch 重设密码
.\bin\elasticsearch-reset-password.bat -u elastic
# elastic账号重设密码
.\bin\elasticsearch-reset-password.bat -u elastic --url http://127.0.0.1:9200 -i
#kibana默认账号重设密码
.\elasticsearch-reset-password -u kibana_system -i --url http://127.0.0.1:9200
如果报错: WARN org.elasticsearch.common.ssl.DiagnosticTrustManager – failed to establish trust with server at。。。, 使用后面指定url的形式。
-i
参数进入交互模式,允许指定密码。
-u
参数指定用户.
目前密码:123456
附录: ip变更时,重设证书
一般不会遇到这种情况.可以略过.
https://discuss.elastic.co/t/elastic-8-7-enrollement-token-failed-to-establish-trust-with-server/330856/6
I see, it seems your ES node's public IP address changed.
我明白了,看来你的 ES 节点的公网 IP 地址发生了变化。
Again, to get a new certificate from your Elasticsearch central CA follow the link from the previous message and use the ./bin/elasticsearch-certutil http (from where you have ES installed) and follow the prompts.
安全访问: https://codeleading.com/article/12106033759/
附录: 允许公网连接
修改解压目录config\elasticsearch.yml
network.host: 0.0.0.0
试了下,好像跑不起来. 目前暂时用不到,之后再试.
附录: 修改服务名
regedit 注册表编辑器
计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
修改Display Name, 加个ELKB-
前缀.
生效需要重启计算机.
Windows下Kibana安装
前面安装好了elasticsearch,这是个搜索引擎. 可视化界面kibana需要单独安装.
下载解压
下载压缩包,解压(注意,要和elasticsearch版本一致)
这里是特定的目录
D:\vivachekcloud\elkb\kibana-8.7.1
配置
可以直接设置config\kibana.yml
的配置。
参考: https://stackoverflow.com/questions/71890575/kibana-unable-to-retrieve-version-information-from-elasticsearch-nodes-missing-a
elasticsearch.hosts: ["http://localhost:9200"]
elasticsearch.username: "kibana_system"
elasticsearch.password: "123456"
取消上面三行的注释, 配置elasticsearch的地址, kibana连接账号,密码 (前面安装elasticsearch的时候设置过相应kibana_system这个系统账号的密码)
单次启动
cmd或powershell中执行如下命令
.\bin\kibana
然后浏览器输入:http://127.0.0.1:5601
进入elasticsearch连接设置界面,
使用elatisc 123456这个账号登录.
也可以使用entrollment token的方式(这里没有采用这种方式,这里只是介绍一下).
在elasticsearch中重新生成kibana的entrollment token (30分钟有效期),需要在elasticsearch的bin目录下执行如下命令:
#进入es目录 cd D:\vivachekcloud\elkb\elasticsearch-8.7.1\bin #生成看kibana所所需的entrollment token .\bin\elasticsearch-create-enrollment-token -s kibana --url "http://localhost:9200"
会在kibana安装目录的config目录下,自动生成如下的配置kibana.yml.
没问题的话,可以先停掉cmd中打开的kibana. 下面我们安装为服务.
kibana安装为服务
默认的kibana是没有服务的(windows下)
下载winsw工具(也可以复制vivachek_server.exe和vivachek_server.xml,然后改下xml文件)。
复制到kibana目录,winsw.exe 改名为 elkb-kibana.exe, winsw.xml改名为elkb-kibana.xml.
elkb-kibana.xml改为如下内容:
<configuration>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>ELKB-kibana</id>
<!-- Display name of the service -->
<name>ELKB-kibana</name>
<!-- Service description -->
<description>ELKB-kibana service</description>
<!-- Path to the executable, which should be started -->
<executable>D:\vivachekcloud\elkb\kibana-8.7.1\bin\kibana.bat</executable>
<!--<arguments></arguments>-->
<log mode="reset"/>
</configuration>
打开cmd / powershell(管理员),进入kibana主目录,执行如下命令
.\elkb-kibana.exe install
即可安装为服务。
配置服务依赖(elasticsearch启动后启动此服务):
sc config "ELKB-kibana" depend= "elasticsearch-service-x64"
注意等号后面的空格。注意这条命令需要在cmd中执行,powershell不行。
附录: 允许公网连接
修改解压目录config\kibana.yml
server.host: "0.0.0.0"
Windows下logstash安装
Windows下logstash安装
https://www.elastic.co/guide/en/logstash/8.7/installing-logstash.html
https://www.elastic.co/guide/en/logstash/8.7/running-logstash-windows.html
下载解压
下载,解压, 到bin目录
D:\vivachekcloud\elkb\logstash-8.7.1\bin
确认logstash能正常启动
logstash.bat -e "input { stdin { } } output { stdout {} }"
The
-e
flag enables you to specify a configuration directly from the command line.上面的指令会在当前命令行启动logstash, 从 标准输入接收数据, 并输出到 标准输出 (简单的回显).
进入logstash解压目录下,
新建first-pipeline.conf文件
input { beats { port => "5044" } } #filter { #} output { stdout { codec => rubydebug } }
进入bin目录,启动logstash
#测试 .\logstash.bat -f first-pipeline.conf --config.test_and_exit #启动 .\logstash.bat -f first-pipeline.conf --config.reload.automatic
注意,这里配置文件没有
bin\
前缀, 默认的工作目录在解压目录.而不是bin目录一个简单的logstash配置大概就是这样. 从指定的input接收输入, 通过filter做一些过滤和解析操作, 然后输出到指定的output. 这样的一个流程称之为pipeline.
微策云业务相关配置
config\logstash.yml
, 其中指定了logstash功能相关的配置(不需要调整).
config\pipelines.yml
,其中指定了logstash中多个pipeline的配置. 当启动logstash.bat, 并指定了-e
从命令行接收配置, 或者-f
从文件接收配置时,默认只能建立起一个pipeline. 而不指定参数时, 就使用config\pipelines.yml
中的设置建立pipeline, 可以建立多个.
配置微策云相关的pipeline
config\pipelines.yml
中配置有哪些pipeline.
- pipeline.id: vivachek_log_pipeline
path.config: "config/pipelines/vivachek_log.conf"
如果还要加其他pipeline,可以按yaml的格式,继续加其他配置文件.
新建配置文件config\pipelines\vivachek_log.conf
, 添加微策云日志相关的配置.
如下内容:
# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.
input {
beats {
port => 5044
}
}
filter {
#logstash条件判断语句 https://blog.csdn.net/sxf_123456/article/details/77773953
#logstash条件判断语句 https://blog.csdn.net/qq_36025814/article/details/108844128
#logstash监测多个不同的file path目录文件输入源,判断采集message输入来源 https://blog.csdn.net/zhangphil/article/details/127671736
if ([log][file][path] =~ "log_vivachekcloud") {
# grok是正则匹配插件
# grok在线调试: https://grokdebugger.com/
# grok插件文档: https://www.elastic.co/guide/en/logstash/8.7/plugins-filters-grok.html#_custom_patterns
# java相关的正则参考: https://github.com/daschl/grok/blob/main/patterns/java.pattern
# Oniguruma syntax https://www.cuminas.jp/sdk/regularExpression.html
# 日期时间正则参考: https://stackoverflow.com/questions/64027423/parse-the-date-with-format-yyyy-mm-dd-hh-mm-ss-ssssss-with-ingest-node-processor
# 日期时间正则参考: https://stackoverflow.com/questions/69939659/grok-pattern-for-timestamp-in-the-format-yyyy-mm-dd-hhmmss
grok {
match => {
#log4j2 pattern layout说明: https://logging.apache.org/log4j/2.x/manual/layouts.html#pattern-layout
#log_vivachekcloud.log使用的log4j2 pattern: %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{traceId}] [%t-%L] %-5level %logger{36} %L %M - %msg%xEx%n
#logstash内置字段类型: https://blog.csdn.net/weixin_42854038/article/details/105577140
"message" => "(?<timestamp>%{YEAR}-%{MONTHNUM}.%{MONTHDAY} %{TIME}) \[(%{WORD:traceId}|)\] \[(?<threadId>(\S+))-%{WORD:lineNumber}\] %{LOGLEVEL:logLevel}(\s+)(?<logger>(?:[a-zA-Z0-9-]+\.)+[A-Za-z0-9$]+) %{WORD} %{WORD:method} - %{GREEDYDATA:detail}"
}
}
} else if ([log][file][path] =~ "log_his") {
grok {
match => {
#log_his.log使用的log4j2 pattern: %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{traceId}] [%t-%L] %-5level %logger{36} %L %M - %msg%xEx%n
"message" => "(?<timestamp>%{YEAR}-%{MONTHNUM}.%{MONTHDAY} %{TIME}) \[(%{WORD:traceId}|)\] \[(?<threadId>(\S+))-%{WORD:lineNumber}\] %{LOGLEVEL:logLevel}(\s+)(?<logger>(?:[a-zA-Z0-9-]+\.)+[A-Za-z0-9$]+) %{WORD} %{WORD:method} - %{GREEDYDATA:detail}"
}
}
} else if ([log][file][path] =~ "error") {
grok {
match => {
#error.log使用的log4j2 pattern: %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{traceId}] [%t-%L] %-5level %logger{36} %L %M - %msg%xEx%n
"message" => "(?<timestamp>%{YEAR}-%{MONTHNUM}.%{MONTHDAY} %{TIME}) \[(%{WORD:traceId}|)\] \[(?<threadId>(\S+))-%{WORD:lineNumber}\] %{LOGLEVEL:logLevel}(\s+)(?<logger>(?:[a-zA-Z0-9-]+\.)+[A-Za-z0-9$]+) %{WORD} %{WORD:method} - %{GREEDYDATA:detail}"
}
}
} else if ([log][file][path] =~ "log_trace") {
grok {
match => {
#log_trace.log使用的log4j2 pattern: %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{platform}:%X{sn}] [%X{traceId}] %-5level - %msg%xEx%n
"message" => "(?<timestamp>%{YEAR}-%{MONTHNUM}.%{MONTHDAY} %{TIME}) \[(%{DATA:platform}|)(\:)(%{WORD:sn}|)\] \[(%{WORD:traceId}|)\] %{LOGLEVEL:logLevel}(\s+)- %{GREEDYDATA:detail}"
}
}
}
date {
#日期时间解析
match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z", "ISO8601", "yyyy-MMM-dd HH:mm:ss Z", "yyyy-MM-dd HH:mm:ss.SSS" ]
#注入到index的时序字段
target => "@timestamp"
}
}
output {
elasticsearch {
hosts => ["http://localhost:9200"]
index => "vivachek-%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM}"
#index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
#user => "logstash_system"
user => "elastic"
password => "123456"
}
#stdout {
# codec => rubydebug
#}
}
这个配置文件表示:
从input中接收输入:从5044端口接收输入(稍后安装filebeat后,会将日志文件转发到这里).
然后在filter中做一些处理: 数据判断是log_vivachekcloud, log_trace, log_his, error 日志,分别做不同的解析.
最后向output中输出: 这里是elasticsearch.
安装为服务
默认的logstash是没有服务的(windows下)
下载winsw工具。复制到logstash目录,winsw.exe 改名为 elkb-logstash.exe, winsw.xml改名为elkb-logstash.xml.
elkb-logstash.xml改为如下内容:
<configuration>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>ELKB-logstash</id>
<!-- Display name of the service -->
<name>ELKB-logstash</name>
<!-- Service description -->
<description>logstash service</description>
<!-- -->
<!--<workingdirectory>D:\vivachekcloud\elkb\logstash-8.7.1</workingdirectory>-->
<!-- Path to the executable, which should be started -->
<executable>D:\vivachekcloud\elkb\logstash-8.7.1\bin\logstash.bat</executable>
<!--<arguments></arguments>-->
<log mode="reset"/>
</configuration>
打开cmd / powershell(管理员),进入kibana主目录,执行如下命令
.\elkb-logstash.exe install
即可安装为服务。
附录: logstash中pipeline配置
https://www.cnblogs.com/caoweixiong/p/11791396.html
https://blog.csdn.net/fu_huo_1993/article/details/116765492
https://www.cnblogs.com/caoweixiong/p/11791396.html
如何在ELK中解析各类日志文件 https://cloud.tencent.com/developer/article/1116059
接收filebeat的输入参考: https://www.elastic.co/guide/en/logstash/8.7/advanced-pipeline.html
附录: 多pipeline的使用
https://www.elastic.co/guide/en/logstash/current/multiple-pipelines.html
https://www.elastic.co/guide/en/logstash/8.7/multiple-pipelines.html
When you start Logstash without arguments, it will read the pipelines.yml
file and instantiate all pipelines specified in the file. On the other hand, when you use -e
or -f
, Logstash ignores the pipelines.yml
file and logs a warning about it. 当您不带参数启动 Logstash 时,它将读取 pipelines.yml
文件并实例化文件中指定的所有管道。另一方面,当您使用 -e
或 -f
时,Logstash 会忽略 pipelines.yml
文件并记录有关它的警告。
附录: ingest pipeline介绍和基本使用
https://www.elastic.co/guide/en/elasticsearch/reference/8.8/ingest.html
我应该使用 Logstash 还是 Elasticsearch 采集节点呢
Example: Parse logs in the Common Log Format
这里没有用到 elasticsearch的ingest pipeline. 这里只是和logstash做下辨析.
Windows下filebeat安装
参考: https://www.elastic.co/guide/en/beats/filebeat/current/index.html
先下载filebeat,解压
配置filebeat收集业务日志
filebeat解压目录下, filebeat.yml
# filestream is an input for collecting log messages from files.
- type: filestream
# Unique ID among all inputs, an ID is required.
id: vivachek-filestream-id
enabled: true
paths:
#- /var/log/*.log
- D:\vivacheckcloud\server\logs\*.log
#- c:\programdata\elasticsearch\logs\*
output.logstash:
# The Logstash hosts
hosts: ["localhost:5044"]
这里指定从指定位置接收文件流(一般是日志文件)
单次启动filebeat
.\filebeat -e -c filebeat.yml -d "publish"
删除filebeat的data\registry目录,重启filebeat,可以强制重新读取日志
合并多行日志
比如error.log中,很可能会打印java异常栈, 需要合并到一条记录中
参考: https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-input-filestream.html#_parsers
参考: https://www.elastic.co/guide/en/beats/filebeat/current/multiline-examples.html
参考: https://www.elastic.co/guide/en/beats/filebeat/current/regexp-support.html
调整后的filebeat.yml
# filestream is an input for collecting log messages from files.
- type: filestream
# Unique ID among all inputs, an ID is required.
id: vivachek-filestream-id
enabled: true
paths:
#- /var/log/*.log
- D:\vivacheckcloud\server\logs\*.log
#- c:\programdata\elasticsearch\logs\*
parsers:
- multiline:
type: pattern
pattern: '^([[:space:]]|###|;|Caused)'
negate: false
match: after
匹配模式(“nagate:false”), 匹配: 以空白字符, 或者“###”, 或者“;”, 或者“Caused”开头的行, 这样的行,合并到前一行的后面(after)
安装filebeat服务
powershell中进入filebeat解压路径, 执行
.\install-service-filebeat.ps1
如果执行失败,提示权限不足,可以按如下操作:
参考: https://www.cnblogs.com/urwlcm/p/4333119.html
管理员打开powershell, 更改策略,运行执行ps1脚本:
Set-ExecutionPolicy Unrestricted
附录: 修改服务名
regedit 注册表编辑器
计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
修改Display Name, 加个ELKB-
前缀.
生效需要重启计算机.
配置和查询微策云日志
在Kibana中查看微策云日志.
浏览器输入:http://127.0.0.1:5601
进入elasticsearch连接设置界面,
使用elatisc 123456这个账号登录.
第一次启动会有点慢.
左上角菜单(图标“三”) ->Observability->Logs->Stream.
配置index template以及lifecycle policy
前面在logstash中, 微策云相关的日志,都输出到了elasticsearch中
#...
output {
elasticsearch {
#...
index => "vivachek-%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM}"
}
}
大概会生成类似 vivachek-filebeat-8.7.1-2023.11
类似的index
我们把以vivachek开头的index, 都归到同一个index template中管理.再应用同一个lifecycyle policy.
创建index-template: 左上角菜单->Management->Stack Management->index-templates->create template->(name: vivachek
, index-patterns: vivachek-*
,一路默认next) ->create template.
创建lifecycle-policy: 左上角菜单->Management->Stack Management->Index Lifecycle Policies->create policy->(policy name: vivachek
, 勾选warm phase, 15days; 勾选 cold phase, 30days; 选中右下角垃圾箱按钮,出现delete phase, 60days)->save.
在Index Lifecycle Policies界面,找到vivachek这个policy, 点击右侧“+”,选择 “vivachek” 这个index template, 应用此policy.
关闭vivachek这个policy上hot phase中的rollover. (否则不设置rollover_alias的情况下会报错.) [Rollover | Elasticsearch Guide 8.11] | Elastic [Tutorial: Automate rollover with ILM | Elasticsearch Guide 8.11] | Elastic
配置显示微策云相关日志
左上角菜单->Observability->Logs->Stream->右上角Settings->(indices中,追加vivachek-*
; Log columns 中, add column, 选中traceId,logLevel,logger,log.file.path )->apply
使用Kibana查询微策云日志
进入 Observability->Logs->Stream (如果没有数据,可能点不开右边详情), 可以在此界面中查询相关日志(比如根据traceId过滤)

kql 查询语法: https://developer.aliyun.com/article/1135069 https://www.elastic.co/guide/en/kibana/7.7/lucene-query.html
发表回复