-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Here I will be documenting the Deployment of a Factorial App on Kubernetes Using Google Cloud
This documentation outlines the steps to create and deploy a Python-based Flask application that calculates the factorial of a number. The application is containerized using Docker, pushed to Google Container Registry (GCR), and deployed on a Kubernetes cluster using Google Kubernetes Engine (GKE).
Google Cloud Platform (GCP) account. Google Cloud SDK installed and configured. Docker installed and running. Basic knowledge of Kubernetes and Docker.
Create a Python script named 'factorial.py':
from flask import Flask, request, jsonify
app = Flask(__name__)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
@app.route('/factorial', methods=['GET'])
def calculate_factorial():
try:
num = int(request.args.get('number'))
result = factorial(num)
return jsonify({'number': num, 'factorial': result})
except (ValueError, TypeError):
return jsonify({'error': 'Please provide a valid integer.'}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
To containerize the application, create a 'Dockerfile':
FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install flask
EXPOSE 8080
CMD ["python", "factorial.py"]
Build the Docker Image:
docker build -t gcr.io/YOUR_PROJECT_ID/factorial-app .
Push the Docker Image to Google Container Registry: Authenticate Docker with GCR:
gcloud auth configure-docker
Then push the image:
docker push gcr.io/YOUR_PROJECT_ID/factorial-app
Replace YOUR_PROJECT_ID with your actual Google Cloud project ID.
Create a Kubernetes cluster using Google Kubernetes Engine:
gcloud container clusters create factorial-cluster --zone us-central1-c --num-nodes=3
Get credentials to interact with the cluster:
gcloud container clusters get-credentials factorial-cluster --zone us-central1-c
Create a Deployment YAML file ('deployment.yaml'):
apiVersion: apps/v1
kind: Deployment
metadata:
name: factorial-app-deployment
spec:
replicas: 2
selector:
matchLabels:
app: factorial-app
template:
metadata:
labels:
app: factorial-app
spec:
containers:
- name: factorial-app
image: gcr.io/YOUR_PROJECT_ID/factorial-app
ports:
- containerPort: 8080
Create a Service YAML file ('service.yaml'):
apiVersion: v1
kind: Service
metadata:
name: factorial-app-service
spec:
type: LoadBalancer
selector:
app: factorial-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
Apply the deployment and service configuration:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Check the status of your nodes:
kubectl get nodes
Check the status of the services:
kubectl get services
Once the service has an external IP, you can access the app at:
http://EXTERNAL_IP/factorial?number=5
Replace EXTERNAL_IP with the actual external IP address of the service.
To monitor resource usage of nodes and pods:
- Install Metrics Server:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
- Check Node Resource Consumption:
kubectl top nodes
- Check Pod Resource Consumption:
kubectl get pods
This documentation covers the entire process of setting up a Python Flask application, containerizing it with Docker, deploying it to a Kubernetes cluster on Google Cloud, and monitoring its resource usage. This setup ensures the application is scalable, resilient, and accessible via a public IP.