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
  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,便可啓動全部容器
相關文章
相關標籤/搜索