Prerequisites
Kubernetes Cluster
A running Kubernetes cluster at version >= 1.33 with access configured using kubectl
Permissions
Appropriate permissions to list, create, edit and delete pods in your cluster
DNS
Kubernetes DNS configured in your cluster
Docker Images
Container images for Spark driver and executor pods
If you donβt have a Kubernetes cluster, you can set up a test cluster on your local machine using minikube. We recommend 3 CPUs and 4g of memory to be able to start a simple Spark application with a single executor.
How It Works
spark-submit can be directly used to submit a Spark application to a Kubernetes cluster. The submission mechanism works as follows:
- Spark creates a Spark driver running within a Kubernetes pod
- The driver creates executors which are also running within Kubernetes pods and connects to them, and executes application code
- When the application completes, the executor pods terminate and are cleaned up, but the driver pod persists logs and remains in βcompletedβ state until itβs eventually garbage collected or manually cleaned up
In the completed state, the driver pod does not use any computational or memory resources.
Docker Images
Kubernetes requires users to supply images that can be deployed into containers within pods. Spark ships with a Dockerfile that can be used for this purpose, or customized to match an individual applicationβs needs. It can be found in thekubernetes/dockerfiles/ directory.
Spark also ships with a bin/docker-image-tool.sh script that can be used to build and publish the Docker images:
Language-Specific Images
By defaultbin/docker-image-tool.sh builds docker image for running JVM jobs. You need to opt-in to build additional language binding docker images:
apache/spark:<version>) directly.
Submitting Applications to Kubernetes
Cluster Mode
To launch Spark Pi in cluster mode:The Kubernetes API server URL with format
k8s://<api_server_host>:<k8s-apiserver-port>. The port must always be specified, even if itβs the HTTPS port 443. If no HTTP protocol is specified, it defaults to https.Must be
cluster for Kubernetes modeContainer image to use for the Spark application (e.g.,
apache/spark:v3.5.0)Finding the API Server URL
One way to discover the apiserver URL is by executingkubectl cluster-info:
kubectl proxy:
--master k8s://http://127.0.0.1:8001 as the argument to spark-submit.
Client Mode
Starting with Spark 2.4.0, it is possible to run Spark applications on Kubernetes in client mode. When your application runs in client mode, the driver can run inside a pod or on a physical host.Client Mode Networking
Client Mode Networking
Spark executors must be able to connect to the Spark driver over a hostname and a port that is routable from the Spark executors. When running your driver inside a Kubernetes pod, you can use a headless service to allow your driver pod to be routable from the executors.Specify the driverβs hostname via
spark.driver.host and your spark driverβs port to spark.driver.port.Executor Pod Garbage Collection
Executor Pod Garbage Collection
If you run your Spark driver in a pod, it is highly recommended to set
spark.kubernetes.driver.pod.name to the name of that pod. When this property is set, the Spark scheduler will deploy the executor pods with an OwnerReference, which in turn will ensure that once the driver pod is deleted from the cluster, all of the applicationβs executor pods will also be deleted.Dependency Management
If your applicationβs dependencies are all hosted in remote locations like HDFS or HTTP servers, they may be referred to by their appropriate remote URIs. Application dependencies can also be pre-mounted into custom-built Docker images. Dependencies can be added to the classpath by referencing them withlocal:// URIs and/or setting the SPARK_EXTRA_CLASSPATH environment variable in your Dockerfiles.
Using S3 for Dependencies
A typical example using S3:Secret Management
Kubernetes Secrets can be used to provide credentials for a Spark application to access secured services.Mounting Secrets as Files
To mount a secret into the driver and executor containers:Using Secrets as Environment Variables
To use a secret through an environment variable:Pod Template
Kubernetes allows defining pods from template files. Spark users can similarly use template files to define the driver or executor pod configurations that Spark configurations do not support.The executor pod template file will be automatically mounted onto a volume in the driver pod when itβs created.
Using Kubernetes Volumes
Users can mount the following types of Kubernetes volumes into the driver and executor pods:- hostPath: mounts a file or directory from the host nodeβs filesystem into a pod
- emptyDir: an initially empty volume created when a pod is assigned to a node
- nfs: mounts an existing NFS (Network File System) into a pod
- persistentVolumeClaim: mounts a
PersistentVolumeinto a pod
Mounting Volumes
To mount a volume into the driver pod:Example: NFS Volume
Example: Persistent Volume Claim
On-Demand PVC
You can mount a dynamically-created persistent volume claim per executor by usingOnDemand as a claim name:
Local Storage
Spark supports using volumes to spill data during shuffles and other operations. To use a volume as local storage, the volumeβs name should start withspark-local-dir-:
Introspection and Debugging
Accessing Logs
Logs can be accessed using the Kubernetes API and thekubectl CLI:
Accessing Driver UI
The UI associated with any application can be accessed locally usingkubectl port-forward:
http://localhost:4040.
Debugging
To get basic information about the scheduling decisions made around the driver pod:Deleting the driver pod will clean up the entire Spark application, including all executors, associated service, etc. The driver pod can be thought of as the Kubernetes representation of the Spark application.
RBAC
In Kubernetes clusters with RBAC enabled, users can configure Kubernetes RBAC roles and service accounts used by the various Spark on Kubernetes components. The Spark driver pod uses a Kubernetes service account to access the Kubernetes API server to create and watch executor pods. The service account must have the appropriate permissions.Creating a Service Account
Creating a Role Binding
Using the Service Account
Application Management
Kubernetes provides simple application management via the spark-submit CLI tool in cluster mode.Killing an Application
Checking Application Status
Using Glob Patterns
You can specify the grace period for pod termination via the
spark.kubernetes.appKillPodDeletionGracePeriod property (default is 30 seconds).