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)))
requirements.txt
pip
autopep8
Flask==3.0.3
gunicorn==22.0.0
Werkzeug==3.0.6
Pillow
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"]
Put those three in a single folder and make the magic happen.
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'.
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"
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
Then run the commands of deployment:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Get the IP address for your endpoint:
kubectl get service image-reducer-service
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
And that's it, simple as pie. It gets you a nice little sample image resizer. Chomp Chomp.
Top comments (0)