Flume 架構及部署

1.Flume概述

Flume是由 Cloudera 提供的一個分佈式、高可靠、高可用的服務,用於分佈式的海量日誌的高效收集、聚合、移動系統。簡單來說,Flume 就是一個針對日誌數據進行採集和彙總的一個工具(把日誌從A地方移動到B地方)

Flume 官網:http://flume.apache.org/
Flume 官方文檔:http://flume.apache.org/FlumeUserGuide.html

Flume 特點:

(1).可靠性:當節點出現故障時,日誌能夠被傳送到其他節點上而不會丟失。Flume提供了三種級別的可靠性保障,從強到弱依次分別爲:

①.end-to-end(收到數據agent首先將event寫到磁盤上,當數據傳送成功後,再刪除;如果數據發送失敗,可以重新發送);
②.Store on failure(這也是scribe採用的策略,當數據接收方crash時,將數據寫到本地,待恢復後,繼續發送);
③.Best effort(數據發送到接收方後,不會進行確認)。

(2).可擴展性:Flume採用了三層架構,分別爲agent,collector和storage,每一層均可以水平擴展所有agent和collector由master統一管理,這使得系統容易監控和維護,且master允許有多個(使用ZooKeeper進行管理和負載均衡),這就避免了單點故障問題。

(3).可管理性
①.所有agent和colletor由master統一管理,這使得系統便於維護。
②.多master情況,Flume利用ZooKeeper和gossip,保證動態配置數據的一致性。
③.用戶可以在master上查看各個數據源或者數據流執行情況,且可以對各個數據源配置和動態加載。
④.Flume提供了web 和shell script command兩種形式對數據流進行管理。①

(4).功能可擴展性
①.用戶可以根據需要添加自己的agent,collector或者storage。
②.此外,Flume自帶了很多組件,包括各種agent(file, syslog等),collector和storage(file,HDFS等)。

(5).文檔豐富,社區活躍
Flume 是Apache下的一個頂級項目,已經成爲 Hadoop 生態系統的標配,它的文檔比較豐富,社區比較活躍,方便我們學習。

2.Flume 架構

這裏寫圖片描述

Flume 架構介紹

      Flume 最簡單的部署單元叫做Flume Agent,Agent 是一個Java 應用程序,接收或者生成數據並緩衝數據,直到最終寫入到Agent或者存儲或索引系統。

      一個Flume Agent可以連接一個或多個其它的Agent,一個Agent也可以從一個或多個Agent接收數據,通過相互連接的多個Flume Agent,一個流作業被建立,這個Flume Agent鏈條可以用於將數據從一個位置移動到另一個位置——特別是,從生產數據的應用程序到HDFS、HBase等。

每個Flume Agent有三個組件:Source、Channel、Sink

      Source 專門用來收集數據,可以處理各種類型、各種格式的日誌數據,比如avro、exec、HTTP、Kafka、Spooling Directory等

      Channel 是一個存儲Source已經接收到的數據的緩衝區,簡單來說就是對Source採集到數據進行緩存,可以緩存在memory、file、jdbc、Kafka等。

      Sink 用於把數據發送到目的地,目的地可以是:HDFS、Hive、HBase、ES、Kafka、Logger等

Flume 運行機制

      flume的核心就是一個agent,這個agent對外有兩個進行交互的地方,一個是接受數據的輸入——source,一個是數據的輸出sink,sink負責將數據發送到外部指定的目的地。source接收到數據之後,將數據發送給channel,chanel作爲一個數據緩衝區會臨時存放這些數據,隨後sink會將channel中的數據發送到指定的地方—-例如HDFS等,注意:只有在sink將channel中的數據成功發送出去之後,channel纔會將臨時數據進行刪除,這種機制保證了數據傳輸的可靠性與安全性

3.Flume 部署

目前Flume 的最新版本爲1.8,筆者安裝的是1.6,是Flume的一個經典版本,通常在生產環境中使用的就是這個版本,在安裝Flume前,看先它的前置條件
(1).JDK1.7
(2).足夠的內存
(3).足夠的磁盤空間
(4).目錄及文件要有讀寫權限

官網下載地址:http://archive.apache.org/dist/flume/
從官網下載,或者在linux中使用下面的命令下載,然後解壓

$ wget http://archive.apache.org/dist/flume/1.6.0/apache-flume-1.6.0-bin.tar.gz
$ sudo tar -zxvf apache-flume-1.6.0-bin.tar.gz
$ sudo mv apache-flume-1.6.0-bin /opt/flume

flume 目錄結構

這裏寫圖片描述

配置flume

進入flume的conf目錄下,拷貝flume-env.sh.template然後重命名爲flume-env.sh

$ sudo cp flume-env.sh.template flume-env.sh

在flume-env.sh中配置jdk

export JAVA_HOME=/usr/java/jdk1.7.0

配置環境變量

# Flume Environment Variable
export FLUME_HOME=/opt/flume
export PATH=$PATH:$FLUME_HOME/bin

查看Flume 的命令幫助

[hadoop@Master ~]$ flume-ng help
Usage: /opt/flume/bin/flume-ng <command> [options]...

commands:
  help                      display this help text
  agent                     run a Flume agent
  avro-client               run an avro Flume client
  version                   show Flume version info

global options:
  --conf,-c <conf>          use configs in <conf> directory
  --classpath,-C <cp>       append to the classpath
  --dryrun,-d               do not actually start Flume, just print the command
  --plugins-path <dirs>     colon-separated list of plugins.d directories. See the
                            plugins.d section in the user guide for more details.
                            Default: $FLUME_HOME/plugins.d
  -Dproperty=value          sets a Java system property value
  -Xproperty=value          sets a Java -X option

agent options:
  --name,-n <name>          the name of this agent (required)
  --conf-file,-f <file>     specify a config file (required if -z missing)
  --zkConnString,-z <str>   specify the ZooKeeper connection to use (required if -f missing)
  --zkBasePath,-p <path>    specify the base path in ZooKeeper for agent configs
  --no-reload-conf          do not reload config file if changed
  --help,-h                 display help text

avro-client options:
  --rpcProps,-P <file>   RPC client properties file with server connection params
  --host,-H <host>       hostname to which events will be sent
  --port,-p <port>       port of the avro source
  --dirname <dir>        directory to stream to avro source
  --filename,-F <file>   text file to stream to avro source (default: std input)
  --headerFile,-R <file> File containing event headers as key/value pairs on each new line
  --help,-h              display help text

  Either --rpcProps or both --host and --port must be specified.

Note that if <conf> directory is specified, then it is always included first
in the classpath.

查看Flume 版本信息

[hadoop@Master ~]$ flume-ng version
Flume 1.6.0
Source code repository: https://git-wip-us.apache.org/repos/asf/flume.git
Revision: 2561a23240a71ba20bf288c7c2cda88f443c2080
Compiled by hshreedharan on Mon May 11 11:15:44 PDT 2015
From source with checksum b29e416802ce9ece3269d34233baf43f

4.Flume 簡單使用

需求:從網絡端口接受數據,輸出到控制檯

Agent選型:netcat+source + memory channel + logger sink

Flume中最重要的就是Agent配置文件的編寫,如何編寫,官網都有說明,下面是官網提供的一個Agent配置文件模板

# example.conf: A single-node Flume configuration

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# Describe the sink
a1.sinks.k1.type = logger

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

參照上述官網的配置,新建simple-example.conf文件,配置如下

# Name the components on this agent
simple-agent.sources = netcat-source
simple-agent.sinks = logger-sink
simple-agent.channels = memory-channel

# Describe/configure the source
simple-agent.sources.netcat-source.type = netcat
simple-agent.sources.netcat-source.bind = Master
simple-agent.sources.netcat-source.port = 44444

# Describe the sink
simple-agent.sinks.logger-sink.type = logger

# Use a channel which buffers events in memory
simple-agent.channels.memory-channel.type = memory

# Bind the source and sink to the channel
simple-agent.sources.netcat-source.channels = memory-channel
simple-agent.sinks.logger-sink.channel = memory-channel

配置完成後,輸入如下命令來啓動flume

flume-ng agent \
--conf $FLUME_HOME/conf \
--name simple-agent \
--conf-file $FLUME_HOME/config/simple-example.conf \
-Dflume.root.logger=INFO,console

參數說明:
–conf:指定flume的配置文件所在目錄
–name:指定Agent的名稱
–conf-file:指定編寫的flume配置文件
-Dflume.root.logger:指定日誌級別

flume啓動後,打印的日誌中可以看到如下信息(部分日誌)

Creating instance of source netcat-source, type netcat
Creating instance of sink: logger-sink, type: logger
Creating instance of channel memory-channel type memory
Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/192.168.242.150:44444]

輸入jps命令可以查看到一個Application的進程,如果有這個進程,說明我們的flume啓動成功了

[hadoop@Master ~]$ jps
3091 Jps
2806 Application
[hadoop@Master ~]$ jps -m
2806 Application --name simple-agent --conf-file /opt/flume/config/simple-example.conf
3101 Jps -m

測試(啓動一個telnet進程,telnet 數據輸入)

$ telnet Master 44444

這裏寫圖片描述

查看Flume控制檯的輸出(Event是Flume數據傳輸的基本單元,由可選的header和一個byte array的數據構成)

Event: { headers:{} body: 68 65 6C 6C 6F 0D            hello. }
Event: { headers:{} body: 66 6C 75 6D 65 0D            flume. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章