<p>Suppose if I have 3 node Kafka cluster setup. Then how do I expose it outside a cloud using Load Balancer service? I have read reference material but have a few doubts.</p>

<p>Say for example below is a service for a broker</p>

<pre><code>apiVersion: v1 
  kind: Service metadata: 
  name: kafka-0 
  annotations: dns.alpha.kubernetes.io/external: kafka-0.kafka.my.company.com 
  spec: 
    externalTrafficPolicy: Local 
    type: LoadBalancer 
    ports: 
      - port: 9092 
      name: outside 
      targetPort: 9092 
    selector: app: kafka kafka-pod-id: "0"
</code></pre>

<ol>
<li>What is port and targetPort?</li>
<li>Do I setup LoadBalancer service for each of the brokers?</li>
<li>Do these multiple brokers get mapped to single public IP address of cloud LB?</li>
<li>How does a service outside k8s/cloud access individual broker? By using <code>public-ip:port</code>? or by using <code>kafka-&lt;pod-id&gt;.kafka.my.company.com:port</code>?. Also which port is used here? <code>port</code> or <code>targetPort</code>?</li>
<li>How do I specify this configuration in Kafka broker's <code>Advertised.listeners</code> property? As port can be different for services inside k8s cluster and outside it.</li>
</ol>

<p>Please help.</p>

<p>Based on the information you provided I will try give you some answers, eventually give some advise.</p>

<p><strong>1)</strong> <code>port:</code> is the port number which makes a service visible to other services running within the same K8s cluster.  In other words, in case a service wants to invoke another service running within the same Kubernetes cluster, it will be able to do so using port specified against <code>port</code> in the service spec file.</p>

<p><code>targetPort:</code> is the port on the <code>POD</code> where the service is running. Your application needs to be listening for network requests on this port for the service to work.</p>

<p><strong>2/3)</strong>  Each Broker should be exposed as <code>LoadBalancer</code> and be configured as headless service for internal communication. There should be one addiational <code>LoadBalancer</code> with external ip for external connection. </p>

<p>Example of Service</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: kafka-0
  annotations: dns.alpha.kubernetes.io/external: kafka-0.kafka.my.company.com
spec:
  ports:
  - port: 9092
    name: kafka-port
    protocol: TCP
  selector:
    pod-name: kafka-0
  type: LoadBalancer
</code></pre>

<p><strong>4)</strong> You have to use <code>kafka-&lt;pod-id&gt;.kafka.my.company.com:port</code></p>

<p><strong>5)</strong> It should be set to the external addres so that clients can connect to it. <a href="https://rmoff.net/2018/08/02/kafka-listeners-explained/" rel="nofollow noreferrer">This</a> article might help with understanding.</p>

<p>Similar case was on Github, it might help you also - <a href="https://github.com/kow3ns/kubernetes-kafka/issues/3" rel="nofollow noreferrer">https://github.com/kow3ns/kubernetes-kafka/issues/3</a></p>

<p>In addition, You could also think about Ingress - <a href="https://tothepoint.group/blog/accessing-kafka-on-google-kubernetes-engine-from-the-outside-world/" rel="nofollow noreferrer">https://tothepoint.group/blog/accessing-kafka-on-google-kubernetes-engine-from-the-outside-world/</a></p>
