<p>I am trying to convert my dockerised application for testing a Kafka functionality, to a Kubernetes deployment file.</p>
<p>The docker command for execution of the container which is working as expected is:</p>
<pre><code>docker run --name consumer-1 --network=&quot;host&quot; -dt 56d57e1538d3 pizzaapp_multiconsumer1.py bash
</code></pre>
<p>However, when converting it to the below Kubernetes deployment file and executing it, I am getting a CrashLoopBackOff error on the pods.</p>
<pre><code>spec:
      hostNetwork: true
      containers:
      - name: kafka-consumer
        image: bhuvidockerhub/kafkaproject:v1.0
        imagePullPolicy: IfNotPresent
        args: [&quot;pizzaapp_multiconsumer1.py&quot;, &quot;bash&quot;]
      imagePullSecrets:
      - name: regcred
</code></pre>
<p>On checking the logs of the failed pods I am seeing this error:</p>
<pre><code>Traceback (most recent call last):
  File &quot;//pizzaapp_multiconsumer1.py&quot;, line 12, in &lt;module&gt;
    multiconsume_pizza_messages()
  File &quot;/testconsumer1.py&quot;, line 14, in multiconsume_pizza_messages
    kafka_admin_client: KafkaAdminClient = KafkaAdminClient(
  File &quot;/usr/local/lib/python3.9/site-packages/kafka/admin/client.py&quot;, line 208, in __init__
    self._client = KafkaClient(metrics=self._metrics,
  File &quot;/usr/local/lib/python3.9/site-packages/kafka/client_async.py&quot;, line 244, in __init__
    self.config['api_version'] = self.check_version(timeout=check_timeout)
  File &quot;/usr/local/lib/python3.9/site-packages/kafka/client_async.py&quot;, line 900, in check_version
    raise Errors.NoBrokersAvailable()
kafka.errors.NoBrokersAvailable: NoBrokersAvailable
</code></pre>
<p>But the broker container is already up and running</p>
<pre><code>my-cluster-with-metrics-entity-operator-7d8894b79f-99fwt   3/3     Running   181        27d
my-cluster-with-metrics-kafka-0                            1/1     Running   57         19d
my-cluster-with-metrics-kafka-1                            1/1     Running   5          19h
my-cluster-with-metrics-kafka-2                            1/1     Running   0          27m
my-cluster-with-metrics-kafka-exporter-568968bd5c-mrg7f    1/1     Running   108        27d
</code></pre>
<p>and the corresponding services are also there</p>
<pre><code>my-cluster-with-metrics-kafka-bootstrap            ClusterIP   10.98.78.168     &lt;none&gt;        9091/TCP,9100/TCP                     27d
my-cluster-with-metrics-kafka-brokers              ClusterIP   None             &lt;none&gt;        9090/TCP,9091/TCP,9100/TCP            27d
my-cluster-with-metrics-kafka-external-0           NodePort    10.110.196.75    &lt;none&gt;        9099:30461/TCP                        27d
my-cluster-with-metrics-kafka-external-1           NodePort    10.107.225.187   &lt;none&gt;        9099:32310/TCP                        27d
my-cluster-with-metrics-kafka-external-2           NodePort    10.103.99.151    &lt;none&gt;        9099:31950/TCP                        27d
my-cluster-with-metrics-kafka-external-bootstrap   NodePort    10.98.131.151    &lt;none&gt;        9099:31248/TCP                        27d
</code></pre>
<p>And I have port forwarded the svc port so that the brokers can be found:</p>
<pre><code>kubectl port-forward svc/my-cluster-with-metrics-kafka-external-bootstrap  9099:9099 -n kafka
</code></pre>
<p>And post this when I run the docker command it executes, as expected.</p>
<p>But in K8s even after adding the bash in the args and trying, it still gives no brokers available.</p>
<p>Can anyone suggest what changes shall I try out in the deployment file, so that it works exactly as the successful docker command run as stated above?</p>

Bhuvi
<p>If an application is deployed in K8s, then we don't need port forwarding since there is nothing to expose outside the cluster. When we run things inside K8s, we normally do not access things using localhost. Localhost refers to the pod's container itself.
Therefore, to resolve the above issue, completely removed the localhost reference from the bootstrap server. This was then replaced with the external bootstrap service IP [10.X.X.X:9099] and then executed the K8s deployment file. Following which the producer and consumer pods came up successfully, and this resolved the issue.</p>
