获取镜像

  1. # 查看可用的稳定版本
  2. sudo docker search kafka
  3. sudo docker pull wurstmeister/kafka
  4. sudo docker image ls |grep kafka

服务编排

  1. mkdir -p /share/kafka && cd /share/kafka
  2. rm -rf /share/kafka/docker-compose.yml
  3. vi /share/kafka/docker-compose.yml

单节点kafka编排

  1. version: '2'
  2. services:
  3. zoo1:
  4. image: zookeeper:3.4.10
  5. restart: always
  6. hostname: zoo1
  7. privileged: true
  8. container_name: zoo1
  9. networks:
  10. my_net:
  11. ipv4_address: 172.24.12.5
  12. ports:
  13. - 2181:2181
  14. environment:
  15. ZOO_MY_ID: 1
  16. ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
  17. zoo2:
  18. image: zookeeper:3.4.10
  19. restart: always
  20. hostname: zoo2
  21. privileged: true
  22. container_name: zoo2
  23. networks:
  24. my_net:
  25. ipv4_address: 172.24.12.6
  26. ports:
  27. - 2182:2181
  28. environment:
  29. ZOO_MY_ID: 2
  30. ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
  31. zoo3:
  32. image: zookeeper:3.4.10
  33. restart: always
  34. hostname: zoo3
  35. privileged: true
  36. container_name: zoo3
  37. networks:
  38. my_net:
  39. ipv4_address: 172.24.12.7
  40. ports:
  41. - 2183:2181
  42. environment:
  43. ZOO_MY_ID: 3
  44. ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
  45. kafka1:
  46. image: wurstmeister/kafka
  47. networks:
  48. my_net:
  49. ipv4_address: 172.24.12.8
  50. ports:
  51. - "9092:9092"
  52. environment:
  53. KAFKA_ADVERTISED_HOST_NAME: localhost
  54. KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
  55. KAFKA_BROKER_ID: 1
  56. KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  57. KAFKA_CREATE_TOPICS: "mykafka1,mykafka2"
  58. depends_on:
  59. - zoo1
  60. - zoo2
  61. - zoo3
  62. container_name: kafka
  63. networks:
  64. my_net:
  65. ipam:
  66. driver: default
  67. config:
  68. - subnet: "172.24.12.0/24"

多节点kafka编排

version: '2'
services:
        zoo1:
                image: zookeeper:3.4.10
                restart: always
                hostname: zoo1
                privileged: true
                container_name: zoo1
                networks:
                    my_net:
                        ipv4_address: 172.24.12.5
                ports:
                        - 2181:2181
                environment:
                        ZOO_MY_ID: 1
                        ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
        zoo2:
                image: zookeeper:3.4.10
                restart: always
                hostname: zoo2
                privileged: true
                container_name: zoo2
                networks:
                    my_net:
                        ipv4_address: 172.24.12.6
                ports:
                        - 2182:2181
                environment:
                        ZOO_MY_ID: 2
                        ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
        zoo3:
                image: zookeeper:3.4.10
                restart: always
                hostname: zoo3
                privileged: true
                container_name: zoo3
                networks:
                    my_net:
                        ipv4_address: 172.24.12.7
                ports:
                        - 2183:2181
                environment:
                        ZOO_MY_ID: 3
                        ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888

        kafka1:
                image: wurstmeister/kafka
                networks:
                    my_net:
                        ipv4_address: 172.24.12.8
                ports:
                  - "9092:9092"
                environment:
                  KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
                  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.24.12.8:9092
                  KAFKA_BROKER_ID: 1
                  KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
                depends_on:
                  - zoo1
                  - zoo2
                  - zoo3
                container_name: kafka1

        kafka2:
                image: wurstmeister/kafka
                networks:
                    my_net:
                        ipv4_address: 172.24.12.9
                ports:
                  - "9093:9093"
                environment:
                  KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
                  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.24.12.9:9093
                  KAFKA_BROKER_ID: 2
                  KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093
                depends_on:
                  - zoo1
                  - zoo2
                  - zoo3
                container_name: kafka2

        kafka3:
                image: wurstmeister/kafka
                networks:
                    my_net:
                        ipv4_address: 172.24.12.10
                ports:
                  - "9094:9094"
                environment:
                  KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
                  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.24.12.10:9094
                  KAFKA_BROKER_ID: 3
                  KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9094
                depends_on:
                  - zoo1
                  - zoo2
                  - zoo3
                container_name: kafka3

networks:
  my_net:
    ipam:
      driver: default
      config:
        - subnet: "172.24.12.0/24"

服务构建

cd /share/kafka
sudo docker-compose -f docker-compose.yml build --no-cache # 不带缓存构建(只创建镜像,不会启动容器)
sudo docker-compose -f docker-compose.yml up -d  # 构建后运行
sudo docker-compose -f docker-compose.yml up --build # 跟踪方式构建,可用于调试

验证

cd /share/kafka
# 查看进程
sudo docker-compose -f docker-compose.yml ps
# 列出所有Kafka brokers
docker exec zoo1 bin/zkCli.sh ls /brokers/ids
# 进入终端
sudo docker exec -it kafka /bin/bash
sudo docker exec -it kafka1 /bin/bash
# 查看kafka主题列表
kafka-topics.sh --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 --list
# 删除kafka主题
kafka-topics.sh --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 --delete --topic mykafka
# 创建kafka主题
kafka-topics.sh --create --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 --replication-factor 1 --partitions 3 --topic mykafka
# kafka生产者
kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
# kafka消费者
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mykafka --from-beginning
# 查看日志
sudo docker logs kafka
sudo docker logs -f -t --tail=50 kafka
# 查看网络
sudo docker network ls
sudo docker inspect kafka
# 停止
sudo docker-compose -f docker-compose.yml stop
# 移除
sudo docker-compose -f docker-compose.yml down

参考

博文:docker安装kafka
https://www.cnblogs.com/linjiqin/p/11891776.html