How to Set Up and Run Kafka on Kubernetes
How to Set Up and Run Kafka on Kubernetes
In this tutorial we are going to see an example Kafka deployment within Platform9 Free Tier Kubernetes platform, backed up by some DigitalOcean droplets.
Join the DZone community and get the full member experience.Join For Free
Apache Kafka is a leading open-source distributed streaming platform first developed at LinkedIn. It consists of several APIs such as the Producer, the Consumer, the Connector and the Streams. Together, those systems act as high-throughput, low-latency platforms for handling real-time data. This is why Kafka is preferred among several of the top-tier tech companies such as Uber, Zalando, and AirBnB.
Quite often, we would like to deploy a fully-fledged Kafka cluster in Kubernetes, just because we have a collection of microservices and we need a resilient message broker in the center. We also want to spread the Kafka instances across nodes, to minimize the impact of a failure.
Let’s get started.
Setting Up the Platform9 Free Tier Cluster
Below are the brief instructions to get you up and running with a working Kubernetes Cluster from Platform9:
Sign up with Platform9
Click the Create Cluster button and inspect the instructions. We need a server to host the Cluster.
Create a few Droplets with at least 3GB Ram and 2vCPU’s. Follow instructions to install the pf9cli tool and prepping the nodes.
Switch to the Platform9 UI and click the refresh button. You should see the new nodes in the list. Assign the first node as a master and the other ones as workers.
Leave the default values in the next steps. Then create the cluster.
Wait until the cluster becomes healthy. It will take at least 20 minutes to finish.
Click on the API Access tab and select to download the kubeconfig button:
Once downloaded, export the config and test the cluster health:
Creating Persistent Volumes
Before we install Helm and the Kafka chart, we need to create some persistent volumes for storing Kafka replication message files.
This step is crucial to be able to enable persistence in our cluster because without that, the topics and messages would disappear after we shut down any of the servers, as they live in memory.
In our example, we are going to use a local file system, Persistent Volume (PV), and we need one persistent volume for each Kafka instance; so if we plan to deploy three instances, we need three PV’s.
Create and apply first the Kafka namespace and the PV specs:
If you are using the Kubernetes UI, you should be able to see the PV volumes on standby:
We begin by installing Helm on our computer and installing it in Kubernetes, as it’s not bundled by default.
First, we download the install script:
Make the script executable with chmod:
Create the tiller service account:
Next, bind the tiller serviceaccount to the cluster-admin role:
Now we can run
helm init :
Now we are ready to install the Kafka chart.
Deploying the Helm Chart
In the past, trying to deploy Kafka on Kubernetes was a lengthy exercise. You had to deploy a working Zookeeper Cluster, role bindings, persistent volume claims and apply the correct configuration.
Hopefully for us, with the use of the Kafka Incubator Chart, the whole process is mostly automated (with a few quirks here and there).
We add the Helm chart:
Export the chart values in a file:
Carefully inspect the configuration values, particularly around the parts about persistence and about the number of Kafka stateful sets to deploy.
Then install the chart:
Check the status of the deployment
During this phase, you may want to navigate to the Kubernetes UI and inspect the dashboard for any issues. Once everything is complete, then the pods and Persistent Volume Claims should be bound and green.
Now we can test the Kafka cluster.
Testing the Kafka Cluster
We are going to deploy a test client that will execute scripts against the Kafka cluster.
Create and apply the following deployment:
Then, using the testclient, we create the first topic, which we are going to use to post messages:
Created topic "messages."
Here we need to use the correct hostname for the zookeeper cluster and the topic configuration.
Next, verify that the topic exists:
Now we can create one consumer and one producer instance so that we can send and consume messages.
First create one or two listeners, each on its own shell:
Then create the producer session and type some messages. You will be able to see them propagate to the consumer sessions:
Destroying the Helm Chart
To clean up our resources, we just destroy the Helm Chart and delete the PVs we created earlier:
Stay put for more tutorials showcasing common deployment scenarios within Platform9’s fully-managed Kubernetes platform.
Opinions expressed by DZone contributors are their own.