{"id":691,"date":"2016-10-25T15:08:39","date_gmt":"2016-10-25T15:08:39","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=691"},"modified":"2016-10-25T15:08:39","modified_gmt":"2016-10-25T15:08:39","slug":"multi-broker-kafka-cluster-beginners-guide","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=691","title":{"rendered":"Setting up a Multi-Broker Kafka Cluster &#8211; Beginners Guide"},"content":{"rendered":"<p>Kafka is an open source distributed messaging system that is been used by many organizations for many use cases. Its use cases include stream processing, log aggregation, metrics collection and so on.<\/p>\n<p>Note: This tutorial is based on Redhat 7 derivative. However, it will work on most Linux systems.<\/p>\n<h2 id=\"multi-node-kafka-cluster-setup\">Multi-Node Kafka Cluster Setup<\/h2>\n<p>This tutorial will guide you to set up a latest Kafka cluster from scratch.<\/p>\n<h3 id=\"prerequisites\">Prerequisites<\/h3>\n<p>1. You need a Zookeeper cluster before setting up a Kafka cluster. Refer this zookeeper cluster setup if you don&#8217;t have one.<\/p>\n<p>2. Launch three instances. Make sure you allow the traffic between Zookeeper and Kafka instances in the security groups.<\/p>\n<p>3. Set hostnames for three instances for identification using the following command.<\/p>\n<pre><code>hostnamectl set-hostname (node1,2,3)<\/code><\/pre>\n<h3 id=\"kafka-installation\">Kafka Installation<\/h3>\n<p>Perform the following tasks on all the servers. 1. Update the server. <\/p>\n<pre><code>sudo yum update -y<\/code><\/pre>\n<p>2. Install java 8. <\/p>\n<pre><code>sudo yum  -y install java-1.8.0-openjdk<\/code><\/pre>\n<p>3. Get the latest version of Kafka from here. <\/p>\n<pre><code>cd \/opt\nsudo wget http:\/\/mirror.fibergrid.in\/apache\/kafka\/0.10.0.0\/kafka_2.11-0.10.0.0.tgz<\/code><\/pre>\n<p>4. Untar the Kafka binary. <\/p>\n<pre><code>sudo tar -xvf kafka_2*<\/code><\/pre>\n<p>5. Rename the extracted Kafka folder with versions to Kafka. <\/p>\n<pre><code>sudo mv kafka_2.11-0.10.0.0 kafka<\/code><\/pre>\n<h3 id=\"creating-a-kafka-service\">Creating a Kafka Service<\/h3>\n<p>6. Open the <code>server.properties<\/code> file, find <code>zookeeper.connect<\/code><\/p>\n<p>at the bottom and enter the zookeeper IPs as shown below. Replace zk1, zk2, and zk3 with the IPs or DNS names of your zookeeper instances.<\/p>\n<pre><code>zookeeper.connect=zk1:2181,zk2:2181,zk3:2181<\/code><\/pre>\n<h3 id=\"create-a-kafka-service\">Create a Kafka Service<\/h3>\n<p>1. Create a systemd file. <\/p>\n<pre><code>sudo vi \/lib\/systemd\/system\/kafka.service<\/code><\/pre>\n<p>Copy the following contents on to the kafka.service unit file.<\/p>\n<pre><code>[Unit]\nDescription=Kafka\nBefore=\nAfter=network.target\n\n[Service]\nUser=ec2-user\nCHDIR= {{ data_dir }}\nExecStart=\/opt\/kafka\/bin\/kafka-server-start.sh \/opt\/kafka\/config\/server.properties\nRestart=on-abort\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre>\n<p>2. Reload the daemon.<\/p>\n<pre><code>sudo systemctl daemon-reload<\/code><\/pre>\n<h3 id=\"managing-kafka-service\">Managing Kafka Service<\/h3>\n<p>Once the Kafka service is created, you can manage the Kafka service using the Linux service module.<\/p>\n<ol>\n<li>To start the Kafka service,<\/li>\n<\/ol>\n<pre><code>sudo service kafka start<\/code><\/pre>\n<p>2. To stop and restart,<\/p>\n<pre><code>sudo service kafka stop\nsudo service kafka restart<\/code><\/pre>\n<h3 id=\"testing-the-kafka-cluster\">Testing The Kafka Cluster<\/h3>\n<p>To test the kafka cluter setup, we will create a topic and few messages. Andn we will try to consume it from different node to conform that the cluster is working as intended.<\/p>\n<p>To test, cd in to the kafka  bin directory to get access to the kafka scripts.<\/p>\n<pre><code>cd \/opt\/kafka\/bin<\/code><\/pre>\n<p>Step 1: Create a Topic from kafka node 1 with replication-factor 3. Replace <code>zk<\/code> with zookeper IP or DNS name.<\/p>\n<p>.\/kafka-topics.sh &#8211;create &#8211;zookeeper zk:2181 &#8211;replication-factor 3 &#8211;partitions 1 &#8211;topic test<\/p>\n<p>Step 2: Describe Topic from kafka node 2.<\/p>\n<p>.\/kafka-topics.sh  &#8211;describe &#8211;zookeeper zk:2181 &#8211;topic test<\/p>\n<p>Step 3: Create a Message using the following command. Enter the message in the terminal with weach message per line. Use <code>Ctrl + C<\/code> to exit.<\/p>\n<p>.\/kafka-console-producer.sh &#8211;broker-list kafka-node:9092 &#8211;topic test<\/p>\n<p>Step 4: From a different node, try to consume the message using the following command.<\/p>\n<p>.\/kafka-console-consumer.sh &#8211;zookeeper zk:2181 &#8211;from-beginning &#8211;topic test<\/p>\n<p>If you were able to do all the test mentioned above, you have a working kafka cluster.<\/p>\n<p>If you face any issue during the setup, feel free to drop us a comment below.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/multi-broker-kafka-cluster-beginners-guide\/\" target=\"_blank\" rel=\"noopener noreferrer\">Setting up a Multi-Broker Kafka Cluster &#8211; Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/multi-broker-kafka-cluster-beginners-guide\/<\/p>\n","protected":false},"author":1,"featured_media":692,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=691"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/691\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/692"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}