https://blog.csdn.net/yunxiao6/article/details/108705244
Standalone:需要配合 kubectl + yaml 部署,Flink 无法感知 K8s 集群的存在,资源被动
Native: 仅使用 flink 客户端 kubernetes-session.sh or flink run 部署,Flink 主动与 K8s 申请资源
FROM flink:1.12.1-scala_2.11-java8
# 安装 python3 and pip3 及需要的debug工具
RUN apt-get update -y && \
apt-get install -y python3.7 python3-pip python3.7-dev \
&& rm -rf /var/lib/apt/lists/*
RUN rm -rf /usr/bin/python
RUN ln -s /usr/bin/python3 /usr/bin/python
# 安装 Python Flink
RUN pip3 install apache-flink==1.12.1
# 如果有引用第三方 Python 依赖库, 可以在构建镜像时安装上这些依赖
#COPY /path/to/requirements.txt /opt/requirements.txt
# RUN pip3 install -r requirements.txt
# 如果有引用第三方 Java 依赖, 也可以在构建镜像时加入到 ${FLINK_HOME}/usrlib 目录下
RUN mkdir -p $FLINK_HOME/usrlib
COPY /path/of/external/jar/dependencies $FLINK_HOME/usrlib/
COPY /path/of/python/codes /opt/python_codes
build PyFlink App image
首先创建静态 session 集群。通过 kubectl create -f yaml 的方式创建 ConfigMap、Service、JobManager Deployment、TaskManager Deployment 资源
向创建好的 session 集群中提交任务。
创建 Session 集群
kubectl create -f flink-configuration-configmap.yaml
kubectl create -f jobmanager-service.yaml
kubectl create -f jobmanager-session-deployment.yaml
kubectl create -f taskmanager-session-deployment.yaml
kubectl create -f jobmanager-rest-service.yaml
提交 Job
# 提交一个flink stream job
./bin/flink run -m 192.168.0.1:30081 ./examples/streaming/TopSpeedWindowing.jar
# 提交一个pyflink batch job
sudo flink run -m 192.168.0.1:30081 -pyfs ./examples/python/table/batch -pym word_count
回收 Session 集群
kubectl delete -f jobmanager-rest-service.yaml
kubectl delete -f jobmanager-service.yaml
kubectl delete -f flink-configuration-configmap.yaml
kubectl delete -f taskmanager-session-deployment.yaml
kubectl delete -f jobmanager-session-deployment.yaml
由 Standalone JobCluster EntryPoint 执行,从 classpath 找到用户 Jar,执行它的 main 方法得到 JobGraph 。再提交到 Dispathcher,这时候走 Recover Job 的逻辑,提交到 JobMaster。JobMaster 向 ResourceManager 申请资源,请求 slot,执行 Job。
提交 Job
kubectl create -f flink-configuration-configmap.yaml
kubectl create -f jobmanager-service.yaml
kubectl create -f jobmanager-rest-service.yaml
kubectl create -f jobmanager-application.yaml
kubectl create -f taskmanager-job-deployment.yaml
创建 Session 集群
./bin/kubernetes-session.sh \
-Dkubernetes.cluster-id=session-cluster-1 \
-Dtaskmanager.numberOfTaskSlots=1 \
-Dresourcemanager.taskmanager-timeout=3600000 \
-Dkubernetes.rest-service.exposed.type=NodePort \
-Dkubernetes.container.image=demo-pyflink-app:1.12.1
提交 Job
./bin/flink run \
--target kubernetes-session \
-Dkubernetes.cluster-id=session-cluster-1 \
-pyfs ./examples/python/table/batch \
-pym word_count
# 或者
sudo flink run -m 192.168.0.1:30081 -pyfs ./examples/python/table/batch -pym word_count
回收 Session 集群
echo 'stop' | ./bin/kubernetes-session.sh \
-Dkubernetes.cluster-id=session-cluster-1 \
-Dexecution.attached=true
# 或者
kubectl delete deployment/session-cluster-1
提交 Job
# 1.12版本对pyflink on k8s具有良好支持
./bin/flink run-application -p 2 -t kubernetes-application \
-Dkubernetes.cluster-id=app-cluster \
-Dtaskmanager.memory.process.size=4096m \
-Dkubernetes.taskmanager.cpu=2 \
-Dtaskmanager.numberOfTaskSlots=4 \
-Dkubernetes.container.image=demo-pyflink-app:1.12.1 \
-pyfs /opt/python_codes \
-pym new_word_count
# 1.11版本命令如下
./bin/flink run-application -p 2 -t kubernetes-application \
-Dexecution.attached=true \
-Dkubernetes.cluster-id=app-cluster \
-Dtaskmanager.memory.process.size=4096m \
-Dkubernetes.taskmanager.cpu=2 \
-Dtaskmanager.numberOfTaskSlots=4 \
-Dkubernetes.container.image=demo-pyflink-app:1.11.3 \
-Dpython.files=/opt/python_codes \
-c org.apache.flink.client.python.PythonDriver local:///opt/flink/opt/flink-python_2.11-1.11.2.jar \
-py /opt/python_codes/PyFlinkDriver.py