1. 获取镜像
# 查看可用的稳定版本
sudo docker search kafka
sudo docker pull wurstmeister/kafka:2.11-0.11.0.3
sudo docker image ls |grep -E 'kafka|zookeeper'
2. 启动容器
创建Kafka容器(方式1)
# 构建并启动kafka容器(需要先启动ZooKeeper),宿主机IP:10.8.0.137,根据实际情况修改
# zk使用伪分布式部署模式(参考:https://www.yuque.com/polaris-docs/container/docker-standalone_zookeeper-cluster)
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
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
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
- **启动kafka容器**
```bash
for node in `seq 1 3`; do \
docker start kafka${node}; \
done
重启kafka容器
for node in `seq 1 3`; do \
docker restart kafka${node}; \
done
停止kafka容器
for node in `seq 1 3`; do \
docker stop kafka${node}; \
done
删除kafka容器
for node in `seq 1 3`; do \
docker rm kafka${node}; \
done
容器开机自动启动设置
for node in `seq 1 3`; do \
docker update --restart=always kafka${node}; \
done
3. 验证
#zk查看brokers
sudo docker exec zookeeper_node1 bin/zkCli.sh ls /kafka/brokers/ids
# zk查看topic
sudo docker exec zookeeper_node1 bin/zkCli.sh ls /kafka/brokers/topics
# zkCli
sudo docker exec -it zookeeper_node1 bin/zkCli.sh
# 进入终端
sudo docker exec -it kafka1 /bin/bash
sudo docker exec -it kafka2 /bin/bash
sudo docker exec -it kafka3 /bin/bash
# 查看日志
sudo docker logs kafka1
sudo docker logs -f -t --tail=50 kafka1
# 查看网络
sudo docker network ls
sudo docker inspect kafka1
Kafka集群操作
sudo docker exec -it kafka1 /bin/bash
# 注意:开启JMX状态下,需要取消JMX环境变量设置,否则会发生端口占用情况
# java.rmi.server.ExportException: Port already in use: ${JMX_PORT};
# 如果已经配置过“kafka-run-class.sh”,则不需要执行“unset JMX_PORT”
unset JMX_PORT;
# 查看kafka主题列表
kafka-topics.sh --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka --list
# 查看kafka主题详情
kafka-topics.sh --describe --topic mykafka --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka
# 删除kafka主题
kafka-topics.sh --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka --delete --topic mykafka
# 创建kafka主题
kafka-topics.sh --create --zookeeper zookeeper_node1:2181,zookeeper_node2:2181,zookeeper_node3:2181/kafka --replication-factor 1 --partitions 3 --topic mykafka
# kafka生产者
kafka-console-producer.sh --broker-list kafka1:9092,kafka2:9092,kafka3:9092 --topic mykafka
# kafka消费者
kafka-console-consumer.sh --bootstrap-server kafka1:9092,kafka2:9092,kafka3:9092 --topic mykafka --from-beginning
4. 开启Kafka JMX监控(选)
修改Kafka各节点JMX启动配置,开启监控功能:
sudo docker exec -it kafka1 /bin/bash
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 “$@”
重启服务:
```bash
for node in `seq 1 3`; do \
docker restart kafka${node}; \
done
5. 开启支持Topic删除配置(选)
sudo docker exec -it kafka1 /bin/bash
vi /opt/kafka_2.11-0.11.0.3/config/server.properties
配置如下:
delete.topic.enable=true
重启服务:
for node in `seq 1 3`; do \
docker restart kafka${node}; \
done
参考
博客园:Docker安装Kafka
https://www.cnblogs.com/linjiqin/p/11891776.html