Kafka Single-Node Deployment
| Server IP | Host Role |
|---|---|
| 192.168.10.7 | Kafka Broker and Controller |
Kafka Server
-
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 the 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/java -
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 the deployment server.https://pdpublic.mingdao.com/private-deployment/offline/common/kafka_2.13-4.3.1.tgz -
Extract 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 -
Configure Kafka
cat > /usr/local/kafka/config/server.properties <<'EOF'process.roles=broker,controllernode.id=1controller.quorum.bootstrap.servers=192.168.10.7:9093listeners=PLAINTEXT://0.0.0.0:9092,CONTROLLER://0.0.0.0:9093advertised.listeners=PLAINTEXT://192.168.10.7:9092listener.security.protocol.map=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXTinter.broker.listener.name=PLAINTEXTcontroller.listener.names=CONTROLLERlog.dirs=/data/kafka/kafka-logsnum.partitions=10offsets.topic.replication.factor=1transaction.state.log.replication.factor=1transaction.state.log.min.isr=1default.replication.factor=1min.insync.replicas=1log.retention.hours=168log.segment.bytes=1073741824log.retention.check.interval.ms=300000group.initial.rebalance.delay.ms=3000message.max.bytes=10485760replica.fetch.max.bytes=10485760EOFConfirm the node IP address and ports for your environment, and update the following properties as needed:
controller.quorum.bootstrap.servers: Enter the local IP address and Controller port9093in the formatLOCAL_IP:9093. Do not also configure the deprecatedcontroller.quorum.votersproperty.advertised.listeners: Enter the local IP address and Broker port9092that applications use to connect to Kafka.
-
Generate the cluster ID and format the storage directory
KAFKA_CLUSTER_ID=$(/usr/local/kafka/bin/kafka-storage.sh random-uuid)/usr/local/kafka/bin/kafka-storage.sh format --standalone -t "$KAFKA_CLUSTER_ID" -c /usr/local/kafka/config/server.properties- Run this command only when initializing an empty data directory for the first time. Do not format a node that already contains data.
-
Create the Kafka user and grant directory permissions
useradd -M -s /sbin/nologin kafkachown -R kafka:kafka /usr/local/kafka /data/kafka -
Configure systemd to manage Kafka
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.targetEOF -
Start Kafka and enable it at boot
systemctl daemon-reloadsystemctl enable kafkasystemctl start kafkasystemctl status kafka -
Verify message production and consumption
Create a test Topic
/usr/local/kafka/bin/kafka-topics.sh --bootstrap-server 127.0.0.1:9092 --create --if-not-exists --topic test01 --partitions 1 --replication-factor 1Start a producer
Run the following command in the current terminal. Enter a test message and press Enter to send it.
/usr/local/kafka/bin/kafka-console-producer.sh --bootstrap-server 127.0.0.1:9092 --topic test01Start a consumer
Open another terminal and run the following command. If the consumer displays the message sent by the producer, Kafka can produce and consume messages correctly.
/usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic test01 --from-beginning