[toc]

Logstash收集java日志并输出到ES中

因为我们现在需要用Logstash收集tomcat日志,所以我们暂时将tomcat安装到Logstash所在机器,也就是db03:10.0.0.83这台机器,收集tomcat访问日志以及tomcat错误日志进行实时统计,在企业中,tomcat机器肯定不是单台,而是一个集群的形式,那么我们每台tomcat上都需要安装一个Logstash,然后将收集到的日志输出给Elasticsearch进行分析。

将tomcat日志改成json格式

在企业中,我们看到tomcat日志遇到异常(exception)一条日志可能是几行或者十几行甚至几十行,组成的,那么,我们需要将多行日志变成一行日志,来收集。

这里我们有几种方式可以实现:

  1. 将日志改成Json格式

    • 企业中,java日志格式并不能随意改成json格式,因为将日志改成Json格式,查看起来会很难受,有些开发人员不希望将日志格式改成Json,所以需要跟开发人员进行沟通。将tomcat日志格式改成Json格式的两种方式:

      1. 开发自己更改,通过程序代码,或者log4j

      2. 运维修改tomcat的server配置文件

        1
        2
        3
        4
        5
        # 编辑tomcat配置文件
        vim conf/server.xml
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
        prefix="tomcat_access_log" suffix=".log"
        pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/>
  2. 通过Logstash其他模块来收集例:multiline多行匹配

    • 以下是tomcat日志文件中exception展示

      img

      img

安装tomcat

安装JDK环境

  • 二进制安装JDK

    下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # 解压JDK安装包
    tar xf jdk-8u121-linux-x64.tar.gz

    # 将JDK安装包移动到安装目录下
    mv jdk1.8.0_121 /usr/local/

    # 做软链接(方便日后升级)
    ln -s /usr/local/jdk1.8.0_121 /usr/local/jdk1.8

    # 添加环境变量
    vim /etc/profile.d/jdk1.8.sh
    export JAVA_HOME=/usr/local/jdk1.8
    export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    export PATH=$PATH:$JAVA_HOME/bin

    # 加载环境变量
    source /etc/profile

    # 检查是否加载成功
    java -version
    java version "1.8.0_121"
    Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
  • yum安装JDK

    1
    yum install -y java

安装tomcat

  • 二进制安装tomcat

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    # 解压tomcat安装包
    tar xf apache-tomcat-8.0.38.tar.gz

    # 将安装包移动到安装路径并改名
    mv apache-tomcat-8.0.38 /usr/local/tomcat-8.0.38

    # 做软链接
    ln -s /usr/local/tomcat-8.0.38 /usr/local/tomcat

    # 进入tomcat站点目录
    cd /usr/local/tomcat/webapps/

    # 创建新项目目录
    mkdir webdir

    # 写一个测试页面到站点目录下的index.html文件中
    echo 'test tomcat' > webdir/index.html

    # 进入tomcat程序目录
    cd /usr/local/tomcat/bin/

    # 启动tomcat
    ./catalina.sh start

    # 检测tomcat端口是否启动
    netstat -lntup|grep 8080
    tcp 0 0 :::8080 :::* LISTEN 12569/java
  • yum安装tomcat

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    # 安装tomcat
    yum install -y tomcat

    # 创建文件
    mkdir /usr/share/tomcat/webapps/ROOT
    vim /usr/share/tomcat/webapps/ROOT/index.jsp
    test tomcat

    # 启动tomcat
    systemctl start tomcat

    # 检查端口
    netstat -lntup
    tcp6 0 0 :::8080 :::* LISTEN 23350/java

    # 检查日志
    ll /var/log/tomcat/
    total 12
    -rw-r--r-- 1 tomcat tomcat 4366 Feb 15 09:29 catalina.2023-02-15.log
    -rw-rw---- 1 tomcat tomcat 28 Nov 17 2020 catalina.out
    -rw-r--r-- 1 tomcat tomcat 0 Feb 15 09:29 host-manager.2023-02-15.log
    -rw-r--r-- 1 tomcat tomcat 0 Feb 15 09:29 localhost.2023-02-15.log
    -rw-r--r-- 1 tomcat tomcat 0 Feb 15 09:29 localhost_access_log.2023-02-15.txt
    -rw-r--r-- 1 tomcat tomcat 0 Feb 15 09:29 manager.2023-02-15.log

    # 访问浏览器
    10.0.0.83:8080
  • 访问页面

    1
    2
    # 访问浏览器
    10.0.0.83:8080

    image-20230508092024972

修改tomcat日志格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 编辑server配置文件
vim /usr/local/tomcat/conf/server.xml

# 在138行,添加如下内容
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="tomcat_access_log" suffix=".log"
pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/>

# 进入tomcat程序目录
cd /usr/local/tomcat/bin/

# 停止tomcat
./catalina.sh stop

# 启动tomcat
./catalina.sh start

# 进入tomcat日志目录
cd /usr/local/tomcat/logs/

# 查看新生成的tomcat日志
ll
总用量 40
-rw-r--r-- 1 root root 14601 3月 31 10:10 tomcat_access_log.2019-03-31.log

# 实时跟进日志
tail -f tomcat_access_log.2019-03-31.log

验证Json格式

复制一条日志,打开浏览器,访问:http://www.kjson.com/

img

img

配置Logstash收集tomcat日志输出到ES中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 编辑Logstash配置文件
vim /etc/logstash/conf.d/tomcat_es.conf
## 输入插件
input {
## 文件模块
file {
## 文件路径
path => "/usr/local/tomcat/logs/tomcat_access_log.2023-02-15.log"
## 从结束位置点开始收集
start_position => "end"
## 日志类型
type => "tomct_access_log"
}
}
## 输出插件
output {
## ES模块
elasticsearch {
## 主机信息
hosts => ["10.0.0.81:9200"]
## 索引名称,也就是日志名称
index => "tomcat_access-%{+YYYY.MM.dd}"
## 输出成json格式
codec => "json"
}
}

#启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat_es.conf &

启动成功,如下图所示:

img

打开浏览器,访问:http://10.0.0.81:9100/ 查看是否生成日志,如果没有,则访问tomcat页面。

img

解析json格式

将tomcat日志索引添加到Kibana中

img

查看日志内容,不难发现,即便是用了Json格式的,所有日志在message中还是 一坨 看起来很麻烦,并不是以KEY:VALUE的形式展示出来的。

img

所以,我们需要获取到message中的KEY:VALUE将他解析成键值对的形式,展现出来

1
2
3
4
5
6
7
8
9
# 在Logstash的配置文件中,添加filter过滤规则
filter {
json {
source => "message"
}
}

# 重新启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat_es.conf &

再次查看日志内容

img

删除多余的message

两条日志对比,可以看出修改后的Logstash日志,前面多出很多KEY,虽然还message里还是有一坨,但是message中的所有Json已经被解析出来变成了KEY:VALUE的形式,当然我们也可以取消message的显示,操作如下:

1
2
3
4
5
6
7
8
9
10
# 将Logstash中的filter规则添加一行,remove_field,删除列 message
filter {
json {
source => "message"
remove_field => ["message"]
}
}

# 重新启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat_es.conf &

再次查看日志,可以看到message已经没有了,但是所有的KEY:VALUE都还在。

为什么要这么做呢,一定要展示成Json格式呢?

因为,如果我们想要Kibana画图,那么必须用KEY:VALUE的形式,获取值,来画图。

img

最终配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vim tomcat_file_es_json.conf

input{
file{
type => 'tomcat_access_log'
path => '/var/log/tomcat/localhost_access_log.*.txt'
start_position => 'end'
}
}

filter{
json{
source => 'message'
remove_field => ["message"]
}
}

output{
elasticsearch{
hosts => ["10.0.0.81:9200"]
index => "%{type}-%{+yyyy.MM.dd}"
codec => 'json'
}
}

Logstash收集nginx日志并输出到ES中

源码安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 安装nginx依赖包
yum install -y gcc gcc-c++ automake pcre-devel zlib-devel openssl-devel

# 下载nginx安装包
wget http://nginx.org/download/nginx-1.10.3.tar.gz

# 解压
tar xf nginx-1.10.3.tar.gz

# 进入nginx安装目录
cd nginx-1.10.3/

# 生成编译文件
./configure --prefix=/usr/local/nginx-1.10.3

# 编译
make

# 安装
make install

# 做软链接
ln -s /usr/local/nginx-1.10.3 /usr/local/nginx

# 检测nginx语法
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is successful

# 启动nginx
/usr/local/nginx/sbin/nginx

配置nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 简化nginx配置文件
grep -Ev '#|^$' /usr/local/nginx/conf/nginx.conf.default > /usr/local/nginx/conf/nginx.conf

# 编辑nginx主配置文件
vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;

include /etc/nginx/mime.types;
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf;

}

# 编辑www网页配置文件
vim /etc/nginx/conf.d/www.conf
server{
listen 80;
server_name www.xxx.com;
root /code;
index index.html;
access_log /var/log/nginx/www.xxx.com_access_json.log json;
}

# 编辑xxx网页配置文件
vim /etc/nginx/conf.d/xxx.conf
server{
listen 80;
server_name xxx.xxx.com;
root /code1;
index index.html;
access_log /var/log/nginx/xxx.xxx.com_access_json.log json;
}

# 创建nginx站点目录
mkdir /code
mkdir /code1

# 写测试页面
echo test nginx > /code/index.html
echo test nginx1 > /code1/index.html

# 重新加载nginx
/usr/local/nginx/sbin/nginx -s reload

# 打开浏览器,访问:http://10.0.0.83/

修改nginx日志格式为Json

之前我们讲了tomcat日志,在企业中,修改格式需要与开发商量,但是nginx我们不需要,如果需要原来的格式日志,我们可以将日志输出两份,一份 main格式,一份Json格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 编辑nginx日志,添加日志格式,源main格式和Json格式
vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}
http {
## main格式日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# access_log /var/log/access.log main;

## Json格式日志
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
# access_log /var/log/access_json.log json;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;

include /etc/nginx/mime.types;
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf;

}

# 检测nginx配置文件语法
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is successful

# 重新加载nginx
/usr/local/nginx/sbin/nginx -s reload

打开浏览器,访问:http://10.0.0.83/ 查看日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看目录中日志
ll /usr/local/nginx/logs/
总用量 24
## 修改后的Json格式日志
-rw-r--r-- 1 root root 1280 4月 8 10:47 access_json.log
## 源main格式日志
-rw-r--r-- 1 root root 5286 4月 8 10:47 access.log
-rw-r--r-- 1 root root 4218 4月 8 10:46 error.log
-rw-r--r-- 1 root root 5 4月 8 10:20 nginx.pid

# 查看Json格式日志
cat access_json.log
{"@timestamp":"2019-04-08T10:47:41+08:00","host":"10.0.0.53","clientip":"10.0.0.1","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"10.0.0.53","url":"/index.html","domain":"10.0.0.53","xff":"-","referer":"-","status":"304"}

# 查看main格式日志
cat access.log
10.0.0.1 - - [08/Apr/2019:10:29:11 +0800] "GET / HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"

通过Logstash收集nginx日志输出到ES中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 编辑logstash配置文件
vim /etc/logstash/conf.d/nginx_es.conf
input {
file {
type => "www_nginx_access"
path => "/var/log/nginx/www.xxx.com_access_json.log"
start_position => "beginning"
}
file {
type => "xxx_nginx_access"
path => "/var/log/nginx/xxx.xxx.com_access_json.log"
start_position => "beginning"
}
}

filter{
json{
source => 'message'
remove_field => ["message"]
}
}

output {
elasticsearch {
hosts => ["10.0.0.81:9200"]
index => "nginx_access-%{+YYYY.MM.dd}"
codec => json
}
}

# 检测Logstash语法
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_es.conf -t

# 启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx_es.conf &

# 打开浏览器,访问:http://10.0.0.81:9100/

image-20230508205727643

image-20230508205731891

image-20230508205736758

Logstash通过TCP/UDP收集日志并输出到ES中

通过logstash的tcp/udp插件收集日志,通常用于在向elasticsearch日志补录丢失的部分日志,可以将丢失的日志通过一个TCP端口直接写入到elasticsearch服务器。

配置Logstash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 进入Logstash配置文件目录
cd /etc/logstash/conf.d/

# 编辑Logstash配置文件
vim tcp.conf
input {
tcp {
port => 1234
type => "tcplog"
mode => "server"
}
}

output {
stdout {
codec => rubydebug
}
}

#启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tcp.conf

# 检测端口是否启动成功
netstat -lntup
tcp 0 0 :::1234 :::* LISTEN 8656/java

使用nc传输日志

NetCat简称nc,在网络工具中有瑞士军刀美誉,其功能实用,是一个简单、可靠的网络工具,可通过TCP或UDP协议传输读写数据,另外还具有很多其他功能。

在其它服务器安装nc命令

1
2
3
4
5
# 使用yum安装nc
yum install -y nc

# 使用nc传输数据
echo "test nc" | nc 10.0.0.83 1234

通过nc发送一个文件

1
2
# 将/etc/passwd文件当成日志文件传送
nc 10.0.0.83 1234 < /etc/passwd

结果如下,我们不难发现,Logstash会将传送来的日志文件 一行一行 读取,收集成日志

img

通过伪设备的方式发送日志

在类Unix操作系统中,设备节点并不一定要对应物理设备。没有这种对应关系的设备是伪设备。操作系统运用了它们提供的多种功能,tcp只是dev下面众多伪设备当中的一种设备。

1
2
# 发送伪设备数据
echo "测试设备" > /dev/tcp/10.0.0.83/1234

将输出改成ES

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 编辑logstash配置文件
vim tcp.conf
input {
tcp {
port => 1234
type => "tcplog"
mode => "server"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tcp_log-%{+YYYY.MM.dd}"
}
}

# 启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tcp.conf &

# 测试数据
echo "测试设备1" > /dev/tcp/10.0.0.53/1234
echo "测试设备2" > /dev/tcp/10.0.0.53/1234

打开浏览器,访问:http://10.0.0.51:9100/

将ES索引添加到Kibana中

img

img

img

img

Logstash配合rsyslog收集haproxy日志并输出到ES中

rsyslog详解

  • 在centos 6及之前的版本叫做syslog,centos 7开始叫做rsyslog,根据官方的介绍,rsyslog(2013年版本)可以达到每秒转发百万条日志的级别,官方网址:http://www.rsyslog.com/
  • rsyslog是linux系统中用来实现日志功能的服务,默认已经安装,并且自动启用。
  • 作用:
    • 主要用来采集日志,不生产日志
  • 特性:
    1. 支持输出日志到各种数据库,如 MySQL,PostgreSQL,MongoDB ElasticSearch,等等;
    2. 通过 RELP + TCP 实现数据的可靠传输(基于此结合丰富的过滤条件可以建立一种 可靠的数据传输通道供其他应用来使用);
    3. 精细的输出格式控制以及对消息的强大 过滤能力;
    4. 高精度时间戳;队列操作(内存,磁盘以及混合模式等); 支持数据的加密和压缩传输等。

haproxy详解

  • 原理
    • haproxy提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
  • 优点
    1. 免费开源,稳定性也是非常好。单haproxy也跑得不错,稳定性可以与硬件级的F5相媲美。
    2. 根据官方文档,haproxy可以跑满10Gbps,这个数值作为软件级负载均衡器是相当惊人的。
    3. haproxy支持连接拒绝:因为维护一个连接的打开的开销是很低的,有时我们很需要限制攻击蠕虫(attack bots),也就是说限制它们的连接打开从而限制它们的危害。这个已经为一个陷于小型DDoS攻击的网站开发了而且已经拯救了很多站点,这个优点也是其它负载均衡器没有的。
    4. haproxy支持全透明代理(已具备硬件防火墙的典型特点):可以用客户端IP地址或者任何其他地址来连接后端服务器。这个特性仅在Linux 2.4/2.6内核打了tcp proxy补丁后才可以使用。这个特性也使得为某特殊服务器处理部分流量同时又不修改服务器的地址成为可能。
    5. haproxy现多于线上的Mysql集群环境,我们常用于它作为MySQL(读)负载均衡。
    6. 自带强大的监控服务器状态的页面,实际环境中我们结合Nagios进行邮件或短信报警。
    7. HAProxy支持虚拟主机,许多朋友说它不支持虚拟主机是错误的,通过测试我们知道,HAProxy是支持虚拟主机的。

架构流程图示

image-20230509215529394

修改nginx配置文件(同时配置多台nginx服务器)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 修改nginx主配置文件,将端口改为8081(避免端口冲突)
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';

server {
listen 8081;
server_name 10.0.0.83;
location / {
root /code/html;
index index.html index.htm;
}
}
}

# 修改nginx网页配置文件
vim /etc/nginx/conf.d/www.conf
server{
listen 8090;
server_name www.xxx.com;
root /code;
index index.html;
access_log /var/log/nginx/www.xxx.com_access_json.log json;
}

vim /etc/nginx/conf.d/xxx.conf
server{
listen 8091;
server_name xxx.xxx.com;
root /code1;
index index.html;
access_log /var/log/nginx/xxx.xxx.com_access_json.log json;
}

# 重启nginx
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is successful
/usr/local/nginx/sbin/nginx -s reload

安装配置rsyslog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装rsyslog
yum install -y rsyslog

# 编辑rsyslog配置文件
vim /etc/rsyslog.conf
## 注释打开
15 $ModLoad imudp
16 $UDPServerRun 514

19 $ModLoad imtcp
20 $InputTCPServerRun 514

## 最后面一行添加,local7对应haproxy配置文件定义的local级别,端口为Logstash的端口
92 local7.* @@10.0.0.83:2222

# 启动rsyslog
systemctl start rsyslog

安装配置haproxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 安装haproxy
yum install -y haproxy

# 编辑haproxy配置文件
vim /etc/haproxy/haproxy.cfg

global
maxconn 100000
chroot /var/lib/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /var/run/haproxy.pid
## 指定日志输出的local级别,需对应本地或远端的rsyslog配置文件
log 127.0.0.1 local7 info

defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms

listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
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 pc hdr_dom(host) -i www.xxx.com
acl mobile hdr_dom(host) -i xxx.xxx.com
###################USE ACL##############################
use_backend pc_host if pc
use_backend mobile_host if mobile
########################################################

backend pc_host
mode http
option httplog
## 设置为轮循模式
balance static-rr
server web1 10.0.0.83:8090 check inter 2000 rise 3 fall 2 weight 1
server web1 10.0.0.52:8090 check inter 2000 rise 3 fall 2 weight 1

backend mobile_host
mode http
option httplog
## 设置为轮循模式
balance static-rr
server web1 10.0.0.83:8091 check inter 2000 rise 3 fall 2 weight 1
server web1 10.0.0.52:8091 check inter 2000 rise 3 fall 2 weight 1

# 启动haproxy
systemctl start haproxy

# 验证端口
netstat -lntup
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 9082/haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9631/haproxy

# 验证进程
ps -ef|grep haproxy
nobody 9082 1 0 14:04 ? 00:00:00 /usr/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid

# 修改本地hosts文件
10.0.0.83 www.xxx.com xxx.xxx.com

image-20230509222211538

image-20230509222216392

image-20230509222224356

logstash通过rsyslog收集haproxy日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 编辑Logstash配置文件
vim haproxy.cof
input{
syslog1{
type => "rsyslog_haproxy"
port => "2222"
}
}
output{
stdout{
codec => rubydebug
}
elasticsearch{
hosts => ["10.0.0.81:9200"]
index => "%{type}-%{+yyyy.MM.dd}"
}
}

# 启动Logstash
/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/haproxy.conf

# 检查Logstash端口
netstat -lntup|grep 2222
tcp 0 0 :::2222 :::* LISTEN 9867/java
udp 0 0 :::2222 :::* LISTEN 9867/java
  • logstash监听2222端口
  • rsyslog是接收到了haproxy的日志然后转发给10.0.0.83:2222
  • logstash启动2222端口实时接收rsyslog传来的日志
  • 如果logstash停掉 2222端口就会消失 2222是logstash起来的

打开浏览器,访问:http://10.0.0.81:9100/

img

将ES索引添加到Kibana中

打开浏览器,访问:http://10.0.0.83:5601/

img

img

img

img