<p>I have k8s cluster with core-pod(this is my spring-boot application) and kafka pod.
There are services for connections to this services, but, when start application pod - there is connectivety issue. I tries to connect this containers in just simple docker and it was success</p>
<p>So, my application yml and pod yml</p>
<pre><code>server:
  port: 8080
logging:
  level:
    nuclear.bot.core: TRACE
notification:
  level: INFO
spring:
  kafka:
    template:
      default-topic: PARSER.EVENT
    consumer:
      bootstrap-servers: kafka-service:9092
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: nuclear.bot.core.config.MessageAdapter
      group-id: core
    properties:
      key-class: java.lang.String
      value-class: nuclear.bot.core.dto.AgentMessage
</code></pre>
<p>so application try to connect to kafka-service on port 9092</p>
<p>and my app pod like this</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: core
  labels:
    app: core
spec:
  replicas: 1
  selector:
    matchLabels:
      app: core-pod
  template:
    metadata:
      name: core-pod
      labels:
        app: core-pod
    spec:
      containers:
        - name: nuclear-bot-core
          image: vladi15151/nuclear-bot-core:0.9.7
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
</code></pre>
<p>So, kafka which I trying to connect like this (pod and service together)</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: kafka-service
  name: kafka-service
spec:
  type: NodePort
  selector:
    app: kafka-broker
  ports:
    - name: kafka-port
      port: 9092
      targetPort: 9092
      nodePort: 30126
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: kafka-broker
  name: kafka-broker
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kafka-broker
  template:
    metadata:
      labels:
        app: kafka-broker
    spec:
      hostname: kafka-broker
      containers:
        - image: bitnami/kafka
          imagePullPolicy: IfNotPresent
          name: kafka-broker
          ports:
            - containerPort: 9092
          env:
            - name: KAFKA_BROKER_ID
              value: &quot;1&quot;
            - name: KAFKA_ZOOKEEPER_CONNECT
              value: &quot;zookeeper-service:2181&quot;
            - name: KAFKA_LISTENERS
              value: PLAINTEXT://localhost:9092
            - name: KAFKA_ADVERTISED_LISTENERS
              value: PLAINTEXT://localhost:9092
            # Creates a topic with one partition and one replica.
            - name: KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE
              value: &quot;true&quot;
            - name: KAFKA_CREATE_TOPICS
              value: &quot;PARSEREVENT:1:1&quot;
            - name: MY_POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: ALLOW_PLAINTEXT_LISTENER
              value: &quot;yes&quot;
</code></pre>
<p>Kafka successfully started on kluster, but application throw error message when try to start:</p>
<pre><code>2023-06-09 21:53:26.193  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Bootstrap broker kafka-service:9092 (id: -1 rack: null) disconnected
2023-06-09 21:53:27.249  INFO 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Node -1 disconnected.
2023-06-09 21:53:27.249  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Connection to node -1 (kafka-service/10.99.46.194:9092) could not be established. Broker may not be available.
2023-06-09 21:53:27.249  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Bootstrap broker kafka-service:9092 (id: -1 rack: null) disconnected
2023-06-09 21:53:28.205  INFO 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Node -1 disconnected.
2023-06-09 21:53:28.205  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Connection to node -1 (kafka-service/10.99.46.194:9092) could not be established. Broker may not be available.
2023-06-09 21:53:28.205  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Bootstrap broker kafka-service:9092 (id: -1 rack: null) disconnected
2023-06-09 21:53:29.309  INFO 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Node -1 disconnected.
2023-06-09 21:53:29.309  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Connection to node -1 (kafka-service/10.99.46.194:9092) could not be established. Broker may not be available.
2023-06-09 21:53:29.309  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Bootstrap broker kafka-service:9092 (id: -1 rack: null) disconnected
2023-06-09 21:53:30.113  INFO 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Node -1 disconnected.
2023-06-09 21:53:30.113  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Connection to node -1 (kafka-service/10.99.46.194:9092) could not be established. Broker may not be available.
2023-06-09 21:53:30.113  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Bootstrap broker kafka-service:9092 (id: -1 rack: null) disconnected
2023-06-09 21:53:31.143  INFO 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Node -1 disconnected.
2023-06-09 21:53:31.143  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Connection to node -1 (kafka-service/10.99.46.194:9092) could not be established. Broker may not be available.
2023-06-09 21:53:31.143  WARN 1 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-core-1, groupId=core] Bootstrap broker kafka-service:9092 (id: -1 rack: null) disconnected
</code></pre>
<p>What can I try to use more?</p>

Roberto
<p>You've made the broker only allow for connection from itself, not external connections. You need to modify the two listener env variables.  <a href="https://stackoverflow.com/questions/51630260/connect-to-kafka-running-in-docker">Connect to Kafka running in Docker</a></p>
<p>Then, it would be better if you used <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/" rel="nofollow noreferrer">fully qualified address</a> <code>kafka-service.svc.cluster.local:9092</code>.</p>
<p>If you <a href="https://strimzi.io/blog/2019/04/17/accessing-kafka-part-1/" rel="nofollow noreferrer">use Strimzi</a>, you'd not have this issue.</p>
