-8.9 C
New York
Monday, December 23, 2024

Use Batch Processing Gateway to automate job administration in multi-cluster Amazon EMR on EKS environments


AWS clients usually course of petabytes of knowledge utilizing Amazon EMR on EKS. In enterprise environments with numerous workloads or various operational necessities, clients ceaselessly select a multi-cluster setup as a result of following benefits:

  • Higher resiliency and no single level of failure – If one cluster fails, different clusters can proceed processing crucial workloads, sustaining enterprise continuity
  • Higher safety and isolation – Elevated isolation between jobs enhances safety and simplifies compliance
  • Higher scalability – Distributing workloads throughout clusters allows horizontal scaling to deal with peak calls for
  • Efficiency advantages – Minimizing Kubernetes scheduling delays and community bandwidth rivalry improves job runtimes
  • Elevated flexibility – You possibly can get pleasure from easy experimentation and value optimization by way of workload segregation to a number of clusters

Nevertheless, one of many disadvantages of a multi-cluster setup is that there is no such thing as a easy technique to distribute workloads and help efficient load balancing throughout a number of clusters. This publish proposes an answer to this problem by introducing the Batch Processing Gateway (BPG), a centralized gateway that automates job administration and routing in multi-cluster environments.

Challenges with multi-cluster environments

In a multi-cluster atmosphere, Spark jobs on Amazon EMR on EKS should be submitted to totally different clusters from numerous purchasers. This structure introduces a number of key challenges:

  • Endpoint administration – Purchasers should preserve and replace connections for every goal cluster
  • Operational overhead – Managing a number of shopper connections individually will increase the complexity and operational burden
  • Workload distribution – There is no such thing as a built-in mechanism for job routing throughout a number of clusters, which impacts configuration, useful resource allocation, price transparency, and resilience
  • Resilience and excessive availability – With out load balancing, the atmosphere lacks fault tolerance and excessive availability

BPG addresses these challenges by offering a single level of submission for Spark jobs. BPG automates job routing to the suitable EMR on EKS clusters, offering efficient load balancing, simplified endpoint administration, and improved resilience. The proposed answer is especially useful for purchasers with multi-cluster Amazon EMR on EKS setups utilizing the Spark Kubernetes Operator with or with out Yunikorn scheduler.

Nevertheless, though BPG presents vital advantages, it’s at the moment designed to work solely with Spark Kubernetes Operator. Moreover, BPG has not been examined with the Volcano scheduler, and the answer shouldn’t be relevant in environments utilizing native Amazon EMR on EKS APIs.

Answer overview

Martin Fowler describes a gateway as an object that encapsulates entry to an exterior system or useful resource. On this case, the useful resource is the EMR on EKS clusters operating Spark. A gateway acts as a single level to confront this useful resource. Any code or connection interacts with the interface of the gateway solely. The gateway then interprets the incoming API request into the API provided by the useful resource.

BPG is a gateway particularly designed to offer a seamless interface to Spark on Kubernetes. It’s a REST API service to summary the underlying Spark on EKS clusters particulars from customers. It runs in its personal EKS cluster speaking to Kubernetes API servers of various EKS clusters. Spark customers submit an software to BPG by way of purchasers, then BPG routes the applying to one of many underlying EKS clusters.

The method for submitting Spark jobs utilizing BPG for Amazon EMR on EKS is as follows:

  1. The person submits a job to BPG utilizing a shopper.
  2. BPG parses the request, interprets it right into a customized useful resource definition (CRD), and submits the CRD to an EMR on EKS cluster in response to predefined guidelines.
  3. The Spark Kubernetes Operator interprets the job specification and initiates the job on the cluster.
  4. The Kubernetes scheduler schedules and manages the run of the roles.

The next determine illustrates the high-level particulars of BPG. You possibly can learn extra about BPG within the GitHub README.

Image showing the high-level details of Batch Processing Gateway

The proposed answer entails implementing BPG for a number of underlying EMR on EKS clusters, which successfully resolves the drawbacks mentioned earlier. The next diagram illustrates the main points of the answer.

Image showing the end to end architecture of of Batch Processing Gateway

Supply Code

You will discover the code base within the AWS Samples and Batch Processing Gateway GitHub repository.

Within the following sections, we stroll by way of the steps to implement the answer.

Stipulations

Earlier than you deploy this answer, ensure that the next conditions are in place:

Clone the repositories to your native machine

We assume that each one repositories are cloned into the house listing (~/). All relative paths supplied are primarily based on this assumption. In case you have cloned the repositories to a distinct location, modify the paths accordingly.

  1. Clone the BPG on EMR on EKS GitHub repo with the next command:
cd ~/
git clone [email protected]:aws-samples/batch-processing-gateway-on-emr-on-eks.git

The BPG repository is at the moment below lively growth. To offer a steady deployment expertise in step with the supplied directions, now we have pinned the repository to the steady commit hash aa3e5c8be973bee54ac700ada963667e5913c865.

Earlier than cloning the repository, confirm any safety updates and cling to your group’s safety practices.

  1. Clone the BPG GitHub repo with the next command:
git clone [email protected]:apple/batch-processing-gateway.git
cd batch-processing-gateway
git checkout aa3e5c8be973bee54ac700ada963667e5913c865

Create two EMR on EKS clusters

The creation of EMR on EKS clusters shouldn’t be the first focus of this publish. For complete directions, consult with Working Spark jobs with the Spark operator. Nevertheless, on your comfort, now we have included the steps for establishing the EMR on EKS digital clusters named spark-cluster-a-v and spark-cluster-b-v within the GitHub repo. Comply with these steps to create the clusters.

After efficiently finishing the steps, you must have two EMR on EKS digital clusters named spark-cluster-a-v and spark-cluster-b-v operating on the EKS clusters spark-cluster-a and spark-cluster-b, respectively.

To confirm the profitable creation of the clusters, open the Amazon EMR console and select Digital clusters below EMR on EKS within the navigation pane.

Image showing the Amazon EMR on EKS setup

Arrange BPG on Amazon EKS

To arrange BPG on Amazon EKS, full the next steps:

  1. Change to the suitable listing:
cd ~/batch-processing-gateway-on-emr-on-eks/bpg/

  1. Arrange the AWS Area:
export AWS_REGION="<AWS_REGION>"

  1. Create a key pair. Be sure you comply with your group’s greatest practices for key pair administration.
aws ec2 create-key-pair 
--region "$AWS_REGION" 
--key-name ekskp 
--key-type ed25519 
--key-format pem 
--query "KeyMaterial" 
--output textual content > ekskp.pem
chmod 400 ekskp.pem
ssh-keygen -y -f ekskp.pem > eks_publickey.pem
chmod 400 eks_publickey.pem

Now you’re able to create the EKS cluster.

By default, eksctl creates an EKS cluster in devoted digital personal clouds (VPCs). To keep away from reaching the default comfortable restrict on the variety of VPCs in an account, we use the --vpc-public-subnets parameter to create clusters in an current VPC. For this publish, we use the default VPC for deploying the answer. Modify the next code to deploy the answer within the acceptable VPC in accordance together with your group’s greatest practices. For official steering, consult with Create a VPC.

  1. Get the general public subnets on your VPC:
export DEFAULT_FOR_AZ_SUBNET=$(aws ec2 describe-subnets --region "$AWS_REGION" --filters "Identify=default-for-az,Values=true" --query "Subnets[?AvailabilityZone != 'us-east-1e'].SubnetId" | jq -r '. | map(tostring) | be a part of(",")')

  1. Create the cluster:
eksctl create cluster 
--name bpg-cluster 
--region "$AWS_REGION" 
--vpc-public-subnets "$DEFAULT_FOR_AZ_SUBNET" 
--with-oidc 
--ssh-access 
--ssh-public-key eks_publickey.pem 
--instance-types=m5.xlarge 
--managed

  1. On the Amazon EKS console, select Clusters within the navigation pane and verify for the profitable provisioning of the bpg-cluster

Image showing the Amazon EKS based BPG cluster setup

Within the subsequent steps, we make the next adjustments to the prevailing batch-processing-gateway code base:

To your comfort, now we have supplied the up to date recordsdata within the batch-processing-gateway-on-emr-on-eks repository. You possibly can copy these recordsdata into the batch-processing-gateway repository.

  1. Substitute POM xml file:
cp ~/batch-processing-gateway-on-emr-on-eks/bpg/pom.xml ~/batch-processing-gateway/pom.xml

  1. Substitute DAO java file:
cp ~/batch-processing-gateway-on-emr-on-eks/bpg/LogDao.java ~/batch-processing-gateway/src/primary/java/com/apple/spark/core/LogDao.java

  1. Substitute the Dockerfile:
cp ~/batch-processing-gateway-on-emr-on-eks/bpg/Dockerfile ~/batch-processing-gateway/Dockerfile

Now you’re able to construct your Docker picture.

  1. Create a personal Amazon Elastic Container Registry (Amazon ECR) repository:
aws ecr create-repository --repository-name bpg --region "$AWS_REGION"

  1. Get the AWS account ID:
export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output textual content)

  1. Authenticate Docker to your ECR registry:
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com

  1. Construct your Docker picture:
cd ~/batch-processing-gateway/
docker construct 
--platform linux/amd64 
--build-arg VERSION="1.0.0" 
--build-arg BUILD_TIME=$(date -u +"%Y-%m-%dTpercentH:%M:%SZ") 
--build-arg GIT_COMMIT=$(git rev-parse HEAD) 
--progress=plain 
--no-cache 
-t bpg:1.0.0 .

  1. Tag your picture:
docker tag bpg:1.0.0 "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com/bpg:1.0.0

  1. Push the picture to your ECR repository:
docker push "$AWS_ACCOUNT_ID".dkr.ecr."$AWS_REGION".amazonaws.com/bpg:1.0.0

The ImagePullPolicy within the batch-processing-gateway GitHub repo is about to IfNotPresent. Replace the picture tag in case it is advisable replace the picture.

  1. To confirm the profitable creation and add of the Docker picture, open the Amazon ECR console, select Repositories below Personal registry within the navigation pane, and find the bpg repository:

Image showing the Amazon ECR setup

Arrange an Amazon Aurora MySQL database

Full the next steps to arrange an Amazon Aurora MySQL-Appropriate Version database:

  1. Listing all default subnets for the given Availability Zone in a selected format:
DEFAULT_FOR_AZ_SUBNET_RFMT=$(aws ec2 describe-subnets --region "$AWS_REGION" --filters "Identify=default-for-az,Values=true" --query "Subnets[*].SubnetId" | jq -c '.')

  1. Create a subnet group. Seek advice from create-db-subnet-group for extra particulars.
aws rds create-db-subnet-group 
--db-subnet-group-name bpg-rds-subnetgroup 
--db-subnet-group-description "BPG Subnet Group for RDS" 
--subnet-ids "$DEFAULT_FOR_AZ_SUBNET_RFMT" 
--region "$AWS_REGION"

  1. Listing the default VPC:
export DEFAULT_VPC=$(aws ec2 describe-vpcs --region "$AWS_REGION" --filters "Identify=isDefault,Values=true" --query "Vpcs[0].VpcId" --output textual content)

  1. Create a safety group:
aws ec2 create-security-group 
--group-name bpg-rds-securitygroup 
--description "BPG Safety Group for RDS" 
--vpc-id "$DEFAULT_VPC" 
--region "$AWS_REGION"

  1. Listing the bpg-rds-securitygroup safety group ID:
export BPG_RDS_SG=$(aws ec2 describe-security-groups --filters "Identify=group-name,Values=bpg-rds-securitygroup" --query "SecurityGroups[*].GroupId" --output textual content)

  1. Create the Aurora DB Regional cluster. Seek advice from create-db-cluster for extra particulars.
aws rds create-db-cluster 
--database-name bpg 
--db-cluster-identifier bpg 
--engine aurora-mysql 
--engine-version 8.0.mysql_aurora.3.06.1 
--master-username admin 
--manage-master-user-password 
--db-subnet-group-name bpg-rds-subnetgroup 
--vpc-security-group-ids "$BPG_RDS_SG" 
--region "$AWS_REGION"

  1. Create a DB Author occasion within the cluster. Seek advice from create-db-instance for extra particulars.
aws rds create-db-instance 
--db-instance-identifier bpg 
--db-cluster-identifier bpg 
--db-instance-class db.r5.giant 
--engine aurora-mysql 
--region "$AWS_REGION"

  1. To confirm the profitable creation of the RDS Regional cluster and Author occasion, on the Amazon RDS console, select Databases within the navigation pane and verify for the bpg database.

Image showing the RDS setup

Arrange community connectivity

Safety teams for EKS clusters are sometimes related to the nodes and the management aircraft (if utilizing managed nodes). On this part, we configure the networking to permit the node safety group of the bpg-cluster to speak with spark-cluster-a, spark-cluster-b, and the bpg Aurora RDS cluster.

  1. Establish the safety teams of bpg-cluster, spark-cluster-a, spark-cluster-b, and the bpg Aurora RDS cluster:
# Establish Node Safety Group of the bpg-cluster
BPG_CLUSTER_NODEGROUP_SG=$(aws ec2 describe-instances 
--filters Identify=tag:eks:cluster-name,Values=bpg-cluster 
--query "Reservations[*].Situations[*].SecurityGroups[?contains(GroupName, 'eks-cluster-sg-bpg-cluster-')].GroupId" 
--region "$AWS_REGION" 
--output textual content | uniq)

# Establish Cluster safety group of spark-cluster-a and spark-cluster-b
SPARK_A_CLUSTER_SG=$(aws eks describe-cluster --name spark-cluster-a --query "cluster.resourcesVpcConfig.clusterSecurityGroupId" --output textual content)
SPARK_B_CLUSTER_SG=$(aws eks describe-cluster --name spark-cluster-b --query "cluster.resourcesVpcConfig.clusterSecurityGroupId" --output textual content)

# Establish Cluster safety group of bpg Aurora RDS cluster Author Occasion
BPG_RDS_WRITER_SG=$(aws ec2 describe-security-groups --filters "Identify=group-name,Values=bpg-rds-securitygroup" --query "SecurityGroups[*].GroupId" --output textual content)

  1. Enable the node safety group of the bpg-cluster to speak with spark-cluster-a, spark-cluster-b, and the bpg Aurora RDS cluster:
# spark-cluster-a
aws ec2 authorize-security-group-ingress --group-id "$SPARK_A_CLUSTER_SG" --protocol tcp --port 443 --source-group "$BPG_CLUSTER_NODEGROUP_SG"

# spark-cluster-b
aws ec2 authorize-security-group-ingress --group-id "$SPARK_B_CLUSTER_SG" --protocol tcp --port 443 --source-group "$BPG_CLUSTER_NODEGROUP_SG"

# bpg-rds
aws ec2 authorize-security-group-ingress --group-id "$BPG_RDS_WRITER_SG" --protocol tcp --port 3306 --source-group "$BPG_CLUSTER_NODEGROUP_SG"

Deploy BPG

We deploy BPG for weight-based cluster choice. spark-cluster-a-v and spark-cluster-b-v are configured with a queue named dev and weight=50. We count on statistically equal distribution of jobs between the 2 clusters. For extra info, consult with Weight Primarily based Cluster Choice.

  1. Get the bpg-cluster context:
BPG_CLUSTER_CONTEXT=$(kubectl config view --output=json | jq -r '.contexts[] | choose(.title | comprises("bpg-cluster")) | .title')
kubectl config use-context "$BPG_CLUSTER_CONTEXT"

  1. Create a Kubernetes namespace for BPG:
kubectl create namespace bpg

The helm chart for BPG requires a values.yaml file. This file contains numerous key-value pairs for every EMR on EKS clusters, EKS cluster, and Aurora cluster. Manually updating the values.yaml file will be cumbersome. To simplify this course of, we’ve automated the creation of the values.yaml file.

  1. Run the next script to generate the values.yaml file:
cd ~/batch-processing-gateway-on-emr-on-eks/bpg
chmod 755 create-bpg-values-yaml.sh
./create-bpg-values-yaml.sh

  1. Use the next code to deploy the helm chart. Be certain the tag worth in each values.template.yaml and values.yaml matches the Docker picture tag specified earlier.
cp ~/batch-processing-gateway/helm/batch-processing-gateway/values.yaml ~/batch-processing-gateway/helm/batch-processing-gateway/values.yaml.$(date +'%YpercentmpercentdpercentHpercentMpercentS') 
&& cp ~/batch-processing-gateway-on-emr-on-eks/bpg/values.yaml ~/batch-processing-gateway/helm/batch-processing-gateway/values.yaml 
&& cd ~/batch-processing-gateway/helm/batch-processing-gateway/

kubectl config use-context "$BPG_CLUSTER_CONTEXT"

helm set up batch-processing-gateway . --values values.yaml -n bpg

  1. Confirm the deployment by itemizing the pods and viewing the pod logs:
kubectl get pods --namespace bpg
kubectl logs <BPG-PODNAME> --namespace bpg

  1. Exec into the BPG pod and confirm the well being verify:
kubectl exec -it <BPG-PODNAME> -n bpg -- bash 
curl -u admin:admin localhost:8080/skatev2/healthcheck/standing

We get the next output:

{"standing":"OK"}

BPG is efficiently deployed on the EKS cluster.

Check the answer

To check the answer, you possibly can submit a number of Spark jobs by operating the next pattern code a number of instances. The code submits the SparkPi Spark job to the BPG, which in flip submits the roles to the EMR on EKS cluster primarily based on the set parameters.

  1. Set the kubectl context to the bpg cluster:
kubectl config get-contexts | awk 'NR==1 || /bpg-cluster/'
kubectl config use-context "<CONTEXT_NAME>"

  1. Establish the bpg pod title:
kubectl get pods --namespace bpg

  1. Exec into the bpg pod:

kubectl exec -it "<BPG-PODNAME>" -n bpg -- bash

  1. Submit a number of Spark jobs utilizing the curl. Run the beneath curl command to submit jobs to spark-cluster-a and spark-cluster-b:
curl -u person:go localhost:8080/skatev2/spark -i -X POST 
-H 'Content material-Kind: software/json' 
-d '{
"applicationName": "SparkPiDemo",
"queue": "dev",
"sparkVersion": "3.5.0",
"mainApplicationFile": "native:///usr/lib/spark/examples/jars/spark-examples.jar",
"mainClass":"org.apache.spark.examples.SparkPi",
"driver": {
"cores": 1,
"reminiscence": "2g",
"serviceAccount": "emr-containers-sa-spark",
"labels":{
"model": "3.5.0"
}
},
"executor": {
"situations": 1,
"cores": 1,
"reminiscence": "2g",
"labels":{
"model": "3.5.0"
}
}
}'

After every submission, BPG will inform you of the cluster to which the job was submitted. For instance:

HTTP/1.1 200 OK
Date: Sat, 10 Aug 2024 16:17:15 GMT
Content material-Kind: software/json
Content material-Size: 67
{"submissionId":"spark-cluster-a-f72a7ddcfde14f4390194d4027c1e1d6"}
{"submissionId":"spark-cluster-a-d1b359190c7646fa9d704122fbf8c580"}
{"submissionId":"spark-cluster-b-7b61d5d512bb4adeb1dd8a9977d605df"}

  1. Confirm that the roles are operating within the EMR cluster spark-cluster-a and spark-cluster-b:
kubectl config get-contexts | awk 'NR==1 || /spark-cluster-(a|b)/'
kubectl get pods -n spark-operator --context "<CONTEXT_NAME>"

You possibly can view the Spark Driver logs to search out the worth of Pi as proven beneath:

kubectl logs <SPARK-DRIVER-POD-NAME> --namespace spark-operator --context "<CONTEXT_NAME>"

After profitable completion of the job, you must be capable to see the beneath message within the logs:

Pi is roughly 3.1452757263786317

We now have efficiently examined the weight-based routing of Spark jobs throughout a number of clusters.

Clear up

To scrub up your sources, full the next steps:

  1. Delete the EMR on EKS digital cluster:
VIRTUAL_CLUSTER_ID=$(aws emr-containers list-virtual-clusters --region="$AWS_REGION" --query "virtualClusters[?name=='spark-cluster-a-v' && state=='RUNNING'].id" --output textual content)
aws emr-containers delete-virtual-cluster --region="$AWS_REGION" --id "$VIRTUAL_CLUSTER_ID"
VIRTUAL_CLUSTER_ID=$(aws emr-containers list-virtual-clusters --region="$AWS_REGION" --query "virtualClusters[?name=='spark-cluster-b-v' && state=='RUNNING'].id" --output textual content)
aws emr-containers delete-virtual-cluster --region="$AWS_REGION" --id "$VIRTUAL_CLUSTER_ID"

  1. Delete the AWS Identification and Entry Administration (IAM) function:
aws iam delete-role-policy --role-name sparkjobrole --policy-name EMR-Spark-Job-Execution
aws iam delete-role --role-name sparkjobrole

  1. Delete the RDS DB occasion and DB cluster:
aws rds delete-db-instance 
--db-instance-identifier bpg 
--skip-final-snapshot

aws rds delete-db-cluster 
--db-cluster-identifier bpg 
--skip-final-snapshot

  1. Delete the bpg-rds-securitygroup safety group and bpg-rds-subnetgroup subnet group:
BPG_SG=$(aws ec2 describe-security-groups --filters "Identify=group-name,Values=bpg-rds-securitygroup" --query "SecurityGroups[*].GroupId" --output textual content)
aws ec2 delete-security-group --group-id "$BPG_SG"
aws rds delete-db-subnet-group --db-subnet-group-name bpg-rds-subnetgroup

  1. Delete the EKS clusters:
eksctl delete cluster --region="$AWS_REGION" --name=bpg-cluster
eksctl delete cluster --region="$AWS_REGION" --name=spark-cluster-a
eksctl delete cluster --region="$AWS_REGION" --name=spark-cluster-b

  1. Delete bpg ECR repository:
aws ecr delete-repository --repository-name bpg --region="$AWS_REGION" --force

  1. Delete the important thing pairs:
aws ec2 delete-key-pair --key-name ekskp
aws ec2 delete-key-pair --key-name emrkp

Conclusion

On this publish, we explored the challenges related to managing workloads on EMR on EKS cluster and demonstrated the benefits of adopting a multi-cluster deployment sample. We launched Batch Processing Gateway (BPG) as an answer to those challenges, showcasing the way it simplifies job administration, enhances resilience, and improves horizontal scalability in multi-cluster environments. By implementing BPG, we illustrated the sensible software of the gateway structure sample for submitting Spark jobs on Amazon EMR on EKS. This publish offers a complete understanding of the issue, the advantages of the gateway structure, and the steps to implement BPG successfully.

We encourage you to guage your current Spark on Amazon EMR on EKS implementation and think about adopting this answer. It permits customers to submit, study, and delete Spark purposes on Kubernetes with intuitive API calls, while not having to fret in regards to the underlying complexities.

For this publish, we centered on the implementation particulars of the BPG. As a subsequent step, you possibly can discover integrating BPG with purchasers corresponding to Apache Airflow, Amazon Managed Workflows for Apache Airflow (Amazon MWAA), or Jupyter notebooks. BPG works properly with the Apache Yunikorn scheduler. You too can discover integrating BPG to make use of Yunikorn queues for job submission.


In regards to the Authors

Image of Author: Umair NawazUmair Nawaz is a Senior DevOps Architect at Amazon Net Providers. He works on constructing safe architectures and advises enterprises on agile software program supply. He’s motivated to unravel issues strategically by using trendy applied sciences.

Image of Author: Ravikiran RaoRavikiran Rao is a Information Architect at Amazon Net Providers and is enthusiastic about fixing complicated information challenges for numerous clients. Exterior of labor, he’s a theater fanatic and newbie tennis participant.

Image of Author: Sri PotluriSri Potluri is a Cloud Infrastructure Architect at Amazon Net Providers. He’s enthusiastic about fixing complicated issues and delivering well-structured options for numerous clients. His experience spans throughout a variety of cloud applied sciences, guaranteeing scalable and dependable infrastructure tailor-made to every undertaking’s distinctive challenges.

Image of Author: Suvojit DasguptaSuvojit Dasgupta is a Principal Information Architect at Amazon Net Providers. He leads a group of expert engineers in designing and constructing scalable information options for AWS clients. He makes a speciality of creating and implementing progressive information architectures to deal with complicated enterprise challenges.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles