Skip to main content
Version: Preview

Scale for a Kafka cluster

You can scale a Kafka cluster in two ways, vertical scaling and horizontal scaling.

Vertical scaling

You can vertically scale a cluster by changing resource requirements and limits (e.g. CPU and storage). For example, you can change the resource class from 1C2G to 2C4G by performing vertical scaling.

Before you start

Check whether the cluster status is Running. Otherwise, the following operations may fail.

kubectl get cluster mycluster -n demo  

Steps

There are two ways to apply vertical scaling.

  1. Apply an OpsRequest to the specified cluster. Configure the parameters according to your needs.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-vertical-scaling
    namespace: demo
    spec:
    clusterRef: mycluster
    type: VerticalScaling
    verticalScaling:
    - componentName: broker
    requests:
    memory: "2Gi"
    cpu: "1"
    limits:
    memory: "4Gi"
    cpu: "2"
    EOF
  2. Check the operation status to validate the vertical scaling.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-vertical-scaling VerticalScaling mycluster Succeed 3/3 6m

    If an error occurs, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo
    >
    ......
    Component Specs:
    Component Def Ref: kafka
    Enabled Logs:
    running
    DisableExporter: true
    Name: kafka
    Replicas: 2
    Resources:
    Limits:
    Cpu: 2
    Memory: 4Gi
    Requests:
    Cpu: 1
    Memory: 2Gi

Horizontal scaling

Horizontal scaling changes the amount of pods. For example, you can scale out replicas from three to five.

From v0.9.0, besides replicas, KubeBlocks also supports scaling in and out instances, refer to Horizontal Scale for more details and examples.

Before you start

  • Check whether the cluster status is Running. Otherwise, the following operations may fail.

  • You are not recommended to perform horizontal scaling on the controller node, including the controller node both in combined mode and separated node.

  • When scaling horizontally, you must know the topic partition storage. If the topic has only one replication, data loss may caused when you scale in broker.

    kubectl get cluster mycluster -n demo  

Steps

There are two ways to apply horizontal scaling.

  1. Apply an OpsRequest to a specified cluster. Configure the parameters according to your needs.

    The example below means adding two replicas.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-horizontal-scaling
    namespace: demo
    spec:
    clusterRef: mycluster
    type: HorizontalScaling
    horizontalScaling:
    - componentName: broker
    scaleOut:
    replicaChanges: 2
    EOF

    If you want to scale in replicas, replace scaleOut with scaleIn.

    The example below means deleting two replicas.

    kubectl apply -f - <<EOF
    apiVersion: apps.kubeblocks.io/v1alpha1
    kind: OpsRequest
    metadata:
    name: ops-horizontal-scaling
    namespace: demo
    spec:
    clusterRef: mycluster
    type: HorizontalScaling
    horizontalScaling:
    - componentName: broker
    scaleIn:
    replicaChanges: 2
    EOF
  2. Check the operation status to validate the horizontal scaling.

    kubectl get ops -n demo
    >
    NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
    demo ops-horizontal-scaling HorizontalScaling mycluster Succeed 3/3 6m

    If an error occurs, you can troubleshoot with kubectl describe ops -n demo command to view the events of this operation.

  3. Check whether the corresponding resources change.

    kubectl describe cluster mycluster -n demo
    >
    ......
    Component Specs:
    Component Def Ref: kafka
    Enabled Logs:
    running
    DisableExporter: true
    Name: kafka
    Replicas: 2
    Resources:
    Limits:
    Cpu: 2
    Memory: 4Gi
    Requests:
    Cpu: 1
    Memory: 2Gi

Handle the snapshot exception

If STATUS=ConditionsError occurs during the horizontal scaling process, you can find the cause from cluster.status.condition.message for troubleshooting.

In the example below, a snapshot exception occurs.

Status:
conditions:
- lastTransitionTime: "2023-02-08T04:20:26Z"
message: VolumeSnapshot/mycluster-kafka-scaling-dbqgp: Failed to set default snapshot
class with error cannot find default snapshot class
reason: ApplyResourcesFailed
status: "False"
type: ApplyResources

Reason

This exception occurs because the VolumeSnapshotClass is not configured. This exception can be fixed after configuring VolumeSnapshotClass, but the horizontal scaling cannot continue to run. It is because the wrong backup (volumesnapshot is generated by backup) and volumesnapshot generated before still exist. Delete these two wrong resources and then KubeBlocks re-generates new resources.

Steps:

  1. Configure the VolumeSnapshotClass by running the command below.

    kubectl create -f - <<EOF
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotClass
    metadata:
    name: csi-aws-vsc
    annotations:
    snapshot.storage.kubernetes.io/is-default-class: "true"
    driver: ebs.csi.aws.com
    deletionPolicy: Delete
    EOF
  2. Delete the wrong backup (volumesnapshot is generated by backup) and volumesnapshot resources.

    kubectl delete backup -l app.kubernetes.io/instance=ivy85

    kubectl delete volumesnapshot -l app.kubernetes.io/instance=ivy85

Result

The horizontal scaling continues after backup and volumesnapshot are deleted and the cluster restores to running status.