Colocation of Spark Jobs

Apache Spark is an analysis engine for large-scale data processing, which is widely used in Big Data, SQL Analysis and Machine Learning scenarios. This tutorial provides a quick practice guide about running Spark jobs in colocation mode with other latency sensitive applications by Koordinator, which is helpful for improving cluster resource utilization. For more details about how to use, compose, and work with Koordinator colocation, please refer to the Introduction

Requirements

Koordinator Components

Before submitting Spark jobs as colocate mode, you need to ensure all Koordinator components have already been successfully installed. Please follow the step in Installation guide.

Install Kubernetes Operator for Apache Spark

To simplify running of Spark jobs in Cluster, we import the Kubernetes Operator for Apache Spark in this practice, which uses Kubernetes custom resource for managing Spark applications.

With the help of Helm chart, Kubernetes Operator for Apache Spark can be easily installed using the command below.

  1. $ helm install koord-spark-operator ./spark-operator-chart/ --namespace spark-operator

Installing the chart will create a namespace spark-operator and if doesn’t exist, besides, helm will create a spark-operator Deployment and set up RBAC role for it. After the installation, you should see the operator in running successfully by checking the status of helm release.

  1. $ helm status --namespace spark-operator koord-spark-operator

Run Spark Applications with Koordinator

Due to the mechanism that Spark driver pod needs a Kubernetes service account to manage executor pods, the service account must be authorized with appropriate permissions. Run the following command to create namespace spark-demo and service account spark before submitting jobs.

  1. $ kubectl apply -f examples/spark-jobs/service-account.yaml

Next, run the following command to create Colocation Profile so that all pods submitted following in namespace spark-demo will run in colocation mode. See this tutorial to learn more about Colocation Profile.

  1. $ kubectl apply -f examples/spark-jobs/cluster-colocation-profile.yaml

Submit a Spark TC example job to namespace spark-demo with the command:

  1. $ kubectl apply -f examples/spark-jobs/spark-tc-complex.yaml

Then, check the status of Spark application by running the following command.

  1. $ kubectl get sparkapplication -n spark-demo spark-tc-complex

This will show similar content as following:

  1. NAME STATUS ATTEMPTS START FINISH AGE
  2. spark-tc-complex RUNNING 1 2022-03-30T09:11:22Z <no value> 14s

Now, all pods submitted to namespace spark-demo will be switched to colocation mode, check spark-driver pod as below for example. We can see the protocols likekoordinator.sh/qosClass: BE and kubernetes.io/batch-cpu are successfully injected to pod by Colocation Profile.

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. labels:
  5. koordinator.sh/qosClass: BE
  6. spark-role: driver
  7. ...
  8. spec:
  9. containers:
  10. - args:
  11. - driver
  12. - --properties-file
  13. - /opt/spark/conf/spark.properties
  14. - --class
  15. - org.apache.spark.examples.SparkTC
  16. - local:///opt/spark/examples/jars/spark-examples_2.12-3.2.1-tc1.2.jar
  17. resources:
  18. limits:
  19. kubernetes.io/batch-cpu: "1000"
  20. kubernetes.io/batch-memory: 3456Mi
  21. requests:
  22. kubernetes.io/batch-cpu: "1000"
  23. kubernetes.io/batch-memory: 3456Mi
  24. ...

Evaluation

With the help of Koordinator, when pods resource usage is idle, resources already requested can be reallocated to other colocation pods by the overcommitment model, which can significantly improve the resource utilization of cluster.

In our experiment environment, before the Spark job submitted, we can see the cluster allocatable resources run out while the actual resource usage is in low level.

  1. $ kubectl describe node
  2. Allocated resources:
  3. Resource Requests
  4. cpu 7620m (95.25%)
  5. $ kubectl top node
  6. NAME CPU(cores) CPU%
  7. cn-hangzhou.your-node-1 1190m 14.8%
  8. cn-hangzhou.your-node-2 1620m 20.25%

After submit the Spark job in colocation mode, those unused resources will be reallocated through batch priority to Spark pods, so that we can make the cluster a higher utilization level.

  1. $ kubectl top node
  2. NAME CPU(cores) CPU%
  3. cn-hangzhou.your-node-1 4077m 52%
  4. cn-hangzhou.your-node-2 3830m 49%