<p>I  successfully deployed Kafka to Kubernetes on local Docker (gcp &amp; minikube) using <a href="https://github.com/Yolean/kubernetes-kafka" rel="nofollow noreferrer">Yolean/kubernetes-kafka</a> &amp; <a href="https://github.com/helm/charts/tree/master/incubator/kafka" rel="nofollow noreferrer">Helm chart</a></p>

<p>and tested topic production successfully from within the cluster using this python script:</p>

<pre><code>#!/usr/bin/env python

from kafka import KafkaConsumer, KafkaProducer

KAFKA_TOPIC = 'demo'
# KAFKA_BROKERS = 'localhost:32400' # see step 1

# from inside the cluster in a different namespace
# KAFKA_BROKERS = 'bootstrap.kafka.svc.cluster.local:9092'

KAFKA_BROKERS = 'kafka.kafka.svc.cluster.local:9092'

print('KAFKA_BROKERS: ' + KAFKA_BROKERS)

producer = KafkaProducer(bootstrap_servers=KAFKA_BROKERS)


messages = [b'hello kafka', b'Falanga', b'3 test messages']


for m in messages:
    print(f"sending: {m}")
    producer.send(KAFKA_TOPIC, m)

producer.flush()
</code></pre>

<p>On helm I used this option to enable external use:</p>

<pre><code>helm install --name kafka --set external.enabled=true --namespace kafka incubator/kafka
</code></pre>

<p>and on the original repo I used:</p>

<pre><code>kubectl apply -f ./outside-0.yml
</code></pre>

<p>The resulting services have endpoints and node ports but the script doesn't work from outside the cluster.</p>

<p>here is the original service (branch master)</p>

<pre><code>➜  ~ kubectl describe svc outside-0 --namespace kafka
Name:                     outside-0
Namespace:                kafka
Labels:                   &lt;none&gt;
Annotations:              kubectl.kubernetes.io/last-applied-        configuration={"apiVersion":"v1","kind":"Service","metadata":    {"annotations":{},"name":"outside-0","namespace":"kafka"},"spec":{"ports":    [{"nodePort":32400,"port":3240...
Selector:                 app=kafka,kafka-broker-id=0
Type:                     NodePort
IP:                       10.99.171.133
LoadBalancer Ingress:     localhost
Port:                     &lt;unset&gt;  32400/TCP
TargetPort:               9094/TCP
NodePort:                 &lt;unset&gt;  32400/TCP
Endpoints:                10.1.3.63:9094
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   &lt;none&gt;
</code></pre>

<p>here is the helm service description:</p>

<pre><code>Name:                     kafka-0-external
Namespace:                kafka
Labels:                   app=kafka
                          chart=kafka-0.9.2
                          heritage=Tiller
                          pod=kafka-0
                          release=kafka
Annotations:                  dns.alpha.kubernetes.io/internal=kafka.cluster.local
                      external-    dns.alpha.kubernetes.io/hostname=kafka.cluster.local
Selector:                 app=kafka,pod=kafka-0,release=kafka
Type:                     NodePort
IP:                       10.103.70.223
LoadBalancer Ingress:     localhost
Port:                     external-broker  19092/TCP
TargetPort:               31090/TCP
NodePort:                 external-broker  31090/TCP
Endpoints:                10.1.2.231:31090
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   &lt;none&gt;
</code></pre>

<p>The local docker node does not have an externalIP field:</p>

<pre><code>kubectl describe node docker-for-desktop | grep IP
InternalIP:  192.168.65.3
</code></pre>

<p>I followed the instruction on the outside <a href="https://github.com/Yolean/kubernetes-kafka/blob/master/outside-services/README.md" rel="nofollow noreferrer">Readme</a> i.e.</p>

<ol>
<li>add hostPort to 50kafka statefullset 9094 port</li>
<li>add node port discovery in 10broker-config</li>
</ol>

<p>&amp; discovered that the local docker node has no externalIP field </p>

<p>How can I connect to kafka from outside the cluster on docker? 
Does this work on GKE or other deployments?</p>

<p>The service is exposing the pod to the internal Kubernetes network. In order to expose the service (which exposes the pod) to the internet, you need to set up an Ingress that points to the service.</p>

<p>Ingresses are basically the equivalent of Apache/Nginx for Kubernetes. You can read up on how to do it at the following URL:</p>

<p><a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/ingress/</a></p>

<p>Alternatively, you can expose a pod on the node network by defining the <code>service type</code> as a <code>NodePort</code> and assigning your specific port to it. It should be something like the following:</p>

<p><code>
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 31090
      name: http
</code></p>
