First steps
After going through the Installation section and having installed all the operators, you now deploy a Kafka cluster in KRaft mode and declare a topic. Afterward you can verify that it works by producing test data into the topic and consuming it.
Setup
Kafka in KRaft mode manages its metadata itself, so no ZooKeeper is needed. The cluster consists of two roles:
-
The
controllersstore the cluster metadata and take part in the metadata quorum. -
The
brokersserve the clients.
Kafka
Create a file named kafka.yaml with the following contents:
---
apiVersion: kafka.stackable.tech/v1alpha1
kind: KafkaCluster
metadata:
name: simple-kafka
spec:
image:
productVersion: 4.3.1
clusterConfig:
metadataManager: KRaft
managementAccess: # Deploys the agent, which creates and updates KafkaTopics in this cluster
enabled: true
authentication:
userProvided:
anonymous: {}
controllers:
roleGroups:
default:
replicas: 1
brokers:
config:
bootstrapListenerClass: external-unstable # This exposes your Stacklet outside of Kubernetes. Remove this property if this is not desired
brokerListenerClass: external-unstable # This exposes your Stacklet outside of Kubernetes. Remove this property if this is not desired
roleGroups:
default:
replicas: 3
and apply it:
kubectl apply --server-side -f kafka.yaml
This creates the actual Kafka instance. The cluster has a single controller to keep the resource requirements low. For production, use three or more (odd number of) controllers.
The managementAccess section makes the operator deploy an agent next to the cluster.
The agent manages resources inside Kafka, such as topics, so that you can declare them in Kubernetes.
Here, the agent connects without a client certificate, which is possible because the cluster does not require client authentication.
Learn more in Management access.
Wait until the controllers, the brokers and the agent are ready:
kubectl rollout status --watch --timeout=5m statefulset/simple-kafka-controller-default
kubectl rollout status --watch --timeout=5m statefulset/simple-kafka-broker-default
kubectl rollout status --watch --timeout=5m deployment/simple-kafka-agent
Topic
Instead of running the Kafka command line tools, you declare the topic as a KafkaTopic resource.
The agent creates the topic in Kafka and keeps it in the declared state.
Create a file named kafka-topic.yaml with the following contents:
---
apiVersion: kafka.stackable.tech/v1alpha1
kind: KafkaTopic
metadata:
name: test-data-topic
spec:
clusterRef:
name: simple-kafka
partitions: 1
replicationFactor: 3
and apply it:
kubectl apply -f kafka-topic.yaml
The topic is named after the resource, test-data-topic, and refers to the Kafka cluster with clusterRef.
Wait until the agent reports the topic as ready:
kubectl wait --for=condition=Ready --timeout=5m kafkatopics.kafka.stackable.tech/test-data-topic
Other operators, such as Strimzi, also provide a resource of the kind KafkaTopic, so the commands use the full resource name.
|
Show the topic as the agent sees it:
kubectl get kafkatopics.kafka.stackable.tech test-data-topic
The output should look somewhat like this:
NAME TOPIC CLUSTER PARTITIONS REPLICATION FACTOR READY SYNCED REASON AGE test-data-topic test-data-topic simple-kafka 1 3 True True Synced 30s
Learn more about topics in Topics.
Verify that it works
Next, use the Kafka client scripts to publish and consume data on the topic created by the agent.
The Kafka operator has created a service called simple-kafka-broker-default-bootstrap.
This service represents the endpoint clients should initially connect to in order to publish and consume data.
First, make sure that the service exists and it is healthy:
kubectl describe svc simple-kafka-broker-default-bootstrap
The output should look somewhat like this:
Name: simple-kafka-broker-default-bootstrap
Namespace: default
Labels: app.kubernetes.io/component=broker
app.kubernetes.io/instance=simple-kafka-broker-default-bootstrap
app.kubernetes.io/managed-by=listeners.stackable.tech_listener
app.kubernetes.io/name=listener
app.kubernetes.io/role-group=default
app.kubernetes.io/version=4.3.1-stackable0.0.0-dev
stackable.tech/vendor=Stackable
Annotations: <none>
Selector: listener.stackable.tech/mnt.9555cbb6f38d4b0ca1771e6d83d28e27=simple-kafka-broker-default-bootstrap
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.105.88.52
IPs: 10.105.88.52
Port: kafka-tls 9093/TCP
TargetPort: 9093/TCP
NodePort: kafka-tls 32608/TCP
Endpoints: 10.244.4.22:9093,10.244.4.24:9093,10.244.4.23:9093
Session Affinity: None
External Traffic Policy: Local
Internal Traffic Policy: Cluster
Events: <none>
The output shows that there are three endpoints serviced here. They correspond to the three broker pods belonging to the Kafka cluster.
Use the Kafka performance producer script to send a couple of messages to the topic previously created. The brokers use TLS, so the command uses the client configuration that the operator provides in the broker Pod and connects to the address of the broker listener:
kubectl exec -n default simple-kafka-broker-default-0 -c kafka -- bash -c '
bootstrap="$(cat /stackable/listener-broker/default-address/address):$(cat /stackable/listener-broker/default-address/ports/kafka-tls)"
/stackable/kafka/bin/kafka-producer-perf-test.sh \
--producer-props bootstrap.servers="$bootstrap" \
--producer.config /stackable/config/client.properties \
--topic test-data-topic \
--payload-monotonic \
--throughput 1 \
--num-records 5
'
The output should contain the following line:
5 records sent, 1.138434 records/sec (0.00 MB/sec), 83.40 ms avg latency, 395.00 ms max latency, 3 ms 50th, 395 ms 95th, 395 ms 99th, 395 ms 99.9th.
This confirms that there were five messages sent to the topic and it also displays performance timers. We are not interested in any performance indicators but appreciate the fact that there were five unique messages that we consume later.
Now let’s consume the messages from above:
kubectl exec -n default simple-kafka-broker-default-0 -c kafka -- bash -c '
bootstrap="$(cat /stackable/listener-broker/default-address/address):$(cat /stackable/listener-broker/default-address/ports/kafka-tls)"
/stackable/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server "$bootstrap" \
--consumer.config /stackable/config/client.properties \
--topic test-data-topic \
--offset earliest \
--partition 0 \
--max-messages 5 \
--timeout-ms 30000
'
The consumer should print the messages in between logging statements
0 1 2 3 4
You successfully created a Kafka cluster in KRaft mode, declared a topic with a KafkaTopic, and produced and consumed data.
What’s next
Have a look at the Usage guide page to find out more about the features of the Kafka Operator, for example declarative topics and security.