Kafka Docker集群搭建

1. Zookeeper下载
http://apache.org/dist/zookeeper/

http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
2.Kafka下载
http://apache.org/dist/kafka/
http://mirror.bit.edu.cn/apache/kafka/0.10.0.0/kafka_2.10-0.10.0.0.tgz
3,Zookeeper的Docker镜像制作
  • 可以直接使用Zookeeper镜像
docker pull zookeeper
  • 也可以自己使用Dockerfile制作
    #基础镜像使用java,这样可以免于设置java环境
    FROM java

    #作者
    MAINTAINER Bonker 

    #定义工作目录
    ENV WORK_PATH /opt/zkcluster/zkconf

    #定义zookeeper文件夹名称
    ENV ZOOKEEPER_PACKAGE_NAME zookeeper-3.4.9

    #创建工作目录
    RUN mkdir -p $WORK_PATH

    #把zookeeper压缩文件复制到工作目录
    COPY ./$ZOOKEEPER_PACKAGE_NAME.tar.gz $WORK_PATH/

    #解压缩
    RUN tar -xvf $WORK_PATH/$ZOOKEEPER_PACKAGE_NAME.tar.gz -C $WORK_PATH/

    #删除压缩文件
    RUN rm $WORK_PATH/$ZOOKEEPER_PACKAGE_NAME.tar.gz

    #给shell赋予执行权限
    RUN chmod a+x $WORK_PATH/$ZOOKEEPER_PACKAGE_NAME/bin/zkServer.sh

    RUN mv $WORK_PATH/$ZOOKEEPER_PACKAGE_NAME/conf/zoo_sample.cfg $WORK_PATH/$ZOOKEEPER_PACKAGE_NAME/conf/zoo.cfg

    ENTRYPOINT  $WORK_PATH/$ZOOKEEPER_PACKAGE_NAME/bin/zkServer.sh start-foreground
    #ENTRYPOINT  ["/bin/sh -c",$WORK_PATH/$ZOOKEEPER_PACKAGE_NAME/bin/zkServer.sh]
    #CMD [start-foreground]
  • 构建zookeeper
docker build -t bonker/zookeeper:3.4.9 .
  • 运行docker
docker run -d -p 2181:2181 --name zookeeper3.4.9 bonker/zookeeper:3.4.9
4,Kafka的Docker镜像制作
  • 编写执行脚本kafkaStart.sh
    sed -i "s/broker.id=0/broker.id=$BROKER_ID/g" $WORK_PATH/$KAFKA_PACKAGE_NAME/config/server.properties
    $WORK_PATH/$KAFKA_PACKAGE_NAME/bin/kafka-server-start.sh $WORK_PATH/$KAFKA_PACKAGE_NAME/config/server.properties
  • 编写Dockerfile
    #基础镜像使用java,这样可以免于设置java环境
    FROM java

    #作者
    MAINTAINER Bonker 

    #定义工作目录
    ENV WORK_PATH /usr/local/work

    #定义kafka文件夹名称
    ENV KAFKA_PACKAGE_NAME kafka_2.10-0.10.0.0

    #创建工作目录
    RUN mkdir -p $WORK_PATH

    #把kafka压缩文件复制到工作目录
    COPY ./$KAFKA_PACKAGE_NAME.tgz $WORK_PATH/

    #把kafka执行脚本复制到工作目录
    COPY ./kafkaStart.sh $WORK_PATH/

    #给shell赋予执行权限
    RUN chmod a+x $WORK_PATH/kafkaStart.sh

    #解压缩
    RUN tar -xvf $WORK_PATH/$KAFKA_PACKAGE_NAME.tgz -C $WORK_PATH/

    #删除压缩文件
    RUN rm $WORK_PATH/$KAFKA_PACKAGE_NAME.tgz

    #执行sed命令修改文件,将连接zk的ip改为link参数对应的zookeeper容器的别名
    RUN sed -i 's/zookeeper.connect=localhost:2181/zookeeper.connect=zkhost:2181/g' $WORK_PATH/$KAFKA_PACKAGE_NAME/config/server.properties

    #执行sed命令修改文件,改变brokerId
    #RUN sed -i "s/broker.id=0/broker.id=$BROKER_ID/g" $WORK_PATH/$KAFKA_PACKAGE_NAME/config/server.properties

    #CMD $WORK_PATH/$KAFKA_PACKAGE_NAME/bin/kafka-server-start.sh $WORK_PATH/$KAFKA_PACKAGE_NAME/config/server.properties
    CMD $WORK_PATH/kafkaStart.sh
  • 构建kafka
docker build -t bonker/kafka:2.10-0.10.0.0 .
5,安装docker-compose
  1. 安装python-pip
yum -y install epel-release
yum -y install python-pip
  1. 安装docker-compose
pip install docker-compose
待安装完成后,执行查询版本的命令,即可安装docker-compose
docker-compose version
6,运行Dokcer容器
  • 编写docker-compose.yml
version: '2'
services: 
  zk_server: 
    image: bonker/zookeeper:3.4.9
    restart: always
  kafka_server: 
    image: bonker/kafka:2.10-0.10.0.0
    ports: 
      - "9091:9092"
    environment: 
     BROKER_ID: 1
    links: 
      - zk_server:zkhost
    restart: always
  message_producer: 
    image: bonker/kafka:2.10-0.10.0.0
    ports: 
      - "9092:9092"
    environment: 
     BROKER_ID: 2
    links: 
      - zk_server:zkhost
    restart: always
  message_consumer: 
    image: bonker/kafka:2.10-0.10.0.0
    ports: 
      - "9093:9092"
    environment: 
     BROKER_ID: 3
    links: 
      - zk_server:zkhost
    restart: always
  • 启动容器
现在打开终端,在docker-compose.yml所在目录下执行docker-compose up -d,即可启动所有容器

你可能感兴趣的:(大数据,运维,java)