Cruise Control: rebalancing

Kafka places a partition when it is created and never moves it, so a broker added to a busy cluster stays empty and a broker that holds replicas cannot be removed. Cruise Control computes a plan from the load, a KafkaRebalance holds it until it is approved, and autoRebalance lets the operator ask and approve when a node pool grows or shrinks.

60 minutesChapter 33Running on Kubernetes with Strimzi

A cluster that is running out of room gets another broker. In Kubernetes that is one number in a node pool. The pod starts, joins the cluster, passes its readiness probe and holds nothing. Every existing partition still lives where it was created, and the new broker takes only the partitions that are created after it arrives. The capacity was added and none of the load moved to it.

The reverse is worse. A broker that holds replicas cannot simply be deleted, because the replicas on it would go with it. Both directions need the same operation: choose which replicas go where, and move them while clients keep working.

A KafkaRebalance goes from PendingProposal to ProposalReady, where the optimization result is shown and nothing has moved, then on approval to Rebalancing and Ready; a refresh annotation computes a new plan, and an auto-approval annotation or the operator's autoRebalance replaces the manual approval
Figure 33.1: A proposal is a plan, and it waits for approval before anything moves

Learning objectives

  • Request a rebalance proposal before approving any data movement
  • Read numReplicaMovements and dataToMoveMB to judge the cost of a plan
  • Let the operator ask and approve a rebalance when a node pool scales
  • Explain why scaling down a broker holding replicas is refused by default

Why this matters

When a broker is added to a struggling cluster, it buys nothing until something decides where its share of the load should come from, so an unrebalanced scale-up pays for capacity nobody uses. The reverse costs more: when a broker that still holds replicas is deleted, its copies of the data go with it. A rebalance also moves data over the same disks and network the cluster uses to serve traffic, so an unreviewed plan can trade a capacity problem for a performance one at the worst time.

Pattern: Rebalance by proposal

The problem

Placement is decided once. Kafka assigns the replicas of a partition when the topic is created, and the cluster does not revisit the choice when brokers arrive or leave. A new broker stays empty until a plan moves replicas to it, and a plan written by hand has to know the size and traffic of every partition to be any better than a guess. Moving the replicas also copies data, so a plan that is applied without being read can saturate the disks and the network of a cluster that is serving traffic.

The solution

Ask Cruise Control for a proposal, read what it would move, approve it only when the plan is acceptable, and let the operator ask and approve for the two cases it can recognise, a node pool that grows and one that shrinks.

Cruise Control keeps a model of the cluster built from partition load, in the words of its documentation, “standard Kafka metrics” that its load monitor turns into per-partition resource use (Cruise Control wiki). A proposal is the smallest set of movements that brings that model within its goals.

How Strimzi and Cruise Control model it

Cruise Control is a section of the Kafka resource. The operator runs it as a Deployment next to the brokers and installs its metrics reporter in them.

spec:
  cruiseControl:
    config:
      metric.sampling.interval.ms: 60000
      partition.metrics.window.ms: 60000
      num.partition.metrics.windows: 3
      min.samples.per.partition.metrics.window: 1

The sampling keys set how soon Cruise Control has enough windows of samples to trust its model. The interval has a lower bound: ten seconds makes the Cruise Control pod crash with a ConfigException, because it must be longer than the metadata refresh timeout, and the Kafka resource then never becomes Ready; an interval of 60000 works. The cluster uses node pools, and the replicas being placed belong to KafkaTopic resources. The catalog topics here have two replicas each, on a pool of two brokers.

A KafkaRebalance asks for a plan. Its mode names the case, and the label binds it to a cluster.

apiVersion: kafka.strimzi.io/v1
kind: KafkaRebalance
metadata:
  name: add-broker-3
  labels:
    strimzi.io/cluster: edp
spec:
  mode: add-brokers
  brokers: [3]

Which id to write is not obvious. Node ids are shared by all pools of a cluster, and the controller of this cluster already holds one, so a pool that grows from two brokers to three gets the next free id, not the third number of a sequence. The id comes from status.nodeIds of the pool.

A proposal, then an approval

The operator passes the request to Cruise Control and writes the result into the resource: a condition of ProposalReady and an optimizationResult.

status:
  conditions:
    - type: ProposalReady
  optimizationResult:
    numReplicaMovements: 50
    dataToMoveMB: 182
    numLeaderMovements: 0
    onDemandBalancednessScoreBefore: 79.77
    onDemandBalancednessScoreAfter: 85.69

The numbers are what the proposal costs and buys: how many replicas it moves, how much data that copies, and how balanced the cluster is before and after. A proposal with a large dataToMoveMB is the signal to wait for a quiet hour. The replicas stay where they are at this point. The proposal is a status, and nothing is applied until the resource carries the approval.

kubectl -n edp-kafka annotate kafkarebalance add-broker-3 strimzi.io/rebalance=approve

The state moves to Rebalancing, then Ready, and the new broker holds replicas. The Strimzi documentation names the same states and the refresh annotation that discards a stale plan and computes a new one. Approval is a human decision by default. The annotation strimzi.io/rebalance-auto-approval: "true" on the resource removes it, and that is the choice a team makes when it trusts the goals enough to skip the review.

The state changes on the operator’s reconciliation, which by default runs every two minutes (Strimzi documentation), so a rebalance of a small cluster takes minutes from the approval to Ready.

Cruise Control restarts with the new broker’s capacity after a scale, so a KafkaRebalance applied before it is back goes NotReady; wait for its Deployment, then annotate the resource with refresh to get the plan. The load that fills the topics comes from a Job pod of its own, because a producer tool run with kubectl exec inside a broker container shares the broker’s 768 MiB limit.

Rebalancing when the pool grows

Nothing in the manual path is wrong, and each step is one the operator can take itself. The autoRebalance section of the Kafka resource names a template for each mode.

spec:
  cruiseControl:
    autoRebalance:
      - mode: add-brokers
        template:
          name: edp-add
      - mode: remove-brokers
        template:
          name: edp-remove

A template is a KafkaRebalance with the annotation strimzi.io/rebalance-template: "true" and, as the Strimzi blog on auto-rebalancing explains, its mode and brokers are ignored: the operator fills them in. The trigger is a change of spec.replicas of an existing node pool. Strimzi creates a resource named after the cluster and the mode, edp-auto-rebalancing-add-brokers, requests the proposal, approves it and applies it. The progress is in status.autoRebalance of the Kafka resource, which reports RebalanceOnScaleUp and then Idle, and the generated KafkaRebalance is deleted when it is done.

With autoRebalance present, the pool that grows is rebalanced at once, and no empty broker is left to propose against. The manual rebalance therefore runs on a pool that has no autoRebalance block, and the block is added afterwards. A pool that is created is not a scaling event: the documentation says auto-rebalancing is not triggered when a node pool is created or deleted.

The operator’s own rebalance runs on the same timer, so how long growing from three brokers to four takes depends on where the timer is: from the scale change to the operator reporting Idle it takes anywhere from under a minute to several minutes.

Replicas per broker in three stages: two brokers that hold every replica, a pool grown to three where the new broker holds none, and after the approved rebalance where the new broker holds a share; the operator's own rebalance does the same when the pool grows once more
Figure 33.2: A broker added to the pool starts empty; replicas arrive only through a rebalance

Shrinking the pool

A pool that gets smaller loses a broker. Strimzi checks first whether that broker still holds replicas. Without autoRebalance, the check fails: the broker’s pod keeps running, the pool’s status.nodeIds still lists it and the Kafka status carries a warning with the reason ScaleDownPreventionCheck. The pool’s spec.replicas keeps the smaller number you wrote, so the pool is set back to three by hand. The operator’s log names the broker and says it “has assigned partition-replicas”. The refusal is the safe outcome, and it is repeated on every reconciliation until someone drains the broker or turns the setting on.

With the remove-brokers template configured, the same scale-down is accepted. The operator creates edp-auto-rebalancing-remove-brokers, moves the replicas off the broker, and only then does the pod go (from seconds to a couple of minutes to a settled pool of three, again depending on the timer). Afterwards every partition has both of its replicas on the remaining brokers and none is out of sync.

Question Manual KafkaRebalance autoRebalance
Who asks for the proposal A person, with a resource The operator, from the template
Who approves A person, by annotation The operator
What triggers it Any time A change of replicas in an existing pool
Scale-down of a broker with replicas Refused Drained, then removed

Build, run, observe

cd examples/33-cruise-control
./verify.sh

A cluster of two brokers and Cruise Control is filled by a load Job, grown to three nodes and rebalanced by a proposal that is approved, then grown to four and shrunk back to three with autoRebalance; the run ends with 17/17 checks passed.

Try one change and rerun:

  • AUTO_REBALANCE=off ./verify.sh (demo code): the pool grows to four and the new broker holds no replica, because no resource asks for a plan.
  • kubectl --context edp -n edp-kafka annotate kafkarebalance <name> strimzi.io/rebalance=refresh while a proposal waits: Cruise Control computes a new plan from the current load.
  • Add strimzi.io/rebalance-auto-approval: "true" to the manual resource: it goes from ProposalReady to Rebalancing with no annotation from you.

When not to use it

A cluster that does not grow, shrink or skew has no use for it, and Cruise Control then costs a Deployment and the minutes it needs to build a model after each restart. Strimzi runs it only when the Kafka resource asks.

A rebalance copies data. On a loaded cluster the copy competes with producers and consumers for disks and network, so a large dataToMoveMB is a reason to read the proposal, not to approve it. Automatic approval trades that review for speed, and it is a fit for clusters where the goals are trusted and the movement is small.

Auto-rebalancing reacts to a changed replica count in an existing pool and to nothing else. A skewed cluster that did not scale needs a manual full rebalance.

Common pitfalls

  • Setting the metric sampling interval too low crashes the Cruise Control pod with a configuration error, because the interval must exceed the metadata refresh timeout.
  • Guessing a new broker’s node id as the next number in a sequence breaks, since node ids are shared across every pool and the real id comes only from the pool’s status.
  • Applying a KafkaRebalance right after a scale, before Cruise Control restarts with the new broker’s capacity, leaves it stuck NotReady until refreshed.
  • Scaling a pool down without a remove-brokers template leaves the smaller replica count refused at every reconciliation until someone drains the broker by hand.

🤖 Explore with your agent “In examples/33-cruise-control, read status.nodeIds of the broker KafkaNodePool, scale it by one with kubectl patch, and apply a KafkaRebalance with mode add-brokers for the new id. Compare kafka-topics.sh –describe before and after the approve annotation, and say what the optimizationResult predicted, which resource the operator creates instead when autoRebalance is configured, and why scaling down a broker that holds replicas is refused without it.” Check: the new broker holds no replica until the approve annotation is set, the replica counts then change as numReplicaMovements said, and the refused scale-down shows the reason ScaleDownPreventionCheck on the Kafka resource.

References