<p>I have a single node Kafka broker running inside a pod on a single node kubernetes environment. I am using this image for kafka: <a href="https://hub.docker.com/r/wurstmeister/kafka" rel="nofollow noreferrer">https://hub.docker.com/r/wurstmeister/kafka</a></p>

<p><strong>kafka version</strong> = 1.1.0</p>

<p>Kubernetes cluster is running inside a VM on a server. The VM has the following IP on the active interface ens32 - <code>192.168.3.102</code></p>

<p><strong>Kafka.yaml</strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: casb-deployment
  name: kafkaservice
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: kafkaservice
    spec:
      hostname: kafkaservice
      containers:
      - name: kafkaservice
        imagePullPolicy: IfNotPresent
        image: wurstmeister/kafka:1.1.0
        env:
         - name: KAFKA_BROKER_ID
           value: "1"
#         - name: KAFKA_ADVERTISED_HOST_NAME
#           value: "kafkaservice"
         - name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
           value: "INTERNAL_PLAINTEXT:PLAINTEXT,EXTERNAL_PLAINTEXT:PLAINTEXT"
         - name: KAFKA_LISTENERS
           value: "INTERNAL_PLAINTEXT://0.0.0.0:9092,EXTERNAL_PLAINTEXT://0.0.0.0:9093"
         - name: KAFKA_ADVERTISED_LISTENERS
           value: "INTERNAL_PLAINTEXT://kafkaservice:9092,EXTERNAL_PLAINTEXT://192.168.3.102:9093"
         - name: KAFKA_INTER_BROKER_LISTENER_NAME
           value: "INTERNAL_PLAINTEXT"
         - name: KAFKA_CREATE_TOPICS
           value: "topic-1:100:1,topic-2:1:1"  
         - name: KAFKA_ZOOKEEPER_CONNECT
           value: "zookeeper:2181"
        ports: 
        - name: port9092
          containerPort: 9092
        - name: port9093
          containerPort: 9093
        volumeMounts:
        - mountPath: /kafka/kafka-logs-kafkaservice
          name: kafka-volume
      volumes:
      - name: kafka-volume
        hostPath:
          path: /home/volume/kafka-logs

---

apiVersion: v1
kind: Service
metadata:
  namespace: casb-deployment
  name: kafkaservice
  labels:
    app: kafkaservice
spec:
  selector:
    app: kafkaservice
  ports:
  - name: port9092
    port: 9092
    targetPort: 9092
    protocol: TCP

---

apiVersion: v1
kind: Service
metadata:
  namespace: casb-deployment
  name: kafkaservice-external
  labels:
    app: kafkaservice-external
spec:
  selector:
    app: kafkaservice
  ports:
  - name: port9093
    port: 9093
    protocol: TCP
    nodePort: 30035
  type: NodePort
</code></pre>

<p>I am able to ping the VM i.e. the kubernetes node from my local machine <code>ping 192.168.3.102</code> and I am using nodePort to expose the service.</p>

<p>I can also telnet <code>telnet 192.168.3.102 30035</code> and it gives:</p>

<pre><code>Trying 192.168.3.102...
Connected to 192.168.3.102.
Escape character is '^]'.
</code></pre>

<p>I tried running kafka console consumer and producer from my local machine:</p>

<p><strong>Consumer:</strong>
    bin/kafka-console-consumer.sh --bootstrap-server 192.168.3.102:30035 --topic topic-1 --from-beginning</p>

<p><strong>Output:</strong></p>

<pre><code>[2019-09-25 12:30:40,716] WARN [Consumer clientId=consumer-1, groupId=console-consumer-20551] Connection to node 1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
</code></pre>

<p><strong>Producer:</strong></p>

<pre><code>bin/kafka-console-producer.sh --broker-list 192.168.3.102:30035 --topic topic-1
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>[2019-09-25 12:32:07,958] WARN [Producer clientId=console-producer] Connection to node 1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
</code></pre>

<p><strong>Netstat:</strong></p>

<pre><code>netstat -tulpn | grep 30035
tcp6       0      0 :::30035                :::*                    LISTEN      113545/kube-proxy
</code></pre>

<p>I tried running a python based consumer i.e <a href="https://pypi.org/project/kafka-python/" rel="nofollow noreferrer">kafka-python==1.4.2</a> it gave me the following logs:</p>

<pre><code>[2019-09-25T12:15:39+0500] INFO kafka.client Bootstrapping cluster metadata from [('192.168.3.102', 30035, &lt;AddressFamily.AF_INET: 2&gt;)]
[2019-09-25T12:15:39+0500] INFO kafka.conn &lt;BrokerConnection node_id=bootstrap host=192.168.3.102:30035 &lt;connecting&gt; [IPv4 ('192.168.3.102', 30035)]&gt;: connecting to 192.168.3.102:30035 [('192.168.3.102', 30035) IPv4]
[2019-09-25T12:15:39+0500] INFO kafka.conn &lt;BrokerConnection node_id=bootstrap host=192.168.3.102:30035 &lt;connecting&gt; [IPv4 ('192.168.3.102', 30035)]&gt;: Connection complete.
[2019-09-25T12:15:39+0500] INFO kafka.client Bootstrap succeeded: found 1 brokers and 26 topics.
[2019-09-25T12:15:39+0500] INFO kafka.conn &lt;BrokerConnection node_id=bootstrap host=192.168.3.102:30035 &lt;connected&gt; [IPv4 ('192.168.3.102', 30035)]&gt;: Closing connection. 
[2019-09-25T12:15:39+0500] INFO kafka.conn &lt;BrokerConnection node_id=1 host=192.168.3.102:9093 &lt;connecting&gt; [IPv4 ('192.168.3.102', 9093)]&gt;: connecting to 192.168.3.102:9093 [('192.168.3.102', 9093) IPv4]
[2019-09-25T12:15:39+0500] INFO kafka.conn Probing node 1 broker version
[2019-09-25T12:15:39+0500] ERROR kafka.conn Connect attempt to &lt;BrokerConnection node_id=1 host=192.168.3.102:9093 &lt;connecting&gt; [IPv4 ('192.168.3.102', 9093)]&gt; returned error 111. Disconnecting.
[2019-09-25T12:15:39+0500] INFO kafka.conn &lt;BrokerConnection node_id=1 host=192.168.3.102:9093 &lt;connecting&gt; [IPv4 ('192.168.3.102', 9093)]&gt;: Closing connection. ConnectionError: 111 ECONNREFUSED
[2019-09-25T12:15:40+0500] INFO kafka.conn &lt;BrokerConnection node_id=1 host=192.168.3.102:9093 &lt;connecting&gt; [IPv4 ('192.168.3.102', 9093)]&gt;: connecting to 192.168.3.102:9093 [('192.168.3.102', 9093) IPv4]
[2019-09-25T12:15:40+0500] ERROR kafka.conn Connect attempt to &lt;BrokerConnection node_id=1 host=192.168.3.102:9093 &lt;connecting&gt; [IPv4 ('192.168.3.102', 9093)]&gt; returned error 111. Disconnecting.
[2019-09-25T12:15:40+0500] INFO kafka.conn &lt;BrokerConnection node_id=1 host=192.168.3.102:9093 &lt;connecting&gt; [IPv4 ('192.168.3.102', 9093)]&gt;: Closing connection. ConnectionError: 111 ECONNREFUSED
[2019-09-25T12:15:40+0500] INFO Activity URL collector Exception in activity url collector: NoBrokersAvailable
</code></pre>

<p>From the logs it seems like the connection was made i.e.</p>

<pre><code>&lt;connecting&gt; [IPv4 ('192.168.3.102', 30035)]&gt;: Connection complete.
Bootstrap succeeded: found 1 brokers and 26 topics.
</code></pre>

<p>But then it got disconnected.</p>

<p>Please help me out in figuring out what am I missing and how can I resolve this. Thanks.</p>

el323
<p>You're bootstrapping to port 30035, and getting the initial connection, then the advertised port of 9093 is being returned for subsequent connections, not 30035.</p>

<p>You need the NodePort and advertised one to be the same, or at least both be externally routable. Then you additionally need to have port forwarding on your VM if your code is running on your host machine </p>

<p>Note: Confluent or Strimzi Helm Charts exist for setting up Kafka in Kubernetes </p>
