網(wǎng)站首頁 編程語言 正文
引言
公司需要搭建一個(gè)日志收集服務(wù)器,用于將公司的項(xiàng)目日志匯總到一臺服務(wù)器上面,方便查看和減輕各項(xiàng)目服務(wù)器壓力。但是由于目前資源不夠充足,所以放棄使用ELK、EFK。最后在調(diào)研嘗試過Linux自帶的Syslog和fluentd之后,Linux自帶的Syslog雖然更簡單,但是匯總的數(shù)據(jù)中會有亂碼的情況(懷疑是Appender問題),并且將日志信息組合成Json格式比較麻煩,因此選擇使用fluentd來做日志收集。
1. 搭建環(huán)境準(zhǔn)備工作
選用fluentd來做日志收集選擇使用源碼來進(jìn)行安裝,并且需要安裝一些依賴的插件,如Ruby等,下面依次介紹需要安裝的組件。
1.1 安裝ruby
下載ruby壓縮包 ruby-2.6.5.tar.gz,版本:2.6.5,并解壓
tar -zxvf ruby-2.6.5.tar.gz
cd ruby-2.6.5
安裝ruby,安裝需要gcc
yum install -y gcc
./configure prefix=/export/source/ruby
make && make install
安裝完成后修改系統(tǒng)配置文件 /etc/profile,在最后添加ruby目錄并加到path中
export RUBY_HOME=/export/source/ruby
export PATH=$PATH:$JAVA_HOME/bin:$RUBY_HOME/bin
驗(yàn)證是否安裝成功
[root@localhost ruby-2.6.5]# ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
顯示ruby版本號,安裝成功
1.2 獲取fluentd源碼
從github上拉取fluentd源碼,github地址:fluentd
git clone https://github.com/fluent/fluentd.git
cd fluentd
1.3 修改gem源
先查看當(dāng)前默認(rèn)源
gem source
*** CURRENT SOURCES ***
https://rubygems.org/
移除默認(rèn)源
gem sources -r https://rubygems.org/
添加源,目前可用的有ruby-china的源,添加ruby-china源
gem sources -a https://gems.ruby-china.com
https://gems.ruby-china.com added to sources
# 查看當(dāng)前源
gem source
*** CURRENT SOURCES ***
https://gems.ruby-china.com
更新緩存
gem sources -u
1.4 安裝Bundle
gem install bundler
等待提示安裝成功
1.5 構(gòu)建fluentd
進(jìn)入fluentd文件夾后,構(gòu)建項(xiàng)目
[root@localhost fluentd]# bundle install
Fetching gem...
...
Bundle complete! 11 Gemfile dependencies, 37 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
[root@localhost fluentd]# bundle exec rake build
fluentd 1.7.3 built to pkg/fluentd-1.7.3.gem.
[root@localhost fluentd]# gem install pkg/fluentd-1.7.3.gem
Successfully installed fluentd-1.7.3
Parsing documentation for fluentd-1.7.3
Installing ri documentation for fluentd-1.7.3
Done installing documentation for fluentd after 3 seconds
1 gem installed
安裝完成
1.6 運(yùn)行fluentd
[root@localhost fluentd]# fluentd --setup ./fluent
[root@localhost fluentd]# fluentd -c ./fluent/fluent.conf -vv &
最后在控制臺顯示fluentd讀取的配置文件信息并且打印相關(guān)日志。至此日志收集服務(wù)器所需要的fluentd已經(jīng)搭建完成,接下來就是配置fluentd以完成日記收集功能。
2. 安裝過程遇到的問題
2.1 安裝bundler沒有zlib包
ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass
解決方案 安裝zlib-devel
yum -y install zlib-devel
進(jìn)入ruby源碼文件夾,安裝ruby自身提供的zlib包ruby-2.5.1/ext/zlib
cd ruby-2.6.5/ext/zlib
ruby ./extconf.rb
make
make install
在make時(shí)會報(bào)錯(cuò)
make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop.
修改目錄中Makefile文件
zlib.o: $(top_srcdir)/include/ruby.h
改成
zlib.o: ../../include/ruby.h
到這里就可以make成功了。
2.2 沒有openssl
錯(cuò)誤描述:cannot load such file -- openssl
/export/source/ruby/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- openssl (LoadError)
29: from /export/source/ruby/bin/gem:21:in `<main>'
28: from /export/source/ruby/lib/ruby/2.6.0/rubygems/gem_runner.rb:59:in `run'
27: from /export/source/ruby/lib/ruby/2.6.0/rubygems/command_manager.rb:148:in `run'
26: from /export/source/ruby/lib/ruby/2.6.0/rubygems/command_manager.rb:178:in `process_args'
...
/export/source/ruby/lib/ruby/2.6.0/rubygems/request.rb:84:in `rescue in configure_connection_for_https': Unable to require openssl, install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources (Gem::Exception)
22: from /export/source/ruby/bin/gem:21:in `<main>'
21: from /export/source/ruby/lib/ruby/2.6.0/rubygems/gem_runner.rb:59:in `run'
20: from /export/source/ruby/lib/ruby/2.6.0/rubygems/command_manager.rb:148:in `run'
19: from /export/source/ruby/lib/ruby/2.6.0/rubygems/command_manager.rb:178:in `process_args'
...
解決方案
yum install openssl-devel -y
在ruby自身提供的額外文件夾中ruby-2.6.5/ext/openssl,執(zhí)行ruby ./extconf.rb,再make
ruby ./extconf.rb
make
make install
make時(shí)出錯(cuò)
make: *** No rule to make target `/include/ruby.h', needed by `ossl.o'. Stop.
修改Makefile文件,在文件頂部添加top_srcdir = ../..
3. 完成需求及修改配置
在上一節(jié)中完成了在日志服務(wù)器上單間fluentd,該日志收集服務(wù)器有五個(gè)需求,通過修改fluentd等配置文件來完成相關(guān)的需求。
需求1 日志存放路徑應(yīng)用+日期劃分
修改配置文件,在 模塊中添加path屬性,如果需要將匹配相同的tag輸出到不同文件中則需要將 的@type設(shè)置為copy,然后再 塊中添加 塊并在 中添加path屬性。
在path屬性中可以讀取過濾的tag,tag的格式為logback配置文件中的.,因此可以將應(yīng)用名設(shè)定在logback配置文件中,然后再fluentd.conf中進(jìn)行讀取。在path中通過${tag}占位符讀取客戶端發(fā)送的tag,tag屬性可以根據(jù).切分為數(shù)組,例如tag為test.demo時(shí),tag[0]表示test,tag[1]表示demo。對于時(shí)間的配置可直接在path路徑中設(shè)置,并且在buffer塊加上time及timekey配置,配置文件如下:
<store> @type file @id demo path /data/log/${tag[3]}/${tag[4]}/%Y-%m-%d/${tag[4]} symlink_path /data/tmp/${tag[3]}_${tag[4]}_data.log append false time_slice_wait 10m time_slice_format %Y%m%d <buffer tag, time> flush_mode interval flush_interval 30s chunk_limit_size 5MB timekey 1d </buffer> </store>
最后得到的目錄如下
├── [ 21] dab
│ └── [ 56] logdemo
│ ├── [4.0K] 2019-10-14
│ │ ├── [3.2M] logdemo.20191014_0.log
│ │ ├── [3.2M] logdemo.20191014_1.log
│ │ ├── [3.5M] logdemo.20191014_2.log
│ │ ├── [3.9M] logdemo.20191014_3.log
│ │ ├── [4.3M] logdemo.20191014_4.log
│ │ ├── [4.2M] logdemo.20191014_5.log
├── [ 27] sms
│ └── [ 62] opapplication
│ ├── [ 114] 2019-10-14
│ │ ├── [2.5M] opapplication.20191014_0.log
│ │ ├── [4.3M] opapplication.20191014_1.log
需求2 存放日志文件按文件大小進(jìn)行切分
修改配置文件,在match塊中添加buffer塊配置,配置內(nèi)容如下
<match level.com.zhy.**> @type file @id demo path /data/log/${tag[3]}/${tag[4]}/%Y-%m-%d/${tag[5]}/${tag[4]}_${tag[5]} symlink_path /data/tmp/${tag[3]}_${tag[4]}_data.log # 不追加日志文件 append false <buffer tag, time> # 刷新模式 flush_mode interval # 刷新周期 flush_interval 10s # 塊大小限制 chunk_limit_size 5MB timekey 1d </buffer> </store> </match>
將buffer刷新模式修改為interval按固定時(shí)間間隔刷新,并且設(shè)置刷新的周期以及不追加文件。這樣當(dāng)buffer有內(nèi)容時(shí),每過10s將日志文件保存一次,或者當(dāng)buffer塊達(dá)到5MB就立即存一次,保證每個(gè)日志文件的大小都保證在5MB以內(nèi)。生成的日志目錄結(jié)構(gòu)如下:
.
├── [ 21] dab
│ └── [ 56] logdemo
│ ├── [4.0K] 2019-10-14
│ │ ├── [3.2M] logdemo.20191014_0.log
│ │ ├── [1.7M] logdemo.20191014_10.log
│ │ ├── [1.9M] logdemo.20191014_11.log
│ │ ├── [3.2M] logdemo.20191014_1.log
│ │ ├── [3.5M] logdemo.20191014_2.log
│ │ ├── [3.9M] logdemo.20191014_3.log
│ │ ├── [4.3M] logdemo.20191014_4.log
│ │ ├── [4.2M] logdemo.20191014_5.log
│ │ ├── [4.5M] logdemo.20191014_6.log
│ │ ├── [4.8M] logdemo.20191014_7.log
│ │ ├── [4.8M] logdemo.20191014_8.log
需求3 按照日志等級劃分日志
為方便查看各類等級日志的情況,需要在目錄中添加日志等級并將相應(yīng)的日志放入。在不修改客戶端tag情況下,在fluentd服務(wù)端重新生成tag以包含日志等級,此時(shí)需要 rewrite_tag_filter 插件來完成重置tag。在使用插件前需要安裝該插件
[root@localhost fluentd]$ fluent-gem install fluent-plugin-rewrite-tag-filter
Fetching fluent-plugin-rewrite-tag-filter-2.2.0.gem
Fetching fluent-config-regexp-type-1.0.0.gem
Successfully installed fluent-config-regexp-type-1.0.0
Successfully installed fluent-plugin-rewrite-tag-filter-2.2.0
Parsing documentation for fluent-config-regexp-type-1.0.0
Installing ri documentation for fluent-config-regexp-type-1.0.0
Parsing documentation for fluent-plugin-rewrite-tag-filter-2.2.0
Installing ri documentation for fluent-plugin-rewrite-tag-filter-2.2.0
Done installing documentation for fluent-config-regexp-type, fluent-plugin-rewrite-tag-filter after 0 seconds
2 gems installed
然后在fluent.conf配置文件中配置
<match com.zhy.**> @type rewrite_tag_filter <rule> key msg pattern ^\[(\w+)\] tag level.${tag}.$1 </rule> </match>
原來的輸入日志格式如下
2019-10-14 14:44:09.000000000 +0800 com.zhy.sms.opapplication: {"msg":"[ERROR] 這是error日志...error\r\n","level":"ERROR","logger":"com.sk.controller.LogController","thread":"pool-3-thread-397","message":"這是error日志...error"}
在日志信息中msg、level對應(yīng)的值都包含日志等級,因此可以從鍵msg、level中選出日志等級來組成新的tag,這里選則提取msg對應(yīng)的value中的日志等級。配置文件中的pattern屬性所寫正則表達(dá)式就是用來匹配msg中[]中的值的,然后再tag上將取出來的日志等級通過$1放到原來tag的最后面。
注意:如果match上的匹配規(guī)則和修改后的tag能夠?qū)?yīng),則會一直進(jìn)行匹配,從而導(dǎo)致修改tag失敗,如下
<match com.zhy.**> @type rewrite_tag_filter <rule> key msg pattern ^\[(\w+)\] tag ${tag}.$1 </rule> </match>
此時(shí)修改后的tag仍然是以com.zhy開頭,所以會導(dǎo)致修改失敗,所以在新的tag前加上level前綴,level可自定義
<match com.zhy.**> @type rewrite_tag_filter <rule> key msg pattern ^\[(\w+)\] tag level.${tag}.$1 </rule> </match>
需求4 將目錄通過nginx映射出去
在nginx配置文件中增加server配置,在server中添加root屬性,設(shè)置映射到服務(wù)器根目錄下的/data目錄中,location屬性分為兩步第一步開啟文件瀏覽,第二步設(shè)置在瀏覽器文本格式顯示文件內(nèi)容
location /log {
autoindex on;
}
通過autoindex來開啟文件瀏覽功能,配置客戶端瀏覽路徑為/log
location ^/log/.*\.log$ {
add_header Content-Type text/plain;
}
首先通過正則表達(dá)式匹配瀏覽路徑,匹配規(guī)則以/log開頭并且以.log結(jié)尾的文件,然后設(shè)置Content-Type為text/plain,在瀏覽器便可直接瀏覽日志內(nèi)容,如果不設(shè)置將會彈出下載窗口進(jìn)行下載,并且需要清理瀏覽器的緩存
server {
listen 8002;
autoindex_exact_size off;
charset utf-8;
autoindex_localtime on;
root /data;
location /log {
autoindex on;
}
location ^/log/.*\.log$ {
add_header Content-Type text/plain;
}
}
需求5 生成一個(gè)定大小的日志文件,并能夠即時(shí)查看日志
需求是希望能夠在項(xiàng)目文件夾下生成一個(gè)日志文件,與前面的日志文件不同的是該日志是顯示當(dāng)前日志內(nèi)容,并且大小固定。由于fluentd的append生成的文件雖然能夠追加到一個(gè)文件中,但是不能夠固定大小,日志大小會無限膨脹。通過查閱資料,最后選擇使用linux自帶的日志轉(zhuǎn)存工具 logrotate 來實(shí)現(xiàn)這需求。
由于目前l(fā)inux都會自帶logrotate,所以不需要安裝,比較便捷。并且logrotate在github的更新還是比較活躍的。github地址:logrotate
1.修改配置
在使用logrotate之前,先修改fluentd配置,使其在每個(gè)項(xiàng)目中生成一個(gè)對應(yīng)的即時(shí)日志文件。在 match 塊中添加一個(gè) store 塊。
<store> @type file @id ImmediateLog path /data/log/${tag[3]}/${tag[4]}/${tag[4]}_immediate append true <buffer tag> path /data/tmp/${tag[3]}/${tag[4]}/${tag[4]}_immediate flush_mode interval flush_interval 5s </buffer> </store>
配置中主要是在對應(yīng)項(xiàng)目文件夾下生成一個(gè)名為 appname_immediate.log 的日志文件,并且該日志文件用append追加內(nèi)容,選擇以5s為周期性刷新,由于是即時(shí)日志文件,所以刷新頻率高。之所以不用immediate模式是因?yàn)榭紤]的服務(wù)器負(fù)載,如果立即刷新的話可能會造成負(fù)載較高,所以選用延遲幾秒來追加日志內(nèi)容
然后修改logrotate配置文件,器配置文件在 /etc/logrotate.conf 中,但是在該配置文件中不方便管理,因此可以自己寫一個(gè)配置文件,保存到 /etc/logrotate.d 目錄中,在該目錄中已經(jīng)保存有幾個(gè)自帶的logrotate配置。這里我們新建一個(gè)配置文件,命名 applog ,并在文件中添加配置項(xiàng):
/data/log/*/*/*_immediate.log {
daily # 每天運(yùn)行
rotate 3 # 轉(zhuǎn)存文件最多三個(gè)
size 10M # 當(dāng)待轉(zhuǎn)存文件大于10MB時(shí)才轉(zhuǎn)存
compress # 壓縮轉(zhuǎn)存文件
copytruncate # 復(fù)制轉(zhuǎn)存文件并且清空原文件
}
logrotate配置文件中第一行表示在配置轉(zhuǎn)存文件的路徑,這里轉(zhuǎn)存的是在/data/log目錄下的所有以_immediate.log結(jié)尾的文件,路徑配置可以使用通配符。其余配置見注釋,這里主要的作用時(shí)每天轉(zhuǎn)存一次日志文件,當(dāng)文件大小超過10MB時(shí)轉(zhuǎn)存,并且進(jìn)行壓縮,轉(zhuǎn)存文件最多存在3個(gè)。其余配置參考官網(wǎng):logrotate配置詳解
在完成配置后,可以通過命令查看配置文件是否正確
# -d:debug模式,校驗(yàn)配置文件
logrotate -d /etc/logrotate.conf
運(yùn)行后會在控制臺顯示所讀取配置文件、轉(zhuǎn)存信息等。如果想看實(shí)際效果,將命令中的-d變?yōu)?f強(qiáng)制執(zhí)行。運(yùn)行-f命令后會在應(yīng)用對應(yīng)的immediate.log目錄中生成一個(gè)以日期結(jié)尾的壓縮文件,并且原來的日志文件被清空。這時(shí)logrotate配置完成。
2.logrotate執(zhí)行時(shí)間
logrotate是通過cron定時(shí)任務(wù)執(zhí)行的,查看cron配置文件可以看到定時(shí)任務(wù)的執(zhí)行時(shí)間,CentOS使用的是anacron,配置文件在 /etc/anacrontab 中
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
配置文件中 START_HOURS_RANGE=3-22 表示定時(shí)任務(wù)開始時(shí)間,默認(rèn)是在3點(diǎn)到22點(diǎn)之間, RANDOM_DELAY=45 表示啟動任務(wù)后的最大隨機(jī)延遲時(shí)間,默認(rèn)最大延遲45分鐘執(zhí)行。最后三行是對于不同的定時(shí)任務(wù)配置,在cron.daily行就是定義每天的定時(shí)任務(wù)執(zhí)行方式,每天一次,并且強(qiáng)制延遲5分鐘執(zhí)行,所以默認(rèn)配置一般會在凌晨三點(diǎn)過執(zhí)行。
在這里修改START_HOURS_RANGE、RANDOM_DELAY以及cron.daily的delay,可以自定義每日定時(shí)任務(wù)的執(zhí)行時(shí)間。
4. 遇到的問題
問題描述
在所有都配置好后,使用 logrotate -d 不報(bào)錯(cuò),手動執(zhí)行 logrotate -f 也能夠生成轉(zhuǎn)存文件,但是轉(zhuǎn)存文件卻不能按照配置每天自動生成。
查看cron日志(cron日志地址:/var/log/cron)發(fā)現(xiàn)每天的定時(shí)任務(wù)都會執(zhí)行
Oct 16 3:00:47 localhost anacron[24471]: Job `cron.daily' started
Oct 16 3:00:47 localhost run-parts(/etc/cron.daily)[24475]: starting logrotate
Oct 16 3:00:50 localhost run-parts(/etc/cron.daily)[24484]: finished logrotate
Oct 16 3:00:50 localhost run-parts(/etc/cron.daily)[24475]: starting man-db.cron
Oct 16 3:00:51 localhost run-parts(/etc/cron.daily)[24495]: finished man-db.cron
Oct 16 3:00:51 localhost anacron[24471]: Job `cron.daily' terminated
但是沒有生成轉(zhuǎn)存文件。
解決方案
出現(xiàn)該問題是由于selinux的安全策略導(dǎo)致的,只需要給轉(zhuǎn)存日志所在的目錄增加安全上下文 var_log_t 即可
chcon -Rv --type=var_log_t /data/log
執(zhí)行上面命令會對/data/log目錄中的所有文件添加安全上下文。之后再觀察轉(zhuǎn)存文件便能夠成功生成。
原文鏈接:https://juejin.cn/post/7143507823681339400
相關(guān)推薦
- 2022-09-06 pandas?如何將字符串映射為數(shù)字_python
- 2023-03-01 shell輸出重定向的實(shí)現(xiàn)_linux shell
- 2022-07-26 “Field ‘id‘ doesn‘t have a default value“
- 2022-12-23 Android同步異步任務(wù)與多線程及Handler消息處理機(jī)制基礎(chǔ)詳細(xì)講解_Android
- 2023-06-21 Flutter學(xué)習(xí)之Navigator的高級用法分享_Android
- 2023-07-07 electron 運(yùn)行以及打包超時(shí)解決方法
- 2022-10-17 C#使用集合實(shí)現(xiàn)二叉查找樹_C#教程
- 2022-06-20 深入淺析C#?11?對?ref?和?struct?的改進(jìn)_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支