一、Logstash將數據收集到Redis

0.建議
· 不要將es和redis存放在一台服務器中,兩者都消耗內存,防止內存不足,下列操作僅供案例參考。
1.准備環境
| 主機 | IP | 部署的服務 |
|---|---|---|
| web01 | 172.16.1.7 | nginx,tomcat,logstash |
| db01 | 172.16.1.51 | es,kibana,redis |
| db02 | 172.16.1.52 | es,logstash |
| db03 | 172.16.1.53 | es |
2.安裝redis、ES、kibana、logstash
# 注意:
這里的安裝操作不做演示,可以參考之前的博文。redis的啟動建議使用自定義目錄啟動,否則配置文件中有些地方需要修改。
3.配置收集Nginx日志到redis
[root@web01 ~]# vim /etc/logstash/conf.d/nginx_to_redis.conf
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
codec => "json"
}
}
output {
redis {
host => "172.16.1.51"
port => "6379"
data_type => "list"
db => "0"
key => "nginx_log"
}
}
4.收集Nginx和tomcat日志到redis
# 將nginx和tomcat的日志發送至redis中。需要以list列表形式存儲。
[root@web01 ~]# vim /etc/logstash/conf.d/more_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/tomcat_access_json.*.log"
start_position => "beginning"
codec => "json"
}
}
output {
if [type] == "nginx_log" {
redis {
host => "172.16.1.51"
port => "6379"
data_type => "list"
db => "0"
key => "nginx_log"
}
}
if [type] == "tomcat_log" {
redis {
host => "172.16.1.51"
port => "6379"
data_type => "list"
db => "1"
key => "tomcat_log"
}
}
}
#驗證:訪問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
5.配置將redis取出,寫入ES
# 通過db02中安裝的logstash將db01的redis中的數據發送到db01的es中。
[root@db02 ~]# yum localinstall -y logstash-6.6.0.rpm
[root@db02 ~]# vim /etc/logstash/conf.d/redis_to_es.conf
input {
redis {
host => "172.16.1.51"
port => "6379"
db => "0"
data_type => "list"
key => "nginx_log"
}
redis {
host => "172.16.1.51"
port => "6379"
db => "1"
data_type => "list"
key => "tomcat_log"
}
}
output {
if [type] == "nginx_log" {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "nginx_log_%{+YYYY-MM-dd}"
}
}
if [type] == "tomcat_log" {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tomcat_log_%{+YYYY-MM-dd}"
}
}
}
6.頁面查看結果
# 測試結果
頁面訪問http://10.0.0.51:9200查看是否存在nginx和tomcat對應的日志索引
二、通過TCP/UDP收集日志
0.建議
# 說明:
一般情況不建議使用下面的方式進行日志收集,只是展示說明logstash支持通過tcp/udp方式進行日志收集。。。
1.配置收集日志
# 收集當前服務器的1234端口的消息,屏幕輸出
[root@web01 ~]# vim /etc/logstash/conf.d/tcp.conf
input {
tcp {
port => "1234"
mode => "server"
}
}
output {
stdout {}
}
2.使用telnet測試
# telnet連接至logstash(web01)日志收集1234端口的服務器,成功后隨意輸入,發現web01屏幕輸出對應消息。
[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"
}
3.使用nc工具測試
# 工具說明:
· 此工具和telnet類似,支持將消息輸出至其他服務器中
#安裝
[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
#發送偽設備數據(此操作就將echo輸出內容發送至10.0.0.7:1234中)
[root@web01 ~]# echo "偽設備測試" > /dev/tcp/10.0.0.7/1234
4.收集日志到ES
# 將本地開放的1234端口中的消息輸出到es中
[root@web01 ~]# vim /etc/logstash/conf.d/tcp.conf
input {
tcp {
port => "1234"
mode => "server"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "tcp_log_%{+YYYY-MM-dd}"
}
}
三、Logstash配合rsyslog收集haproxy日志

1.rsyslog介紹
在centos 6及之前的版本叫做syslog,centos 7開始叫做rsyslog,根據官方的介紹,rsyslog(2013年版本)可以達到每秒轉發百萬條日志的級別,官方網址:http://www.rsyslog.com/
2.安裝
[root@web01 ~]# yum isntall -y rsyslog
3.配置rsyslog
[root@web01 ~]# vim /etc/rsyslog.conf
#打開注釋
$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
#添加日志收集級別,指定這個級別中的日志發送至對應ip端口中去
local6.* @@172.16.1.52:2222
4.安裝haproxy
[root@web01 ~]# yum install -y haproxy
5.配置haproxy
[root@web01 ~]# vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /var/lib/haproxy
uid 99
gid 99
daemon
nbproc 1
pidfile /var/run/haproxy.pid
log 127.0.0.1 local6 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.elk.com
acl mobile hdr_dom(host) -i m.elk.com
###################USE ACL##############################
use_backend pc_host if pc
use_backend mobile_host if mobile
########################################################
backend pc_host
mode http
option httplog
balance source
server web1 10.0.0.53:8081 check inter 2000 rise 3 fall 2 weight 1
backend mobile_host
mode http
option httplog
balance source
server web1 10.0.0.53:8080 check inter 2000 rise 3 fall 2 weight 1
[root@web01 ~]# vim /etc/haproxy/haproxy.cfg
#全局配置
global
#最大並發
maxconn 100000
#安全機制
chroot /var/lib/haproxy
#指定啟動的用戶和組
uid 99
gid 99
#守護進程
daemon
#haproxy的進程數
nbproc 1
#指定pid文件
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 status
#支持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的nginx中
acl tomcat hdr_dom(host) -i www.tomcat.com # 當訪問此域名就代理到名為acl的tomcat中
###################USE ACL##############################
use_backend nginx_host if nginx # 根據上面acl的nginx名稱對應這里的nginx_host
use_backend tomcat_host if tomcat #根據上面acl的tomcat名稱對應這里的nginx_host
########################################################
backend nginx_host # 根據上面的nginx_host對應到這里,代理到10.0.0.7:8081中
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 # 根據上面的nginx_host對應到這里,代理到10.0.0.7:8081中
mode http
option httplog
balance source
server web01 10.0.0.7:8080 check inter 2000 rise 3 fall 2 weight 1
6.修改Nginx啟動端口
[root@web01 ~]# vim /etc/nginx/nginx.conf # 因為nginx上安裝了haproxy防止沖突,改變端口號
server {
listen 8081 default_server;
...
7.啟動服務
#啟動haproxy(一旦haporxy產生日志,通過rsyslog發送至其他服務器中)
[root@web01 ~]# systemctl start haproxy.service
#啟動rsyslog
[root@web01 ~]# systemctl start rsyslog
#驗證
[root@web01 ~]# netstat -lntp
8.訪問狀態頁面
http://10.0.0.7:9999/haproxy-status
haadmin
123456
9.測試訪問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/
10.測試配置收集proxy日志
[root@db02 ~]# vim /etc/logstash/conf.d/haproxy.conf
input {
syslog {
port => "2222"
}
}
output {
stdout {}
}
#訪問haproxy的頁面,查看有無輸出
11.配置收集proxy日志到ES
# 使用logstash時指定收集2222端口的消息,默認這種方式就是開啟一個端口的方式
[root@db02 ~]# vim /etc/logstash/conf.d/haproxy_es.conf
input {
syslog {
port => "2222"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200"]
index => "haproxy_log_%{+YYYY-MM-dd}"
}
}
