ELK + Redis 日志收集 & HAProxy
参考博文: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的更多相关文章
- [原创]ubuntu14.04部署ELK+redis日志分析系统
ubuntu14.04部署ELK+redis日志分析系统 [环境] host1:172.17.0.4 搭建ELK+redis服务 host2:172.17.0.3 搭建logstash+nginx服务 ...
- ELK分布式日志收集搭建和使用
大型系统分布式日志采集系统ELK全框架 SpringBootSecurity1.传统系统日志收集的问题2.Logstash操作工作原理3.分布式日志收集ELK原理4.Elasticsearch+Log ...
- StringBoot整合ELK实现日志收集和搜索自动补全功能(详细图文教程)
@ 目录 StringBoot整合ELK实现日志收集和搜索自动补全功能(详细图文教程) 一.下载ELK的安装包上传并解压 1.Elasticsearch下载 2.Logstash下载 3.Kibana ...
- 05 . ELK Stack+Redis日志收集平台
环境清单 IP hostname 软件 配置要求 网络 备注 192.168.43.176 ES/数据存储 elasticsearch-7.2 内存2GB/硬盘40GB Nat,内网 192.168. ...
- SpringBoot+kafka+ELK分布式日志收集
一.背景 随着业务复杂度的提升以及微服务的兴起,传统单一项目会被按照业务规则进行垂直拆分,另外为了防止单点故障我们也会将重要的服务模块进行集群部署,通过负载均衡进行服务的调用.那么随着节点的增多,各个 ...
- Centos7下ELK+Redis日志分析平台的集群环境部署记录
之前的文档介绍了ELK架构的基础知识,日志集中分析系统的实施方案:- ELK+Redis- ELK+Filebeat - ELK+Filebeat+Redis- ELK+Filebeat+Kafka+ ...
- 传统ELK分布式日志收集的缺点?
传统ELK图示: 单纯使用ElK实现分布式日志收集缺点? 1.logstash太多了,扩展不好. 如上图这种形式就是一个 tomcat 对应一个 logstash,新增一个节点就得同样的拥有 logs ...
- ELK:日志收集分析平台
简介 ELK是一个日志收集分析的平台,它能收集海量的日志,并将其根据字段切割.一来方便供开发查看日志,定位问题:二来可以根据日志进行统计分析,通过其强大的呈现能力,挖掘数据的潜在价值,分析重要指标的趋 ...
- 日志分析平台ELK之日志收集器logstash
前文我们聊解了什么是elk,elk中的elasticsearch集群相关组件和集群搭建以及es集群常用接口的说明和使用,回顾请查看考https://www.cnblogs.com/qiuhom-187 ...
随机推荐
- 使用ogg实现oracle到postgresql表的实时同步
参考:https://docs.oracle.com/goldengate/c1221/gg-winux/index.html https://blog.51cto.com/hbxztc/188071 ...
- 1.5V转5V的最少电路的芯片电路图
PW5100满足1.5V转5V的很简洁芯片电路,同时达到了最少的元件即可组成DC-DC电路1.5V转5V的升压转换器系统. PW5100在1.5V转5V输出无负载时,输入效率电流极低,典型值10uA. ...
- Windows下nginx设置开机自启动
第一步:下载 WinSW https://github.com/winsw/winsw/releases/download/v2.10.3/WinSW.NET4.exe 64位系统 https://g ...
- Nginx(七):location的使用以及nginx关闭原理
上一篇中,我们了解了如何nginx的配置原则及解析框架,以及解析location配置的具体实现,相信大家对该部分已经有了比较深刻的认识. 本篇,我们进一步来了解下,解析之后的配置,如何应用到实际中的吧 ...
- IE浏览器兼容问题总结
IE浏览器兼容问题总结 引自掘金:https://juejin.cn/post/6844903825854185480 一.标准盒模型和怪异盒模型 浏览器的盒子模型分为两类: 标准的W3C盒子模型. ...
- odoo之技巧合集一
罗列一些odoo开发中的简单但有效的方法: 1.重写odoo登录代码 参考链接:odoo10-重写登录方法 from odoo import models, fields, api, SUPERUSE ...
- Ajax函数的封装
Ajax函数的封装 function ajax(options) { // 1 创建Ajax对象 let xhr = new XMLHttpRequest(); // 2 告诉Ajax对象要想哪儿发送 ...
- GStreamer各个包构建
GStreamer按功能.维护的标准化程度.依赖库的版权差异等分了若干个包(package),如 gstreamer, gst-plugins-base, gst-plugins-good, gst- ...
- Hash Join: Basic Steps
Joins https://docs.oracle.com/database/121/TGSQL/tgsql_join.htm#TGSQL242 tidb/index_lookup_hash_join ...
- C++学习之STL(一)vector
前言 C++ Primer Plus读书笔记(三)复合类型 中已经简单介绍过vector是什么,这个系列主要是介绍STL特性. 声明 vector<ElemType> c; //创建一个空 ...