跨机器部署的Kafka多Broker集群无法正常工作
Let’s break down the key checks and fixes to get your cross-machine Kafka cluster up and running, starting with the gaps in your provided configuration and common pitfalls.
1. First, Fix Machine 2's Kafka server.properties
Your machine 2 Kafka config is incomplete, and this is almost certainly a critical issue. Here’s what you need to set to match the cluster requirements:
- Unique
broker.id: Must be different from machine 1’s1— set this to2 - Port: Use a unique port (e.g.,
9095) or ensure port9094isn’t in use on machine 2 - ZooKeeper connection string: Exact same as machine 1:
10.0.215.22:2181,10.0.229.48:2181 - Match critical flags: Keep
delete.topic.enable=trueandauto.create.topics.enable=trueconsistent with machine 1
Sample critical config snippet for machine 2:
broker.id=2 port=9095 zookeeper.connect=10.0.215.22:2181,10.0.229.48:2181 delete.topic.enable=true auto.create.topics.enable=true num.partitions=1
2. Validate Your ZooKeeper Cluster Health
Kafka relies entirely on a healthy ZooKeeper cluster, so let’s confirm that first:
- Check
myidfiles: Each ZooKeeper node needs amyidfile in itsdataDirdirectory with a value matching itsserver.Xnumber:- Machine 1 (server.1): Run
cat /path/to/zookeeper/data/myid— it should return1 - Machine 2 (server.2): Same command should return
2
If this file is missing or has the wrong value, ZooKeeper won’t join the cluster.
- Machine 1 (server.1): Run
- Verify port connectivity: Ensure all required ports are open between the two machines (no firewall/iptables blocks):
- 2181 (ZooKeeper client port)
- 2888 (ZooKeeper peer communication)
- 3888 (ZooKeeper election port)
Test withnc -zv 10.0.229.48 2181ortelnet 10.0.215.22 2888from each machine.
- Check ZooKeeper status: Run
zkServer.sh statuson both machines. One should showLeader, the otherFollower. If either says "Error contacting service", start it withzkServer.sh startand check logs inzookeeper.out.
3. Check Kafka Broker Startup & Logs
Once ZooKeeper is healthy, confirm your Kafka brokers are registering correctly:
- Start order first: Always start both ZooKeeper nodes before starting Kafka brokers.
- Inspect Kafka logs: Check the
server.logfile in your Kafkalogsdirectory for red flags:Connection to node -1 could not be established: Means Kafka can’t reach ZooKeeper — double-check thezookeeper.connectstring and ZooKeeper status.Invalid broker.id: You have duplicate broker IDs (both set to 1) — fix machine 2’sbroker.idto 2.Address already in use: Port is occupied — uselsof -i :9094to find the process and kill it, or change the port.
- Verify broker registration: Use the ZooKeeper client to check if brokers are connected:
- Run
zkCli.sh -server 10.0.215.22:2181to connect - Execute
ls /brokers/ids— you should see[1,2]if both brokers are registered.
- Run
4. Test Cluster Functionality
Once all the above checks pass, validate end-to-end functionality:
- Create a test topic with replication across both brokers:
kafka-topics.sh --create --topic test_cluster --bootstrap-server 10.0.215.22:9094,10.0.229.48:9095 --partitions 2 --replication-factor 2 - Describe the topic to confirm partition distribution:
kafka-topics.sh --describe --topic test_cluster --bootstrap-server 10.0.215.22:9094 - Send test messages with a producer:
kafka-console-producer.sh --topic test_cluster --bootstrap-server 10.0.215.22:9094 - Receive messages from the other broker to confirm replication:
kafka-console-consumer.sh --topic test_cluster --bootstrap-server 10.0.229.48:9095 --from-beginning
If you hit errors at any step, share the exact log lines or error messages, and we can narrow it down further.
内容的提问来源于stack exchange,提问作者Abhishek Mittal

