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 <bonker@foxmail.com>
    
    #定义工作目录
    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 <bonker@foxmail.com>
    
    #定义工作目录
    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
  2. 安装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,即可启动所有容器
作者:Bonker
出处:http://www.cnblogs.com/Bonker
QQ:519841366
       
本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利
上一篇:《正则表达式经典实例(第2版)》——导读


下一篇:Greenplum 空间(GIS)数据检索 B-Tree & GiST 索引实践 - 阿里云HybridDB for PostgreSQL最佳实践