kubernetesapache-kafkaspring-kafkaazure-aks

a deployed mircoservice in AKS is not able to connect to deployed kafka in AKS


I am deploying my Spring micro-services project to AKS, the rest of the services work fine, but when I deployed a service that uses Kafka, after I deployed Kafka alone using helm, I got repetitive messages in logs that my service is not able to establish a connection, even though I have configured my application.properties, and still trying to establish a connection to localhost.

This is my application.properties:

spring.kafka.consumer.group-id=jihed
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.bootstrap-servers=kafka:9092
spring.kafka.properties.security.protocol=SASL_PLAINTEXT
spring.kafka.properties.sasl.mechanism=PLAIN
spring.kafka.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="user1" password="Ut8uZDJzPR";
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.trusted.packages=*

spring.kafka.producer.bootstrap-servers=kafka:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer

These are the logs when I type kubectl logs -f pod-name:

2024-05-13T16:11:17.713Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:17.713Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:18.917Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:18.917Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:20.021Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:20.021Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:21.024Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:21.025Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:22.228Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:22.228Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:23.432Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:23.432Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:24.335Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:24.335Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:25.339Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:25.339Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:26.342Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:26.342Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:27.246Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:27.246Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:28.149Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:28.149Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2024-05-13T16:11:29.052Z  INFO 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Node -1 disconnected.
2024-05-13T16:11:29.053Z  WARN 1 --- [payload-service] [| adminclient-1] org.apache.kafka.clients.NetworkClient   : [AdminClient clientId=adminclient-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Brok

Important thing to mention is these logs show when I try to access this method:

public AppTopic createAppTopic(AppTopic topic) throws TopicException {
    try {
        // Persist the AppSchema object if it's not already persisted
        if (topic.getSchema() != null && topic.getSchema().getId() == null) {
            AppSchema persistedSchema = appSchemaRepo.save(topic.getSchema());
            topic.setSchema(persistedSchema);
        }

        // Create or modify Kafka topic
        kafkaAdmin.createOrModifyTopics(TopicBuilder.name(topic.getName()).partitions(topic.getPartitions()).replicas(topic.getReplicas()).config(TopicConfig.RETENTION_MS_CONFIG, topic.getRetentionPeriod()).build());
        // Save the AppTopic entity
        return appTopicRepo.save(topic);
    } catch (Exception e) {
        throw new TopicException("Cannot create topic for this reason", e);
    }
}

I already tried to push the docker images again and when I checked the env of my pod:

Using kubectl exec -it pod-name -- env:

KAFKA_PORT_9092_TCP=tcp://10.0.148.52:9092
KAFKA_PORT_9092_TCP_PROTO=tcp
KAFKA_SERVICE_HOST=10.0.148.52
KAFKA_SERVICE_PORT=9092
KAFKA_SERVICE_PORT_TCP_CLIENT=9092
KAFKA_PORT_9092_TCP_PORT=9092
KAFKA_PORT_9092_TCP_ADDR=10.0.148.52

It shows that the link between Kafka and the service is done, since it knows it.


Solution

  • spring.kafka.bootstrap-servers can be used to set the bootstrap servers once for each client including the AdminClient you're getting an error for

    Otherwise, you'll need another section for spring.kafka.admin.* properties