docker-compose部署kafka、kafkamanager、zookeeper
我这里是将kafka和zookeeper放在一个yml文件中的,其实可以把这三个都放在一个yml里面,就不会出现网络不通的问题了。
zookeeper和kafka
# yaml 配 version: '2' services: zookeeper: image: wurstmeister/zookeeper restart: always ports: - 2181:2181 kafka1: image: wurstmeister/kafka restart: always depends_on: - zookeeper ports: - 9093:9093 environment: KAFKA_ADVERTISED_HOST_NAME: kafka1 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_LISTENERS: PLAINTEXT://:9093 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.1.6:9093 KAFKA_BROKER_ID: 1 kafka2: image: wurstmeister/kafka restart: always depends_on: - zookeeper ports: - 9094:9094 environment: KAFKA_ADVERTISED_HOST_NAME: kafka2 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_LISTENERS: PLAINTEXT://:9094 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.1.6:9094 KAFKA_BROKER_ID: 2 kafka3: image: wurstmeister/kafka restart: always depends_on: - zookeeper ports: - 9095:9095 environment: KAFKA_ADVERTISED_HOST_NAME: kafka3 KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 KAFKA_LISTENERS: PLAINTEXT://:9095 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.1.6:9095 KAFKA_BROKER_ID: 3
kafkamanager
docker run -it -d --net zookeeper_default --link kafka-zookeeper-1:zookeeper --link kafka-kafka1-1:kafka --link kafka-kafka2-1:kafka --link kafka-kafka3-1:kafka -p 9000:9000 -e ZK_HOSTS=192.168.1.6:2181 sheepkiller/kafka-manager
注意这里我遇到了三个坑:
1:网段不同 导致容器启动失败
/usr/bin/docker-current: Error response from daemon: Cannot link to /kafka-kafka1-1, as it does not belong to the default network.
这是因为之前部署的zookeeper和kafka在同一个网络中,而kafkmanager是另外部署的,重新启了一个容器,他们就不在一个网络中了,所以需要加一个 --net zookeepe_default
这是查询容器网络的命令,然后使用--link连接相应的网段
docker network ls
2:容器启动成功但是kafkamanager连不上zookeeper
没有指定IP,导致连接zookeeper失败
docker run -it -d --net zookeeper_default --link kafka-zookeeper-1:zookeeper --link kafka-kafka1-1:kafka --link kafka-kafka2-1:kafka --link kafka-kafka3-1:kafka -p 9000:9000 -e ZK_HOSTS=zookeeper:2181 sheepkiller/kafka-manager
o.a.z.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_161]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_161]
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) ~[org.apache.zookeeper.zookeeper-3.4.6.jar:3.4.6-1569965]
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) ~[org.apache.zookeeper.zookeeper-3.4.6.jar:3.4.6-1569965]
然后我就指定了具体的IP问题得到解决
docker run -it -d --net zookeeper_default --link kafka-zookeeper-1:zookeeper --link kafka-kafka1-1:kafka --link kafka-kafka2-1:kafka --link kafka-kafka3-1:kafka -p 9000:9000 -e ZK_HOSTS=192.168.1.6:2181 sheepkiller/kafka-manager
3:没有关闭防火墙,在访问http://192.168.1.6:9000/ 页面时报错
关闭防火墙后解决
//查看防火墙状态 systemctl status firewalld //关闭防火墙 systemctl stop firewalld //永久关闭防火墙 systemctl disable firewalld //启用防火墙 systemctl enable firewalld //开启防火墙 systemctl start firewalld