متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 46Apache Kafka · Data

Install and set up Apache Kafka (KRaft mode)

Apache Kafka is an event streaming platform: apps write events (orders, clicks, logs, changes) to topics, Kafka keeps them on disk for days, and many consumers read them at their own speed — even again from the start. It is the backbone of data pipelines and event-driven systems. This guide installs Kafka 4 in KRaft mode (no ZooKeeper any more) on RHEL and Ubuntu, runs it as a systemd service, makes topics, sends and reads messages, checks consumer lag, and covers retention, security and fixes.

  • 8 الدروس
  • 15 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

46.1

Know it before you install

A topic is split into partitions (for parallel work); each partition is an ordered log on disk. Producers append; consumers in a consumer group share the partitions and remember their offset. Brokers store the data; controllers (KRaft) manage the cluster — one server can be both.

ItemValue
Downloadkafka_2.13-<version>.tgz from kafka.apache.org/downloads
NeedsJava 17 or newer
Folderfor example /opt/kafka
Config/opt/kafka/config/server.properties
Data (logs of the topics)log.dirs, for example /var/lib/kafka
Ports9092 (clients), 9093 (controller)
Toolsbin/kafka-topics.sh, kafka-console-producer.sh, kafka-console-consumer.sh, kafka-consumer-groups.sh, kafka-storage.sh

46.2

Install Java and Kafka (RHEL and Ubuntu)

dnf -y install java-21-openjdk-headless
apt-get install -y openjdk-21-jre-headless
useradd --system --home-dir /opt/kafka --shell /sbin/nologin kafka
KVER=4.1.0
curl -fsSLO https://downloads.apache.org/kafka/$KVER/kafka_2.13-$KVER.tgz
curl -fsSLO https://downloads.apache.org/kafka/$KVER/kafka_2.13-$KVER.tgz.sha512
sha512sum kafka_2.13-$KVER.tgz
cat kafka_2.13-$KVER.tgz.sha512
tar -xzf kafka_2.13-$KVER.tgz -C /opt
ln -sfn /opt/kafka_2.13-$KVER /opt/kafka
mkdir -p /var/lib/kafka
chown -R kafka: /opt/kafka_2.13-$KVER /var/lib/kafka

(Use the first install line on RHEL, the second on Ubuntu. Compare the two checksums. Older versions are on archive.apache.org/dist/kafka/.)

46.3

First configuration: a single KRaft server

Change these lines in server.properties (the file already has KRaft settings for a combined broker + controller):

/opt/kafka/config/server.properties
process.roles=broker,controller
node.id=1
controller.quorum.bootstrap.servers=kafka1.example.com:9093
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
advertised.listeners=PLAINTEXT://kafka1.example.com:9092
controller.listener.names=CONTROLLER
log.dirs=/var/lib/kafka
num.partitions=3
default.replication.factor=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
min.insync.replicas=1
log.retention.hours=168
log.retention.check.interval.ms=300000
auto.create.topics.enable=false
SettingWhy
advertised.listenersThe address clients use. It must be a name/IP they can reach — the most common mistake.
log.dirsWhere the data lives — a big, fast disk, not /tmp.
log.retention.hours=168Keep events 7 days, then delete.
auto.create.topics.enable=falseTopics only made on purpose (no topics from typos).
Replication factors1 on a single server; 3 with min.insync.replicas=2 in a 3-node cluster.

Format the storage once with a new cluster ID:

su -s /bin/bash kafka -c '/opt/kafka/bin/kafka-storage.sh format --standalone -t "$(/opt/kafka/bin/kafka-storage.sh random-uuid)" -c /opt/kafka/config/server.properties'
Formatting metadata directory /var/lib/kafka with metadata.version 4.1-IV1.

A systemd service:

/etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka (KRaft)
After=network-online.target
 
[Service]
User=kafka
Environment="KAFKA_HEAP_OPTS=-Xms2g -Xmx2g"
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=on-failure
LimitNOFILE=100000
 
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now kafka
journalctl -u kafka -f
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.1.0/24" port port="9092" protocol="tcp" accept'
firewall-cmd --reload

46.4

Topics, producers and consumers

cd /opt/kafka
bin/kafka-topics.sh --bootstrap-server kafka1.example.com:9092 --create --topic orders --partitions 6 --replication-factor 1
bin/kafka-topics.sh --bootstrap-server kafka1.example.com:9092 --list
bin/kafka-topics.sh --bootstrap-server kafka1.example.com:9092 --describe --topic orders
bin/kafka-console-producer.sh --bootstrap-server kafka1.example.com:9092 --topic orders
> {"id": 1, "total": 49.90}
bin/kafka-console-consumer.sh --bootstrap-server kafka1.example.com:9092 --topic orders --from-beginning --group test
bin/kafka-consumer-groups.sh --bootstrap-server kafka1.example.com:9092 --describe --group billing
GROUP    TOPIC   PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG
billing  orders  0          1520            1544            24
bin/kafka-configs.sh --bootstrap-server kafka1.example.com:9092 --alter --entity-type topics --entity-name orders --add-config retention.ms=259200000

LAG = how many events a consumer group is behind. Growing lag = consumers too slow or down.

Real uses at work:

  • Event-driven microservices (order created → billing, shipping, email).
  • Data pipelines: database changes (CDC with Debezium), logs and metrics into data lakes and Elasticsearch.
  • Buffer between fast producers and slow systems.

46.5

Make it safe

Security checklist:

  • PLAINTEXT only inside a trusted network for labs; in production use SASL_SSL (TLS + SCRAM or mTLS) for clients and between brokers.
  • ACLs (kafka-acls.sh) so each app may read/write only its topics.
  • Firewall 9092/9093 to app and broker networks only.
  • 3+ brokers, replication factor 3, min.insync.replicas=2, producers with acks=all for important data.
  • Retention by time and size, and disk monitoring.
  • Keep Kafka and Java updated; upgrade one version at a time.

46.6

Update and remove

Unpack the new version next to the old one, copy server.properties, switch the /opt/kafka link, restart (one broker at a time in a cluster):

tar -xzf kafka_2.13-4.1.1.tgz -C /opt
cp /opt/kafka/config/server.properties /opt/kafka_2.13-4.1.1/config/
chown -R kafka: /opt/kafka_2.13-4.1.1
ln -sfn /opt/kafka_2.13-4.1.1 /opt/kafka
systemctl restart kafka

46.7

Fix common problems

ProblemCauseFix
Clients connect, then fail with Connection to node 1 (localhost/127.0.0.1:9092) could not be establishedadvertised.listeners says localhost.Set it to a name/IP the clients can reach; restart.
No readable meta.properties files found at startStorage not formatted.Run kafka-storage.sh format once (see above).
UnsupportedClassVersionErrorJava too old.Install Java 17+.
NOT_ENOUGH_REPLICAS on produceFewer in-sync replicas than min.insync.replicas.Bring brokers back; check replication factor vs. settings.
Disk fullRetention too long or a huge topic.Lower retention.ms/retention.bytes; add disk.
UNKNOWN_TOPIC_OR_PARTITIONThe topic does not exist (auto-create off).Create it with kafka-topics.sh --create.
Consumer group lag growsConsumers too slow, crashing, or fewer consumers than needed.More consumers (up to the number of partitions); check their logs.

46.8

Next steps

1Clients reach the broker on 9092 but then try to connect to localhost. What is wrong?

advertised.listeners points to localhost; it must be an address the clients can reach.

2What does consumer group LAG show?

How many events the group has not read yet; growing lag means the consumers are too slow or down.

كل الأوامر في هذا الفصل