
In this article, you will learn how to run Apache Kafka on Kubernetes using the open-source Strimzi project. You will setup Strimzi on Vultr Kubernetes Engine, setup a Kafka cluster, sent messages to a topic and received messages from that topic. You will also secure the Kafka cluster using encryption and user authentication.
Apache Kafka is a messaging system that allows clients to publish and read streams of data (also called events). It has an ecosystem of open-source solutions that you can combine to store, process, and integrate these data streams with other parts of your system in a secure, reliable, and scalable manner.
Key components of Apache Kafka:
Strimzi can be used to run an Apache Kafka cluster on Kubernetes. In addition to the cluster itself, Strimzi can also help you manage topics, users, Mirror Maker and Kafka Connect deployments. With Strimzi, you can configure the cluster as per your needs. This includes advanced features such as rack awareness configuration to distribute Kafka nodes across availability zones, as well as Kubernetes taints and tolerations to pin Kafka to dedicated worker nodes in your Kubernetes cluster. You can also expose Kafka to external clients outside the Kubernetes cluster using Service types such as NodePort, LoadBalancer etc. and these can be secured using SSL.
All this is made possible with a combination of Custom resources, Operators and respective Docker container images.
You can customize Strimzi Kafka components in a Kubernetes cluster using custom resources. These are created as instances of APIs added by Custom resource definitions (CRDs) that extend Kubernetes resources. Each Strimzi component has an associated CRD which is used to describe that component. Thanks to CRDs, Strimzi resources benefit from Kubernetes features such as CLI accessibility and configuration validation.
Once a Strimzi custom resource is created, it's managed using Operators. Operators are a method of packaging, deploying, and managing a Kubernetes-native application. Because Strimzi Operators automate common and complex tasks related to a Kafka deployment, Kafka administration tasks are simplified and require less manual intervention.
Let's look at the Strimzi operators and the custom resources they manage.
Cluster Operator
Strimzi Cluster Operator is used to deploy and manage Kafka components. Although a single Cluster Operator instance is deployed by default, you can add replicas with leader election to ensure operator high availability.
The Cluster Operator manages the following Kafka components:
Kafka resource is used to configure a Kafka deployment. Configuration options for the ZooKeeper cluster also included within the Kafka resource.Entity Operator
Entity Operator comprises the Topic and User Operator.
KafkaTopic resource as part of your application’s deployment and the Topic Operator will take care of creating the topic for you and keeping them in-sync with corresponding Kafka topics. Information about each topic in a topic store, which is continually synchronized with updates from Kafka topics or Kubernetes KafkaTopic custom resources. If a topic is reconfigured or reassigned to other brokers, the KafkaTopic will always be up to date.KafkaUser resource as part of your application’s deployment along with authentication and authorization mechanisms for the user. You can also configure user quotas that control usage of Kafka resources. In addition to managing credentials for authentication, the User Operator also manages authorization rules by including a description of the user’s access rights in the KafkaUser declaration.Install kubectl on your local workstation. It is a Kubernetes command-line tool that allows you to run commands against Kubernetes clusters.
Deploy a Vultr Kubernetes Engine (VKE) cluster using the Reference Guide. Once it's deployed, from the Overview tab, click the Download Configuration button in the upper-right corner to download your kubeconfig file and save it to a local directory.
Point kubectl to Vultr Kubernetes Engine cluster by setting the KUBECONFIG environment variable to the path where you downloaded the cluster kubeconfig file in the previous step.
Verify the same using the following command:
Create a namespace called kafka.
You should see this output:
Apply the Strimzi installation files, including ClusterRoles, ClusterRoleBindings and Custom Resource Definitions (CRDs).
You should see this output:
Follow the deployment of the Strimzi cluster operator and wait for the Pod to transition to Running status.
You should see this output (the Pod name might differ in your case):
To check the operator’s log:
Create a directory and switch to it:
Create a new file kafka-cluster-1.yml:
Add the below contents to kafka-cluster-1.yml file and save it.
Install the Kafka cluster:
You should see this output:
Wait for cluster to be created.
Once completed, you will see this output:
Verify Kafka cluster
You should see this output:
Verify Kafka Pod
You should see this output:
Verify Zookeeper Pod
You should see this output:
Check the ConfigMaps associated with the cluster:
You should see this output:
Check Services associated with the cluster:
You should see this output (the ClusterIPs might differ in your case):
Check Secrets associated with the cluster:
You should see this output:
You can test the Kafka cluster using the Kafka CLI based consumer and producer.
You will verify cluster functionality by producing data using Kafka CLI producer and consuming data using Kafka CLI consumer.
Run a Pod to execute Kafka CLI producer and send data to a topic
You should see the following output with prompt
Enter messages in the prompt. These will be send to the specified Kafka topic.
Open a new terminal. Point kubectl to Vultr Kubernetes Engine cluster by setting the KUBECONFIG environment variable to the path where you downloaded the cluster kubeconfig file.
Run a Pod to execute Kafka CLI consumer to consume data from a topic
You should receive messages you sent from the producer terminal.
Press ctrl+c on each terminal to close them. This will delete both the Pods.
Delete the Kafka cluster
Verify that the associated Pods were deleted. Wait for my-cluster-1-kafka-0 and my-cluster-1-zookeeper-0 Pods to terminate.
So far, you have setup a simple Kafka cluster. In the next section, you will learn how to secure the setup by using the following:
SASL SCRAM.Create a new file kafka-cluster-2.yml:
Add the below contents to kafka-cluster-2.yml file and save it.
Install the Kafka cluster:
You should see this output:
Wait for cluster to be created.
Once completed, you will see this output:
Verify Kafka cluster
You should see this output:
Create a new file kafka-user.yml:
Add the below contents to kafka-user.yml file and save it.
Create the KafkaUser resource
You should see this output:
Verify user creation
You should see this output:
When the user is created, the User Operator creates a Kubernetes Secret and seeds it with the user credentials required to authenticate to the Kafka cluster.
Verify the Secret
You will verify cluster functionality by producing data using Kafka CLI producer and consuming data using Kafka CLI consumer.
Fetch the password for the Kafka user that you had created and save it to your local workstation.
Fetch the Kafka server certificate and save it to your local workstation.
Fetch the Kafka server certificate password and save it to your local workstation.
Open a new terminal. Point kubectl to Vultr Kubernetes Engine cluster by setting the KUBECONFIG environment variable to the path where you downloaded the cluster kubeconfig file.
Start a new Pod name kafka-producer
You should see a shell prompt after the Pod starts
From the previous terminal, copy the local certificate into the kafka-producer Pod that you just started:
Go back to the terminal where the kafka-producer Pod is running and execute the below commands
Import the server CA certificate in to the keystore. For keypass, use the password you had saved to your local ca.password file
You should see this output
Create the configuration file which will be used by the Kafka CLI producer. For password, use the password you had saved to your local user.password file
Send data to a topic
You should see the following output with prompt
Enter messages in the prompt. These will be send to the specified Kafka topic.
Open a new terminal. Point kubectl to Vultr Kubernetes Engine cluster by setting the KUBECONFIG environment variable to the path where you downloaded the cluster kubeconfig.
Start a new Pod name kafka-consumer
You should see a shell prompt after the Pod starts
From the previous terminal, copy the local certificate into the kafka-consumer Pod that you just started:
Go back to the terminal where the kafka-consumer Pod is running and execute the below commands to
Import the server CA certificate in to the keystore. For keypass, use the password you had saved to your local ca.password file
You should see this output
Create the configuration file which will be used by the Kafka CLI consumer. For password, use the password you had saved to your local user.password file
Use Kafka CLI consumer to consume data from the topic
You should receive messages you sent from the producer terminal.
From a new terminal, delete the Kafka cluster
Verify that the associated Pods were deleted. Wait for my-cluster-2-kafka-0 and my-cluster-2-zookeeper-0 Pods to terminate.
After you have completed the tutorial in this article, you can delete the Vultr Kubernetes Engine cluster.
In this article, you learnt how to use Strimzi to run Kafka and its related components on Kubernetes. You installed Strimzi on Vultr Kubernetes Engine, setup a Kafka cluster, sent messages to a topic and received messages from that topic. Next, you secured the Kafka cluster by enforcing TLS encryption as well as SASL authentication. TLS encryption ensured that the clients could only connect via SSL and with SASL authentication, clients had to specify the username and password to interact with the cluster (send or receive data).
You can also learn more in the following documentation:
0 Comments
Be the first to comment and share your perspective with the community.