DEV Community

CrimsonSpiderShark
CrimsonSpiderShark

Posted on

🟥🕷🦈presents: EKS fun for Data Science (Part 1)

I added the 'fun' to the title because I'm going to get a little silly with it. Just a bit though, there will be plenty of serious stuff in it for you data science sickos.

So, cliffnotes version of the regular spiel on Kubernetes and EKS: Kubernetes is how you get a bunch of small services to work together without one crashing the other, EKS is how you use Kubernetes in AWS. AWS, by volume, is essentially the Internet. Today, we'll deploy a simple image size reduction service in EKS (this is part 1, after all).

Write the Flask code in Python

You don't need it to be Flask or Python, that's just the example I'm going to use. The example looks something like this:

app.py

from flask import Flask, request, send_file
from PIL import Image
import io
import os

app = Flask(__name__)

@app.route('/resize', methods=['POST'])
def resize_image():
    if 'image' not in request.files:
        return "No image file provided", 400

    image_file = request.files['image']

    try:
        img = Image.open(image_file)
        max_size = int(request.form.get('size', 512))

        img.thumbnail((max_size,max_size))
        img_io = io.BytesIO()
        img.save(img_io, 'JPEG')
        img_io.seek(0)

        return send_file(img_io, mimetype='image/jpeg')

    except Exception as e:
        return f"Error processing image: {str(e)}", 500

if __name__ == "__main__":
  app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 3000)))
Enter fullscreen mode Exit fullscreen mode

requirements.txt

pip
autopep8
Flask==3.0.3
gunicorn==22.0.0
Werkzeug==3.0.6
Pillow
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY app.py .

EXPOSE 3000

CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Put those three in a single folder and make the magic happen.

Image description

This image sums it up pretty well. Just a list of commands to setup the things you'll need for the image for your Kubernetes cluster.

Push to your ECR repository

Oh, right. ECR is where you keep all your container images for Docker and Kubernetes services. Forgot to mention than one. But here's the link in AWS where you create one.

I also just noticed this blog is about using images (the non-typical kind) to reduce images (the typical kind).

Anyways, create the repository in your console, then go to the list of repos and click on 'View push commands'.

Image description

Then, in the directory where you have all those previouse files, run the commands you get from than console. And voila, you've got your repo up and running.

Create and test your Kubernetes Cluster

Use this deployment code after modifying it:

  GNU nano 5.8                                                                                     deployment.yaml                                                                                               
apiVersion: apps/v1
kind: Deployment
metadata:
  name: image-reducer
spec:
  replicas: 2
  selector:
    matchLabels:
      app:
  template:
    metadata:
      labels:
        app: image-reducer
    spec:
      containers:
      - name: image-reducer
        image: <your_image_name>
        ports:
        - containerPort: 3000
        resources:
          requests:
            cpu: "100m"
            memory: "256Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
Enter fullscreen mode Exit fullscreen mode

And also one for load balancing:

apiVersion: v1
kind: Service
metadata:
  name: image-reducer-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
  selector:
    app: image-reducer
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

Then run the commands of deployment:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode

Get the IP address for your endpoint:

kubectl get service image-reducer-service
Enter fullscreen mode Exit fullscreen mode

This will give you an IP which you can send a POST request to in the /resize endpoint with an image and get a resized image back.

curl -X POST -F <your_image_path> -F "size=200" http://<your-external-ip>/resize -o resized.jpg
Enter fullscreen mode Exit fullscreen mode

And that's it, simple as pie. It gets you a nice little sample image resizer. Chomp Chomp.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay