参考博文:http://www.ttlsa.com/linux/haproxy-log-configuration-syslog/

引入 Redis 消息队列

Log-file 收集数据到 Redis

主机 IP 部署服务
web01 172.16.1.7 Nginx,Tomcat,Logstash
dbtest01 172.16.1.121 ElasticSearch,Kibana,Redis
dbtest02 172.16.1.122 ElasticSearch
dbtest03 172.16.1.123 ElasticSearch

收集 Nginx & Tomcat 日志

# 日志文件到 Redis
[root@web01 ~]# vim /etc/logstash/conf.d/file_to_redis.conf
input {
file {
type => "nginx_log"
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
file {
type => "tomcat_log"
path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
start_position => "beginning"
codec => "json"
}
}
output {
if [type] == "nginx_log" {
redis {
host => "172.16.1.121"
port => "6379"
data_type => "list"
db => "0"
key => "nginx_log"
}
}
if [type] == "tomcat_log" {
redis {
host => "172.16.1.121"
port => "6379"
data_type => "list"
db => "0"
key => "tomcat_log"
}
}
} #=============== 或者(简写)================#
# 日志文件到 Redis
[root@web01 conf.d]# cat file_to_redis.conf
input {
file {
type => "nginx_log"
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
file {
type => "tomcat_log"
path => "/usr/local/tomcat/logs/localhost_access_log.*.log"
start_position => "beginning"
codec => "json"
}
} output {
redis {
host => ["172.16.1.121"]
port => "6379"
data_type => "list"
db => "0"
key => "%{type}"
}
} # 验证:访问 Nginx 和 Tomcat 页面,查看 Redis 里面有没有 Key
127.0.0.1:6379> LLEN nginx_log
(integer) 1
127.0.0.1:6379> LLEN nginx_log
(integer) 888
127.0.0.1:6379> LRANGE nginx_log 0 -1

Redis 收集数据到 ElasticSearch

# Redis 到 es
[root@web01 conf.d]# cat redis_to_es.conf
input {
redis {
host => "172.16.1.121"
port => "6379"
db => "0"
data_type => "list"
key => "nginx_log"
}
redis {
host => "172.16.1.121"
port => "6379"
db => "0"
data_type => "list"
key => "tomcat_log"
}
} output {
if [type] == "nginx_log" {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "nginx_log_%{+YYYY-MM-dd}"
}
}
if [type] == "tomcat_log" {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "tomcat_log_%{+YYYY-MM-dd}"
}
}
}

启动 Logstash 多实例

[root@web01 conf.d]# mkdir /data/logstash/file_to_redis
[root@web01 conf.d]# mkdir /data/logstash/redis_to_es
[root@web01 conf.d]# chown logstash.logstash /data -R [root@web01 conf.d]# logstash -f /etc/logstash/conf.d/file_to_redis.conf --path.data=/data/logstash/file_to_redis & [root@web01 conf.d]# logstash -f /etc/logstash/conf.d/redis_to_es.conf --path.data=/data/logstash/redis_to_es &

TCP / UDP 模块

TCP 模块初识

[root@web01 ~]# vim /etc/logstash/conf.d/tcp.conf
input {
tcp {
port => "1234"
mode => "server"
}
}
output {
stdout {}
}

使用 telnet 工具测试

[root@db02 ~]# telnet 172.16.1.7 1234
Trying 172.16.1.7...
Connected to 172.16.1.7.
Escape character is '^]'.
123
345 # 输出内容
{
"@timestamp" => 2020-08-17T02:23:05.833Z,
"host" => "172.16.1.52",
"port" => 33002,
"message" => "\r",
"@version" => "1"
}
{
"@timestamp" => 2020-08-17T02:23:32.562Z,
"host" => "172.16.1.52",
"port" => 33002,
"message" => "123\r",
"@version" => "1"
}
{
"@timestamp" => 2020-08-17T02:23:38.300Z,
"host" => "172.16.1.52",
"port" => 33002,
"message" => "345\r",
"@version" => "1"
}

使用 nc 工具测试

# 安装
[root@db02 ~]# yum install -y nc # 使用 nc 工具
[root@db02 ~]# nc 172.16.1.7 1234
123
456 # 使用 nc 工具收集日志到 logstash 的服务器
[root@web01 ~]# tail -f /var/log/nginx/access.log | nc 10.0.0.7 1234 &
[1] 29595 # 发送伪设备数据
[root@web01 ~]# echo "伪设备测试" > /dev/tcp/10.0.0.7/1234

收集日志到 ElasticSearch

[root@web01 ~]# vim /etc/logstash/conf.d/tcp.conf
input {
tcp {
port => "1234"
mode => "server"
}
}
output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "tcp_log_%{+YYYY-MM-dd}"
}
}

Rsyslog + Logstash 收集日志

Rsyslog 介绍

Rsyslog 是一个快速处理收集系统日志的程序,提供了高性能、安全功能和模块化设计,Rsyslog 是 Syslog 的升级版,它将多种来源输入输出转换结果到目的地,据官网介绍,现在可以处理 100 万条信息

Rsyslog 安装

[root@web01 ~]# yum isntall -y rsyslog

Rsyslog 配置

[root@web01 ~]# vim /etc/rsyslog.conf
# 打开注释
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
# 添加日志收集级别
local6.* @@172.16.1.7:2222

安装 HAProxy

[root@web01 ~]# yum install -y haproxy

配置 HAProxy

[root@web01 ~]# cat /etc/haproxy/haproxy.cfg
# 全局配置
global
# 最大并发
maxconn 100000
# 安全机制
chroot /var/lib/haproxy
# 指定启动的用户和组
uid 99
gid 99
daemon
# haproxy 的进程数
nbproc 1
pidfile /var/run/haproxy.pid
# 指定日志级别
log 127.0.0.1 local6 info # 默认配置
defaults
# 开启长连接
option http-keep-alive
# 获取用户真实 IP
option forwardfor
# 最大连接数
maxconn 100000
# 支持 http 协议
mode http
# 设置连接超时时间
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms # 监控状态
listen stats
# 支持 http
mode http
# 监听端口
bind 0.0.0.0:9999
# 启动
stats enable
# 日志级别
log global
# 访问 uri 地址
stats uri /haproxy-status
# 状态页用户名和密码
stats auth haadmin:123456 #frontend web_port
frontend web_port
bind 0.0.0.0:80
mode http
option httplog
log global
option forwardfor
###################ACL Setting##########################
acl nginx hdr_dom(host) -i www.nginx.com
acl tomcat hdr_dom(host) -i www.tomcat.com
###################USE ACL##############################
use_backend nginx_host if nginx
use_backend tomcat_host if tomcat
######################################################## backend nginx_host
mode http
option httplog
balance source
server web01 10.0.0.7:8081 check inter 2000 rise 3 fall 2 weight 1 backend tomcat_host
mode http
option httplog
balance source
server web01 10.0.0.7:8080 check inter 2000 rise 3 fall 2 weight 1

修改 Nginx 启动端口

[root@web01 ~]# vim /etc/nginx/nginx.conf
server {
listen 8081 default_server;
...

启动 HAProxy

# 启动 haproxy
[root@web01 ~]# systemctl start haproxy.service # 启动 rsyslog,用来收集 haproxy 日志,转发到 172.16.1.7:2222 端口
[root@web01 ~]# systemctl start rsyslog # 验证,rsyslog 开启 514 端口, haproxy 开启 80 端口(frontend)& 9999 端口(stats)
[root@web01 ~]# netstat -lntp

访问 Status 页面

# 访问 http://10.0.0.7:9999/haproxy-status
# 用户:haadmin
# 密码:123456

访问 Nginx 和 Tomcat

# 配置本地 hosts
10.0.0.7 www.nginx.com
10.0.0.7 www.tomcat.com # 访问页面
http://www.nginx.com/
http://www.tomcat.com/

Logstash 收集 HAProxy 日志到 Stdout

[root@web01 ~]# cat /etc/logstash/conf.d/haproxy.conf
input {
syslog {
port => "2222"
}
}
output {
stdout {}
} # 访问 haproxy 的页面,查看有无输出

Logstash 收集 HAProxy 日志到 ElasticSearch

[root@web01 ~]# cat /etc/logstash/conf.d/haproxy.conf
input {
syslog {
port => "2222"
}
}
output {
elasticsearch {
hosts => ["10.0.0.121:9200"]
index => "haproxy_%{+YYYY-MM-dd}"
}
}

ELK + Redis 日志收集 & HAProxy的更多相关文章

  1. [原创]ubuntu14.04部署ELK+redis日志分析系统

    ubuntu14.04部署ELK+redis日志分析系统 [环境] host1:172.17.0.4 搭建ELK+redis服务 host2:172.17.0.3 搭建logstash+nginx服务 ...

  2. ELK分布式日志收集搭建和使用

    大型系统分布式日志采集系统ELK全框架 SpringBootSecurity1.传统系统日志收集的问题2.Logstash操作工作原理3.分布式日志收集ELK原理4.Elasticsearch+Log ...

  3. StringBoot整合ELK实现日志收集和搜索自动补全功能(详细图文教程)

    @ 目录 StringBoot整合ELK实现日志收集和搜索自动补全功能(详细图文教程) 一.下载ELK的安装包上传并解压 1.Elasticsearch下载 2.Logstash下载 3.Kibana ...

  4. 05 . ELK Stack+Redis日志收集平台

    环境清单 IP hostname 软件 配置要求 网络 备注 192.168.43.176 ES/数据存储 elasticsearch-7.2 内存2GB/硬盘40GB Nat,内网 192.168. ...

  5. SpringBoot+kafka+ELK分布式日志收集

    一.背景 随着业务复杂度的提升以及微服务的兴起,传统单一项目会被按照业务规则进行垂直拆分,另外为了防止单点故障我们也会将重要的服务模块进行集群部署,通过负载均衡进行服务的调用.那么随着节点的增多,各个 ...

  6. Centos7下ELK+Redis日志分析平台的集群环境部署记录

    之前的文档介绍了ELK架构的基础知识,日志集中分析系统的实施方案:- ELK+Redis- ELK+Filebeat - ELK+Filebeat+Redis- ELK+Filebeat+Kafka+ ...

  7. 传统ELK分布式日志收集的缺点?

    传统ELK图示: 单纯使用ElK实现分布式日志收集缺点? 1.logstash太多了,扩展不好. 如上图这种形式就是一个 tomcat 对应一个 logstash,新增一个节点就得同样的拥有 logs ...

  8. ELK:日志收集分析平台

    简介 ELK是一个日志收集分析的平台,它能收集海量的日志,并将其根据字段切割.一来方便供开发查看日志,定位问题:二来可以根据日志进行统计分析,通过其强大的呈现能力,挖掘数据的潜在价值,分析重要指标的趋 ...

  9. 日志分析平台ELK之日志收集器logstash

    前文我们聊解了什么是elk,elk中的elasticsearch集群相关组件和集群搭建以及es集群常用接口的说明和使用,回顾请查看考https://www.cnblogs.com/qiuhom-187 ...

随机推荐

  1. AQS之ReentrantReadWriteLock写锁

    用法 1.1 定义一个安全的list集合 public class LockDemo { ArrayList<Integer> arrayList = new ArrayList<& ...

  2. 两节锂电池保护IC,芯片电路图如何设计

    两节锂电池出了充电电路外,必须搭配的也就是两节锂电池的保护板电路和芯片了.对两节节串联可再充电锂离子/锂聚合物电池的过充电.过放电和过电流进行保护.和电池反接保护功能,这些都是极其重要的. 首先设计两 ...

  3. Linux下利用ifconfig命令查看和操纵网络接口

    为了说明这个问题,首先我们需要解释一下在Linux系统下"网络接口"的含义.通俗来讲,Linux中的所谓网络接口就是指本机的网卡,它相当于计算机的一台负责对网络进行收发数据的外设. ...

  4. linux 文件目录权限

    文件目录权限: 什么是文件权限: 在Linux中,每个文件都有所属的所有者,和所有组,并且规定了文件的所有者,所有组以及其他人对文件的,可读,可写,可执行等权限. 对于目录的权限来说,可读是读取目录文 ...

  5. C++ Primer Plus读书笔记(六)分支语句和逻辑运算符

    1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...

  6. MySQL下载地址与Centos7安装MySQL以及启动问题排查

    目录 一.MySQL国内镜像下载 二.国内镜像相关站点 三.Centos7安装MySQL5.7 1. 下载并解压至/usr/local 2. 配置信息 3. 用户及用户组管理(提高安全) 4. 初始化 ...

  7. Java8新特性_四大内置核心函数式接口

    Consumner : 消费型接口 Supplier :供给型接口 Function:函数式接口 Predicate:断言型接口 其他接口: 四大内置核心函数式接口: Consumner : 消费型接 ...

  8. docker版mysql的使用和配置(1)——docker的基本操作

    最近实在是忙成狗,其他的内容等稍微闲一点了一起更新. 这篇主要是讲docker版的mysql的使用和配置信息.因为实习公司需要搞一个docker做测试环境用,还需要包括基本的依赖.最重要的是,因为这个 ...

  9. shell (颜色输出)

    摘自https://www.cnblogs.com/t-road/p/10257296.html #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:30-37 echo - ...

  10. 【函数分享】每日PHP函数分享(2021-2-5)

    array_column - 返回数组中指定的一列 说明: array_column ( array $input , mixed $column_key , mixed $index_key = n ...