Flume 高可用配置案例+load balance負載均衡+ 案例:日誌的采集及匯總
高可用配置案例
(一)、failover故障轉移
在完成單點的Flume NG搭建後,下面我們搭建一個高可用的Flume NG集群,架構圖如下所示:
(1)節點分配
Flume的Agent和Collector分布如下表所示:
名稱 |
Ip地址 |
Host |
角色 |
Agent1 |
192.168.137.188 |
hadoop-001 |
WebServer |
Collector1 |
192.168.137.189 |
hadoop-002 |
AgentMstr1 |
Collector2 |
192.168.137.190 |
hadoop-003 |
AgentMstr2 |
Agent1數據分別流入到Collector1和Collector2,Flume NG本身提供了Failover機制,可以自動切換和恢復。下面我們開發配置Flume NG集群。
(2)配置
在下面單點Flume中,基本配置都完成了,我們只需要新添加兩個配置文件,它們是flume-client.conf和flume-server.conf,其配置內容如下所示:
1、hadoop-001上的flume-client.conf配置
#agent1 name agent1.channels = c1 agent1.sources = r1 agent1.sinks = k1 k2
#set gruop agent1.sinkgroups = g1 #set sink group agent1.sinkgroups.g1.sinks = k1 k2
#set channel agent1.channels.c1.type = memory agent1.channels.c1.capacity = 1000 agent1.channels.c1.transactionCapacity = 100
agent1.sources.r1.channels = c1 agent1.sources.r1.type = exec agent1.sources.r1.command = tail -F /root/log/test.log
agent1.sources.r1.interceptors = i1 i2 agent1.sources.r1.interceptors.i1.type = static agent1.sources.r1.interceptors.i1.key = Type agent1.sources.r1.interceptors.i1.value = LOGIN agent1.sources.r1.interceptors.i2.type = timestamp
# set sink1 agent1.sinks.k1.channel = c1 agent1.sinks.k1.type = avro agent1.sinks.k1.hostname = hadoop-002 agent1.sinks.k1.port = 52020
# set sink2 agent1.sinks.k2.channel = c1 agent1.sinks.k2.type = avro agent1.sinks.k2.hostname = hadoop-003 agent1.sinks.k2.port = 52020
#set failover agent1.sinkgroups.g1.processor.type = failover agent1.sinkgroups.g1.processor.priority.k1 = 10 agent1.sinkgroups.g1.processor.priority.k2 = 5 agent1.sinkgroups.g1.processor.maxpenalty = 10000 #這裏首先要申明一個sinkgroups,然後再設置2個sink ,k1與k2,其中2個優先級是10和5,#而processor的maxpenalty被設置為10秒,默認是30秒。‘ |
|
啟動命令:
bin/flume-ng agent -n agent1 -c conf -f conf/flume-client.conf -Dflume.root.logger=DEBUG,console |
2、Hadoop-002和hadoop-003上的flume-server.conf配置
#set Agent name a1.sources = r1 a1.channels = c1 a1.sinks = k1
#set channel a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100
# other node,nna to nns a1.sources.r1.type = avro a1.sources.r1.bind = 0.0.0.0 a1.sources.r1.port = 52020 a1.sources.r1.channels = c1 a1.sources.r1.interceptors = i1 i2 a1.sources.r1.interceptors.i1.type = timestamp a1.sources.r1.interceptors.i2.type = host a1.sources.r1.interceptors.i2.hostHeader=hostname
#set sink to hdfs a1.sinks.k1.type=hdfs a1.sinks.k1.hdfs.path=/data/flume/logs/%{hostname} a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d a1.sinks.k1.hdfs.fileType=DataStream a1.sinks.k1.hdfs.writeFormat=TEXT a1.sinks.k1.hdfs.rollInterval=10 a1.sinks.k1.channel=c1
|
啟動命令:
bin/flume-ng agent -n agent1 -c conf -f conf/flume-server.conf -Dflume.root.logger=DEBUG,console |
(3)測試failover
1、先在hadoop-002和hadoop-003上啟動腳本
bin/flume-ng agent -n a1 -c conf -f conf/flume-server.conf -Dflume.root.logger=DEBUG,console |
2、然後啟動hadoop-001上的腳本
bin/flume-ng agent -n agent1 -c conf -f conf/flume-client.conf -Dflume.root.logger=DEBUG,console |
3、Shell腳本生成數據
while true;do date >> test.log; sleep 1s ;done |
4、觀察HDFS上生成的數據目錄。只觀察到hadoop-002在接受數據
5、Hadoop-002上的agent被幹掉之後,繼續觀察HDFS上生成的數據目錄,hadoop-003對應的ip目錄出現,此時數據收集切換到hadoop-003上
6、Hadoop-002上的agent重啟後,繼續觀察HDFS上生成的數據目錄。此時數據收集切換到hadoop-002上,又開始繼續工作!
load balance負載均衡
(1)節點分配
如failover故障轉移的節點分配
(2)配置
在failover故障轉移的配置上稍作修改
hadoop-001上的flume-client-loadbalance.conf配置
#agent1 name agent1.channels = c1 agent1.sources = r1 agent1.sinks = k1 k2
#set gruop agent1.sinkgroups = g1
#set channel agent1.channels.c1.type = memory agent1.channels.c1.capacity = 1000 agent1.channels.c1.transactionCapacity = 100 agent1.sources.r1.channels = c1 agent1.sources.r1.type = exec agent1.sources.r1.command = tail -F /root/log/test.log
# set sink1 agent1.sinks.k1.channel = c1 agent1.sinks.k1.type = avro agent1.sinks.k1.hostname = hadoop-002 agent1.sinks.k1.port = 52020
# set sink2 agent1.sinks.k2.channel = c1 agent1.sinks.k2.type = avro agent1.sinks.k2.hostname = hadoop-003 agent1.sinks.k2.port = 52020
#set sink group agent1.sinkgroups.g1.sinks = k1 k2
#set load-balance agent1.sinkgroups.g1.processor.type = load_balance # 默認是round_robin,還可以選擇random agent1.sinkgroups.g1.processor.selector = round_robin #如果backoff被開啟,則 sink processor會屏蔽故障的sink agent1.sinkgroups.g1.processor.backoff = true
|
Hadoop-002和hadoop-003上的flume-server-loadbalance.conf配置
#set Agent name a1.sources = r1 a1.channels = c1 a1.sinks = k1
#set channel a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100
# other node,nna to nns a1.sources.r1.type = avro a1.sources.r1.bind = 0.0.0.0 a1.sources.r1.port = 52020 a1.sources.r1.channels = c1 a1.sources.r1.interceptors = i1 i2 a1.sources.r1.interceptors.i1.type = timestamp a1.sources.r1.interceptors.i2.type = host a1.sources.r1.interceptors.i2.hostHeader=hostname a1.sources.r1.interceptors.i2.useIP=false #set sink to hdfs a1.sinks.k1.type=hdfs a1.sinks.k1.hdfs.path=/data/flume/loadbalance/%{hostname} a1.sinks.k1.hdfs.fileType=DataStream a1.sinks.k1.hdfs.writeFormat=TEXT a1.sinks.k1.hdfs.rollInterval=10 a1.sinks.k1.channel=c1 a1.sinks.k1.hdfs.filePrefix=%Y-%m-%d |
(3)測試load balance
1、先在hadoop-002和hadoop-003上啟動腳本
bin/flume-ng agent -n a1 -c conf -f conf/flume-server-loadbalance.conf -Dflume.root.logger=DEBUG,console |
2、然後啟動hadoop-001上的腳本
bin/flume-ng agent -n agent1 -c conf -f conf/flume-client-loadbalance.conf -Dflume.root.logger=DEBUG,console |
3、Shell腳本生成數據
while true;do date >> test.log; sleep 1s ;done |
4、觀察HDFS上生成的數據目錄,由於輪訓機制都會收集到數據
5、Hadoop-002上的agent被幹掉之後,hadoop-002上不在產生數據
6、Hadoop-002上的agent重新啟動後,兩者都可以接受到數據
1. 案例場景:日誌的采集及匯總
A、B兩臺日誌服務機器實時生產日誌主要類型為access.log、nginx.log、web.log
現在要求:
把A、B 機器中的access.log、nginx.log、web.log 采集匯總到C機器上然後統一收集到hdfs中。
但是在hdfs中要求的目錄為:
/source/logs/access/20190101/**
/source/logs/nginx/20190101/**
/source/logs/web/20190101/**
2. 場景分析
圖一
3. 數據流程處理分析
4. 實現
服務器A對應的IP為 192.168.137.188
服務器B對應的IP為 192.168.137.189
服務器C對應的IP為 192.168.137.190
① 在服務器A和服務器B上的$FLUME_HOME/conf 創建配置文件 exec_source_avro_sink.conf 文件內容為
# Name the components on this agent
a1.sources = r1 r2 r3
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/data/access.log
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = static
## static攔截器的功能就是往采集到的數據的header中插入自己定## 義的key-value對
a1.sources.r1.interceptors.i1.key = type
a1.sources.r1.interceptors.i1.value = access
a1.sources.r2.type = exec
a1.sources.r2.command = tail -F /root/data/nginx.log
a1.sources.r2.interceptors = i2
a1.sources.r2.interceptors.i2.type = static
a1.sources.r2.interceptors.i2.key = type
a1.sources.r2.interceptors.i2.value = nginx
a1.sources.r3.type = exec
a1.sources.r3.command = tail -F /root/data/web.log
a1.sources.r3.interceptors = i3
a1.sources.r3.interceptors.i3.type = static
a1.sources.r3.interceptors.i3.key = type
a1.sources.r3.interceptors.i3.value = web
# Describe the sink
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = 192.168.200.101
a1.sinks.k1.port = 41414
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sources.r2.channels = c1
a1.sources.r3.channels = c1
a1.sinks.k1.channel = c1
② 在服務器C上的$FLUME_HOME/conf 創建配置文件 avro_source_hdfs_sink.conf 文件內容為
#定義agent名, source、channel、sink的名稱
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#定義source
a1.sources.r1.type = avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port =41414
#添加時間攔截器
a1.sources.r1.interceptors = i1
a1.sources.r1.interceptors.i1.type = org.apache.flume.interceptor.TimestampInterceptor$Builder
#定義channels
a1.channels.c1.type = memory
a1.channels.c1.capacity = 20000
a1.channels.c1.transactionCapacity = 10000
#定義sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path=hdfs://192.168.200.101:9000/source/logs/%{type}/%Y%m%d
a1.sinks.k1.hdfs.filePrefix =events
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.writeFormat = Text
#時間類型
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件不按條數生成
a1.sinks.k1.hdfs.rollCount = 0
#生成的文件按時間生成
a1.sinks.k1.hdfs.rollInterval = 30
#生成的文件按大小生成
a1.sinks.k1.hdfs.rollSize = 10485760
#批量寫入hdfs的個數
a1.sinks.k1.hdfs.batchSize = 10000
flume操作hdfs的線程數(包括新建,寫入等)
a1.sinks.k1.hdfs.threadsPoolSize=10
#操作hdfs超時時間
a1.sinks.k1.hdfs.callTimeout=30000
#組裝source、channel、sink
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
③ 配置完成之後,在服務器A和B上的/root/data有數據文件access.log、nginx.log、web.log。先啟動服務器C上的flume,啟動命令
在flume安裝目錄下執行 :
bin/flume-ng agent -c conf -f conf/avro_source_hdfs_sink.conf -name a1 -Dflume.root.logger=DEBUG,console
然後在啟動服務器上的A和B,啟動命令
在flume安裝目錄下執行 :
bin/flume-ng agent -c conf -f conf/exec_source_avro_sink.conf -name a1 -Dflume.root.logger=DEBUG,console
Flume 高可用配置案例+load balance負載均衡+ 案例:日誌的采集及匯總