Setting up Apache Spark on Kubernetes with microk8s

Versions: Apache Spark 2.4.4

When I discovered microk8s I was delighted! An easy installation in very few steps and you can start to play with Kubernetes locally (tried on Ubuntu 16). However, running Apache Spark 2.4.4 on top of microk8s is not an easy piece of cake. In this post I will show you 4 different problems you may encounter, and propose possible solutions.

Looking for a better data engineering position and skills?

You have been working as a data engineer but feel stuck? You don't have any new challenges and are still writing the same jobs all over again? You have now different options. You can try to look for a new job, now or later, or learn from the others! "Become a Better Data Engineer" initiative is one of these places where you can find online learning resources where the theory meets the practice. They will help you prepare maybe for the next job, or at least, improve your current skillset without looking for something else.

👉 I'm interested in improving my data engineering skillset

See you there, Bartosz

microk8s installation is pretty well explained on its official documentation (link in Further reading section), so I won't cover it here. Let's suppose it's up and running. But unfortunately, it isn't enough to make Apache Spark application running :( In the next 4 sections I will show you the problems I encountered during testing SparkPi on Kubernetes locally.

PKIX path building failed: unable to find valid certification path to requested target

Throughout this article we'll try to submit a new Apache Spark job with that command:

 ./bin/spark-submit  --master k8s://  --deploy-mode cluster  --name spark-pi --class org.apache.spark.examples.SparkPi --conf spark.executor.instances=2  --conf spark.kubernetes.container.image=waitingforcode_spark:v0.2_spark2.4.4   --conf --conf spark.kubernetes.authenticate.driver.serviceAccountName=spark local:///opt/spark/examples/target/original-spark-examples_2.11-2.4.4.jar  5

If you setup it like this, there is a big risk that it doesn't work because of this exception (truncated for readability):

Exception in thread "kubernetes-dispatcher-0" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@38727517 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@2a3f563d[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(
    at java.util.concurrent.ThreadPoolExecutor.reject(
    at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(
    at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(
    at java.util.concurrent.ScheduledThreadPoolExecutor.submit(
Exception in thread "main" io.fabric8.kubernetes.client.KubernetesClientException: Failed to start websocket
    at io.fabric8.kubernetes.client.dsl.internal.WatchConnectionManager$2.onFailure(
    at okhttp3.RealCall$AsyncCall.execute(
Caused by: PKIX path building failed: unable to find valid certification path to requested target
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(
    at okhttp3.internal.connection.StreamAllocation.newStream(
    at okhttp3.internal.connection.ConnectInterceptor.intercept(
    at okhttp3.internal.http.RealInterceptorChain.proceed(
    at okhttp3.internal.http.RealInterceptorChain.proceed(
    at okhttp3.internal.cache.CacheInterceptor.intercept(
    ... 4 more
Caused by: PKIX path building failed: unable to find valid certification path to requested target

According to microk8s documentation about ports, our master's port (16443) is SSL encrypted. The exception clearly points out a problem with the provided certificates, or rather the lack of them. Fortunately, the community behind Spark on Kubernetes thought about that case and added some authentication parameter to spark-submit command. The one that will solve our problem is called spark.kubernetes.authenticate.submission.caCertFile which provides CA cert file when we start the driver.

Thanks to caCertFile, our spark-submit command should look like --conf spark.kubernetes.authenticate.caCertFile=/var/snap/microk8s/current/certs/ca.crt and execute correctly on Kubernetes. Or, almost correctly.


I wrote "almost correctly" because you should now get an authorization exception like:

Exception in thread "main" io.fabric8.kubernetes.client.KubernetesClientException: Unauthorized
    at io.fabric8.kubernetes.client.dsl.internal.WatchConnectionManager$2.onFailure(
    at okhttp3.RealCall$AsyncCall.execute(
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$

The problem indicates some issues with authentication against Kubernetes API server. According to the Kubernetes documentation, "Kubernetes uses client certificates, bearer tokens, an authenticating proxy, or HTTP basic auth to authenticate API requests through authentication plugins". From that you can easily find another Spark configuration property to overcome the issue, called spark.kubernetes.authenticate.submission.oauthToken. It defines OAuth token to use for Kubernetes API authorization. To retrieve your token, get first the default-token by looking for a name starting with "default-token-":

bartosz:/data/spark-2.4.4$ microk8s.kubectl -n kube-system get secret
NAME                                             TYPE                                  DATA   AGE
attachdetach-controller-token-2462t       3      2d3h
certificate-controller-token-d2msj        3      2d3h
clusterrole-aggregation-controller-token-kgkh5   3      2d3h
coredns-token-5xwj5                       3      52m
cronjob-controller-token-v9lj5            3      2d3h
daemon-set-controller-token-mswf4         3      2d3h

default-token-cndqq                       3      2d3h

After that, issue microk8s.kubectl -n kube-system describe secret default-token-cndqq:

bartosz:/data/spark-2.4.4$ microk8s.kubectl -n kube-system describe secret default-token-cndqq
Name:         default-token-cndqq
Namespace:    kube-system
Annotations: default


ca.crt:     1103 bytes
namespace:  11 bytes
token:      MY_TOKEN

After that, spark-submit should have an extra parameter --conf spark.kubernetes.authenticate.submission.oauthToken=MY_TOKEN.

UnknownHostException: kubernetes.default.svc: Try again

After adding 2 properties to spark-submit we're able to send the job to Kubernetes. However, the server can not be able to execute the request successfully. It happened to me because of this exception (truncated for readability):

20/01/10 16:36:31 ERROR SparkContext: Error initializing SparkContext.
org.apache.spark.SparkException: External scheduler cannot be instantiated
    at org.apache.spark.SparkContext$.org$apache$spark$SparkContext$$createTaskScheduler(SparkContext.scala:2794)
    at org.apache.spark.SparkContext.(SparkContext.scala:493)
    at org.apache.spark.SparkContext$.getOrCreate(SparkContext.scala:2520)
    at org.apache.spark.sql.SparkSession$Builder$$anonfun$7.apply(SparkSession.scala:935)
Caused by: io.fabric8.kubernetes.client.KubernetesClientException: Operation: [get]  for kind: [Pod]  with name: [spark-pi-1578674176026-driver]  in namespace: [default]  failed.
    at io.fabric8.kubernetes.client.KubernetesClientException.launderThrowable(
    at io.fabric8.kubernetes.client.KubernetesClientException.launderThrowable(
    at io.fabric8.kubernetes.client.dsl.base.BaseOperation.getMandatory(
Caused by: kubernetes.default.svc: Try again
    at Method)

After some digging, I found a potential solution in Cannot resolve pod ip address #188 issue on Github. What helped me to solve the issue was a simple disable/enable of the DNS:

bartosz:/data/spark-2.4.4$ microk8s.disable dns
Disabling DNS
Reconfiguring kubelet
Removing DNS manifest
[sudo] password for bartosz:
DNS is disabled

bartosz:/data/spark-2.4.4$ microk8s.enable dns
Enabling DNS
Applying manifest
serviceaccount/coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created created created
Restarting kubelet
DNS is enabled

To check if it worked, you can use busybox and test nslookup against kubernetes.default, like here:

bartosz:/data/spark-2.4.4$ microk8s.kubectl run -i --tty --image busybox:1.28.4 dns-test2 --restart=Never --rm /bin/sh

nslookup kubernetes.default
Address 1: kube-dns.kube-system.svc.cluster.local

Name:      kubernetes.default
Address 1: kubernetes.default.svc.cluster.local

Bad Kubernetes client version for Apache Spark 2.4.4

I thought that the unknown host error was the last one but unfortunately, no. After resubmitting the application with spark-submit, I got a new exception:

20/01/10 17:30:50 INFO SparkContext: Added JAR file:///opt/spark/examples/target/original-spark-examples_2.11-2.4.4.jar at spark://spark-pi-1578677440483-driver-svc.default.svc:7078/jars/original-spark-examples_2.11-2.4.4.jar with timestamp 1578677450586
20/01/10 17:30:54 INFO ExecutorPodsAllocator: Going to request 2 executors from Kubernetes.
20/01/10 17:30:54 WARN WatchConnectionManager: Exec Failure: HTTP 403, Status: 403 - Expected HTTP 101 response but was '403 Forbidden'
    at okhttp3.RealCall$AsyncCall.execute(
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$
20/01/10 17:30:54 WARN ExecutorPodsWatchSnapshotSource: Kubernetes client has been closed (this is expected if the application is shutting down.)

Once again, after searching a little, I found Andy Grove's article EKS security patches cause Apache Spark jobs to fail with permissions error explaining that a security fix caused some regressions on Apache Spark 2.4.4. Fortunately, Andy gives 3 possible solutions to overcome the issue and the one which worked for me was the Option 2 where I set io.fabric8 dependency in resource-managers/kubernetes/core/pom.xml to 4.4.2:


After recompiling the project and rebuilding the Apache Spark image with, I was finally able to see my spark-submit working!

As of this writing, Kubernetes resource manager is still marked as an experimental feature and as you can see in this article, it has still some issues making its use difficult for beginners. However, Kubernetes and Spark on Kubernetes community is very active and finding answers to our questions is very often only a matter of time.