Skip to content
Hardik-7892 edited this page Aug 29, 2024 · 1 revision

Welcome to the Simple-Factorial-Website-Using-Kubernetes-and-Docker wiki!

Here I will be documenting the Deployment of a Factorial App on Kubernetes Using Google Cloud

1. Overview

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).

2. Prerequisites

Google Cloud Platform (GCP) account. Google Cloud SDK installed and configured. Docker installed and running. Basic knowledge of Kubernetes and Docker.

3. Project Setup

Step 1: Create a Python Flask Application

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)

Step 2: Create a Dockerfile

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"]

Step 3: Build and Push the Docker Image

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.

4. Kubernetes Deployment

Step 1: Create a Kubernetes Cluster

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

Step 2: Create Kubernetes Deployment and Service

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

Step 3: Deploy to Kubernetes

Apply the deployment and service configuration:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

Step 4: Verify Deployment

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.

5. Monitoring Resource Usage

To monitor resource usage of nodes and pods:

  1. Install Metrics Server:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
  1. Check Node Resource Consumption:
kubectl top nodes
  1. Check Pod Resource Consumption:
kubectl get pods

6. Conclusion

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.