1. 程式人生 > >Flume數據采集之常見集群配置案例

Flume數據采集之常見集群配置案例

大數據 Flume

[TOC]

非集群配置

這種情況非集群配置方式,比較簡單,可以直接參考我整理的《Flume筆記整理》,其基本結構圖如下:

技術分享圖片

Flume集群之多個Agent一個source

結構說明

結構圖如下:

技術分享圖片

說明如下:

即可以把我們的Agent部署在不同的節點上,上面是兩個Agent的情況。其中Agent foo可以部署在日誌產生的節點上,
比如,可以是我們web服務器例如tomcat或者nginx的節點上,foo的source可以配置為監控日誌文件數據的變化,
channel則可以基於內存或基於文件進行存儲,而sink即日誌落地可以配置為avro,即輸出到下一個Agent中。

Agent bar可以部署在另一個節點上,當然跟foo在同一個節點也是沒有問題,因為本身Flume是可以多個實例在同一個
節點上運行的。bar主要作用是收集來自不同avro source的節點的日誌數據,實際上,如果我們的web環境是集群的,
那麽web服務器就會有多個節點,這時就有多個web服務器節點產生日誌,我們需要在這多個web服務器上都部署agent,
此時,bar的source就會有多個,後面的案例正是如此,不過在這個小節中,只討論多個agent一個source的情況。
而對於agent bar的數據下沈方式,也是可以選擇多種方式,詳細可以參考官網文檔,這裏選擇sink為HDFS。

不過需要註意的是,在agent foo中,source只有一個,在後面的案例中,會配置多個source,即在這一個agent中,
可以采集不同的日誌文件,後面要討論的多個source,指的是多個不同日誌文件的來源,即foo中的多個source,例如
data-access.log、data-ugctail.log、data-ugchead.log等等。

配置案例

環境說明

如下:

技術分享圖片

即這裏有兩個節點:

uplooking01:
其中的日誌文件 /home/uplooking/data/data-clean/data-access.log
為web服務器生成的用戶訪問日誌,並且每天會產生一個新的日誌文件。
在這個節點上,我們需要部署一個Flume的Agent,其source為該日誌文件,sink為avro。

uplooking03:
這個節點的作用主要是收集來自不同Flume Agent的日誌輸出數據,例如上面的agent,然後輸出到HDFS中。

說明:在我的環境中,有uplooking01 uplooking02 uplooking03三個節點,並且三個節點配置了Hadoop集群。

配置

  • uplooking01
#########################################################
##
##主要作用是監聽文件中的新增數據,采集到數據之後,輸出到avro
##    註意:Flume agent的運行,主要就是配置source channel sink
##  下面的a1就是agent的代號,source叫r1 channel叫c1 sink叫k1
#########################################################
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#對於source的配置描述 監聽文件中的新增數據 exec
a1.sources.r1.type = exec
a1.sources.r1.command  = tail -F /home/uplooking/data/data-clean/data-access.log

#對於sink的配置描述 使用avro日誌做數據的消費
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = uplooking03
a1.sinks.k1.port = 44444

#對於channel的配置描述 使用文件做數據的臨時緩存 這種的安全性要高
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /home/uplooking/data/flume/checkpoint
a1.channels.c1.dataDirs = /home/uplooking/data/flume/data

#通過channel c1將source r1和sink k1關聯起來
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
  • uplooking03
#########################################################
##
##主要作用是監聽avro,采集到數據之後,輸出到hdfs
##    註意:Flume agent的運行,主要就是配置source channel sink
##  下面的a1就是agent的代號,source叫r1 channel叫c1 sink叫k1
#########################################################
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#對於source的配置描述 監聽avro
a1.sources.r1.type = avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 44444

#對於sink的配置描述 使用log日誌做數據的消費
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /input/data-clean/access/%y/%m/%d
a1.sinks.k1.hdfs.filePrefix = flume
a1.sinks.k1.hdfs.fileSuffix = .log
a1.sinks.k1.hdfs.inUsePrefix = tmpFlume
a1.sinks.k1.hdfs.inUseSuffix = .tmp
a1.sinks.k1.hdfs.useLocalTimeStamp = true
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = second
#配置下面兩項後,保存到HDFS中的數據才是文本
#否則通過hdfs dfs -text查看時,顯示的是經過壓縮的16進制
a1.sinks.k1.hdfs.serializer = TEXT
a1.sinks.k1.hdfs.fileType = DataStream

#對於channel的配置描述 使用內存緩沖區域做數據的臨時緩存
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

#通過channel c1將source r1和sink k1關聯起來
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

測試

首先要確保會有日誌生成,其輸出為/home/uplooking/data/data-clean/data-access.log

在uplooking03上啟動Flume Agent:

[uplooking@uplooking03 flume]$ flume-ng agent -n a1 -c conf --conf-file conf/flume-source-avro.conf -Dflume.root.logger=INFO,console

在uplooking01上啟動Flume Agent:

flume-ng agent -n a1 -c conf --conf-file conf/flume-sink-avro.conf -Dflume.root.logger=INFO,console

一段時間後,便可以在hdfs中看到寫入的日誌文件:

[uplooking@uplooking02 ~]$ hdfs dfs -ls /input/data-clean/access/18/04/07
18/04/07 08:52:02 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 26 items
-rw-r--r--   3 uplooking supergroup       1131 2018-04-07 08:50 /input/data-clean/access/18/04/07/flume.1523062248369.log
-rw-r--r--   3 uplooking supergroup       1183 2018-04-07 08:50 /input/data-clean/access/18/04/07/flume.1523062248370.log
-rw-r--r--   3 uplooking supergroup       1176 2018-04-07 08:50 /input/data-clean/access/18/04/07/flume.1523062248371.log
......

查看文件中的數據:

[uplooking@uplooking02 ~]$ hdfs dfs -text /input/data-clean/access/18/04/07/flume.1523062248369.log
18/04/07 08:55:07 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
1000    220.194.55.244  null    40604   0       POST /check/init HTTP/1.1       500     null    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.3      1523062236368
1002    221.8.9.6 80    886a1533-38ca-466c-86e1-0b84022f781b    20201   1       GET /top HTTP/1.0       500     null      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.3      1523062236869
1002    61.172.249.96   99fb19c4-ec59-4abd-899c-4059dea39ead    0       0       POST /updateById?id=21 HTTP/1.1 408       null    Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko    1523062237370
1003    61.172.249.96   886a1533-38ca-466c-86e1-0b84022f781b    10022   1       GET /tologin HTTP/1.1   null    /update/pass      Mozilla/5.0 (Windows; U; Windows NT 5.1)Gecko/20070309 Firefox/2.0.0.3  1523062237871
1003    125.39.129.67   6839fff8-7b3a-48f5-90cd-0f45c7be1aeb    10022   1       GET /tologin HTTP/1.0   408     null      Mozilla/5.0 (Windows; U; Windows NT 5.1)Gecko/20070309 Firefox/2.0.0.3  1523062238372
1000    61.172.249.96   89019ae0-6140-4e5a-9061-e3af74f3e4a8    10022   1       POST /stat HTTP/1.1     null    /passpword/getById?id=11  Mozilla/4.0 (compatible; MSIE 5.0; WindowsNT)   1523062238873

如果在uplooking03的Flume agent不配置hdfs.serializer=TEXT和hdfs.fileType=DataStream,那麽上面查看到的數據會是16進制數據。

Flume集群之多個Agent多個source

結構說明

如下:

技術分享圖片

配置案例

環境說明

在我們的環境中,如下:
技術分享圖片

即在我們的環境中,日誌源有三份,分別是data-access.log、data-ugchead.log、data-ugctail.log
不過在下面的實際配置中,日誌源的agent我們只使用兩個,uplooking01和uplooking02,它們的sink都
輸出到uplooking03的source中。

配置

uplooking01uplooking02的配置都是一樣的,如下:

#########################################################
##
##主要作用是監聽文件中的新增數據,采集到數據之後,打印在控制臺
##    註意:Flume agent的運行,主要就是配置source channel sink
##  下面的a1就是agent的代號,source叫r1 channel叫c1 sink叫k1
#########################################################
a1.sources = r1 r2 r3
a1.sinks = k1
a1.channels = c1

#對於source r1的配置描述 監聽文件中的新增數據 exec
a1.sources.r1.type = exec
a1.sources.r1.command  = tail -F /home/uplooking/data/data-clean/data-access.log
a1.sources.r1.interceptors = i1 i2
a1.sources.r1.interceptors.i1.type = static
##靜態的在header中添加一個key value,下面就配置了兩個攔截器,i1和i2
a1.sources.r1.interceptors.i1.key = type
a1.sources.r1.interceptors.i1.value = access
a1.sources.r1.interceptors.i2.type = timestamp
## timestamp的作用:這裏配置了的話,在負責集中收集日誌的flume agent就不需要配置
## a1.sinks.k1.hdfs.useLocalTimeStamp = true也能通過這些%y/%m/%d獲取時間信息
## 這樣一來的話,就可以減輕集中收集日誌的flume agent的負擔,因為此時的時間信息可以直接從source中獲取

#對於source r2的配置描述 監聽文件中的新增數據 exec
a1.sources.r2.type = exec
a1.sources.r2.command  = tail -F /home/uplooking/data/data-clean/data-ugchead.log
a1.sources.r2.interceptors = i1 i2
a1.sources.r2.interceptors.i1.type = static
##靜態的在header中添加一個key value,下面就配置了兩個攔截器,i1和i2
a1.sources.r2.interceptors.i1.key = type
a1.sources.r2.interceptors.i1.value = ugchead
a1.sources.r2.interceptors.i2.type = timestamp

#對於source r3的配置描述 監聽文件中的新增數據 exec
a1.sources.r3.type = exec
a1.sources.r3.command  = tail -F /home/uplooking/data/data-clean/data-ugctail.log
a1.sources.r3.interceptors = i1 i2
a1.sources.r3.interceptors.i1.type = static
##靜態的在header中添加一個key value,下面就配置了兩個攔截器,i1和i2
a1.sources.r3.interceptors.i1.key = type
a1.sources.r3.interceptors.i1.value = ugctail
a1.sources.r3.interceptors.i2.type = timestamp

#對於sink的配置描述 使用avro日誌做數據的消費
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = uplooking03
a1.sinks.k1.port = 44444

#對於channel的配置描述 使用文件做數據的臨時緩存 這種的安全性要高
a1.channels.c1.type = file
a1.channels.c1.checkpointDir = /home/uplooking/data/flume/checkpoint
a1.channels.c1.dataDirs = /home/uplooking/data/flume/data

#通過channel c1將source r1 r2 r3和sink k1關聯起來
a1.sources.r1.channels = c1
a1.sources.r2.channels = c1
a1.sources.r3.channels = c1
a1.sinks.k1.channel = c1

uplooking03的配置如下:

#########################################################
##
##主要作用是監聽avro,采集到數據之後,輸出到hdfs
##    註意:Flume agent的運行,主要就是配置source channel sink
##  下面的a1就是agent的代號,source叫r1 channel叫c1 sink叫k1
#########################################################
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#對於source的配置描述 監聽avro
a1.sources.r1.type = avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 44444

#對於sink的配置描述 使用log日誌做數據的消費
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /input/data-clean/%{type}/%Y/%m/%d
a1.sinks.k1.hdfs.filePrefix = %{type}
a1.sinks.k1.hdfs.fileSuffix = .log
a1.sinks.k1.hdfs.inUseSuffix = .tmp
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.rollInterval = 0
a1.sinks.k1.hdfs.rollCount = 0
a1.sinks.k1.hdfs.rollSize = 10485760
# 如果希望上面配置的日誌文件滾動策略生效,則必須要配置下面這一項
a1.sinks.k1.hdfs.minBlockReplicas = 1
#配置下面兩項後,保存到HDFS中的數據才是文本
#否則通過hdfs dfs -text查看時,顯示的是經過壓縮的16進制
a1.sinks.k1.hdfs.serializer = TEXT
a1.sinks.k1.hdfs.fileType = DataStream

#對於channel的配置描述 使用內存緩沖區域做數據的臨時緩存
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

#通過channel c1將source r1和sink k1關聯起來
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

測試

首先需要保證uplooking01uplooking02上都能正常地產生日誌。

uplooking03上啟動Agent:

[uplooking@uplooking03 flume]$ flume-ng agent -n a1 -c conf --conf-file conf/flume-source-avro.conf -Dflume.root.logger=INFO,console

分別在uplooking01uplooking02上啟動Agent:

flume-ng agent -n a1 -c conf --conf-file conf/flume-sink-avro.conf -Dflume.root.logger=INFO,console

一段時間後,可以在HDFS中查看相應的日誌文件:

$ hdfs dfs -ls /input/data-clean
18/04/08 01:34:36 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 3 items
drwxr-xr-x   - uplooking supergroup          0 2018-04-07 22:00 /input/data-clean/access
drwxr-xr-x   - uplooking supergroup          0 2018-04-07 22:00 /input/data-clean/ugchead
drwxr-xr-x   - uplooking supergroup          0 2018-04-07 22:00 /input/data-clean/ugctail

查看某個日誌目錄下的日誌文件:

[uplooking@uplooking02 data-clean]$ hdfs dfs -ls /input/data-clean/access/2018/04/07
18/04/08 01:35:27 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 2 items
-rw-r--r--   3 uplooking supergroup    2447752 2018-04-08 01:02 /input/data-clean/access/2018/04/08/access.1523116801502.log
-rw-r--r--   3 uplooking supergroup       5804 2018-04-08 01:02 /input/data-clean/access/2018/04/08/access.1523120538070.log.tmp

可以看到日誌文件數量非常少,那是因為前面在配置uplooking03的agent時,日誌文件滾動的方式為,單個文件滿10M再進行切分日誌文件。

Flume數據采集之常見集群配置案例