ELK日志分析学习记录
1. 下载安装包
官网下载地址:https://www.elastic.co/cn/downloads/
但是下载速度很慢,可以寻找国内镜像通过wget命令下载:
https://elasticsearch.thans.cn/downloads/elasticsearch/elasticsearch-7.3.2-no-jdk-linux-x86_64.tar.gz
https://mirrors.huaweicloud.com/logstash/7.3.2/logstash-7.3.2.tar.gz
https://mirrors.huaweicloud.com/kibana/7.3.2/kibana-7.3.2-linux-x86_64.tar.gz
2. elasticsearch安装和使用
elasticsearch的安装需要oracle jdk1.8的支持,由于centOS自带的是openJDK,所以需要重装jdk。
解压安装包
tar -zxvf elasticsearch-7.3.2-no-jdk-linux-x86_64.tar.gz
进行配置
cd elasticsearch-7.3.2/config
vim elasticsearch.yml
加入如下配置
# 将绑定地址设置为特定的IP
network.host: 0.0.0.0
# 设置访问端口
http.port: 9200
# 跨域设置
http.cors.enabled: true
http.cors.allow-origin: "*"
由于root用户无法直接启动elasticsearch,所以需要新建用户去启动elasticsearch
#创建用户组和用户
groupadd elk
useradd elk -g elk -p xxxx
#切换用户
su elk
#临时启动
./bin/elasticsearch
#后台启动
./bin/elasticsearch -d
浏览器访问 : 服务器ip+9200:
配置成功!
可能出现的问题:
- max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
#切换到root用户 修改/etc/security/limits.conf,在文件后添加如下配置
* hard nofile 65536
* soft nofile 65536
- max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
#提示虚拟内存太小,最大文件描述进程太小
#切换root用户 修改/etc/sysctl.conf 添加如下配置
vm.max_map_count=2621441
#保存后执行sudo sysctl -p /etc/sysctl.conf 使之生效
- JVM is using the client VM [Java HotSpot™ Client VM] but should be using a server VM for the best performance
#jvm HotSpot client VM 与 server VM
#修改文件
#JAVA_HOME\jre\lib\i386\jvm.cfg
-server KNOWN //原来在第二行
-client IF_SERVER_CLASS -server
-minimal KNOWN
- system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
#Centos不支持SecComp
#修改文件elasticsearch.yml
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
- the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts,discovery.seed_providers, cluster.initial_master_nodes] must be configured
#修改config目录下的 elasticsearch.yml文件
cluster.initial_master_nodes: ["node-1"]
2. logstash的安装使用
解压安装包
tar -zxvf logstash-7.3.2.tar.gz
进入文件目录
cd logstash-7.3.2.tar.gz
测试
./bin/logstash -e 'input { stdin { } } output { stdout { } }'
启动后直接命令行输入,可获得同样的输出
日志读取配置
新建一个conf文件名字随意,编辑以下内容:
input {
file {
path => "/usr/tomcat/apache-tomcat-9.0.31/logs/localhost_access_loh*.log"
type => "systemlog"
start_position => "beginning"
stat_interval => "3"
}
}
output {
if [type] == "systemlog" {
elasticsearch {
hosts => ["服务器ip:9200"]
index => "system-log-%{+YYYY.MM.dd}"
}
}
if [type] == "securelog" {
elasticsearch {
hosts => ["服务器ip:9200"]
index => "secure-log-%{+YYYY.MM.dd}"
}
}
}
这里配置的是读取tomcat日志,修改tomcat/conf/server.xml里的pattern参数
pattern="%h %l %u %t "%r" %s %b"
启动logstash
./bin/logstash -f logstash.conf
可能会遇到的问题:
- 找不到logstash-input-log4j插件
安装:
./logstash-plugin install logstash-input-log4j
- 内存不足
修改conf/jvm.options 参数到合适的值
vim conf/jvm.options
-Xms200m
-Xms200m
3. kibana的安装和使用
解压:
tar -zxvf kibana-7.3.2-linux-x86_64.tar.gz
修改配置
vim kibana-7.3.2-linux-x86_64/config/kibana.yml
server.port: "5601"
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://服务器ip:9200"]
#kibana中文设置
i18n.locale: "zh-CN"
启动kibana
./bin/kibana