## Docker搭建flink+kafka (一)本地环境搭建
- [kafka安装](#kafka-)
- [flink安装](#flink-)
- [docker-compose](#docker-compose-)
#### kafka安装
- kafka_2.12-2.4.0.tgz
- jdk-8u271-linux-x64.tar.gz
- 配置Dockerfile,解压kafka,jdk,并配置环境
``` Dockerfile
FROM centos:7.6.1810
COPY ./ /home/soft
COPY ./kafka-start.sh /home
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo 'Asia/Shanghai' >/etc/timezone && \
mkdir /home/kafka && chmod +x /home/kafka-start.sh && \
tar xzf /home/soft/jdk-8u271-linux-x64.tar.gz -C /usr/local && \
tar xzf /home/soft/kafka_2.12-2.4.0.tgz -C /home/kafka --strip-components 1 && \
rm -rf /home/soft && \
sed -i '32alisteners=PLAINTEXT://adkafka.com:9092' /home/kafka/config/server.properties
ENV JAVA_HOME=/usr/local/jdk1.8.0_271
ENV JRE_HOME=/usr/local/jdk1.8.0_271/jre
ENV CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
ENV PATH=$PATH:${JAVA_HOME}/bin
EXPOSE 9092 2181
CMD ["/home/kafka-start.sh"]
```
- /home/kafka-start.sh 启动脚本编写
``` Bash
#!bin/bash
nohup /home/kafka/bin/zookeeper-server-start.sh /home/kafka/config/zookeeper.properties 1>/dev/null 2>&1 &
nohup /home/kafka/bin/kafka-server-start.sh /home/kafka/config/server.properties 1>/dev/null 2>&1 &
# 创建 testtopic 测试主题
echo 'start server...'
sleep 3
echo 'createing topic testtopic'
/home/kafka/bin/kafka-topics.sh --create --zookeeper adkafka.com:2181 --replication-factor 1 --partitions 1 --topic testtopic
echo 'start consumer topic testtopic...'
/home/kafka/bin/kafka-console-consumer.sh --bootstrap-server adkafka.com:9092 --topic testtopic
```
- 根据Dockerfile生成kafka镜像
```
docker build -t adkafka:1.0 ./
```
- 启动容器
```
docker run -it --name adkafka -h adkafka.com -p 9092:9092 -p 2181:2181 adkafka:1.0
```
#### flink安装
- flink-1.11.2-bin-scala_2.11.tgz
- jdk-8u271-linux-x64.tar.gz
- 使用到的jar包
- flink-connector-jdbc_2.11-1.11.2.jar
- flink-sql-connector-kafka_2.11-1.11.2.jar
- mysql-connector-java-8.0.13.jar
- 配置Dockerfile,解压flink,jdk,并配置环境
``` Dockerfile
FROM centos:7.6.1810
COPY ./ /home/soft
COPY ./flink-start.sh /home
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo 'Asia/Shanghai' >/etc/timezone && \
mkdir /home/flink && chmod +x /home/flink-start.sh && \
tar xzf /home/soft/jdk-8u271-linux-x64.tar.gz -C /usr/local && \
tar xzf /home/soft/flink-1.11.2-bin-scala_2.11.tgz -C /home/flink --strip-components 1 && \
mv /home/soft/jar/*.jar /home/flink/lib/ && \
rm -rf /home/soft && \
sed -i '60c taskmanager.numberOfTaskSlots: 8' /home/flink/conf/flink-conf.yaml
# 为了多测试几个作业,多写几个slots
ENV JAVA_HOME=/usr/local/jdk1.8.0_271
ENV JRE_HOME=/usr/local/jdk1.8.0_271/jre
ENV CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
ENV PATH=$PATH:${JAVA_HOME}/bin
EXPOSE 8081
CMD ["/home/flink-start.sh"]
```
- /home/flink-start.sh 启动脚本编写
``` Bash
#!bin/bash
nohup /home/flink/bin/jobmanager.sh start-foreground 1>/dev/null 2>&1 &
/home/flink/bin/taskmanager.sh start-foreground
```
- 根据Dockerfile生成flink镜像
```
docker build -t adflink:1.0 ./
```
- 启动容器
```
docker run -it --name adflink --link adkafka:adkafka.com -p 8081:8081 -v /Users/adong/code/docker/flink:/home/dev:rw adflink:1.0
```
#### docker-compose 方式运行
- 创建docker-compose.yml
``` yaml
version: "3.3"
services:
adflink:
image: adflink:1.0
container_name: adflink
volumes:
- "/Users/adong/code/docker/flink:/home/dev:rw"
links:
- adkafka.com
ports:
- "8081:8081"
adkafka.com:
image: adkafka:1.0
container_name: adkafka
ports:
- "9092:9092"
- "2181:2181"
# docker-compose up --force-recreate
```
- 结果
- [](https://static.adong.wiki/static/images/md/2021010101.jpg)
- 浏览器输入 http://localhost:8081
- [](https://static.adong.wiki/static/images/md/2021010102.png)
- 到此完成安装