一、logstash收集日志並寫入redis
[k8s玩法](https://kubernetes.hankbook.cn)
用一台服務器按照部署redis服務,專門用於日志緩存使用,用於web服務器產生大量日志的場景,例如下面的服務器內存即將被使用完畢,查看是因為redis服務保存了大量的數據沒有被讀取而占用了大量的內存空間。

整體架構:

1.部署redis
[root@linux-host2 ~]# cd /usr/local/src/ [root@linux-host2 src]# [root@linux-host2 src]# tar xvf redis-3.2.8.tar.gz [root@linux-host2 src]# ln -sv /usr/local/src/redis-3.2.8 /usr/local/redis ‘/usr/local/redis’ -> ‘/usr/local/src/redis-3.2.8’ [root@linux-host2 src]#cd /usr/local/redis/deps [root@linux-host2 redis]# yum install gcc [root@linux-host2 deps]# make geohash-int hiredis jemalloc linenoise lua [root@linux-host2 deps]# cd .. [root@linux-host2 redis]# make [root@linux-host2 redis]# vim redis.conf [root@linux-host2 redis]# grep "^[a-Z]" redis.conf #主要改動的地方 bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 save "" rdbcompression no #是否壓縮 rdbchecksum no #是否校驗 [root@linux-host2 redis]# ln -sv /usr/local/redis/src/redis-server /usr/bin/ ‘/usr/bin/redis-server’ -> ‘/usr/local/redis/src/redis-server’ [root@linux-host2 redis]# ln -sv /usr/local/redis/src/redis-cli /usr/bin/ ‘/usr/bin/redis-cli’ -> ‘/usr/local/redis/src/redis-cli’
2.設置redis訪問密碼
為安全考慮,生產環境必須設置reids連接密碼:
[root@linux-host2 redis]# redis-cli 127.0.0.1:6379> config set requirepass 123456 #動態設置,重啟后無效 OK 480 requirepass 123456 #redis.conf配置文件

3.啟動並測試redis服務
[root@linux-host2 redis]# redis-server /usr/local/redis/redis.conf #啟動服務 [root@linux-host2 redis]# redis-cli 127.0.0.1:6379> ping PONG
4.配置logstash將日志寫入至redis
將tomcat服務器的logstash收集之后的tomcat 訪問日志寫入到redis服務器,然后通過另外的logstash將redis服務器的數據取出在寫入到elasticsearch服務器。
官方文檔:https://www.elastic.co/guide/en/logstash/current/plugins-outputs-redis.html
[root@linux-host2 tomcat]# cat /etc/logstash/conf.d/tomcat_tcp.conf
input {
file {
path => "/usr/local/tomcat/logs/tomcat_access_log.*.log"
type => "tomcat-accesslog-5612"
start_position => "beginning"
stat_interval => "2"
}
tcp {
port => 7800
mode => "server"
type => "tcplog-5612"
}
}
output {
if [type] == "tomcat-accesslog-5612" {
redis {
data_type => "list"
key => "tomcat-accesslog-5612"
host => "192.168.56.12"
port => "6379"
db => "0"
password => "123456"
}}
if [type] == "tcplog-5612" {
redis {
data_type => "list"
key => "tcplog-5612"
host => "192.168.56.12"
port => "6379"
db => "1"
password => "123456"
}}
}
5.測試logstash配置文件語法是否正確
[root@linux-host2 ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat.conf

6.訪問tomcat的web界面並生成系統日志

[root@linux-host1 ~]# echo "偽設備1" > /dev/tcp/192.168.56.12/7800
7.驗證redis是否有數據

8.配置其他logstash服務器從redis讀取數據
配置專門logstash服務器從redis讀取指定的key的數據,並寫入到elasticsearch。
[root@linux-host3 ~]# cat /etc/logstash/conf.d/redis-to-els.conf
[root@linux-host1 conf.d]# cat /etc/logstash/conf.d/redis-tomcat-es.conf
input {
redis {
data_type => "list"
key => "tomcat-accesslog-5612"
host => "192.168.56.12"
port => "6379"
db => "0"
password => "123456"
codec => "json"
}
redis {
data_type => "list"
key => "tcplog-5612"
host => "192.168.56.12"
port => "6379"
db => "1"
password => "123456"
}
}
output {
if [type] == "tomcat-accesslog-5612" {
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "logstash-tomcat5612-accesslog-%{+YYYY.MM.dd}"
}}
if [type] == "tcplog-5612" {
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "logstash-tcplog5612-%{+YYYY.MM.dd}"
}}
}
9.測試logstash
[root@linux-host1 conf.d]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-to-els.conf

10.驗證redis的數據是否被取出

11.在head插件驗證數據

12.kibana添加tomcat訪問日志索引

14.kibana添加tcp日志索引

15.kibana驗證tomcat訪問日志

16.kibana驗證tomcat訪問日志

17.kibana 驗證tcp日志

#注:測試沒有問題之后,請將logstash使用服務的方式正常啟動
二、使用filebeat替代logstash收集日志
Filebeat是輕量級單用途的日志收集工具,用於在沒有安裝java的服務器上專門收集日志,可以將日志轉發到logstash、elasticsearch或redis等場景中進行下一步處理。
官網下載地址:https://www.elastic.co/downloads/beats/filebeat
官方文檔:https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-configuration-details.html
1.確認日志格式為json格式
先訪問web服務器,以產生一定的日志,然后確認是json格式,因為下面的課程中會使用到:
[root@linux-host2 ~]# ab -n100 -c100 http://192.168.56.16:8080/web
2.確認日志格式,后續會用日志做統計
[root@linux-host2 ~]# tail /usr/local/tomcat/logs/localhost_access_log.2017-04-28.txt
{"clientip":"192.168.56.15","ClientUser":"-","authenticated":"-","AccessTime":"[28/Apr/2017:21:16:46 +0800]","method":"GET /webdir/ HTTP/1.0","status":"200","SendBytes":"12","Query?string":"","partner":"-","AgentVersion":"ApacheBench/2.3"}
{"clientip":"192.168.56.15","ClientUser":"-","authenticated":"-","AccessTime":"[28/Apr/2017:21:16:46 +0800]","method":"GET /webdir/ HTTP/1.0","status":"200","SendBytes":"12","Query?string":"","partner":"-","AgentVersion":"ApacheBench/2.3"}
3.安裝配置filebeat
[root@linux-host2 ~]# systemctl stop logstash #停止logstash服務(如果有安裝) [root@linux-host2 src]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.3.2-x86_64.rpm [root@linux-host6 src]# yum install filebeat-5.3.2-x86_64.rpm -y
4.配置filebeat收集系統日志
[root@linux-host2 ~]# cd /etc/filebeat/ [root@linux-host2 filebeat]# cp filebeat.yml filebeat.yml.bak #備份源配置文件
4.1filebeat收集多個系統日志並輸出到本地文件
[root@linux-host2 ~]# grep -v "#" /etc/filebeat/filebeat.yml | grep -v "^$"
grep -v "#" /etc/filebeat/filebeat.yml | grep -v "^$"
filebeat.prospectors:
- input_type: log
paths:
- /var/log/messages
- /var/log/*.log
exclude_lines: ["^DBG","^$"] #不收取的
#include_lines: ["^ERR", "^WARN"] #只收取的
f #類型,會在每條日志中插入標記
output.file:
path: "/tmp"
filename: "filebeat.txt"
4.2啟動filebeat服務並驗證本地文件是否有數據
[root@linux-host2 filebeat]# systemctl start filebeat

5.filebeat收集單個類型日志並寫入redis
Filebeat支持將數據直接寫入到redis服務器,本步驟為寫入到redis當中的一個可以,另外filebeat還支持寫入到elasticsearch、logstash等服務器。
5.1filebeat配置
[root@linux-host2 ~]# grep -v "#" /etc/filebeat/filebeat.yml | grep -v "^$"
filebeat.prospectors:
- input_type: log
paths:
- /var/log/messages
- /var/log/*.log
exclude_lines: ["^DBG","^$"]
document_type: system-log-5612
output.redis:
hosts: ["192.168.56.12:6379"]
key: "system-log-5612" #為了后期日志處理,建議自定義key名稱
db: 1 #使用第幾個庫
timeout: 5 #超時時間
password: 123456 #redis密碼
5.2驗證redis是否有數據

5.3查看redis中的日志數據
注意選擇的db是否和filebeat寫入一致

5.4配置logstash從redis讀取上面的日志
[root@linux-host1 ~]# cat /etc/logstash/conf.d/redis-systemlog-es.conf
input {
redis {
host => "192.168.56.12"
port => "6379"
db => "1"
key => "system-log-5612"
data_type => "list"
}
}
output {
if [type] == "system-log-5612" {
elasticsearch {
hosts => ["192.168.56.11:9200"]
index => "system-log-5612"
}}
}
[root@linux-host1 ~]# systemctl restart logstash #重啟logstash服務
5.5查看logstash服務日志


5.6查看redis中是否有數據

5.7在logstash的head插件驗證索引是否創建

5.8kibana界面添加索引

5.9在kibana驗證system日志

6.監控redis數據長度
實際環境當中,可能會出現reids當中堆積了大量的數據而logstash由於種種原因未能及時提取日志,此時會導致redis服務器的內存被大量使用,甚至出現如下內存即將被使用完畢的情景:

查看reids中的日志隊列長度發現有大量的日志堆積在redis 當中:

6.1腳本內容
#!/usr/bin/env python
#coding:utf-8
#Author Zhang jie
import redis
def redis_conn():
pool=redis.ConnectionPool(host="192.168.56.12",port=6379,db=0,password=123456)
conn = redis.Redis(connection_pool=pool)
data = conn.llen('tomcat-accesslog-5612')
print(data)
redis_conn()
