How to handle Kafka container lifecycle using spring kafka in Kubernetes multipod deployment

Tags: , , ,



I am using Spring kafka implementation and I need to start and stop my kafka consumer through an REST API. For that i am using KafkaListenerEndpointRegistry endpointRegistry

endpointRegistry.getListenerContainer(“consumer1”).stop();

endpointRegistry.getListenerContainer(“consumer1”).start();

We are deploying the microservice on kubernetes pod so there might be multiple deployments for the same microservice. how could i manage to start and stop the consumer on all the container.

Answer

Kubernetes offers nothing to automatically broadcast an http-request to all pods for a service; so you have to do it yourself.

Broadcasting over Kafka

You can publish the start/stop command from the single instance that receives the http-request to a topic, dedicated for broadcasting commands between all instances.

Of course, you must make sure that each instance can read all message on that topic, so you need to prevent the partitions from being balanced between these instances. You can achieve that by setting a unique group-id (e.g. by suffixing your normal groupId with a UUID) on the Consumer for that topic.

Broadcasting over Http

Kubernetes knows which pods are listening on which endpoints, and you can get that information in your service. Spring Cloud Kubernetes (https://cloud.spring.io/spring-cloud-static/spring-cloud-kubernetes/2.0.0.M1/reference/html/#ribbon-discovery-in-kubernetes) makes it easy to get at that information; there’s probably lots of different ways to do that, with Spring Cloud Kubernetes it would go something like this:

Receive the command on the randomly selected pod, get the ServerList from Ribbon (it contains all the instances and the ip-address/port where they can be reached) for your service, and send a new http-request to each of them.

I would prefer the Kafka-approach for its robustness, the http-approach might be easier to implement, if you’re already using Spring Cloud.



Source: stackoverflow