$ run as a normal user# run as root (or with sudo)
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.
| Item | Value |
|---|---|
| Download | kafka_2.13-<version>.tgz from kafka.apache.org/downloads |
| Needs | Java 17 or newer |
| Folder | for example /opt/kafka |
| Config | /opt/kafka/config/server.properties |
| Data (logs of the topics) | log.dirs, for example /var/lib/kafka |
| Ports | 9092 (clients), 9093 (controller) |
| Tools | bin/kafka-topics.sh, kafka-console-producer.sh, kafka-console-consumer.sh, kafka-consumer-groups.sh, kafka-storage.sh |
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/.)
First configuration: a single KRaft server
Change these lines in server.properties (the file already has KRaft settings for a combined broker + controller):
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| Setting | Why |
|---|---|
advertised.listeners | The address clients use. It must be a name/IP they can reach — the most common mistake. |
log.dirs | Where the data lives — a big, fast disk, not /tmp. |
log.retention.hours=168 | Keep events 7 days, then delete. |
auto.create.topics.enable=false | Topics only made on purpose (no topics from typos). |
| Replication factors | 1 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:
[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.targetsystemctl 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 --reloadTopics, 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=259200000LAG = 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.
Make it safe
Security checklist:
PLAINTEXTonly 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 withacks=allfor important data. - Retention by time and size, and disk monitoring.
- Keep Kafka and Java updated; upgrade one version at a time.
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):
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
Clients connect, then fail with Connection to node 1 (localhost/127.0.0.1:9092) could not be established | advertised.listeners says localhost. | Set it to a name/IP the clients can reach; restart. |
No readable meta.properties files found at start | Storage not formatted. | Run kafka-storage.sh format once (see above). |
UnsupportedClassVersionError | Java too old. | Install Java 17+. |
NOT_ENOUGH_REPLICAS on produce | Fewer in-sync replicas than min.insync.replicas. | Bring brokers back; check replication factor vs. settings. |
| Disk full | Retention too long or a huge topic. | Lower retention.ms/retention.bytes; add disk. |
UNKNOWN_TOPIC_OR_PARTITION | The topic does not exist (auto-create off). | Create it with kafka-topics.sh --create. |
| Consumer group lag grows | Consumers too slow, crashing, or fewer consumers than needed. | More consumers (up to the number of partitions); check their logs. |
Next steps
- Quick how-tos: Create and test a topic.
- Command page: kafka-topics.
- At work: Event streaming. The queue-based broker: RabbitMQ.
- Watch it: Prometheus + Grafana (JMX exporter for Kafka metrics).
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.