一、Docker部署(注意要统一版本)
启动顺序 ES、Kibana、Logstash、filebeat
1.1Elasticsearch
1.1.1创建映射文件
1)xxx/config、xxx/plugins、xxx/data
2)config下创建elasticsearch.yml文件:
#es的集群名称(填写对应集群名称)
cluster.name: "***"
# 0.0.0.0为不限制,生产环境请设置为固定IP
network.host: 0.0.0.0
# 开启x-pack安全验证 访问时需要密码
#xpack.security.enabled: true
# 关闭跨域验证(可以不开启)
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Type,Content-Length
# 侦听端口
http.port: 9200
# 不在启动的时候锁定内存
bootstrap.memory_lock: false
1.1.2启动命令
docker run -d --name elasticsearch --network test --restart=always --privileged=true -e TZ=Asia/Shanghai -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 -v /xxx/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /xxx/data:/usr/share/elasticsearch/data -v /xxx/plugins:/usr/share/elasticsearch/plugins elasticsearch:7.17.7
注意:命令中 ‘xxx’为对应服务器文件位置,末尾elasticsearch:7.17.7根据实际镜像与版本调整。
1.1.3验证
浏览器输入 http://ip:9200/ ,显示如下信息,则ES启动成功。
{
"name" : "fb672500ed6d",
"cluster_name" : "test-cluster",
"cluster_uuid" : "_w6PDMDdR0KUdUkNcSW6XQ",
"version" : {
"number" : "7.17.7",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "78dcaaa8cee33438b91eca7f5c7f56a70fec9e80",
"build_date" : "2022-10-17T15:29:54.167373105Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
1.2Kibana
1.2.1创建映射文件
1)xxx/config
2)config下创建kibana.yml文件:
server.name: kibana
server.host: "0"
#ES地址
elasticsearch.hosts: [ "http://ip:port" ]
#elasticsearch.username: elastic
#elasticsearch.password: 4BT5mSA2Weshq4zbNH1U
xpack.monitoring.ui.container.elasticsearch.enabled: true
#汉化
i18n.locale: "zh-CN"
1.2.2启动命令
docker run -d --name kibana --network test --restart=always --privileged=true -e TZ=Asia/Shanghai -p 5601:5601 -v /xxx/config/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:7.17.7
注意:命令中 ‘xxx’为对应服务器文件位置,末尾kibana:7.17.7根据实际镜像与版本调整。
1.2.3验证
http://ip:5601/
1.3Logstash
1.3.1创建映射文件
1)xxx/config、xxx/pipeline
2)config下创建logstash.yml、pipelines.yml
############### logstash.yml ##############
#http.host: "0.0.0.0"
xpack.monitoring.enabled: true
#ES地址
xpack.monitoring.elasticsearch.hosts: [ "http://ip:port" ]
#path.config: /usr/share/logstash/conf.d/*.conf
#path.logs: /var/log/logstash/logs
config:
reload:
automatic: true
interval: 3s
xpack:
management.enabled: false
monitoring.enabled: false
############### pipelines.yml ###############
- pipeline.id: test
path.config: "/usr/share/logstash/pipeline/test.conf"
- pipeline.id: mysql
path.config: "/usr/share/logstash/pipeline/mysql.conf"
3)pipeline下创建test.conf、mysql.conf,下载并放入mysql-connector-j-8.0.31.jar
###################### mysql.conf ######################
input {
stdin {}
jdbc {
type => "jdbc"
# 数据库连接地址
jdbc_connection_string => "jdbc:mysql://ip:port/db_log?useSSL=false&useUnicode=true&&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowMultiQueries=true"
# 数据库连接账号密码;
jdbc_user => "XXX"
jdbc_password => "XXX"
# MySQL依赖包路径;
jdbc_driver_library => "/usr/share/logstash/pipeline/mysql-connector-j-8.0.31.jar"
# mysql 驱动
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
# 数据库重连尝试次数
connection_retry_attempts => "3"
# 判断数据库连接是否可用,默认false不开启
jdbc_validate_connection => "true"
# 数据库连接可用校验超时时间,默认3600S
jdbc_validation_timeout => "3600"
# 开启分页查询(默认false不开启);
jdbc_paging_enabled => "true"
# 单次分页查询条数(默认100000,若字段较多且更新频率较高,建议调低此值);
jdbc_page_size => "500"
# statement为查询数据sql,如果sql较复杂,建议配通过statement_filepath配置sql文件的存放路径;
# sql_last_value为内置的变量,存放上次查询结果中最后一条数据tracking_column的值,此处即为create_time;
# statement_filepath => "mysql/jdbc.sql"
statement => "select * from db_log.log_info WHERE create_time>= :sql_last_value order by create_time asc"
# 是否将字段名转换为小写,默认true(如果有数据序列化、反序列化需求,建议改为false);
lowercase_column_names => true
# 日志等级: fatal,error,warn,info,debug,默认info;
sql_log_level => warn
# 是否记录上次执行结果,true表示会将上次执行结果的tracking_column字段的值保存到last_run_metadata_path指定的文件中;
record_last_run => true
# 需要记录查询结果某字段的值时,此字段为true,否则默认tracking_column为timestamp的值;
use_column_value => true
# 需要记录的字段,用于增量同步,需是数据库字段
tracking_column => "create_time"
# 需要记录的字段的类型 Value can be any of: numeric,timestamp,Default value is "numeric"
tracking_column_type => timestamp
# record_last_run上次数据存放位置;
last_run_metadata_path => "mysql/last_id.txt"
# 是否清除last_run_metadata_path的记录,需要增量同步时此字段必须为false;
clean_run => false
#
# 同步频率(分 时 天 月 年),默认每分钟同步一次;
schedule => "* * * * *"
}
}
output {
#ES地址
elasticsearch {
hosts => "http://ip:port"
#索引名称
index => "mysql_log"
document_id => "%{id}"
}
stdout {
#日志输出
codec => json_lines
}
}
#######################test.conf#######################
input {
beats {
# 监听5044端口
port => "5044"
# 关闭ssl验证
ssl => false
# 指定输入格式为json
codec => json
}
}
output {
stdout {
# 指定输出内容为ruby处理后的内容
codec => rubydebug
}
#ES地址、索引
elasticsearch {
hosts => [ "http://ip:port" ]
index => "test"
}
}
1.3.2启动命令
docker run -d --name logstash --network test--restart=always --privileged=true -e TZ=Asia/Shanghai -p 5044:5044 -p 9600:9600 -v /xxx/pipeline/:/usr/share/logstash/pipeline/ -v /xxx/config/:/usr/share/logstash/config/ logstash:7.17.7
注意:命令中 ‘xxx’为对应服务器文件位置,末尾logstash:7.17.7根据实际镜像与版本调整。
1.4Filebeat
1.4.1创建映射文件
1)xxx/config
2)config下创建filebeat.yml
filebeat.inputs:
- type: log
enabled: true
encoding: UTF-8
paths:
- /usr/share/filebeat/logs/*/*.log
fields:
logfrom: test
# logstash地址
output.logstash:
hosts: ["http://ip:port"]
1.4.2启动命令
docker run -d --name filebeat --network test--restart=always --privileged=true -e TZ=Asia/Shanghai -v /xxx/config/filebeat.yml:/usr/share/filebeat/filebeat.yml -v /xxx/:/usr/share/filebeat/logs/ elastic/filebeat:7.17.7
注意:命令中 ‘xxx’为对应服务器文件位置,末尾elastic/filebeat:7.17.7 根据实际镜像与版本调整。
-v /xxx/:/usr/share/filebeat/logs/中的xxx为项目日志文件位置。
二、tar.gz部署
1、Elasticsearch
1、解压tar包
tar -zxvf elasticsearch-xxx.tar.gz
2、进入安装目录:
cd /xxx/elasticsearch-xxx/
3、修改config/elasticsearch.yml
vim config/elasticsearch.yml
添加以下几项
http.port: 9200 #端口
http.cors.enabled: true #设置跨域
http.cors.allow-origin: "*" #设置访问
4、添加es用户
adduser es
5、给es用户赋权限
chown -R es /usr/local/elasticsearch
6、切换至es用户,启动ES
su es
./bin/elasticsearch -d
7、访问:
http://ip:9200
8、操作后切回root用户
2、Kibana
1、解压tar包
tar -zxvf kibana-xxx.tar.gz
2、修改配置文件:
vim ./config/kibana.yml
# 服务端口
server.port: 5601
# 服务器ip 本机
server.host: "0.0.0.0"
# Elasticsearch 服务地址
elasticsearch.hosts: ["http://ip:9200"]
# 设置语言为中文
i18n.locale: "zh-CN"
3、给 es 用户授予 kibana 目录的权限:
chown -R es /usr/local/kibana-xxx
4、启动
./bin/kibana &
5、访问地址
http://ip:5601/
3、Logstash
1、解压tar包
tar -zxvf logstash-xxx.tar.gz
2、config同目录下创建以下目录结构
myconfig
-mysql
-mysql.conf
-mysql-connector-j-8.0.31.jar(下载jar包放此处,版本与数据库匹配即可)
-filebeat
-filebeat.conf
### mysql.conf
input {
stdin {}
jdbc {
type => "jdbc"
# 数据库连接地址
jdbc_connection_string => "jdbc:mysql://ip:port/db_log?useSSL=false&useUnicode=true&&characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowMultiQueries=true"
# 数据库连接账号密码;
jdbc_user => "XXX"
jdbc_password => "XXX"
# MySQL依赖包路径;
jdbc_driver_library => "/xxx/mysql-connector-j-8.0.31.jar"
# mysql 驱动
jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
# 数据库重连尝试次数
connection_retry_attempts => "3"
# 判断数据库连接是否可用,默认false不开启
jdbc_validate_connection => "true"
# 数据库连接可用校验超时时间,默认3600S
jdbc_validation_timeout => "3600"
# 开启分页查询(默认false不开启);
jdbc_paging_enabled => "true"
# 单次分页查询条数(默认100000,若字段较多且更新频率较高,建议调低此值);
jdbc_page_size => "500"
# statement为查询数据sql,如果sql较复杂,建议配通过statement_filepath配置sql文件的存放路径;
# sql_last_value为内置的变量,存放上次查询结果中最后一条数据tracking_column的值,此处即为create_time;
# statement_filepath => "mysql/jdbc.sql"
statement => "select * from db_log.log_info WHERE create_time>= :sql_last_value order by create_time asc"
# 是否将字段名转换为小写,默认true(如果有数据序列化、反序列化需求,建议改为false);
lowercase_column_names => true
# 日志等级: fatal,error,warn,info,debug,默认info;
sql_log_level => warn
# 是否记录上次执行结果,true表示会将上次执行结果的tracking_column字段的值保存到last_run_metadata_path指定的文件中;
record_last_run => true
# 需要记录查询结果某字段的值时,此字段为true,否则默认tracking_column为timestamp的值;
use_column_value => true
# 需要记录的字段,用于增量同步,需是数据库字段
tracking_column => "create_time"
# 需要记录的字段的类型 Value can be any of: numeric,timestamp,Default value is "numeric"
tracking_column_type => timestamp
# record_last_run上次数据存放位置;
last_run_metadata_path => "mysql/last_id.txt"
# 是否清除last_run_metadata_path的记录,需要增量同步时此字段必须为false;
clean_run => false
#
# 同步频率(分 时 天 月 年),默认每分钟同步一次;
schedule => "* * * * *"
}
}
output {
#ES地址
elasticsearch {
hosts => "http://ip:port"
#索引名称
index => "mysql_log"
document_id => "%{id}"
}
stdout {
#日志输出
codec => json_lines
}
}
### filebeat.conf
input {
beats {
# 监听5044端口
port => "5044"
# 关闭ssl验证
ssl => false
# 指定输入格式为json
codec => json
}
}
output {
stdout {
# 指定输出内容为ruby处理后的内容
codec => rubydebug
}
#ES地址、索引名称
elasticsearch {
hosts => [ "http://ip:port" ]
index => "test"
}
}
3、修改pipelines.yml
- pipeline.id: test
pipeline.workers: 1
path.config: "/xxx/myconfig/filebeat/filebeat.conf"
- pipeline.id: mysql
pipeline.workers: 1
path.config: "/xxx/myconfig/mysql/mysql.conf"
4、启动
nohup ./bin/logstash &
4、Filebeat
1、解压tar包
tar -zxvf filebeat-xxx.tar.gz
2、修改配置文件
vim /xxx/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
encoding: UTF-8
# 日志路径
paths:
- /xxx/*/*.log
fields:
logfrom: test
# logstash地址
output.logstash:
hosts: ["http://ip:port"]
3、启动:
- ./filebeat -e -c filebeat.yml
- ./filebeat &