"本文主要講解fluent-bit和fluentd安裝配置和使用介紹"
1、環境說明
兩臺服務器(注:Fluent-bit只支持centos 7以上版本,Fluentd可以支持centos 6版本),host1寫日誌到本地,然後通過Fluent-bit支持的forward到Fluentd,Fluentd將日誌集中寫入host2本地存儲歸檔。
host1,IP:12.18.7.42,CentOS 7.6
host2,IP:12.18.7.41,CentOS 7.6
2、fluent-bit安裝過程
2.1、安裝編譯運行環境
yum install cmake gcc gcc-g++ -y
2.2、下載fluentbit
wget https://fluentbit.io/releases/1.0/fluent-bit-1.0.4.tar.gz
2.3、安裝
tar -zxf fluent-bit-1.0.4.tar.gz
cd fluent-bit-1.0.4/build
cmake..
make
make install
2.4、 執行
fluent-bit -c /usr/local/etc/fluent-bit/fluent-bit.conf
Fluent Bit v1.0.4
Copyright (C) Treasure Data
[2020/01/19 11:34:39] [Warning] [config] I cannot open /usr/local/etc/fluent-bit/plugins.conf file
[2020/01/19 11:34:39] [ info] [storage] initializing...
[2020/01/19 11:34:39] [ info] [storage] in-memory
[2020/01/19 11:34:39] [ info] [storage] normal synchronization mode, checksum disabled
[2020/01/19 11:34:39] [ info] [engine] started (pid=3270)
[0] cpu.local: [1579404880.000151741, {"cpu_p"=>0.000000, "user_p"=>0.000000, "system_p"=>0.000000, "cpu0.p_cpu"=>0.000000, "cpu0.p_user"=>0.000000, "cpu0.p_syst
3、fluentd安裝
Fluentd是用ruby語言寫的日誌收集工具。安裝ruby默認是2.0版本的,需要通過yum安裝ruby環境。
3.1、安裝ruby環境
[root@host2 src]# yum install ruby
3.2、修改成阿里源,能夠加快下載速度,添加ruby倉庫
[root@host2 src]# gem sources -a http://mirrors.aliyun.com/rubygems/
3.3、安裝RVM
RVM(Ruby Version Manager )是一款RVM的命令行工具,可以使用RVM輕鬆安裝,管理Ruby版本。RVM包含了Ruby的版本管理和Gem庫管理(gemset)
[root@k8s-master src]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BD4
[root@k8s-master src]# curl -sSL https://get.rvm.io | bash -s stable
3.4、生效配置
[root@host2 src]# source /etc/profile.d/rvm.sh
3.5、查看版本
[root@host2 src]# rvm -v
rvm 1.29.9 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
3.6、安裝Ruby
[root@host2 src]# rvm install 2.5
3.7、安裝編譯環境和軟件包
[root@host2 src]# yum install gcc-c++ make automake autoconf libtool openssl-devel jemalloc-devel gmp-devel -y
3.8、安裝Fluentd(當然你也可以安裝必要的的插件,比如:gem install fluent-plugin-elasticsearch)
[root@host2 src]# gem install fluentd --no-document
3.9、初始化Fluentd
[root@host2 src]# fluentd --setup /etc/fluentd
3.10、啓動
[root@host2 src]# fluentd -c /etc/fluentd/fluent.conf
[root@k8s-master src]# fluentd -c 13_fluentd_conf
2020-01-19 10:43:18 +0800 [info]: parsing config file is succeeded path="13_fluentd_conf"
2020-01-19 10:43:18 +0800 [info]: gem 'fluentd' version '1.8.1'
2020-01-19 10:43:18 +0800 [info]: Oj is not installed, and failing back to Yajl for json parser
2020-01-19 10:43:18 +0800 [info]: using configuration file: <ROOT>
<source>
@type forward
port 24222
</source>
<source>
@type http
port 9880
</source>
<match nginx*>
@type stdout
</match>
</ROOT>
2020-01-19 10:43:18 +0800 [info]: starting fluentd-1.8.1 pid=14389 ruby="2.5.5"
2020-01-19 10:43:18 +0800 [info]: spawn command to main: cmdline=["/usr/local/rvm/rubies/ruby-2.5.5/bin/ruby", "-Eascii-8bit:ascii-8bit", "/usr/local/rvm/gems/ruby-2.5.5/bin/fluentd", "-c", "13_fluentd_conf", "--under-supervisor"]
2020-01-19 10:43:19 +0800 [info]: adding match pattern="nginx*" type="stdout"
2020-01-19 10:43:19 +0800 [info]: adding match pattern="nginx*" type="file"
2020-01-19 10:43:19 +0800 [info]: adding source type="forward"
2020-01-19 10:43:19 +0800 [info]: adding source type="http"
2020-01-19 10:43:19 +0800 [info]: #0 Oj is not installed, and failing back to Yajl for json parser
2020-01-19 10:43:19 +0800 [info]: #0 starting fluentd worker pid=14393 ppid=14389 worker=0
2020-01-19 10:43:19 +0800 [info]: #0 listening port port=24222 bind="0.0.0.0"
2020-01-19 10:43:19 +0800 [info]: #0 fluentd worker is now running worker=0
4、使用樣例
4.1、啓動fluentd服務端,命令如下:
fluentd -c fluentd_conf
fluent_conf信息如下:
<source>
@type forward
port 24222
</source>
# http://this.host:9880/myapp.access?json={"event":"data"}
<source>
@type http
port 9880
</source>
<match nginx*> #匹配有tag爲mem的類型
@type stdout #匹配成功直接標準輸出
</match>
4.2、啓動fluent-bit客戶端
fluent-bit -c fluent-bit.conf
fluent-bit.conf配置文件信息如下:
[SERVICE]
Flush 1
Daemon OFF
Log_Level debug
[INPUT]
Name tail
Path /home/logs/ng.log
Db /tmp/ng.db
Db.sync Full
Tag nginxlog8
[OUTPUT]
Name forward
Match *
Host 12.18.7.41
Port 24222
4.3、日誌輸出測試
fluent-bit客戶端執行
echo qquuuuuu>ng.log
flunetd服務端輸出:
2020-01-19 13:52:38.945508352 +0800 nginxlog8: {"log":"qquuuuuu"}
5、總結
本文主要講解了fluentd和fluent-bit原生安裝和入門使用的過程,上一篇介紹了爲什麼用?Kubernetes集羣環境下fluentd日誌收集方案介紹 後續會介紹文件按照規範存儲日誌文件以及鏡像DaemonSet方式運行,持續關注。
推薦閱讀
Kubernetes中如何使用ClusterDNS進行服務發現?
原創不易,隨手關注或者”在看“,誠摯感謝!