Run a SparkApplication
This page shows how to leverage Kueue’s scheduling and resource management capabilities when running Spark Operator SparkApplication.
This guide is for batch users that have a basic understanding of Kueue. For more information, see Kueue’s overview.
SparkApplicationIntegration is currently an alpha feature and is disabled by default.
To enable it, the SparkApplicationIntegration feature gate needs to be activated, and sparkoperator.k8s.io/sparkapplication must be added as an allowed workload.
Before you begin
Enable SparkApplication integration in Kueue. You can modify Kueue configurations from installed releases to include sparkoperator.k8s.io/sparkapplication as an allowed workload.
Enable the SparkApplicationIntegration feature gate. Check the installation guide for details on feature gate configuration.
Check administer cluster quotas for details on the initial cluster setup.
Check the Spark Operator installation guide.
In order to use SparkApplication integration, you must install Spark Operator v2.4.0 or above.
Please also remember belows:
- You will have to activate namespaces that you will deploy SparkApplication as described in the official installation docs.
- You will have to create spark serviceaccount and attach a proper role beforehand. Please refer to Spark Operator’s Getting Started Guide for details.
In order to use SparkApplication, prior to v0.8.1, you need to restart Kueue after the installation.
You can do it by running: kubectl delete pods -l control-plane=controller-manager -n kueue-system.
Spark Operator definition
a. Queue selection
The target local queue should be specified in the metadata.labels section of the SparkApplication configuration.
metadata:
labels:
kueue.x-k8s.io/queue-name: user-queue
Kueue supports dynamic allocation only for SparkApplications with the kueue.x-k8s.io/elastic-job: "true" annotation,
see Dynamic allocation.
If you set spec.dynamicAllocation.enabled=true in any other SparkApplication, Kueue will reject it in the webhook.
b. Optionally set Suspend field in SparkOperation
spec:
suspend: true
By default, Kueue will set suspend to true via webhook and unsuspend it when the SparkApplication is admitted.
c. Resource requests
Spark, not the Spark Operator, computes the CPU and memory requests of the driver and executor Pods from the SparkApplication configuration. Kueue reproduces this computation to reserve the quota the Pods will actually use:
- CPU:
coreRequestif set, otherwisecores, otherwise1. - Memory:
memory(default1g) plusmemoryOverhead. IfmemoryOverheadis not set, the overhead ismax(memoryOverheadFactor * memory, 384m), wherememoryOverheadFactordefaults to0.1for Java and Scala applications and to0.4for Python and R applications. Executors of Python applications also getspark.executor.pyspark.memory, and executors getspark.memory.offHeap.sizewhenspark.memory.offHeap.enabledistrue.
Memory values use the Java format (for example 512m or 2g). The corresponding properties in
spec.sparkConf (for example spark.driver.cores or spark.kubernetes.memoryOverheadFactor) are
honored with the same precedence as spark-submit applies: typed driver and executor fields take
precedence over spec.sparkConf, which takes precedence over spec.memoryOverheadFactor.
Kueue uses spec.type to decide whether an application is a Python or R application, while Spark
decides based on the extension of spec.mainApplicationFile. Make sure they agree, otherwise the
default memory overhead factor reserved by Kueue will differ from the one Spark applies.
Resources set through Spark Pod template files (spark.kubernetes.driver.podTemplateFile and
spark.kubernetes.executor.podTemplateFile) are not taken into account.
d. Dynamic allocation
Kueue supports Spark dynamic allocation by running the SparkApplication as an elastic workload. To enable it:
Activate the
ElasticJobsViaWorkloadSlicesandElasticJobsViaWorkloadSlicesForSparkApplicationfeature gates.Annotate the SparkApplication with:
metadata: annotations: kueue.x-k8s.io/elastic-job: "true"Enable dynamic allocation in the SparkApplication:
spec: executor: instances: 1 dynamicAllocation: enabled: true minExecutors: 0 maxExecutors: 4You can also set these in
spec.sparkConf, asspark.executor.instancesand thespark.dynamicAllocation.*properties. As for the Pod resources, a typed field takes precedence overspec.sparkConf, and thespec.dynamicAllocationfields apply only whenspec.dynamicAllocation.enabledistrue.Optionally, to admit a scale-up partially when the quota does not fit all new executors, activate the
ElasticJobsViaWorkloadSlicesWithPartialReplicaScaleUpfeature gate and add the annotation:metadata: annotations: kueue.x-k8s.io/elastic-job-scale-up-strategy: "partial"
Kueue then admits the executors that the Spark driver adds or removes while the driver keeps running:
- Kueue adds the
kueue.x-k8s.io/elastic-jobscheduling gate to the executor Pod template, so the executors that the driver creates stay gated until a Workload slice that includes them is admitted. The webhook rejects a SparkApplication that removes this gate. - Until the driver has created its initial executors, the Workload requests the driver and the largest of
the executor instances,
minExecutorsandinitialExecutorsexecutors. After that, the executor count follows thePendingandRunningexecutor Pods of the current submission, kept betweenminExecutorsandmaxExecutors. - When the driver adds executors, Kueue creates a new Workload slice for the larger count and removes the scheduling gate from the new executors once that slice is admitted. When the driver removes executors, Kueue lowers the count of the admitted Workload and releases their quota once those executors stop, so terminating executors keep their quota until then.
- By default, each Workload slice is admitted atomically: its new executors stay gated until all of them fit
in the quota. The driver creates executors one by one, so a larger scale-up may be admitted in several slices.
With the
partialscale-up strategy, Kueue admits as many new executors as fit and removes the scheduling gate from those only. The rest stay gated until more quota is available. The first admission is all-or-nothing, as for other elastic jobs: the SparkApplication stays suspended, without holding quota, until all its initial executors fit.
Dynamic allocation with Kueue has the following limitation:
- The driver may delete and recreate executors that stay gated for longer than
spark.kubernetes.allocation.executor.timeout. The new executors are gated too.
Sample SparkApplication
apiVersion: sparkoperator.k8s.io/v1beta2
kind: SparkApplication
metadata:
name: spark-pi
labels:
kueue.x-k8s.io/queue-name: user-queue
spec:
type: Scala
mode: cluster # spark-operator supports "cluster" mode only
sparkVersion: 4.0.0
image: spark:4.0.0
imagePullPolicy: IfNotPresent
mainClass: org.apache.spark.examples.SparkPi
mainApplicationFile: local:///opt/spark/examples/jars/spark-examples.jar
arguments:
- "50000"
driver:
coreRequest: "1"
memory: 1g # In Java format (e.g. 512m, 2g)
serviceAccount: spark # You need to create this service account beforehand,
# and the service account should have proper role
# ref: https://github.com/kubeflow/spark-operator/blob/master/config/rbac/spark-application-rbac.yaml
executor:
instances: 2
coreRequest: "1"
memory: 1g # In Java format (e.g. 512m, 2g)
deleteOnTermination: false # to keep terminated executor pods for demo purpose
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.