<p>I'm trying to acces kafka from outside kubernetes on my local machine. I'm using spring application to produce events on a topic. This is my deployment file for kafka:</p>
<pre><code>kind: Deployment
metadata:
  name: kafka-broker0
  labels:
    app: kafka
spec:
  replicas: 2
  selector:
    matchLabels:
      app: kafka
      id: &quot;0&quot;
  template:
    metadata:
      labels:
        app: kafka
        id: &quot;0&quot;
    spec:
      containers:
      - name: kafka
        image: wurstmeister/kafka
        ports:
        - containerPort: 9092
        env:
        - name: KAFKA_ADVERTISED_PORT
          value: &quot;30718&quot;
        - name: KAFKA_ADVERTISED_HOST_NAME
          value: 192.168.1.240
        - name: KAFKA_ZOOKEEPER_CONNECT
          value: zoo1:2181
        - name: KAFKA_BROKER_ID
          value: &quot;0&quot;
        - name: KAFKA_CREATE_TOPICS
          value: LaunchScraper:1:1
</code></pre>
<p>And service file id:</p>
<pre><code>kind: Service
metadata:
  name: kafka-services
  labels:
    name: kafka
spec:
  selector:
    app: kafka
    id: &quot;0&quot;
  ports:
    - protocol: TCP
      name: kafka-port
      port: 9092
  type: NodePort
</code></pre>
<p>I've allready created a zookeeper pod on kubernetes. My spring boot application shows this error:</p>
<pre><code>2020-09-25 23:56:29.123  WARN 44324 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient   : [Producer clientId=producer-1] Connection to node -1 (/192.168.1.240:9092) could not be established. Broker may not be available.
</code></pre>

amine ben abdallah
<p>It seems like you've not fixed a nodePort in your service. When you make it to the value you've entered in KAFKA_ADVERTISED_PORT. Also set the KAFKA_ADVERTISED_HOST to your K8s node hostname/DNS.</p>
<p>In the spec for your sevice add <code>nodePort: 30718</code> under the <code>ports</code> entry. Then in your client, try to connect on 30718 port using the node's address or hostname</p>
<p>Also, if you're looking to deploy Kafka on production, I'd recommend using operators like Strimzi <a href="https://Strimzi.io" rel="nofollow noreferrer">https://Strimzi.io</a></p>
