Kafka Cluster Deployment
| Node | Server IP | Host Role | Node ID |
|---|---|---|---|
| Node01 | 192.168.10.7 | Kafka Broker and Controller | 1 |
| Node02 | 192.168.10.8 | Kafka Broker and Controller | 2 |
| Node03 | 192.168.10.9 | Kafka Broker and Controller | 3 |
Install Kafka
Execution servers: Node01, Node02, and Node03 (run on all three servers).
-
Download the JDK installation package
- Servers with Internet Access
- Servers without Internet Access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/OpenJDK21U-jdk_x64_linux_hotspot_21.0.8_9.tar.gz# JDK package download link. After downloading, upload it to each deployment server.https://pdpublic.mingdao.com/private-deployment/offline/common/OpenJDK21U-jdk_x64_linux_hotspot_21.0.8_9.tar.gz -
Extract the JDK and configure the Java symbolic link
tar -zxvf OpenJDK21U-jdk_x64_linux_hotspot_21.0.8_9.tar.gz -C /usr/localmv /usr/local/jdk-21.0.8+9 /usr/local/openjdk-21ln -s /usr/local/openjdk-21/bin/java /bin/javajava -version -
Download the Kafka installation package
- Servers with Internet Access
- Servers without Internet Access
wget https://pdpublic.mingdao.com/private-deployment/offline/common/kafka_2.13-4.3.1.tgz# Kafka package download link. After downloading, upload it to each deployment server.https://pdpublic.mingdao.com/private-deployment/offline/common/kafka_2.13-4.3.1.tgz -
Install Kafka and create the data directory
tar -zxvf kafka_2.13-4.3.1.tgz -C /usr/localmv /usr/local/kafka_2.13-4.3.1 /usr/local/kafkamkdir -p /data/kafka/kafka-logs -
Set the Kafka JVM heap size to 4 GB
sed -i ':a;N;$!ba;s/Xm[xs]1G/Xmx4G/1' /usr/local/kafka/bin/kafka-server-start.shsed -i ':a;N;$!ba;s/Xm[xs]1G/Xms4G/1' /usr/local/kafka/bin/kafka-server-start.sh -
Create the Kafka service account
useradd -M -s /sbin/nologin kafka
Configure Each Node
The following sections provide the server.properties configuration for Node01, Node02, and Node03. Before writing each file, confirm the node IP addresses and ports for your environment, and update the following properties as needed:
-
node.id: Set this property to1,2, and3on Node01, Node02, and Node03, respectively. Node IDs must be unique. -
controller.quorum.bootstrap.servers: Use the same value on all three nodes and include the IP address and port 9093 of all three Controller nodes. -
advertised.listeners: Enter the current node's IP address and Broker port9092. Applications use this address to connect to Kafka.
Node01
Execution server: Node01.
cat > /usr/local/kafka/config/server.properties <<'EOF'
process.roles=broker,controller
node.id=1
controller.quorum.bootstrap.servers=192.168.10.7:9093,192.168.10.8:9093,192.168.10.9:9093
listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
advertised.listeners=PLAINTEXT://192.168.10.7:9092
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
inter.broker.listener.name=PLAINTEXT
controller.listener.names=CONTROLLER
log.dirs=/data/kafka/kafka-logs
num.partitions=10
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=2
default.replication.factor=3
min.insync.replicas=2
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
group.initial.rebalance.delay.ms=3000
message.max.bytes=10485760
replica.fetch.max.bytes=10485760
EOF
Node02
Execution server: Node02.
cat > /usr/local/kafka/config/server.properties <<'EOF'
process.roles=broker,controller
node.id=2
controller.quorum.bootstrap.servers=192.168.10.7:9093,192.168.10.8:9093,192.168.10.9:9093
listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
advertised.listeners=PLAINTEXT://192.168.10.8:9092
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
inter.broker.listener.name=PLAINTEXT
controller.listener.names=CONTROLLER
log.dirs=/data/kafka/kafka-logs
num.partitions=10
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=2
default.replication.factor=3
min.insync.replicas=2
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
group.initial.rebalance.delay.ms=3000
message.max.bytes=10485760
replica.fetch.max.bytes=10485760
EOF
Node03
Execution server: Node03.
cat > /usr/local/kafka/config/server.properties <<'EOF'
process.roles=broker,controller
node.id=3
controller.quorum.bootstrap.servers=192.168.10.7:9093,192.168.10.8:9093,192.168.10.9:9093
listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093
advertised.listeners=PLAINTEXT://192.168.10.9:9092
listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
inter.broker.listener.name=PLAINTEXT
controller.listener.names=CONTROLLER
log.dirs=/data/kafka/kafka-logs
num.partitions=10
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=2
default.replication.factor=3
min.insync.replicas=2
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
group.initial.rebalance.delay.ms=3000
message.max.bytes=10485760
replica.fetch.max.bytes=10485760
EOF
Initialize and Start the Cluster
-
Generate and save the cluster ID and Controller directory IDs
Execution server: Node01.
Run the following commands on Node01 to generate one cluster ID and three Controller directory IDs. Record all four values and use the same values when formatting the three nodes.
KAFKA_CLUSTER_ID=$(/usr/local/kafka/bin/kafka-storage.sh random-uuid)CONTROLLER_1_UUID=$(/usr/local/kafka/bin/kafka-storage.sh random-uuid)CONTROLLER_2_UUID=$(/usr/local/kafka/bin/kafka-storage.sh random-uuid)CONTROLLER_3_UUID=$(/usr/local/kafka/bin/kafka-storage.sh random-uuid)echo "KAFKA_CLUSTER_ID=$KAFKA_CLUSTER_ID"echo "CONTROLLER_1_UUID=$CONTROLLER_1_UUID"echo "CONTROLLER_2_UUID=$CONTROLLER_2_UUID"echo "CONTROLLER_3_UUID=$CONTROLLER_3_UUID" -
Format the data directories on all three nodes
Execution servers: Node01, Node02, and Node03 (run on all three servers).
Run the following commands on each Kafka server. Replace the variables with the values recorded in the previous step. Also replace the IP addresses in
--initial-controllerswith the actual Controller IP addresses.KAFKA_CLUSTER_ID=REPLACE_WITH_ACTUAL_CLUSTER_IDCONTROLLER_1_UUID=REPLACE_WITH_ACTUAL_CONTROLLER_DIRECTORY_IDCONTROLLER_2_UUID=REPLACE_WITH_ACTUAL_CONTROLLER_DIRECTORY_IDCONTROLLER_3_UUID=REPLACE_WITH_ACTUAL_CONTROLLER_DIRECTORY_ID/usr/local/kafka/bin/kafka-storage.sh format \--cluster-id "$KAFKA_CLUSTER_ID" \--initial-controllers "1@192.168.10.7:9093:${CONTROLLER_1_UUID},2@192.168.10.8:9093:${CONTROLLER_2_UUID},3@192.168.10.9:9093:${CONTROLLER_3_UUID}" \--config /usr/local/kafka/config/server.properties- Use this operation only when initializing empty data directories for the first time. Never reformat a node that contains data or has already joined the cluster. Doing so removes its local metadata and prevents it from rejoining the cluster correctly.
-
Grant permissions to the Kafka directories
Execution servers: Node01, Node02, and Node03 (run on all three servers).
After formatting the storage directories, reset the directory ownership on all three servers so that the Kafka service can read and write the program and data directories.
chown -R kafka:kafka /usr/local/kafka /data/kafka -
Configure the systemd service
Execution servers: Node01, Node02, and Node03 (run on all three servers).
cat > /etc/systemd/system/kafka.service <<'EOF'[Unit]Description=Kafka KRaftAfter=network.target[Service]User=kafkaGroup=kafkaLimitNOFILE=102400LimitNPROC=102400ExecStart=/usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.propertiesExecStop=/usr/bin/kill $MAINPIDRestart=on-failure[Install]WantedBy=multi-user.targetEOFsystemctl daemon-reload -
Start the Kafka cluster
Execution servers: Node01, Node02, and Node03 (run on all three servers).
systemctl enable kafkasystemctl start kafkasystemctl status kafka
Verify the Cluster
-
Verify the Controller quorum status
Run the following command on any node. Confirm that a Controller Leader has been elected and that all three nodes are members of the quorum.
/usr/local/kafka/bin/kafka-metadata-quorum.sh --bootstrap-server 127.0.0.1:9092 describe --status -
Create and inspect a three-replica Topic
Run the following commands on any node.
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --create --if-not-exists --topic test01 --partitions 3 --replication-factor 3/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --describe --topic test01Confirm that each partition has three replicas and that the
Isrlist contains all three node IDs. -
Start a producer on Node01
/usr/local/kafka/bin/kafka-console-producer.sh --bootstrap-server 127.0.0.1:9092 --topic test01 -
Start a consumer on Node02 or Node03
/usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic test01 --from-beginningIf the consumer can receive the messages sent by the producer, the Kafka cluster message flow is working correctly.