This guide explains how to perform horizontal scaling (scale-out and scale-in) on a Kafka cluster managed by KubeBlocks. You'll learn how to use both OpsRequest and direct Cluster API updates to achieve this.
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
KubeBlocks uses a declarative approach for managing Kafka Clusters. Below is an example configuration for deploying a Kafka Cluster with 3 components
Apply the following YAML configuration to deploy the cluster:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: kafka-separated-cluster
namespace: demo
spec:
terminationPolicy: Delete
clusterDef: kafka
topology: separated_monitor
componentSpecs:
- name: kafka-broker
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
env:
- name: KB_KAFKA_BROKER_HEAP
value: "-XshowSettings:vm -XX:MaxRAMPercentage=100 -Ddepth=64"
- name: KB_KAFKA_CONTROLLER_HEAP
value: "-XshowSettings:vm -XX:MaxRAMPercentage=100 -Ddepth=64"
- name: KB_BROKER_DIRECT_POD_ACCESS
value: "true"
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: metadata
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
- name: kafka-controller
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
volumeClaimTemplates:
- name: metadata
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
- name: kafka-exporter
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "1Gi"
requests:
cpu: "0.1"
memory: "0.2Gi"
These three components will be created strictly in controller->broker->exporter
order as defined in ClusterDefinition
.
Monitor the cluster status until it transitions to the Running state:
kubectl get cluster kafka-separated-cluster -n demo -w
Expected Output:
kubectl get cluster kafka-separated-cluster -n demo
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
kafka-separated-cluster kafka Delete Creating 13s
kafka-separated-cluster kafka Delete Running 63s
Check the pod status and roles:
kubectl get pods -l app.kubernetes.io/instance=kafka-separated-cluster -n demo
Expected Output:
NAME READY STATUS RESTARTS AGE
kafka-separated-cluster-kafka-broker-0 2/2 Running 0 13m
kafka-separated-cluster-kafka-controller-0 2/2 Running 0 13m
kafka-separated-cluster-kafka-exporter-0 1/1 Running 0 12m
Once the cluster status becomes Running, your Kafka cluster is ready for use.
If you are creating the cluster for the very first time, it may take some time to pull images before running.
Expected Workflow:
Pending
to Running
.Updating
to Running
Option 1: Using Horizontal Scaling OpsRequest
Scale out the Kafka cluster by adding 1 replica to kafka component:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: kafka-separated-cluster-scale-out-ops
namespace: demo
spec:
clusterName: kafka-separated-cluster
type: HorizontalScaling
horizontalScaling:
- componentName: kafka-broker
# Specifies the replica changes for scaling in components
scaleOut:
# Specifies the replica changes for the component.
# add one more replica to current component
replicaChanges: 1
Monitor the progress of the scaling operation:
kubectl get ops kafka-separated-cluster-scale-out-ops -n demo -w
Expected Result:
NAME TYPE CLUSTER STATUS PROGRESS AGE
kafka-separated-cluster-scale-out-ops HorizontalScaling kafka-separated-cluster Running 0/1 9s
kafka-separated-cluster-scale-out-ops HorizontalScaling kafka-separated-cluster Running 1/1 16s
kafka-separated-cluster-scale-out-ops HorizontalScaling kafka-separated-cluster Succeed 1/1 16s
Option 2: Direct Cluster API Update
Alternatively, you can perform a direct update to the replicas
field in the Cluster resource:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
componentSpecs:
- name: kafka-broker
replicas: 2 # increase replicas to scale-out
...
Or you can patch the cluster CR with command:
kubectl patch cluster kafka-separated-cluster -n demo --type=json -p='[{"op": "replace", "path": "/spec/componentSpecs/1/replicas", "value": 2}]'
After applying the operation, you will see a new pod created and the Kafka cluster status goes from Updating
to Running
, and the newly created pod has a new role secondary
.
kubectl get pods -n demo -l app.kubernetes.io/instance=kafka-separated-cluster,apps.kubeblocks.io/component-name=kafka-broker
Example Output:
NAME READY STATUS RESTARTS AGE
kafka-separated-cluster-kafka-broker-0 2/2 Running 0 3m7s
kafka-separated-cluster-kafka-broker-1 2/2 Running 0 28s
Expected Workflow:
Updating
to Running
Option 1: Using Horizontal Scaling OpsRequest
Scale in the Kafka cluster by removing ONE replica:
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: kafka-separated-cluster-scale-in-ops
namespace: demo
spec:
clusterName: kafka-separated-cluster
type: HorizontalScaling
horizontalScaling:
- componentName: kafka-broker
# Specifies the replica changes for scaling in components
scaleIn:
# Specifies the replica changes for the component.
# remove one replica from current component
replicaChanges: 1
Monitor progress:
kubectl get ops kafka-separated-cluster-scale-in-ops -n demo -w
Expected Result:
NAME TYPE CLUSTER STATUS PROGRESS AGE
kafka-separated-cluster-scale-in-ops HorizontalScaling kafka-separated-cluster Running 0/1 8s
kafka-separated-cluster-scale-in-ops HorizontalScaling kafka-separated-cluster Running 1/1 24s
kafka-separated-cluster-scale-in-ops HorizontalScaling kafka-separated-cluster Succeed 1/1 24s
Option 2: Direct Cluster API Update
Alternatively, you can perform a direct update to the replicas
field in the Cluster resource:
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
spec:
componentSpecs:
- name: kafka-broker
replicas: 1 # decrease replicas to scale-in
Or you can patch the cluster CR with command:
kubectl patch cluster kafka-separated-cluster -n demo --type=json -p='[{"op": "replace", "path": "/spec/componentSpecs/1/replicas", "value": 1}]'
Example Output (ONE Pod):
kubectl get pods -n demo -l app.kubernetes.io/instance=kafka-separated-cluster,apps.kubeblocks.io/component-name=kafka-broker
NAME READY STATUS RESTARTS AGE
kafka-separated-cluster-kafka-broker-0 2/2 Running 0 5m7s
When performing horizontal scaling:
To remove all created resources, delete the Kafka cluster along with its namespace:
kubectl delete cluster kafka-separated-cluster -n demo
kubectl delete ns demo
In this guide you learned how to:
KubeBlocks ensures seamless scaling with minimal disruption to your database operations.