{"id":4214,"date":"2020-04-02T15:13:49","date_gmt":"2020-04-02T14:13:49","guid":{"rendered":"http:\/\/www.ceyark.com\/?p=4214"},"modified":"2020-06-29T17:51:29","modified_gmt":"2020-06-29T16:51:29","slug":"kafkabrokerpods","status":"publish","type":"post","link":"https:\/\/www.ceyark.com\/index.php\/2020\/04\/02\/kafkabrokerpods\/","title":{"rendered":"Kafka Broker Pods in Kubernetes cluster"},"content":{"rendered":"<p>In this post, we will describe the steps we followed to enable Kafka broker pods within kubernetes cluster using microk8s in Ubuntu 18.04.<\/p>\n<p>First step was to enable firewalls in ubuntu using ufw if not done already. Lets start with Install microk8s.<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n\n<p style=\"text-align: left; padding-left: 40px;\"><strong>$ sudo snap install microk8s &#8211;classic<\/strong><br \/><strong>$ sudo usermod -a -G microk8s $USER<\/strong><br \/><strong>$ su &#8211; $USER<\/strong><\/p>\n\n<p>configure firewall to allow pod-to-pod and pod-to-internet communication<\/p>\n<p style=\"padding-left: 80px;\"><strong>$ sudo ufw allow in on cni0 &amp;&amp; sudo ufw allow out on cni0<\/strong><br \/><strong>$ sudo ufw default allow routed<\/strong><\/p>\n<p>enable addons for microk8s<\/p>\n<p style=\"padding-left: 80px;\"><strong>$ microk8s.enable dashboard dns<\/strong><\/p>\n<p>Enable loadbalancer addon. We require to assign a pool of IP address to assign for new loadbalancer services created within the cluster. We configured a range of 20 IP addresses.<\/p>\n<p style=\"padding-left: 80px;\"><strong>$ microk8s.enable metallb<\/strong><\/p>\n<p>start the cluster.<\/p>\n<p style=\"padding-left: 80px;\"><strong>$ microk8s.start<\/strong><\/p>\n<p style=\"padding-left: 80px;\"><strong>$ microk8s.status<\/strong><\/p>\n\n<p>If you havent received any error message, then the cluster is started successfully and we can proceed to access the dashboard.<\/p>\n\n\n<p style=\"padding-left: 40px;\"><strong>$ microk8s.kubectl get all &#8211;all-namespaces<\/strong><\/p>\n\n\n<p><\/p>\n\n\n<p>the above command will give us the cluster ip address to access the dashboard. The name of the services will be service\/kubernetes-dashboard. The port will be 443 unless you change them through configuration.<br \/>\nWhen you access the dashboard in a browser, we require to login. We use the token approach to login and we obtain token through the two commands below executed in sequence.<\/p>\n\n\n<p style=\"padding-left: 40px;\"><strong>$token=$(microk8s.kubectl -n kube-system get secret | grep default-token | cut -d &#8221; &#8221; -f1)<\/strong><br \/><strong>$microk8s.kubectl -n kube-system describe secret $token<\/strong><\/p>\n\n<p>On successful login to dashboard, we can start to create the services.<br \/>\nTo create Zookeeper service, click the + sign on the dashboard to provide the configuration for the service.<br \/>\nThe configuration we provided is given below.<\/p>\n\n\n<p style=\"padding-left: 120px;\"><em>kind: Deployment<\/em><br \/><em>apiVersion: apps\/v1<\/em><br \/><em>metadata:<\/em><\/p>\n<p style=\"padding-left: 160px;\"><em>name: zookeeper-deployment-1<\/em><br \/><em>spec:<\/em><\/p>\n<p style=\"padding-left: 200px;\"><em>selector:<\/em><\/p>\n<p style=\"padding-left: 240px;\"><em>matchLabels:<\/em><br \/><em>app: zookeeper-1<\/em><br \/><em>template:<\/em><\/p>\n<p style=\"padding-left: 280px;\"><em>metadata:<\/em><br \/><em>labels:<\/em><\/p>\n<p style=\"padding-left: 320px;\"><em>app: zookeeper-1<\/em><br \/><em>spec:<\/em><\/p>\n<p style=\"padding-left: 360px;\"><em>containers:<\/em><br \/><em>&#8211; name: zoo1<\/em><br \/><em>image: bitnami\/zookeeper<\/em><\/p>\n<p style=\"padding-left: 120px;\"><em>ports:<\/em><\/p>\n<p style=\"padding-left: 160px;\"><br \/><em>&#8211; containerPort: 2181<\/em><\/p>\n<p style=\"padding-left: 120px;\"><br \/><em>env:<\/em><\/p>\n<p style=\"padding-left: 160px;\"><br \/><em>&#8211; name: ZOOKEEPER_ID<\/em><br \/><em>value: &#8220;1&#8221;<\/em><br \/><em>&#8211; name: ZOOKEEPER_SERVER_1<\/em><br \/><em>value: zoo1<\/em><br \/><em>&#8211; name: ALLOW_ANONYMOUS_LOGIN<\/em><br \/><em>value: &#8220;yes&#8221;<\/em><\/p>\n<p style=\"padding-left: 80px;\"><br \/><em>&#8212;<\/em><br \/><em>apiVersion: v1<\/em><br \/><em>kind: Service<\/em><\/p>\n<p style=\"padding-left: 120px;\"><em>metadata:<\/em><br \/><em>name: zoo1<\/em><\/p>\n<p style=\"padding-left: 160px;\"><em>labels:<\/em><br \/><em>app: zookeeper-1<\/em><\/p>\n<p style=\"padding-left: 200px;\"><em>spec:<\/em><\/p>\n<p style=\"padding-left: 240px;\"><em>ports:<\/em><br \/><em>&#8211; name: client<\/em><br \/><em>port: 2181<\/em><br \/><em>protocol: TCP<\/em><br \/><em>&#8211; name: follower<\/em><br \/><em>port: 2888<\/em><br \/><em>protocol: TCP<\/em><br \/><em>&#8211; name: leader<\/em><br \/><em>port: 3888<\/em><br \/><em>protocol: TCP<\/em><\/p>\n<p style=\"padding-left: 200px;\"><em>selector:<\/em><br \/><em>app: zookeeper-1<\/em><\/p>\n\n<p>Once we click the upload button, unless you have any format error reported, we should be able to see a successful zookeeper service running in the dashboard overview screen. Format errors are generally alignment issues and are simple to fix through the guidance provided by the error message.<\/p>\n<p>To create kafka pods, click the + sign on the dashboard to provide the configuration for the service.<br \/>\nThe configuration we provided is given below.<\/p>\n\n\n<p style=\"padding-left: 160px;\"><em>kind: Deployment<\/em><br \/><em>apiVersion: apps\/v1<\/em><\/p>\n<p style=\"padding-left: 200px;\"><br \/><em>metadata:<\/em><br \/><em>name: kafka-service<\/em><\/p>\n<p style=\"padding-left: 240px;\"><em>spec:<\/em><\/p>\n<p style=\"padding-left: 280px;\"><em>selector:<\/em><\/p>\n<p style=\"padding-left: 320px;\"><em>matchLabels:<\/em><br \/><em>app: kafka-service<\/em><\/p>\n<p style=\"padding-left: 280px;\"><em>template:<\/em><\/p>\n<p style=\"padding-left: 320px;\"><em>metadata:<\/em><\/p>\n<p style=\"padding-left: 360px;\"><em>labels:<\/em><br \/><em>app: kafka-service <\/em><br \/><em>spec:<\/em><br \/><em>replicas: 1<\/em><br \/><em>containers:<\/em><\/p>\n<p style=\"padding-left: 400px;\"><em>&#8211; name: kafka-service<\/em><br \/><em>image: wurstmeister\/kafka<\/em><\/p>\n<p style=\"padding-left: 360px;\"><em>ports:<\/em><\/p>\n<p style=\"padding-left: 400px;\"><em>&#8211; containerPort: 9092<\/em><\/p>\n<p style=\"padding-left: 360px;\"><em>env:<\/em><\/p>\n<p style=\"padding-left: 400px;\"><em>&#8211; name: KAFKA_LISTENERS<\/em><br \/><em>value: INTERNAL:\/\/:32323,EXTERNAL:\/\/:9092<\/em><br \/><em>&#8211; name: KAFKA_ADVERTISED_LISTENERS<\/em><br \/><em>value: INTERNAL:\/\/kafka-service:32323,EXTERNAL:\/\/10.152.183.230:9092<\/em><br \/><em>&#8211; name: KAFKA_ZOOKEEPER_CONNECT<\/em><br \/><em>value: 10.152.183.182:2181<\/em><br \/><em>&#8211; name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP<\/em><br \/><em>value: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT<\/em><br \/><em>&#8211; name: KAFKA_CREATE_TOPICS<\/em><br \/><em>value: ceyark-test:1:1<\/em><br \/><em>&#8211; name: KAFKA_INTER_BROKER_LISTENER_NAME<\/em><br \/><em>value: INTERNAL<\/em><\/p>\n\n<p>Once we click the upload button, unless you have any format error reported, we should be able to see a successful kafka pods running in the dashboard overview screen. Alternatively, you can view the information through the below command from  console window.<\/p>\n\n\n<p style=\"padding-left: 80px;\"><strong>$microk8s.kubectl get pods &#8211;output=wide<\/strong><\/p>\n\n<p>Format errors are generally alignment issues and are simple to fix through the guidance provided by the error message. If you have any errors, you may check the<br \/>\nlogs in the dashboard through the logs options against the pod. The logs are generally good enough to troubleshoot and fix the issues.<\/p>\n<p>Finally we require to configure a loadbalancer service for our pods to route the request to the pods that originate from outside the cluster. Execute the following command to create, link the loadbalancer service to our pods.<\/p>\n\n\n<p style=\"padding-left: 80px;\"><strong>$ microk8s.kubectl expose deployment kafka-service &#8211;type=LoadBalancer &#8211;name=my-service<\/strong><\/p>\n\n<p>You can view the status of the new loadbalancer service in the dashboard. Alternatively enter the below command to view the details of the service.<\/p>\n\n\n<p style=\"padding-left: 80px;\"><strong>$ sudo microk8s.kubectl get services<\/strong><br \/><strong>$ sudo microk8s.kubectl describe svc my-service<\/strong><\/p>\n<p style=\"padding-left: 120px;\"><em>Name: my-service<\/em><br \/><em>Namespace: default<\/em><br \/><em>Labels: &lt;none&gt;<\/em><br \/><em>Annotations: &lt;none&gt;<\/em><br \/><em>Selector: app=kafka-service<\/em><br \/><em>Type: LoadBalancer<\/em><br \/><em>IP: 10.182.183.167<\/em><br \/><em>LoadBalancer Ingress: 10.182.183.230<\/em><br \/><em>Port: &lt;unset&gt; 9092\/TCP<\/em><br \/><em>TargetPort: 9092\/TCP<\/em><br \/><em>NodePort: &lt;unset&gt; 31985\/TCP<\/em><br \/><em>Endpoints: 10.1.2.122:9092<\/em><br \/><em>Session Affinity: None<\/em><br \/><em>External Traffic Policy: Cluster<\/em><br \/><em>Events:<\/em><br \/><em>Type Reason Age From Message<\/em><br \/><em>&#8212;- &#8212;&#8212; &#8212;- &#8212;- &#8212;&#8212;-<\/em><br \/><em>Normal IPAllocated 5s metallb-controller Assigned IP &#8220;10.182.183.230&#8221;<\/em><br \/><em>Normal nodeAssigned 5s metallb-speaker announcing from node &#8220;ceyark-macserv1<\/em>&#8220;<\/p>\n\n<p>If you happen to see all the configurations are correct in the loadbalancer service as shown above and the dashboard does not report of any failures across the three items we started, we are good to use the kafka brokers deployed in our pods. Notice the endpoints value above. It is the IP address assigned to the pods. Every time we restart the pods, the IP address will get changed. This may not be liked by applications that require IP address to be almost static and in such scenarios, we will be having the benefit of the loadbalancer service.  <\/p>\n<p>But you may be inclined to test it before you start to use it. We used a test utility called kafkacat. install it like below.<\/p>\n\n\n<p style=\"padding-left: 80px;\"><strong>$ apt-get install kafkacat<\/strong><\/p>\n<p>Once it is installed, we start the consumer first in a seperate console.<\/p>\n<p style=\"padding-left: 80px;\"><strong>$kafkacat -b 10.182.183.230:9092 -t ceyark-test<\/strong><\/p>\n<p>we start the producer in seperate console.<\/p>\n<p style=\"padding-left: 80px;\"><strong>$ echo hello | kafkacat -b 10.182.183.230 -t ceyark-test<\/strong><\/p>\n<p>If everything goes fine, we should see the &#8220;hello&#8221; message in the consumer console. Once you are done, issue the following command to stop the cluster.<\/p>\n<p style=\"padding-left: 80px;\"><strong>$ microk8s.stop<\/strong><\/p>\n\n<p>With the configuration files in hand ready, we were able to create the kafka docker clusters in less than<br \/>\n10 min and this utility helps with our development tasks.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will describe the steps we followed to enable Kafka broker pods within kubernetes cluster using microk8s in Ubuntu 18.04. First step was to enable firewalls in ubuntu using ufw if not done already. Lets start with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[138],"tags":[139],"class_list":["post-4214","post","type-post","status-publish","format-standard","hentry","category-ceyarkblogs","tag-ceyark"],"_links":{"self":[{"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/posts\/4214"}],"collection":[{"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/comments?post=4214"}],"version-history":[{"count":10,"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/posts\/4214\/revisions"}],"predecessor-version":[{"id":4791,"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/posts\/4214\/revisions\/4791"}],"wp:attachment":[{"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/media?parent=4214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/categories?post=4214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ceyark.com\/index.php\/wp-json\/wp\/v2\/tags?post=4214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}