centos7搭建flink-1.7.2-bin-hadoop27-scala_2.12 yarn模式集羣

flink集羣

flink可以基於自身的standalone模式進行分佈式集羣計算,也可以利用第三方資源管理器完成分佈式集羣計算。目前比較流行的第三方資源管理器包括Hadoop Yarn,Apache Mesos,Kubernetes等。但是相對來說,因爲yarn能夠同時支持hadoop mapreduce和spark等大數據框架,因此普遍使用yarn模式來管理集羣資源。因此這裏主要對yarn模式進行介紹。

yarn模式

目前flink有兩種方式將應用提交到yarn上,分別是yarn session模式和single job模式。關於這兩種模式的區別可以看這篇文章

搭建yarn模式集羣

這裏採用三臺虛擬機進行集羣搭建,這三臺虛擬機上需要提前配置好Hadoop環境,可以參考這篇文章

因爲在搭建Hadoop集羣時就已經配置好jdk和ssh免密登錄,因此在搭建flink yarn模式集羣的時候只需要對flink進行相關配置即可。

首先下載https://archive.apache.org/dist/flink/flink-1.7.2/flink-1.7.2-bin-hadoop27-scala_2.12.tgz壓縮包上傳到服務器,解壓後生成flink-1.7.2文件夾,接下來需要對conf文件夾下的masters,slaves和flink-conf.yaml三個文件進行配置。

首先配置masters文件,其中的設置指定了flink JobManager的hostname以及端口,配置如下

#該文件用於指定主節點及其web訪問端口,表示集羣的Jobmanager
flink1:8081

然後配置slaves文件 (因爲在配置免密登錄的時候配置過/etc/hosts文件,因此這裏可以直接用主機名映射到IP)

#localhost
#指定從節點,表示集羣的taskManager
flink1
flink2
flink3

 最後配置flink-conf.yaml文件

################################################################################
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
# limitations under the License.
################################################################################


#==============================================================================
# Common
#==============================================================================

# The external address of the host on which the JobManager runs and can be
# reached by the TaskManagers and any clients which want to connect. This setting
# is only used in Standalone mode and may be overwritten on the JobManager side
# by specifying the --host <hostname> parameter of the bin/jobmanager.sh executable.
# In high availability mode, if you use the bin/start-cluster.sh script and setup
# the conf/masters file, this will be taken care of automatically. Yarn/Mesos
# automatically configure the host name based on the hostname of the node where the
# JobManager runs.

#Jobmanager的IP地址,即master地址。
jobmanager.rpc.address: flink1

# The RPC port where the JobManager is reachable.

jobmanager.rpc.port: 6123


# The heap size for the JobManager JVM
#JobManager的堆大小(單位是MB)。當長時間運行operator非常多的程序時,需要增加此值。具體設置多少隻能通過測試不斷調整。
jobmanager.heap.size: 1024m


# The heap size for the TaskManager JVM
#每一個TaskManager的堆大小(單位是MB),由於每個taskmanager要運行operator的各種
#函數(Map、Reduce、CoGroup等,包含sorting、hashing、caching),因此這個值應該儘可能的大。
#如果集羣僅僅跑Flink的程序,建議此值等於機器的內存大小減去1、2G,剩餘的1、2GB用於操作系統。
#如果是Yarn模式,這個值通過指定tm參數來分配給container,同樣要減去操作系統可以容忍的大小(1、2GB)。
taskmanager.heap.size: 1024m


# The number of task slots that each TaskManager offers. Each slot runs one parallel pipeline.
#每個TaskManager的並行度。一個slot對應一個core,默認值是1.一個並行度對應一個線程。總的內存大小要且分給不同的線程使用。
taskmanager.numberOfTaskSlots: 2

# The parallelism used for programs that did not specify and other parallelism.

#每個operator的默認並行度。默認是1.如果程序中對operator設置了setParallelism,或者提交程序
#時指定了-p參數,則會覆蓋此參數。如果只有一個Job運行時,此值可以設置爲
#taskManager的數量 * 每個taskManager的slots數量。即NumTaskManagers  * NumSlotsPerTaskManager 。
parallelism.default: 6

# The default file system scheme and authority.
# 
# By default file paths without scheme are interpreted relative to the local
# root file system 'file:///'. Use this to override the default and interpret
# relative paths relative to a different file system,
# for example 'hdfs://mynamenode:12345'
#
# fs.default-scheme

#==============================================================================
# High Availability
#==============================================================================

# The high-availability mode. Possible options are 'NONE' or 'zookeeper'.
#
# high-availability: zookeeper

# The path where metadata for master recovery is persisted. While ZooKeeper stores
# the small ground truth for checkpoint and leader election, this location stores
# the larger objects, like persisted dataflow graphs.
# 
# Must be a durable file system that is accessible from all nodes
# (like HDFS, S3, Ceph, nfs, ...) 
#
# high-availability.storageDir: hdfs:///flink/ha/

# The list of ZooKeeper quorum peers that coordinate the high-availability
# setup. This must be a list of the form:
# "host1:clientPort,host2:clientPort,..." (default clientPort: 2181)
#
# high-availability.zookeeper.quorum: localhost:2181


# ACL options are based on https://zookeeper.apache.org/doc/r3.1.2/zookeeperProgrammers.html#sc_BuiltinACLSchemes
# It can be either "creator" (ZOO_CREATE_ALL_ACL) or "open" (ZOO_OPEN_ACL_UNSAFE)
# The default value is "open" and it can be changed to "creator" if ZK security is enabled
#
# high-availability.zookeeper.client.acl: open

#==============================================================================
# Fault tolerance and checkpointing
#==============================================================================

# The backend that will be used to store operator state checkpoints if
# checkpointing is enabled.
#
# Supported backends are 'jobmanager', 'filesystem', 'rocksdb', or the
# <class-name-of-factory>.
#
# state.backend: filesystem

# Directory for checkpoints filesystem, when using any of the default bundled
# state backends.
#
# state.checkpoints.dir: hdfs://namenode-host:port/flink-checkpoints

# Default target directory for savepoints, optional.
#
# state.savepoints.dir: hdfs://namenode-host:port/flink-checkpoints

# Flag to enable/disable incremental checkpoints for backends that
# support incremental checkpoints (like the RocksDB state backend). 
#
# state.backend.incremental: false

#==============================================================================
# Web Frontend
#==============================================================================

# The address under which the web-based runtime monitor listens.
#
#web.address: 0.0.0.0

# The port under which the web-based runtime monitor listens.
# A value of -1 deactivates the web server.

rest.port: 8081

# Flag to specify whether job submission is enabled from the web-based
# runtime monitor. Uncomment to disable.

#web.submit.enable: false

#==============================================================================
# Advanced
#==============================================================================

# Override the directories for temporary files. If not specified, the
# system-specific Java temporary directory (java.io.tmpdir property) is taken.
# 指定臨時文件目錄,如果不指定,則使用系統默認的Java臨時目錄(java.io.tmpdir)
io.tmp.dirs: /tmp/flink
# For framework setups on Yarn or Mesos, Flink will automatically pick up the
# containers' temp directories without any need for configuration.
#
# Add a delimited list for multiple directories, using the system directory
# delimiter (colon ':' on unix) or a comma, e.g.:
#     /data1/tmp:/data2/tmp:/data3/tmp
#
# Note: Each directory entry is read from and written to by a different I/O
# thread. You can include the same directory multiple times in order to create
# multiple I/O threads against that directory. This is for example relevant for
# high-throughput RAIDs.
#
# io.tmp.dirs: /tmp

# Specify whether TaskManager's managed memory should be allocated when starting
# up (true) or when memory is requested.
#
# We recommend to set this value to 'true' only in setups for pure batch
# processing (DataSet API). Streaming setups currently do not use the TaskManager's
# managed memory: The 'rocksdb' state backend uses RocksDB's own memory management,
# while the 'memory' and 'filesystem' backends explicitly keep data as objects
# to save on serialization cost.
#
# taskmanager.memory.preallocate: false

# The classloading resolve order. Possible values are 'child-first' (Flink's default)
# and 'parent-first' (Java's default).
#
# Child first classloading allows users to use different dependency/library
# versions in their application than those in the classpath. Switching back
# to 'parent-first' may help with debugging dependency issues.
#
# classloader.resolve-order: child-first

# The amount of memory going to the network stack. These numbers usually need 
# no tuning. Adjusting them may be necessary in case of an "Insufficient number
# of network buffers" error. The default min is 64MB, teh default max is 1GB.
# 
# taskmanager.network.memory.fraction: 0.1
# taskmanager.network.memory.min: 64mb
# taskmanager.network.memory.max: 1gb

#==============================================================================
# Flink Cluster Security Configuration
#==============================================================================

# Kerberos authentication for various components - Hadoop, ZooKeeper, and connectors -
# may be enabled in four steps:
# 1. configure the local krb5.conf file
# 2. provide Kerberos credentials (either a keytab or a ticket cache w/ kinit)
# 3. make the credentials available to various JAAS login contexts
# 4. configure the connector to use JAAS/SASL

# The below configure how Kerberos credentials are provided. A keytab will be used instead of
# a ticket cache if the keytab path and principal are set.

# security.kerberos.login.use-ticket-cache: true
# security.kerberos.login.keytab: /path/to/kerberos/keytab
# security.kerberos.login.principal: flink-user

# The configuration below defines which JAAS login contexts

# security.kerberos.login.contexts: Client,KafkaClient

#==============================================================================
# ZK Security Configuration
#==============================================================================

# Below configurations are applicable if ZK ensemble is configured for security

# Override below configuration to provide custom ZK service name if configured
# zookeeper.sasl.service-name: zookeeper

# The configuration below must match one of the values set in "security.kerberos.login.contexts"
# zookeeper.sasl.login-context-name: Client

#==============================================================================
# HistoryServer
#==============================================================================

# The HistoryServer is started and stopped via bin/historyserver.sh (start|stop)

# Directory to upload completed jobs to. Add this directory to the list of
# monitored directories of the HistoryServer as well (see below).
#jobmanager.archive.fs.dir: hdfs:///completed-jobs/

# The address under which the web-based HistoryServer listens.
#historyserver.web.address: 0.0.0.0

# The port under which the web-based HistoryServer listens.
#historyserver.web.port: 8082

# Comma separated list of directories to monitor for completed jobs.
#historyserver.archive.fs.dir: hdfs:///completed-jobs/

# Interval in milliseconds for refreshing the monitored directories.
#historyserver.archive.fs.refresh-interval: 10000

將配置完的flink-1.7.2整個文件夾scp到另外兩臺機器相同的目錄下。

接下還有一步非常關鍵,就是在master節點(這裏時flink1機器)上需要配置HADOOP_CONF_DIR變量,以便啓動flink的時候能夠自動識別到Hadoop配置信息,連接到yarn的resourcemanager和hdfs。這裏配置的HADOOP_CONF_DIR的值就是Hadoop的配置文件conf所在的路徑

export HADOOP_CONF_DIR=/opt/hadoop-2.7.5/etc/hadoop

(別忘了source使環境變量生效)

下面是別人寫的更詳細的配置策略:

1.會查看YARN_CONF_DIR,HADOOP_CONF_DIR或者HADOOP_CONF_PATH是否設置,按照順序檢查的。然後,假如配置了就會從該文件夾下讀取配置。

2.如果上面環境變量都沒有配置的話,會使用HADOOP_HOME環境變量。對於hadoop2的話會查找的配置路徑是 $HADOOP_HOME/etc/hadoop;對於hadoop1會查找的路徑是$HADOOP_HOME/conf.

通過上面的配置策略可知,這裏也可以不再單獨配置HADOOP_CONF_DIR變量,因爲搭建Hadoop的時候已經配置了HADOOP_HOME。

接下來就可以在集羣上通過測試自己的程序了,這裏使用的是在另一篇文章(IDEA配置flink開發環境及local集羣代碼測試)中寫的測試程序,通過single job的方式進行測試的,具體如下

[root@flink1 opt]# ./flink-1.7.2/bin/flink run -m yarn-cluster -yn 2 ./flinkLearn-1.0-SNAPSHOT-jar-with-dependencies.jar 
2019-10-20 15:30:11,491 INFO  org.apache.hadoop.yarn.client.RMProxy                         - Connecting to ResourceManager at flink1/192.168.89.128:8032
2019-10-20 15:30:11,922 INFO  org.apache.flink.yarn.cli.FlinkYarnSessionCli                 - No path for the flink jar passed. Using the location of class org.apache.flink.yarn.YarnClusterDescriptor to locate the jar
2019-10-20 15:30:11,922 INFO  org.apache.flink.yarn.cli.FlinkYarnSessionCli                 - No path for the flink jar passed. Using the location of class org.apache.flink.yarn.YarnClusterDescriptor to locate the jar
2019-10-20 15:30:12,004 INFO  org.apache.flink.yarn.cli.FlinkYarnSessionCli                 - The argument yn is deprecated in will be ignored.
2019-10-20 15:30:12,004 INFO  org.apache.flink.yarn.cli.FlinkYarnSessionCli                 - The argument yn is deprecated in will be ignored.
2019-10-20 15:30:13,554 INFO  org.apache.flink.yarn.AbstractYarnClusterDescriptor           - Cluster specification: ClusterSpecification{masterMemoryMB=1024, taskManagerMemoryMB=1024, numberTaskManagers=2, slotsPerTaskManager=2}
2019-10-20 15:30:15,444 WARN  org.apache.flink.yarn.AbstractYarnClusterDescriptor           - The configuration directory ('/opt/flink-1.7.2/conf') contains both LOG4J and Logback configuration files. Please delete or rename one of them.
2019-10-20 15:30:27,872 INFO  org.apache.flink.yarn.AbstractYarnClusterDescriptor           - Submitting application master application_1571555933107_0001
2019-10-20 15:30:29,165 INFO  org.apache.hadoop.yarn.client.api.impl.YarnClientImpl         - Submitted application application_1571555933107_0001
2019-10-20 15:30:29,167 INFO  org.apache.flink.yarn.AbstractYarnClusterDescriptor           - Waiting for the cluster to be allocated
2019-10-20 15:30:29,199 INFO  org.apache.flink.yarn.AbstractYarnClusterDescriptor           - Deploying cluster, current state ACCEPTED
2019-10-20 15:30:48,257 INFO  org.apache.flink.yarn.AbstractYarnClusterDescriptor           - YARN application has been deployed successfully.
Starting execution of program
Program execution finished
Job with JobID 9102fbc05daf96f38a53e29d4823e35b has finished.
Job Runtime: 46309 ms

可是你可能會發現雖然運行成功了,但是在flink webui中找不到此job的任何信息。這是因爲使用的是yarn模式,會先在yarn中申請資源啓動yarn-session,然後在yarn-session上運行提交的job。所以相關的job信息已經交給yarn來處理了,所以在yarn webui中可以看到任務的執行信息。

從上圖中可以看到應用的狀態變成了finished,也就是說single job這種提交方式在執行完job後會自動停掉yarn-session,釋放資源。與第一種常駐yarn session模式相比,這種方式具有很大的優勢,只有在需要的時候纔會申請資源,運行完後自動釋放資源,不會佔用資源。而第一種申請完之後,需要通過yarn application -kill applicationID的方式手動釋放資源。

參考:https://www.jianshu.com/p/1b05202c4fb6 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章