前言
Flink的demo環境,可以直接在windows或者Linux下運行。服務會自動啓動一個JobManager以及一個TaskManager。
$ ./bin/start-cluster.sh # Start Flink
生產環境一般都需要高可用支持,屏蔽單點故障產生的影響(這裏的高可用只JobManager的高可用)。Flink支持兩種部署方式:standalone以及 YARN clusters。
standalone方式下,Flink通過Zookeeper管理多個JobManager,並在同一時刻保證一個Leader生效。
YARN方式下,每次只有一個JobManager運行。如果JobManager出現故障,則YARN負責重啓。
本文采用了standalone方式部署。(Hadoop環境的代價太大了。學習成本和維護成本都很高,而且內部機制產生的影響很多,不利於學習Flink。)
Windows WSL
Flink的standalone模式需要Linux環境。之前一直用虛擬機來提供Linux環境,但是太笨重了。啓動、運行佔資源,需要配置網絡,無法和windows共享資源。所以拋棄了虛擬機,採用WSL。
- WSL速度快,輕便。設置開機啓動後,打開對話框就可以直接使用。設置了開機啓動,也不會佔用什麼資源。
- WSL可以共享windows的環境變量。如果windows安裝了java,並配置了JAVA_HOME,linux環境也可以使用。
- WSL可以直接訪問windows的文件系統。
/mnt/系統盤 #系統盤就是C,D,E
#例如訪問C盤的文件,就可以ls /mnt/c
- 網絡共享。WSL直接使用的是windows的網絡。
既然都這麼優秀了,那麼怎麼安裝WSL呢。直接在應用商店搜索安裝就好。我使用的是ubuntu,因爲這個是使用最多的WSL。如有疑問,可以上網搜索。
環境準備
爲了加快軟件安裝速度,可以更改爲阿里的源。
我的源配置:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
java環境
windows安裝了java,不需要額外安裝。
可以通過apt安裝openjdk,也可以下載oralce的jdk。
ssh
安裝ssh-server
sudo apt-get install openssh-server
修改配置
sudo vi /etc/ssh/sshd_config
修改內容:
ListenAddress localhost #可以不修改。監聽本地。默認是0.0.0.0,監聽所有。
PermitRootLogin yes #允許root用戶遠程登錄
PasswordAuthentication yes #密碼認證
重啓sshd-server:
sudo service ssh restart
或者 /etc/init.d/ssh restart
啓動時會發現錯誤提示如下。需要生成祕鑰。
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
生成祕鑰命令:
sudo ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
Flink搭建
首先需要準備安裝文件。
下載flink:
https://mirrors.tuna.tsinghua.edu.cn/apache/flink/flink-1.9.1/flink-1.9.1-bin-scala_2.12.tgz
下載zookeeper:
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.6/apache-zookeeper-3.5.6-bin.tar.gz
zookeeper
配置conf/zoo.cfg。我只啓動了一個節點,生成環境應該至少三個節點。
# ZooKeeper quorum peers
server.1=localhost:2888:3888
# server.2=host:peer-port:leader-port
Flink的發佈版本中攜帶了zookeeper,所以可以通過Flink直接啓動zookeeper。
bin/start-zookeeper-quorum.sh
注意,官方文檔中明確說明,不建議採用Flink的zookeeper。生產環境應該使用zookeeper自己的環境。
運行成功後,可以通過之前下載的zookeeper客戶端工具zkCli.sh,訪問zookeeper信息。我們之後也會通過這種方式查看JobManager的集羣管理數據。
JobManager
配置conf/flink-conf.yaml
high-availability: zookeeper
high-availability.zookeeper.quorum: localhost:2181
high-availability.storageDir: file:///mnt/d/temp/ha/
high-availability.zookeeper.path.root: /flink
high-availability.cluster-id: /flink_default_cluster
配置JobManager啓動實例conf/masters:
localhost:8081
localhost:8082
localhost:8083
啓動cluster:
bin/start-cluster.sh
啓動成功後,可以查看進程。可以看到啓動了三個JobManager。
解壓之前下載的zookeeper,使用bin/zkCli.sh可以訪問zookeeper,查看目錄信息。
其中flink是我們配置的Flink集羣根路徑,flink_default_cluster是我們配置的cluster-id。初始啓動的時候,目錄下可能沒有這麼多子目錄,在之後驗證環境,發佈job後,相關的路徑就會創建出來。
Flink會將信息存放在high-availability.storageDir存儲路徑下。本文配置的是file:///,表示使用本地存儲。可以直接訪問本地路徑/mnt/d/temp/ha/,查看Flink存放了哪些信息。其實,上傳的jar,pid等信息都會再次路徑下。如果有hadoop環境,那麼可以配置爲hdfs:///路徑。
驗證環境
最好的驗證方式,就是運行一個任務。爲了方便,我們使用Flink自帶的example來驗證環境。
Flink貼心的將所有的例子都打包好,和Flink的版本一起發佈。可以通過Flink提供的客戶端上傳,也可以通過頁面操作上傳。爲了簡單,我們採用頁面操作。
訪問頁面地址:
http://localhost:8081/
在submit tab頁,提交jar
運行數據源。由於我們使用的是socketWindowWordCount,所以需要建立socket數據源。
nc -l 12345
運行任務。需要定義參數,指向上文建立的socket數據源。
在running job頁面,查看運行中的任務:
運行任務後,在nc端輸入字符:
在TaskManager的頁面,可以查看統計輸出結果:
停止standalone cluster