1. 获取镜像

  1. # 查看可用的稳定版本
  2. sudo docker search kafka
  3. sudo docker pull wurstmeister/kafka:2.11-0.11.0.3
  4. sudo docker image ls |grep -E 'kafka|zookeeper'

2. 启动容器

  • 创建Kafka容器(方式1)

    1. # 构建并启动kafka容器(需要先启动ZooKeeper),宿主机IP:10.8.0.137,根据实际情况修改
    2. # zk使用伪分布式部署模式(参考:https://www.yuque.com/polaris-docs/container/docker-standalone_zookeeper-cluster)
    3. sudo docker run -d --name kafka1 -p 9092:9092 --network zknet --ip=172.18.0.201 -e KAFKA_BROKER_ID=1 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.8.0.137:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka:2.11-0.11.0.3
    4. sudo docker run -d --name kafka2 -p 9093:9093 --network zknet --ip=172.18.0.202 -e KAFKA_BROKER_ID=2 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.8.0.137:9093 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 -t wurstmeister/kafka:2.11-0.11.0.3
    5. sudo docker run -d --name kafka3 -p 9094:9094 --network zknet --ip=172.18.0.203 -e KAFKA_BROKER_ID=3 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.8.0.137:9094 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 -t wurstmeister/kafka:2.11-0.11.0.3
  • 创建Kafka容器(方式1) ```bash

    开启JMX和Topic删除支持

sudo docker run -d —name kafka1 -p 9092:9092 -p 9997:9997 \ —network bigdata —ip=172.18.0.201 \ -e KAFKA_BROKER_ID=1 \ -e KAFKA_ZOOKEEPER_CONNECT=zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka \ -e KAFKA_JMX_OPTS=”-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.8.0.125 -Dcom.sun.management.jmxremote.rmi.port=9997” \ -e JMX_PORT=9997 \ -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.8.0.137:9092 \ -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \ -e KAFKA_DELETE_TOPIC_ENABLE=true \ -t wurstmeister/kafka:2.11-0.11.0.3

sudo docker run -d —name kafka2 -p 9093:9093 -p 9998:9998 \ —network bigdata —ip=172.18.0.202 \ -e KAFKA_BROKER_ID=2 \ -e KAFKA_ZOOKEEPER_CONNECT=zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka \ -e KAFKA_JMX_OPTS=”-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.8.0.125 -Dcom.sun.management.jmxremote.rmi.port=9998” \ -e JMX_PORT=9998 \ -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.8.0.137:9093 \ -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9093 \ -e KAFKA_DELETE_TOPIC_ENABLE=true \ -t wurstmeister/kafka:2.11-0.11.0.3

sudo docker run -d —name kafka3 -p 9094:9094 -p 9999:9999 \ —network bigdata —ip=172.18.0.203 \ -e KAFKA_BROKER_ID=3 \ -e KAFKA_JMX_OPTS=”-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.8.0.125 -Dcom.sun.management.jmxremote.rmi.port=9999” \ -e JMX_PORT=9999 \ -e KAFKA_ZOOKEEPER_CONNECT=zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka \ -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.8.0.137:9094 \ -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9094 \ -e KAFKA_DELETE_TOPIC_ENABLE=true \ -t wurstmeister/kafka:2.11-0.11.0.3

  1. - **启动kafka容器**
  2. ```bash
  3. for node in `seq 1 3`; do \
  4. docker start kafka${node}; \
  5. done
  • 重启kafka容器

    1. for node in `seq 1 3`; do \
    2. docker restart kafka${node}; \
    3. done
  • 停止kafka容器

    1. for node in `seq 1 3`; do \
    2. docker stop kafka${node}; \
    3. done
  • 删除kafka容器

    1. for node in `seq 1 3`; do \
    2. docker rm kafka${node}; \
    3. done
  • 容器开机自动启动设置

    1. for node in `seq 1 3`; do \
    2. docker update --restart=always kafka${node}; \
    3. done

    3. 验证

    1. #zk查看brokers
    2. sudo docker exec zookeeper_node1 bin/zkCli.sh ls /kafka/brokers/ids
    3. # zk查看topic
    4. sudo docker exec zookeeper_node1 bin/zkCli.sh ls /kafka/brokers/topics
    5. # zkCli
    6. sudo docker exec -it zookeeper_node1 bin/zkCli.sh
    7. # 进入终端
    8. sudo docker exec -it kafka1 /bin/bash
    9. sudo docker exec -it kafka2 /bin/bash
    10. sudo docker exec -it kafka3 /bin/bash
    11. # 查看日志
    12. sudo docker logs kafka1
    13. sudo docker logs -f -t --tail=50 kafka1
    14. # 查看网络
    15. sudo docker network ls
    16. sudo docker inspect kafka1
  • Kafka集群操作

    1. sudo docker exec -it kafka1 /bin/bash
    2. # 注意:开启JMX状态下,需要取消JMX环境变量设置,否则会发生端口占用情况
    3. # java.rmi.server.ExportException: Port already in use: ${JMX_PORT};
    4. # 如果已经配置过“kafka-run-class.sh”,则不需要执行“unset JMX_PORT”
    5. unset JMX_PORT;
    6. # 查看kafka主题列表
    7. kafka-topics.sh --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka --list
    8. # 查看kafka主题详情
    9. kafka-topics.sh --describe --topic mykafka --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka
    10. # 删除kafka主题
    11. kafka-topics.sh --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka --delete --topic mykafka
    12. # 创建kafka主题
    13. kafka-topics.sh --create --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka --replication-factor 1 --partitions 3 --topic mykafka
    14. # kafka生产者
    15. kafka-console-producer.sh --broker-list kafka1:9092,kafka2:9092,kafka3:9092 --topic mykafka
    16. # kafka消费者
    17. kafka-console-consumer.sh --bootstrap-server kafka1:9092,kafka2:9092,kafka3:9092 --topic mykafka --from-beginning

    4. 开启Kafka JMX监控(选)

    修改Kafka各节点JMX启动配置,开启监控功能:

    1. sudo docker exec -it kafka1 /bin/bash
    2. vi /opt/kafka_2.11-0.11.0.3/bin/kafka-server-start.sh

    配置如下: ```bash if [ “x$KAFKA_HEAP_OPTS” = “x” ]; then export KAFKA_HEAP_OPTS=”-server -Xms2G -Xmx2G -XX:PermSize=128m -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=8 -XX:ConcGCThreads=5 -XX:InitiatingHeapOccupancyPercent=70” export JMX_PORT=”9999” fi

kafka1

exec $base_dir/kafka-run-class.sh $EXTRA_ARGS -Djava.rmi.server.hostname=172.18.0.201 kafka.Kafka “$@”

kafka2

exec $base_dir/kafka-run-class.sh $EXTRA_ARGS -Djava.rmi.server.hostname=172.18.0.202 kafka.Kafka “$@”

kafka3

exec $base_dir/kafka-run-class.sh $EXTRA_ARGS -Djava.rmi.server.hostname=172.18.0.203 kafka.Kafka “$@”

  1. 重启服务:
  2. ```bash
  3. for node in `seq 1 3`; do \
  4. docker restart kafka${node}; \
  5. done

5. 开启支持Topic删除配置(选)

  1. sudo docker exec -it kafka1 /bin/bash
  2. vi /opt/kafka_2.11-0.11.0.3/config/server.properties

配置如下:

  1. delete.topic.enable=true

重启服务:

  1. for node in `seq 1 3`; do \
  2. docker restart kafka${node}; \
  3. done

参考

博客园:Docker安装Kafka
https://www.cnblogs.com/linjiqin/p/11891776.html