<p>I have a local k3s Kubernetes cluster created by Rancher Desktop.</p>
<p>I am trying to set up Kafka using load balancer based on <a href="https://strimzi.io/blog/2019/05/13/accessing-kafka-part-4/" rel="nofollow noreferrer">this tutorial</a> to make it accessible for clients running outside of Kubernetes.</p>
<p>Here are my steps:</p>
<pre class="lang-bash prettyprint-override"><code>kubectl create namespace hm-kafka
kubectl apply --filename=&quot;https://strimzi.io/install/latest?namespace=hm-kafka&quot; --namespace=hm-kafka
kubectl apply --filename=my-kafka-persistent.yaml --namespace=hm-kafka
</code></pre>
<p><strong>my-kafka-persistent.yaml</strong> (based on <a href="https://strimzi.io/examples/latest/kafka/kafka-persistent.yaml" rel="nofollow noreferrer">kafka-persistent.yaml</a>):</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
  name: hm-kafka
spec:
  kafka:
    version: 3.4.0
    replicas: 3
    listeners:
      - name: plain
        port: 9092
        type: internal
        tls: false
      - name: tls
        port: 9093
        type: internal
        tls: true
      - name: external
        port: 9094
        type: loadbalancer
        tls: false
        configuration:
          brokers:
            - broker: 0
              advertisedPort: 9095
            - broker: 1
              advertisedPort: 9096
            - broker: 2
              advertisedPort: 9097
    config:
      offsets.topic.replication.factor: 3
      transaction.state.log.replication.factor: 3
      transaction.state.log.min.isr: 2
      default.replication.factor: 3
      min.insync.replicas: 2
      inter.broker.protocol.version: &quot;3.4&quot;
    storage:
      type: jbod
      volumes:
        - id: 0
          type: persistent-claim
          size: 100Gi
          deleteClaim: false
  zookeeper:
    replicas: 3
    storage:
      type: persistent-claim
      size: 100Gi
      deleteClaim: false
  entityOperator:
    topicOperator: {}
    userOperator: {}
</code></pre>
<p>When I deploy, I got this issue</p>
<p>Pods:</p>
<p><a href="https://i.stack.imgur.com/sg6mB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sg6mB.png" alt="enter image description here" /></a></p>
<p>Services:</p>
<p><a href="https://i.stack.imgur.com/VBC6c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VBC6c.png" alt="enter image description here" /></a></p>
<p>This is because <code>advertisedPort</code> option in my YAML file doesn’t really change the port used in the load balancer itself. It changes only the port number used in the <code>advertised.listeners</code> Kafka broker configuration parameter.</p>
<p>And they all try to use the port 9094.</p>
<p>I am wondering is there a way I can change load balancer port so that bootstrap and brokers have different ports? Will it help make Kafka work? Thanks!</p>
<p>Also, I read another similar question and its <a href="https://stackoverflow.com/a/69757166/2000548">answer</a>, does it mean which port to use depends on k3s Kubernetes cluster created by Rancher Desktop and we have no control? Thanks!</p>

Hongbo Miao
<p>To allow clients to access the individual brokers, Strimzi needs to create one <code>type: LoadBalancer</code> service for every broker. In addition, it creates one more service targeting all brokers for bootstrapping (that can be disabled). This normally results in Kubernetes provisioning one load balancer per service -&gt; so there is no need to do any complicated hacks with port numbers.</p>
<p>If your environment does not support load balancers like this, maybe the best thing is to try one of the other mechanisms for exposing the Kafka cluster. For example NodePorts or Kubernetes Nginx Ingress.</p>
<p>You can also do this your self if you want:</p>
<ul>
<li>Configure a regular internal listener instead of the <code>type: loadbalancer</code> one</li>
<li>Use the <code>advertisedHost</code> and <code>advertisedPort</code> options to configure the hostname and port you want to use for each broker</li>
<li>Create your own services with any configuration (e.g. port) you want (make sure the services have some custom name and do not have the Strimzi labels to avoid having them deleted or overwritten by Strimzi)</li>
</ul>
