<p>I am trying to run a Spark job on a separate master Spark server hosted on kubernetes but port forwarding reports the following error:</p>
<pre class="lang-none prettyprint-override"><code>E0206 19:52:24.846137   14968 portforward.go:400] an error occurred forwarding 7077 -&gt; 7077: error forwarding port 7077 to pod 1cf922cbe9fc820ea861077c030a323f6dffd4b33bb0c354431b4df64e0db413, uid : exit status 1: 2022/02/07 00:52:26 socat[25402] E connect(16, AF=2 127.0.0.1:7077, 16): Connection refused
</code></pre>
<p>My setup is:</p>
<ul>
<li>I am using VS Code with a dev container to manage a setup where I can run Spark applications. I can run local spark jobs when I build my context like so : <code>sc = pyspark.SparkContext(appName=&quot;Pi&quot;)</code></li>
<li>My host computer is running Docker Desktop where I have kubernetes running and used Helm to run the Spark release from Bitnami <a href="https://artifacthub.io/packages/helm/bitnami/spark" rel="nofollow noreferrer">https://artifacthub.io/packages/helm/bitnami/spark</a></li>
<li>The VS Code dev container <strong>can</strong> access the host correctly since I can do <code>curl host.docker.internal:80</code> and I get the Spark web UI status page. The port 80 is forwarded from the host using <code>kubectl port-forward --namespace default --address 0.0.0.0 svc/my-release-spark-master-svc 80:80</code></li>
<li>I am also forwarding the port <code>7077</code> using a similar command <code>kubectl port-forward --address 0.0.0.0 svc/my-release-spark-master-svc 7077:7077</code>.</li>
</ul>
<p>When I create a Spark context like this <code>sc = pyspark.SparkContext(appName=&quot;Pi&quot;, master=&quot;spark://host.docker.internal:7077&quot;)</code> I am expecting Spark to submit jobs to that master. I don't know much about Spark but I have seen a few examples creating a context like this.</p>
<p>When I run the code, I see connections attempts failing at port 7077 of kubernetes port forwarding, so the requests are going through but they are being refused somehow.</p>
<pre class="lang-none prettyprint-override"><code>Handling connection for 7077
E0206 19:52:24.846137   14968 portforward.go:400] an error occurred forwarding 7077 -&gt; 7077: error forwarding port 7077 to pod 1cf922cbe9fc820ea861077c030a323f6dffd4b33bb0c354431b4df64e0db413, uid : exit status 1: 2022/02/07 00:52:26 socat[25402] E connect(16, AF=2 127.0.0.1:7077, 16): Connection refused
</code></pre>
<p>Now, I have no idea why the connections are being refused. I know the Spark server is accepting requests because I can see the Web UI from within the docker dev container. I know that the Spark service is exposing port 7077 because I can do:</p>
<pre class="lang-none prettyprint-override"><code>$ kubectl get services
NAME                          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
kubernetes                    ClusterIP   10.96.0.1        &lt;none&gt;        443/TCP           28h
my-release-spark-headless     ClusterIP   None             &lt;none&gt;        &lt;none&gt;            7h40m
my-release-spark-master-svc   ClusterIP   10.108.109.228   &lt;none&gt;        7077/TCP,80/TCP   7h40m
</code></pre>
<p>Can anyone tell why the connections are refused and how I can successfully configure the Spark master to accept jobs from external callers ?</p>
<p>Example code I am using is:</p>
<pre class="lang-py prettyprint-override"><code>import findspark
findspark.init()

import pyspark
import random

#sc = pyspark.SparkContext(appName=&quot;Pi&quot;, master=&quot;spark://host.docker.internal:7077&quot;)
sc = pyspark.SparkContext(appName=&quot;Pi&quot;)
num_samples = 100000000

def inside(p):
  x, y = random.random(), random.random()
  return x*x + y*y &lt; 1

count = sc.parallelize(range(0, num_samples)).filter(inside).count()

pi = 4 * count / num_samples
print(pi)

sc.stop()
</code></pre>

<p>After tinkering with it a bit more, I noticed this output when launching the helm chart for Apache Spark <code>** IMPORTANT: When submit an application from outside the cluster service type should be set to the NodePort or LoadBalancer. **</code>.</p>
<p>This led me to research a bit more into Kubernetes networking. To submit a job, it is not sufficient to forward port 7077. Instead, the cluster itself needs to have an IP assigned. This requires the helm chart to be launched with the following commands to set Spark config values <code>helm install my-release --set service.type=LoadBalancer --set service.loadBalancerIP=192.168.2.50 bitnami/spark</code>. My host IP address is above and will be reachable by the Docker container.</p>
<p>With the LoadBalancer IP assigned, Spark will run using the example code provided.</p>
<p>Recap: Don't use port forwarding to submit jobs, a Cluster IP needs to be assigned.</p>
