目录
背景:因业务需求需要将nginx、java、ingress日志进行收集。
架构:filebeat+logstash+es+kibana
服务器规划:
192.168.7.250(es)
192.168.6.216(filebeat,es)
192.168.7.191(logstash,es)
官网:(Java Archive Downloads - Java SE 8u211 and later)
以下载jdk-8u391-linux-x64.tar.gz为例,并上传至服务器
mkdir -p /usr/local/java
tar xf jdk-8u391-linux-x64.tar.gz -C /usr/local/java/
#在/etc/profile中添加以下内容
export JAVA_HOME=/usr/local/java/jdk1.8.0_391
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
#立即生效
source /etc/profile
通过打印Java 版本验证 Java 安装校验:
java -version
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
tee /etc/yum.repos.d/elasticsearch.repo <<-'EOF'
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
?使用YUM源进行安装
yum install -y elasticsearch-7.17.6
修改配置文件:(/etc/elasticsearch/elasticsearch.yml)
#集群名称,三台节点必须一样
cluster.name: elasticsearch
#节点名称,三台节点必须都不一样
node.name: master
#是否有资格被选举为主节点
node.master: true
#是否存储索引数据
node.data: true
#数据存储位置
path.data: /elk/elasticsearch
#日志存储位置
path.logs: /var/log/elasticsearch
#设置绑定的ip,也是与其他节点交互的ip
network.host: 192.168.6.242
#http访问端口
http.port: 9200
#节点之间交互的端口号
transport.tcp.port: 9300
#是否支持跨域
http.cors.enabled: true
#当设置允许跨域,默认为*,表示支持所有域名
http.cors.allow-origin: "*"
#集群中master节点的初始列表
discovery.zen.ping.unicast.hosts: ["192.168.6.242:9300","192.168.6.170:9300","192.168.7.167:9300"]
#设置几台符合主节点条件的节点为主节点以初始化集群(低版本不适用此配置项,es默认会把第一个加入集群的服务器设置为master)
cluster.initial_master_nodes: ["master"]
discovery.zen.minimum_master_nodes: 2 # 为了避免脑裂,集群节点数最少为 半数+1
将/etc/elasticsearch/elasticsearch.yml拷贝至另外两台节点,并修改node.name、network.host、discovery.zen.ping.unicast.hosts参数,path.data及path.logs可自定义数据和日志存储位置
由于elastic官方未提供ik分词插件,需下载插件进行安装。(适配es7.17.6版本)
链接:https://pan.baidu.com/s/1_RGAzctJk17yJjHOb4OEJw?pwd=to96?
提取码:to96
/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///root/elasticsearch-analysis-ik-7.17.6.zip
将elasticsearch加入开机自启动,并立即启动。
systemctl enable elasticsearch.service --now
查看elasticsearch运行情况
curl -X GET "localhost:9200/"
cat /elk/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /elk-log/*/access.*.log
- /elk-log/*/error.*.log
scan_frequency: 10s
exclude_files: /elk-log/es-elk/*
tail_files: true
fields:
index_name: "tomcat_log"
- type: log
enabled: true
paths:
- /elk-log/*/*.log
exclude_files: ['/access.*', '/error.*', '/elk-log/es-elk/*']
fields:
tag_name: "java_log"
output.logstash:
hosts: ["192.168.7.191:5044"]
enabled: true
# filebeat 部署
docker run -d -it \
--name filebeat \
--restart=always \
--network host \
-e TZ=Asia/Shanghai \
-v /etc/localtime:/etc/localtime \
-v /elk-log/:/elk-log/ \
-v /elk/filebeat/data/:/usr/share/filebeat/data/ \
-v /elk/filebeat/logs/:/usr/share/filebeat/logs/ \
-v /elk/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml \
-v /var/lib/docker/containers:/var/lib/docker/containers:ro \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
docker.elastic.co/beats/filebeat:7.17.10
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/logstash.repo
[logstash-7.x]
name=Elastic repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
yum install -y logstash
目录结构:
数据目录:/usr/share/logstash/
配置目录:/etc/logstash/
日志目录:/var/log/logstash/
logstash.yml #配置Logstash的yml
pipelines.yml #包含在单个Logstash实例中运行多个管道的框架和说明
?
cat /etc/logstash/conf.d/tomcat-java.conf
input {
beats {
port => "5044" #收集主机5044端口
}
}
filter {
if [fields][index_name] == "tomcat_log" { #判断是否是tomcat_log标签进行执行以下操作
json {
source => "message"
}
}
else if [fields][tag_name] == "java_log" { #判断是否是java_log标签进行执行以下操作
grok {
patterns_dir => ["/etc/logstash/patterns"] #引用自定义gork规则文件
match => { "message" => "%{JAVA_TIME:javatime}"} #将message中的匹配JAVA_TIME规则的字段拿出来定义未javatime
}
date {
match => ["javatime","HH:mm:ss.SSS"] #重新定义时间格式
target => "java_time"
}
}
}
output {
if [fields][index_name] == "tomcat_log" {
elasticsearch {
hosts => ["192.168.7.250:9200","192.168.6.216:9200","192.168.7.191:9200"]
index => "tomcat-log-%{+YYYY.MM.dd}"
}
}
if [fields][tag_name] == "java_log" {
elasticsearch {
hosts => ["192.168.7.250:9200","192.168.6.216:9200","192.168.7.191:9200"]
index => "java-log-%{+YYYY.MM.dd}"
}
}
}
下载地址:Download Kibana Free | Get Started Now | Elastic
#解压kibana软件包,可使用-C自定义解压路径
tar xf kibana-7.17.6-linux-x86_64.tar.gz
cd kibana-7.17.6-linux-x86_64/config
vim kibana.yml
# Kibana 访问地址
server.host: 0.0.0.0
# elasticsearch集群列表
elasticsearch.hosts: ["http://***:9200","http://***:9200","http://***:9200"]
#后台启动
nohup ./bin/kibana --allow-root &