参考

官方 Docker Setup

安装Docker

请参考安装docker

拉取镜像

  1. docker search flink:scala
  1. docker pull flink:1.15.2-scala_2.12-java8
  1. docker pull flink:scala_2.12-java8

暂时不清楚flink:1.15.2-scala_2.12-java8和flink:scala_2.12-java8有什么区别,我选择了flink:1.15.2-scala_2.12-java8

docker方式运行容器(没成功)

一个 Flink 会话集群可用于运行多个作业。部署群集后,需要将每个作业提交到群集。要使用 Docker 部署 Flink 会话集群,您需要启动一个 JobManager 容器。为了启用容器之间的通信,我们首先设置一个必需的 Flink 配置属性并创建一个网络:

创建docker网络

  1. docker network create flink-network

docker-compose方式运行容器

  1. mkdir /usr/local/flink/flink-docker/jobmanager
  1. mkdir D:/data/flink/flink-docker/taskmanager

docker-compose.yml

在上述目录创建文件 docker-compose.yml

  1. version: "2.2"
  2. services:
  3. jobmanager:
  4. image: flink:1.15.2-scala_2.12-java8
  5. container_name: jobmanager-1
  6. expose:
  7. - "6123"
  8. ports:
  9. - "8081:8081"
  10. command: jobmanager
  11. volumes:
  12. - D:/data/flink/jobmanager/conf:/opt/flink/conf
  13. - D:/data/flink/jobmanager/lib:/opt/flink/lib
  14. - D:/data/flink/jobmanager/log:/opt/flink/log
  15. environment:
  16. - |
  17. FLINK_PROPERTIES=
  18. jobmanager.rpc.address: jobmanager
  19. parallelism.default: 2
  20. web.upload.dir: /opt/flink/target
  21. taskmanager:
  22. image: flink:1.15.2-scala_2.12-java8
  23. container_name: taskmanager-1
  24. depends_on:
  25. - jobmanager
  26. command: taskmanager
  27. scale: 1
  28. volumes:
  29. - D:/data/flink/taskmanager/conf:/opt/flink/conf
  30. - D:/data/flink/taskmanager/lib:/opt/flink/lib
  31. - D:/data/flink/taskmanager/log:/opt/flink/log
  32. environment:
  33. - |
  34. FLINK_PROPERTIES=
  35. jobmanager.rpc.address: jobmanager
  36. taskmanager.numberOfTaskSlots: 2
  37. parallelism.default: 2

:::warning /opt/flink/lib : 如果配置了这个必须要实现把flink原本来lib中的jar放一份,要不然启动会失败 :::

  1. cd D:/data/flink/flink-docker
  1. docker-compose up -d

配置映射

docker-compose.yml执行后,如果提示错误或者运行不正常,需先启动以下测试容器并将相应的目录文件从docker[容器里拷贝出到宿主机后再执行重新docker-compose.yml

作业管理器

  1. docker run --name=jobmanager2 --network flink-network --publish 8082:8081 -d -it flink:1.15.2-scala_2.12-java8

启动容器jobmanager2成功后,将容器jobmanager2的对应文件考到主机上,如

  1. docker cp jobmanager2:/opt/flink/conf D:/data/flink/jobmanager/conf
  1. docker cp jobmanager2:/opt/flink/lib D:/data/flink/jobmanager/lib
  1. docker cp jobmanager2:/opt/flink/log D:/data/flink/taskmanager/log

任务管理器容器

启动任务管理器容器

  1. docker run --name=taskmanager2 --network flink-network --env FLINK_PROPERTIES="JOB_MANAGER_RPC_ADDRESS=jobmanager" -d -it flink:1.15.2-scala_2.12-java8
  1. docker cp taskmanager2:/opt/flink/conf D:/data/flink/taskmanager/conf
  1. docker cp taskmanager2:/opt/flink/lib D:/data/flink/taskmanager/lib
  1. docker cp taskmanager2:/opt/flink/log D:/data/flink/taskmanager/log

启动flink集群

  1. cd D:/data/flink/flink-docker
  1. docker-compose up -d
  1. docker-compose -f docker-compose.yml stop
  2. docker-compose -f docker-compose.yml down

进入容器

  1. cd D:\data\flink\flink-docker
  1. # jobmanager
  2. docker-compose exec jobmanager /bin/bash
  3. # taskmanager
  4. docker-compose exec taskmanager /bin/bash

或者

  1. docker exec -it flink-docker-taskmanager-1 /bin/bash
  1. docker exec -it flink-docker-jobmanager-1 /bin/bash

验证flink

  1. cd /usr/local/flink/flink-docker
  2. # 查看进程
  3. sudo docker-compose -f docker-compose.yml ps
  4. # jobmanager
  5. sudo docker-compose exec jobmanager /bin/bash
  6. # taskmanager
  7. sudo docker-compose exec taskmanager /bin/bash
  8. # 查看日志
  9. sudo docker logs flink_jobmanager_1
  10. sudo docker logs -f -t --tail=50 flink_jobmanager_1
  11. # 查看网络
  12. sudo docker network ls
  13. sudo docker inspect flink_jobmanager_1

Flink CDC的lib依赖

下载下面列出的依赖包,并将它们放到目录 flink容器的/lib/ 下:
下载地址

选择对应的版本下载,如flink-connector-jdbc-1.15.2.jar,将下载的数据库连接驱动包的jar放到 flink的lib目录下
任务管理器容器

  1. docker cp D:/data/flink/lib/flink-sql-connector-mysql-cdc-2.2.1.jar flink-docker-taskmanager-1
  2. :/opt/flink/lib/flink-sql-connector-mysql-cdc-2.2.1.jar
  1. docker cp D:/data/flink/lib/flink-connector-jdbc-1.15.2.jar flink-docker-taskmanager-1
  2. :/opt/flink/lib/flink-connector-jdbc-1.15.2.jar

作业管理器容器

  1. docker cp D:/data/flink/lib/flink-sql-connector-mysql-cdc-2.2.1.jar flink-docker-jobmanager-1
  2. :/opt/flink/lib/flink-sql-connector-mysql-cdc-2.2.1.jar
  1. docker cp D:/data/flink/lib/flink-connector-jdbc-1.15.2.jar flink-docker-jobmanager-1
  2. :/opt/flink/lib/flink-connector-jdbc-1.15.2.jar

到任务管理器容器查看是否拷贝成功

  1. cd D:\data\flink\flink-docker
  1. docker-compose exec taskmanager /bin/bash
  1. cd ./lib/
  2. ll

1665625694682.png

通过Web查看集群

官方镜像暴露了8081端口,由于是在虚拟机上运行的。所以在浏览器上通过虚拟机IP:端口的方式访问,如果docker是直接装在机器上的,没有装在虚拟机上,那么可以直接通过[http://localhost:8081/](http://localhost:8081/)访问