kafka
by apecloud
Apache Kafka is a distributed streaming platform designed to build real-time pipelines and can be used as a message broker or as a replacement for a log aggregation solution for big data applications.
About this chart
Kafka
Apache Kafka is a distributed streaming platform designed to build real-time pipelines and can be used as a message broker or as a replacement for a log aggregation solution for big data applications.
- A broker is a Kafka server that stores data and handles requests from producers and consumers. Kafka clusters consist of multiple brokers, each identified by a unique ID. Brokers work together to distribute and replicate data across the cluster.
- KRaft was introduced in Kafka 3.3.1 in October 2022 as an alternative to Zookeeper. A subset of brokers are designated as controllers, and these controllers provide the consensus services that used to be provided by Zookeeper.
Features In KubeBlocks
Lifecycle Management
| Topology | Horizontalscaling | Vertical scaling | Expandvolume | Restart | Stop/Start | Configure | Expose | Switchover |
|---|---|---|---|---|---|---|---|---|
| Combined/Separated | Yes | Yes | Yes | Yes | Yes | Yes | Yes | N/A |
- Combine Mode: KRaft (Controller) and Broker components are combined in the same pod.
- Separated Mode: KRaft (Controller) and Broker components are deployed in different pods.
Backup and Restore
| Feature | Method | Description |
|---|---|---|
| N/A | N/A | N/A |
Versions
| Versions |
|---|
| 3.3.2 |
| 2.8.2 |
Prerequisites
-
Kubernetes cluster >= v1.21
-
kubectlinstalled, refer to K8s Install Tools -
Helm, refer to Installing Helm
-
KubeBlocks installed and running, refer to Install Kubeblocks
-
Kafka Addon Enabled, refer to Install Addons
-
Create K8s Namespace
demo, to keep resources created in this tutorial isolated:kubectl create ns demo
Examples
Create
Create a Kafka cluster with combined controller and broker components.
For combined_monitor, 2Gi memory is the recommended minimum for the
kafka-combine component when broker, controller, exporter validation, and
in-pod workload tools share the same pod memory budget; smaller limits are not
recommended for this topology.
# cat examples/kafka/cluster-combined.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: kafka-combined-cluster
namespace: demo
spec:
# Specifies the behavior when a Cluster is deleted.
# Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
# - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
# - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
# - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
terminationPolicy: Delete
# Specifies the name of the ClusterDefinition to use when creating a Cluster.
# Note: DO NOT UPDATE THIS FIELD
# The value must be `kafaka` to create a Kafka Cluster
clusterDef: kafka
# Specifies the name of the ClusterTopology to be used when creating the
# Cluster.
# - combined: combined Kafka controller (KRaft) and broker in one Component
# - combined_monitor: combined mode with monitor component
# - separated: separated KRaft and Broker Components.
# - separated_monitor: separated mode with monitor component
# Valid options are: [combined,combined_monitor,separated,separated_monitor]
topology: combined_monitor
# Specifies a list of ClusterComponentSpec objects used to define the
# individual Components that make up a Cluster.
# This field allows for detailed configuration of each Component within the Cluster
componentSpecs:
- name: kafka-combine
env:
- name: KB_KAFKA_BROKER_HEAP # use this ENV to set BROKER HEAP
value: "-XshowSettings:vm -XX:MaxRAMPercentage=75 -Ddepth=64"
- name: KB_KAFKA_CONTROLLER_HEAP # use this ENV to set CONTOLLER_HEAP
value: "-XshowSettings:vm -XX:MaxRAMPercentage=75 -Ddepth=64"
# Whether to enable direct Pod IP address access mode.
# - If set to 'true', Kafka clients will connect to Brokers using the Pod IP address directly.
# - If set to 'false', Kafka clients will connect to Brokers using the Headless Service's FQDN.
- name: KB_BROKER_DIRECT_POD_ACCESS
value: "false"
# Update `replicas` to your need.
replicas: 1
# Specifies the resources required by the Component.
resources:
limits:
cpu: "1"
memory: "2Gi"
requests:
cpu: "0.5"
memory: "2Gi"
# Specifies a list of PersistentVolumeClaim templates that define the storage
# requirements for the Component.
volumeClaimTemplates:
# Refers to the name of a volumeMount defined in
# `componentDefinition.spec.runtime.containers[*].volumeMounts
- name: data
spec:
# The name of the StorageClass required by the claim.
# If not specified, the StorageClass annotated with
# `storageclass.kubernetes.io/is-default-class=true` will be used by default
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: metadata
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
- name: kafka-exporter # component for exporter
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "1Gi"
requests:
cpu: "0.1"
memory: "0.2Gi"
kubectl apply -f examples/kafka/cluster-combined.yaml
Create a Kafka cluster with separated controller and broker components:
# cat examples/kafka/cluster-separated.yaml
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: kafka-separated-cluster
namespace: demo
spec:
# Specifies the behavior when a Cluster is deleted.
# Valid options are: [DoNotTerminate, Delete, WipeOut] (`Halt` is deprecated since KB 0.9)
# - `DoNotTerminate`: Prevents deletion of the Cluster. This policy ensures that all resources remain intact.
# - `Delete`: Extends the `Halt` policy by also removing PVCs, leading to a thorough cleanup while removing all persistent data.
# - `WipeOut`: An aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss.
terminationPolicy: Delete
# Specifies the name of the ClusterDefinition to use when creating a Cluster.
# Note: DO NOT UPDATE THIS FIELD
# The value must be `kafaka` to create a Kafka Cluster
clusterDef: kafka
# Specifies the name of the ClusterTopology to be used when creating the
# Cluster.
# - combined: combined Kafka controller (KRaft) and broker in one Component
# - combined_monitor: combined mode with monitor component
# - separated: separated KRaft and Broker Components.
# - separated_monitor: separated mode with monitor component
# Valid options are: [combined,combined_monitor,separated,separated_monitor]
topology: separated_monitor
# Specifies a list of ClusterComponentSpec objects used to define the
# individual Components that make up a Cluster.
# This field allows for detailed configuration of each Component within the Cluster
componentSpecs:
- name: kafka-broker
replicas: 1
serviceVersion: 3.3.2
services:
- name: advertised-listener
serviceType: ClusterIP
podService: true
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
env:
- name: KB_KAFKA_BROKER_HEAP # use this ENV to set BROKER HEAP
value: "-XshowSettings:vm -XX:MaxRAMPercentage=75 -Ddepth=64"
- name: KB_KAFKA_CONTROLLER_HEAP # use this ENV to set CONTOLLER_HEAP
value: "-XshowSettings:vm -XX:MaxRAMPercentage=75 -Ddepth=64"
# Whether to enable direct Pod IP address access mode.
# - If set to 'true', Kafka clients will connect to Brokers using the Pod IP address directly.
# - If set to 'false', Kafka clients will connect to Brokers using the Headless Service's FQDN
# and service `advertised-listener` must be set with "podService: true".
- name: KB_BROKER_DIRECT_POD_ACCESS
value: "false"
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
- name: metadata
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
- name: kafka-controller
serviceVersion: 3.3.2
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
serviceVersion: 1.6.0
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "1Gi"
requests:
cpu: "0.1"
memory: "0.2Gi"
kubectl apply -f examples/kafka/cluster-separated.yaml
We also maintain an 2.x version, which will rely on an external zookeeper cluster to work. Refer to cluster-2x-ext-zk-svc-descriptor.yaml to get an reference config.
With authentication enabled
We currently support SASL/SCRAM auth for 2.x version. If you want to enable it, set KB_KAFKA_ENABLE_SASL_SCRAM to true
for both broker and exporter components.
When the cluster creation is done, refer to a secret named $(CLUSTER_NAME)$-kafka-broker-account-admin to get the username/password.
Horizontal scaling
[!IMPORTANT] As per the Kafka documentation, the number of KRaft replicas should be odd to avoid split-brain scenarios. Make sure the number of KRaft replicas, i.e. Controller replicas, is always odd after Horizontal Scaling, either in Separated or Combined mode.
Scale-out
Warning: Combined KRaft mode (
kafka-combine) does not support online horizontal scale-out for serviceVersion 3.3.2. Adding a combined node requires the Kafka controller quorum membership and per-broker metadata view to converge; this addon does not implement that workflow yet. ThememberJoinaction will reject combined-mode scale-out. Use separated topology and scale broker-only components when you need online broker scale-out.
Horizontal scaling out the broker-only kafka-broker component in cluster kafka-separated-cluster by adding ONE more replica:
# cat examples/kafka/scale-out.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: kafka-broker-scale-out
namespace: demo
spec:
# Specifies the name of the Cluster resource that this operation is targeting.
clusterName: kafka-separated-cluster
type: HorizontalScaling
# Lists HorizontalScaling objects, each specifying scaling requirements for a Component, including desired total replica counts, configurations for new instances, modifications for existing instances, and instance downscaling options
horizontalScaling:
# Specifies the name of the Component.
- 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
kubectl apply -f examples/kafka/scale-out.yaml
After applying the operation, you will see a new pod created. You can check the progress of the scaling operation with following command:
kubectl describe -n demo ops kafka-broker-scale-out
Scale-in
Warning: Combined KRaft mode (
kafka-combine) does not support scale-in. ThememberLeaveaction will reject the request because quorum voter removal is not yet implemented — scaling in a combined node that is both broker and controller would break the KRaft quorum. Only broker-only components support scale-in.
The following example scales in a broker-only component in a separated-topology cluster. Combined mode clusters reject online scale-in and scale-out; use separated topology when online horizontal scaling is required.
# cat examples/kafka/scale-in.yaml
apiVersion: operations.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: kafka-broker-scale-in
namespace: demo
spec:
clusterName: kafka-separated-cluster
type: HorizontalScaling
horizontalScaling:
- componentName: kafka-broker
scaleIn:
replicaChanges: 1
kubectl apply -f examples/kafka/scale-in.yaml
Scale-in/out using Cluster API
Alternatively, you can update the replicas field in the spec.componentSpecs.replicas section to your desired non-zero number.
Note: For combined KRaft mode (
kafka-combine), online scale-out and
Deploy kafka with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has kafka on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartkafka is pinned at v1.2.0-alpha.1.test.1 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply