Flink on Yarn模式啟動流程分析

?Flink On Yarn 架構



Flink Cluster on Yarn啟動過程中,大體可以分為二個階段

1. Filnk Client發起請求,申請啟動Flink Cluster on Yarn

2. Yarn RM接收請求,并指定NM分配Container啟動Flink Cluster.

(需要配置YARN_CONF_DIR, HADOOP_CONF_DIR ,HADOOP_CONF_PATH其中一個用來確保Flink能夠訪問HDFS和Yarn的RM。)


主要啟動流程

1. 進程

首先我們通過下面的命令行啟動flink on yarn的集群(Flink Client發起請求)

安裝Flink:只需在一臺可以連接至Yarn & HDFS集群的任意節點安裝即可

啟動腳本(命令):./bin/yarn-session.sh -n {num} -jm {num} -tm {num}

運行實例:yarn-session.sh中運行的最后命令是:java …org.apache.flink.yarn.cli.FlinkYarnSessionCli

/opt/meituan/flink-1.6.2/bin/yarn-session.sh?-D yarn.container-start-command-template="/usr/local/jdk1.8.0_112/bin/java %jvmmem% %jvmopts% -Djob_name=appflow_log_fact -DjobID=10088 -Dengine_type=FLINK -Djob.suffix.topic=appflow-rtdw %logging% %class% %args% %redirects%" -d -n 120 -tm 4096 -jm 8000 -qu hadoop-rt.queue01 -s 1 -nm appflow_log_fact

這里將產生總共五個進程

** 1個FlinkYarnSessionCli ---> Yarn Client **

** 1個YarnApplicationMasterRunner ---> AM + JobManager**

3個YarnTaskManager --> TaskManager

即一個客戶端+4個container,1個container啟動AM,3個container啟動TaskManager。

2.啟動流程

1.FlinkYarnSessionCli 啟動的過程中首先會檢查Yarn上有沒有足夠的資源去啟動所需要的container,如果有,則上傳一些flink的jar和配置文件到HDFS,這里主要是啟動AM進程和TaskManager進程的相關依賴jar包和配置文件

簡單描述FlinkYarnSessionCli的主要內容

1) 根據FLINK_CONF_DIR & (YARN_CONF_DIR | HADOOP_CONF_DIR) load相關配置

2) 創建yarnClient,并申請一個applicationId

3) 將Flink集群運行所需要的Jar & Conf PUT至HDFS上

4) 封裝ApplicationMaster啟動需要的Env & Cmd至Request對象中,并用yarnClient對象發起請求,等待響應

5) 確認啟動成功后,將重要信息封裝成properties文件,并持久化至本地磁盤

注意事項:

步驟三中的HDFS路徑,默認為:/user/{user}/.flink/{applicationId}

- 如果HDFS沒有為該user創建 /user/{user} 目錄,將拋出異常

- 由于該步驟中需要使用到applicationId,所以需要先通過yarnClient申請applicationId

步驟四才會真正的向Yarn申請資源運行ApplicationMaster

- AM并不是Yarn的接口實現類,而是封裝至Context中的啟動命令 & 環境變量等相關信息

啟動成功后生成的properties文件中 最重要的信息為applicationId,將在之后的Flink Job提交時用于查找Cluster信息

- properties文件持久化路徑,默認為:/tmp/.yarn-properties-{user}

- 如果在一個節點啟動多個Session,則需要注意這個文件位置(目前還未研究)

2.接著yarn client會首先向RM申請一個container來 ApplicationMaster(YarnApplicationMasterRunner進程),然后RM會通知其中一個NM啟動這個container,被分配到啟動AM的NM會首先去HDFS上下載第一步上傳的jar包和配置文件到本地,接著啟動AM;在這個過程中會啟動JobManager,因為JobManager和AM在同一進程里面,它會把JobManager的地址重新作為一個文件上傳到HDFS上去TaskManager在啟動的過程中也會去下載這個文件獲取JobManager的地址,然后與其進行通信;AM還負責Flink的web 服務,Flink里面用到的都是隨機端口,這樣就允許了用戶能夠啟動多個yarn session。

啟動命令

兩組件,三階段

1) RM接收請求,并查詢可用NM,并使其啟動Container來運行AM

2) NM接收調度,并依據信息相關信息,將Jar & Conf從HDFS下載至Local,同時還依據Cmd & Env在本地生成launcher腳本

3) 通過運行launcher腳本,來啟動ApplicationMaster(從源碼中可以發現,Flink Client發送來的Cmd為:java … YarnSessionClusterEntrypoint)

簡單描述FlinkSessionClusterEntrypoint的主要內容

1) 啟動基于Akka的 RPC Service & Metric Register Service

2) 啟動HA Service & Heartbeat Server

3) 啟動BLOB Server & ArchivedExecutionGraphStore (會在Local創建臨時目錄用于存儲)

4) 啟動Web Monitor Service(任務管理平臺)

5) 啟動JobManager服務,用以管理TaskManager進程

注意事項:

1) 步驟二中用于存儲Jar & Conf以及launcher腳本的地址為:/data/hadoop/yarn/local/usercache/{user}/appcache/application_{applicationId}/container_{applicationId}_…,其中包含一下內容 :

? ? - launch_container.sh

? ? - 啟動命令 & 環境變量

????- flink-conf.yaml & log配置文件 – 啟動配置 & 日志配置

????- flink.jar & lib – 運行依賴Jar

2) 步驟三中運行YarnSessionClusterEntrypoint,以此來啟動JobManager,而后的TaskManager,則有JobManager來啟動并管理

????- 實際上,在on Yarn模式下,TaskManager的啟動 是推遲到了Filnk Job的調度發起的時候,并且,當一段時間沒有接收到Job時,TaskManager將自動退出,釋放資源?

3.AM 啟動完成以后,就會向RM申請container去啟動TaskManager,啟動的過程中也是首先從HDFS上去下載一些包含TaskManager(yarn模式的話這里就是YarnTaskManager )主類 的jar和啟動過程依賴的配置文件,如JobManager地址所在的文件,然后利用java cp的方式去啟動YarnTaskManager ,一旦這些準備好,就可以接受任務了。這個和spark on yarn的yarn cluster模式其實差不多,也是分為兩個部分,一個是準備工人和工具(spark是啟動sc的過程,flink是初始化ENV的過程),另外一個就是給工人分配具體工作(都是執行具體的操作,action什么的觸發)。

啟動命令:

進程信息

** FlinkYarnSessionCli **

/home/hadoop/ym/jdk1.8.0_101/bin/java -Xmx512m -classpath /home/hadoop/ym/flink-1.1.3/lib/flink-dist_2.10-1.1.3.jar:/home/hadoop/ym/flink-1.1.3/lib/flink-python_2.10-1.1.3.jar:/home/hadoop/ym/flink-1.1.3/lib/log4j-1.2.17.jar:/home/hadoop/ym/flink-1.1.3/lib/slf4j-log4j12-1.7.7.jar::/home/hadoop/ym/hadoop-2.7.1/etc/hadoop: -Dlog.file=/home/hadoop/ym/flink-1.1.3/log/flink-xxxuser-yarn-session-db-180.photo.163.org.log -Dlog4j.configuration=file:/home/hadoop/ym/flink-1.1.3/conf/log4j-yarn-session.properties -Dlogback.configurationFile=file:/home/hadoop/ym/flink-1.1.3/conf/logback-yarn.xml org.apache.flink.yarn.cli.FlinkYarnSessionCli -j /home/hadoop/ym/flink-1.1.3/lib/flink-dist_2.10-1.1.3.jar -n 3 -jm 1024 -nm 1024 -st

** YarnApplicationMasterRunner **

/home/hadoop/ym/jdk1.8.0_101/bin/java -Xmx424M -Dlog.file=/home/hadoop/ym/hadoop-2.7.1/hadoop/nm/application_1480493133223_0009/container_1480493133223_0009_01_000001/jobmanager.log -Dlogback.configurationFile=file:logback.xml -Dlog4j.configuration=file:log4j.properties org.apache.flink.yarn.YarnApplicationMasterRunner

**個YarnTaskManager **

/home/hadoop/ym/jdk1.8.0_101/bin/java -Xms424m -Xmx424m -XX:MaxDirectMemorySize=424m -Dlog.file=/home/hadoop/ym/hadoop-2.7.1/hadoop/nm/application_1480493133223_0009/container_1480493133223_0009_01_000003/taskmanager.log -Dlogback.configurationFile=file:./logback.xml -Dlog4j.configuration=file:./log4j.properties org.apache.flink.yarn.YarnTaskManager --configDir .

其他說明:

日志管理

????FlinkYarnSessionCli的啟動,由Client發起,歸屬于Flink管理,所以日志內容存儲在Flink安裝目錄的log/

????YarnSessionClusterEntrypoint的啟動,又Yarn發起,歸屬于Yarn管理,所以日志內容存儲在Yarn管理的目錄/data/hadoop/yarn/log/…

進程管理

????FlinkYarnSessionCli進程由Flink管理,YarnSessionClusterEntrypoint進程由Yarn管理

????當不通過FlinkYarnSessionCli來stop YarnSessionClusterEntrypoint時,需要使用yarn application -kill …,但是這種方式無法清理由FlinkYarnSessionCli管理和控制的資源,如:/tmp/.yarn-properties-{user}

????發起yarn application -kill …命令,請求停止Cluster時,會先停止TaskManager,然后停止JobManager,但是不會清理HDFS上的緩存

????通過FlinkYarnSessionCli的interact模式,可以對*/tmp/.yarn-properties-{user}* & HDFS緩存統一進行清理

Job提交

????這種模式下,Client將從本地查找/tmp/.yarn-properties-{user}配置,以獲取applicationId來定位Cluster,所以Job提交最好是在FlinkYarnSessionCli的啟動節點,否則需要指定applicationId

集群安裝

????on Yarn模式下,Flink只需要安裝至 一個節點,因為后續的進程,都會從HDFS上獲取Jar & Conf來進行啟動


官網文檔

The YARN client needs to access the Hadoop configuration to connect to the YARN resource manager and to HDFS. It determines the Hadoop configuration using the following strategy:

Test if YARN_CONF_DIR, HADOOP_CONF_DIR or HADOOP_CONF_PATH are set (in that order). If one of these variables are set, they are used to read the configuration.

If the above strategy fails (this should not be the case in a correct YARN setup), the client is using the HADOOP_HOME environment variable. If it is set, the client tries to access $HADOOP_HOME/etc/hadoop (Hadoop 2) and $HADOOP_HOME/conf (Hadoop 1).

When starting a new Flink YARN session, the client first checks if the requested resources (containers and memory) are available. After that, it uploads a jar that contains Flink and the configuration to HDFS (step 1).

The next step of the client is to request (step 2) a YARN container to start the ApplicationMaster (step 3). Since the client registered the configuration and jar-file as a resource for the container, the NodeManager of YARN running on that particular machine will take care of preparing the container (e.g. downloading the files). Once that has finished, the ApplicationMaster (AM) is started.

The JobManager and AM are running in the same container. Once they successfully started, the AM knows the address of the JobManager (its own host). It is generating a new Flink configuration file for the TaskManagers (so that they can connect to the JobManager). The file is also uploaded to HDFS. Additionally, the AM container is also serving Flink’s web interface. All ports the YARN code is allocating are ephemeral ports. This allows users to execute multiple Flink YARN sessions in parallel.

After that, the AM starts allocating the containers for Flink’s TaskManagers, which will download the jar file and the modified configuration from the HDFS. Once these steps are completed, Flink is set up and ready to accept Jobs.?

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容