一、环境准备
服务器
Host | IP |
---|---|
log-manager | 172.17.217.149 |
log-worker | 172.17.118.47 |
配置Swarm集群
# log-manager
docker swarm init --advertise-addr 172.17.217.149
# log-worker
docker swarm join --token token 172.17.217.149:2377
拉取镜像
# all server
docker pull logstash:7.16.2
目录配置
如使用nfs等网络存储,注意检查挂载权限及存储配置权限,避免踩坑
# all server
# base path
mkdir /data/logstash -p
# log path
mkdir /data/logstash/log
# conf path
mkdir /data/logstash/conf.d
chmod 777 +R /data/logstash
配置文件
logstash.yml
vi /data/logstash/logstash.yml
path.config: /usr/share/logstash/conf.d/*.conf
path.logs: /var/log/logstash
demo1-logstash.conf
接收tcp端口5959,输出至es和stdout
vi /data/logstash/conf.d/logstash.yml
input {
tcp {
port => 5959
mode => "server"
tags => ["tags"]
codec => json_lines
}
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "demo"
}
stdout {
codec => rubydebug
}
}
/data/logstash/conf.d/demo2-logstash.conf
接收udp端口6666,输出至文件
input {
udp {
port => 6666
codec => cef
receive_buffer_bytes => 16777216
workers => 16
}
}
output {
file {
path => "/var/log/logstash/demo2-%{+YYYY-MM-dd}.log"
file_mode => 0644
}
}
二、操作
# log-manager
docker service create \
--replicas 2 \
--name my_log \
--publish published=6666,target=6666,protocol=udp \
--mount type=bind,src=/data/logstash/logstash.yml,dst=/usr/share/logstash/config/logstash.yml \
--mount type=bind,src=/data/logstash/conf.d,dst=/usr/share/logstash/conf.d \
--mount type=bind,src=/data/logstash/log,dst=/var/log/logstash \
logstash:7.16.2
三、测试
# 将附件工具上传至服务器
# 重命名并授权
mv logstash-tool.txt logstash-tool
chmod +x logstash-tool
# 测试
./logstash-tool --lmsg=test_msg --lnet=udp --lport=6666