<p>I build kafka cluster on kubernetes following this guilde <a href="https://github.com/kubernetes/contrib/tree/master/statefulsets/kafka" rel="nofollow noreferrer">https://github.com/kubernetes/contrib/tree/master/statefulsets/kafka</a>, it works well for sending/producing message from kubernetes. when I try to expose the kafka cluster using NodePort as follows, kafka clients try to consumer/produce the message from the address <strong><em>10.xx.xx.xx:30092</em></strong></p>

<p>will be failed:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: kafka-nodeport
  labels:
    app: kafka
spec:
  type: NodePort
  ports:
  - port: 9092
    nodePort: 30092
    name: server
  selector:
    app: kafka
</code></pre>

<p>Why that happened and How to expose the kafka service?</p>

zulv
<p>The <code>Kafka Statefulsets</code> require headless service for accessing the brokers. You can change the headless service to Type=NodePort and setting the <code>externalTrafficPolicy=Local</code>. This bypasses the internal load balancing of a Service and traffic destined to a specific node on that node port will only work if a Kafka pod is on that node.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: kafka-nodeport
  labels:
    app: kafka
spec:
  externalTrafficPolicy: Local
  type: NodePort
  ports:
  - port: 9092
    nodePort: 30092
    name: server
  selector:
    app: kafka    
</code></pre>
<p>For example, we have two nodes nodeA and nodeB, nodeB is running a kafka pod. nodeA:30092 will not connect but nodeB:30092 will connect to the kafka pod running on nodeB.</p>
<p>Hope this helps.</p>
