How to work with kcat and KafScale
kcat
kcat (formerly kafkacat) is a small command line Kafka producer, consumer and metadata browser built on librdkafka. It starts fast and needs no JVM. It reads from stdin and writes to stdout, so it fits shell scripts, CI checks and quick debugging. See the kcat install notes, or run the edenhill/kcat container image.
Before you start
- A KafScale v2.2.0 cluster with the proxy enabled. Clients connect to the proxy at
proxy.advertisedHostandproxy.advertisedPortfrom your Helm values. - kcat 1.7 or later. This guide was tested with kcat 1.7.1 (librdkafka 2.8.0) against KafScale v2.2.0 with 2 brokers and 2 proxy replicas.
export BOOTSTRAP=kafka.example.com:9092
kcat does not create topics. Create one first, for example with the Kafka CLI.
Try it yourself
1. Read the metadata
kcat -b "$BOOTSTRAP" -L -t orders
Metadata for orders (from broker 0: kafka.example.com:9092/0):
1 brokers:
broker 0 at kafka.example.com:9092 (controller)
1 topics:
topic "orders" with 4 partitions:
partition 0, leader 0, replicas: 0, isrs: 0
partition 1, leader 0, replicas: 0, isrs: 0
partition 2, leader 0, replicas: 0, isrs: 0
partition 3, leader 0, replicas: 0, isrs: 0
The cluster shows as one broker, node 0, at the proxy address, with one replica per partition. That is expected. See how tools see a KafScale cluster. Add -J for JSON output.
2. Produce
Keyed records, one key:value per line, to partition 1:
printf 'k1:hello\nk2:world\n' | kcat -P -b "$BOOTSTRAP" -t orders -K: -p 1
One record with a key and a header:
echo 'v-with-header' | kcat -P -b "$BOOTSTRAP" -t orders -p 1 -k k3 -H source=kcat
Leave out -p to let the partitioner pick by key. Idempotent producing works. Add -X enable.idempotence=true.
3. Consume
Read everything and exit at the end, with partition, offset, key, value and headers:
kcat -C -b "$BOOTSTRAP" -t orders -o beginning -e -q -f 'p%p o%o k=%k v=%s h=%h\n'
p0 o0 k=a v=1 h=
p0 o1 k=b v=2 h=
p1 o0 k=k1 v=hello h=
p1 o1 k=k2 v=world h=
p2 o0 k=c v=3 h=
p1 o2 k=k3 v=v-with-header h=source=kcat
Records a, b and c come from the Kafka CLI guide. Records are in order within a partition, not across partitions. -q hides the “Reached end of topic” lines.
The last two records of partition 1:
kcat -C -b "$BOOTSTRAP" -t orders -p 1 -o -2 -e -q
world
v-with-header
Consume as a consumer group, with offsets committed to KafScale:
kcat -b "$BOOTSTRAP" -G orders-kcat orders -o beginning -e -q -f '%k=%s\n'
k1=hello
k2=world
c=3
a=1
b=2
k3=v-with-header
Check the group afterwards with kafka-consumer-groups.sh --describe.
4. Look up an offset by time
Print the first offset at or after a time. The time is in milliseconds since the epoch:
kcat -Q -b "$BOOTSTRAP" -t orders:1:"$(( $(date +%s) - 3600 ))000"
orders [1] offset 0
To start reading at a time, use -o s@<timestamp> with the same value. If no record is at or after that time, kcat prints offset -1.
Connect with mTLS
When the proxy has mTLS enabled, give kcat the CA and a client certificate in a config file. SASL is not supported. Keep the file and the key readable only by you, and do not pass key passwords with -X on the command line:
umask 077
cat > kcat.conf <<'CONF'
bootstrap.servers=kafka.example.com:9092
security.protocol=ssl
ssl.ca.location=/path/to/ca.crt
ssl.certificate.location=/path/to/client.crt
ssl.key.location=/path/to/client.key
CONF
chmod 600 kcat.conf
If the key is encrypted, add ssl.key.password to the file with an editor. Then use -F instead of -b:
kcat -F kcat.conf -L -t secure-events
echo 'from-kcat' | kcat -F kcat.conf -P -t secure-events -k k9
kcat -F kcat.conf -C -t secure-events -o beginning -e -q -f '%p %o %k %s\n'
Without a client certificate signed by a CA the proxy trusts, the connection fails with Failed to acquire metadata: Local: Broker transport failure.