Managing Asynchronous Tasks with SQS and EFS Persistent Storage in Amazon EKS
Run background tasks in a job queue and leverage scalable, multi-availability zone storage.
Step 1: Configure Cluster Environment Variables
Step 2: Verify or Create the IAM Role for Service Accounts
Step 3: Verify the EFS CSI Driver Add-On Is Installed
Step 4: Run the Sample Batch Application
Step 5: Preparing and Deploying the Batch Container
Step 6: Create the Multi-Architecture Image
Step 7: Deploy the Kubernetes Job
Step 8: Enable Permissions for Batch Processing Jobs on SQS
Step 9: Create a Kubernetes Secret
Step 10: Deploy the Kubernetes Job With Queue Integration
Step 11: Create the PersistentVolume and PersistentVolumeClaim for EFS
- Install the latest version of kubectl. To check your version, run:
kubectl version --short
. - Install the latest version of eksctl. To check your version, run:
eksctl info
. - Install Python 3.9+. To check your version, run:
python3 --version
. - Install Docker or any other container engine equivalent to build the container.
About | ||
---|---|---|
✅ AWS Level | Intermediate - 200 | |
⏱ Time to complete | 30 minutes | |
🧩 Prerequisites | - AWS Account | |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? | |
⏰ Last Updated | 2023-09-29 |
- First, confirm that you are operating within the correct cluster context. This ensures that any subsequent commands are sent to the intended Kubernetes cluster. You can verify the current context by executing the following command:
- Define the
CLUSTER_NAME
environment variable for your EKS cluster. Replace the sample value for clusterregion
.
- Define the
CLUSTER_REGION
environment variable for your EKS cluster. Replace the sample value for clusterregion
.
- Define the
ACCOUNT_ID
environment variable for the account associated with your EKS cluster.
- Make sure the required service accounts for this tutorial are correctly set up in your cluster:
- To create a Kubernetes service account for Amazon ECR:
- Check that the EFS CSI driver is installed:
input.csv
file, performs data manipulation using randomization for demonstration, and writes the processed data back to an output.csv
file. This serves as a hands-on introduction before we deploy this application to Amazon ECR and EKS.- Create a Python script named
batch_processing.py
and paste the following contents:
- In the same directory as your Python script, create a file named
input.csv
and paste the following contents:
- Run the Python script:
output.csv
file will be generated, containing the processed data with an additional column for the processed values:- In the same directory as the other files you created, create a
Dockerfile
and paste the following contents:
- Build the Docker image:
- Create a new private Amazon ECR repository:
- Authenticate the Docker CLI to your Amazon ECR registry:
- Tag your container image for the ECR repository:
- Push the tagged image to the ECR repository:
- Create and start new builder instances for the batch service:
- Build and push the images for your batch service to Amazon ECR:
- Verify that the multi-architecture image is in the ECR repository:
- Get the details of your ECR URL:
- Create a Kubernetes Job manifest file named
batch-job.yaml
and paste the following contents. Replace the sample value inimage
with your ECR URL.
- Apply the Job manifest to your EKS cluster:
- Monitor the Job execution:
- Create an Amazon SQS queue that will serve as our job queue:
- Annotate the existing Amazon ECR service account with Amazon SQS permissions.
- Generate an Amazon ECR authorization token:
- Create the Kubernetes Secret called “regcred” in the "default" namespace:
- Create a Kubernetes Job manifest file named
batch-job-queue.yaml
and paste the following contents. Replace the sample values forimage
with your ECR URL andvalue
with your SQS queue URL.
- Apply the Job manifest to your EKS cluster:
- Monitor the Job execution:
- Echo and save your EFS URL for the next step:
- Create a YAML file named
batch-pv-pvc.yaml
and paste the following contents. Replace the sample value forserver
with your EFS URL.
- Apply the PV and PVC to your Kubernetes cluster:
- Create a Kubernetes Job manifest file named
update-batch-job.yaml
and paste the following contents. Replace the sample value inimage
with your ECR URL.
- Apply the Job manifest to your EKS cluster:
- Create a Kubernetes Job Queue manifest file named
update-batch-job-queue.yaml
and paste the following contents. Replace the sample values forimage
with your ECR URL andvalue
with your SQS queue URL.
- Apply the Job Queue manifest to your EKS cluster:
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.