For an introduction to this topic, we recommend reading the following blog and/or watching this video:
Add Cruise Control to the cluster configuration:
oc apply -f k8s/rebalance/01-cruise-control-metrics.yaml
oc patch kafka my-cluster --patch '{"spec":{"cruiseControl": {"metricsConfig":{"type":"jmxPrometheusExporter","valueFrom":{"configMapKeyRef":{"key":"metrics-config.yml","name":"cruise-control-metrics"}}}}}}' --type=merge
To simulate an unbalanced workload, the following steps will configure the producer to write messages on the partitions which leader is hosted in the same Kafka server:
-
Locate the partition leaders
oc exec -it my-cluster-kafka-0 -- bin/kafka-topics.sh \ --bootstrap-server my-cluster-kafka-bootstrap:9092 \ --describe --topic event
-
Update the producer configmap to select all the partitions which have the leaders located in the same server:
oc edit configmap kafka-producer-config
-
Update the following variables matching the desired partitions, e.g.:
PRODUCER_PARTED: "true" PRODUCER_PARTITIONS: 0,3,6,9
-
Bump the Kafka producer application:
oc scale deployment/kafka-producer --replicas=0 oc scale deployment/kafka-producer --replicas=1
-
Check that partitions grows at different paces, running the following command:
oc exec -it my-cluster-kafka-0 -- bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic event
-
Deploy the rebalance configuration which will trigger the optimization analysis:
oc apply -f k8s/rebalance/02-kafka-rebalance-full.yaml
-
Review the optimization proposal:
oc describe kafkarebalance full-rebalance
-
Approve the proposal
oc annotate kafkarebalances.kafka.strimzi.io full-rebalance strimzi.io/rebalance=approve
TipIt’s possible to trigger a new analysis on the existing rebalancing configuration:
oc annotate kafkarebalances.kafka.strimzi.io full-rebalance strimzi.io/rebalance=refresh
-
Rebalancing takes some time, you can check the progress through the following command:
oc get kafkarebalance full-rebalance
NAME CLUSTER PENDINGPROPOSAL PROPOSALREADY REBALANCING READY NOTREADY full-rebalance my-cluster True
Rebalancing is complete when the
READY
column displaysTrue
.
Run again the describe topic command, you should spot the overloaded partitions moved on different leaders:
oc exec -it my-cluster-kafka-0 -- bin/kafka-topics.sh \
--bootstrap-server my-cluster-kafka-bootstrap:9092 \
--describe --topic event
When the Kafka cluster scales, existing topics do not leverage the newly available brokers, so they remain idle until new topics are created. The Cruise Control can be used to evenly distribute existing topics on the new available resources, as the following steps will show:
-
Increase the Kafka replicas:
oc patch kafka my-cluster --patch '{"spec":{"kafka": {"replicas": 4}}}' --type=merge
-
Change the producer configurations to create an evenly distributed workload:
oc edit configmap/kafka-producer-config
Modify the environment variables:
PRODUCER_PARTED: "false" PRODUCER_TICK_FREQUENCY: "10"
-
Restart the producer and consumer application
-
Open the Grafana dashboard, after a few minutes, the CPU graph should look like the following:
The new broker uses less resources.
-
Watching at the topic information confirms that all partitions are on the first 3 brokers (0,1,2)
oc exec -it my-cluster-kafka-0 -- bin/kafka-topics.sh \ --bootstrap-server my-cluster-kafka-bootstrap:9092 \ --describe --topic event
-
Deploy the rebalance configuration
mode: add-brokers
, which is tailored to leverage the new available brokers:oc apply -f k8s/rebalance/03-kafka-rebalance-add-brokers.yaml
-
Preparing an optimization proposal could take a couple of minutes, check the progress through the following command:
oc get kafkarebalance add-brokers-rebalance
NAME CLUSTER PENDINGPROPOSAL PROPOSALREADY REBALANCING READY NOTREADY add-brokers-rebalance my-cluster True
A proposal is available when the
PENDINGPROPOSAL
column displaysTrue
. -
Review and approve the optimization proposal:
oc describe kafkarebalance add-brokers-rebalance oc annotate kafkarebalances.kafka.strimzi.io add-brokers-rebalance strimzi.io/rebalance=approve
-
Rebalancing takes a couple of minutes, you can monitor the Grafana dashboard to see the changes and the topic information to understand how the partitions and their replicas are reorganized across the brokers.
-
Finally, you can ask the Cruise Control to shrink your partitions on less brokers and then scale the cluster down.
In order to start the demo from scratch, with minimal effort: delete only the kafka broker and the topics:
oc delete kafkatopics --selector="strimzi.io/cluster=my-cluster"
oc delete kafka my-cluster
Drop the PVC:
oc delete pvc --selector="strimzi.io/cluster=my-cluster"
Delete kafka rebalance:
oc delete kafkarebalance full-rebalance
oc delete kafkarebalance add-brokers-rebalance
oc delete kafkarebalance remove-brokers-rebalance
In order to repeat the rebalancing demo, you have to create again the cluster and the topic.