Kubernetes Microservice Flask Application

Kubernetes Microservice Flask Application

Introduction

In this blog post, we will learn how to deploy a microservices project on Kubernetes using Minikube. Minikube is a tool that allows you to run a single-node Kubernetes cluster on your local machine.

Prerequisites

  • ➡️Basic Understanding of Minikube: This tutorial assumes you have a fundamental understanding of Minikube concepts such as starting and managing a local Kubernetes cluster.

  • ➡️Minikube Installed and Running: To actively engage in this practical exercise, you need to have Minikube installed and a functional local Kubernetes cluster set up.

Let's Start

Step 1: Cloning the Source Code of the Project

git clone https://github.com/Simbaa815/microservices-k8s.git

This step involves cloning the repository where your project's source code is stored.

Step 2: Build the Dockerfile

Create a Dockerfile in the root directory of your project with the following content:

FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENV PORT 5000
EXPOSE 5000
ENTRYPOINT [ "python" ]

Now we will build a Docker Image from this Dockerfile.

docker build . -t your_username/microservicespythonapp:latest

Step 3: Push the Docker Image to Docker Hub

docker login
docker push your_username/microservicespythonapp:latest

This step involves building the Docker image and pushing it to your Docker Hub repository. Replace your_username, with your docker hub username.

Step 4: Creating a Persistent Volume Manifest for K8s

Create a file named mongo-pv.yml with the following content:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
  namespace: microservices-k8s
spec:
  capacity:
    storage: 256Mi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /home/nid/mongo-dv

This defines a PersistentVolume named mongo-pv with a capacity of 256Mi and uses the host path /home/nid/mongo-dv for storage.

Step 5: Creating a Persistent Volume Claim (PVC) Manifest in K8s

Create a file named mongo-pvc.yml with the following content:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
  namespace: microservices-k8s
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 256Mi

This creates a PersistentVolumeClaim named mongo-pvc requesting 256Mi of storage with ReadWriteOnce access mode.

Step 6: Create a Deployment Manifest File of MongoDB

Create a file named mongo-deployment.yml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-deployment
  namespace: microservices-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo:latest
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-storage
              mountPath: /data/db
      volumes:
        - name: mongo-storage
          persistentVolumeClaim:
            claimName: mongo-pvc

This deploys a single MongoDB container with the necessary configurations, including a PersistentVolumeClaim named mongo-pvc.

Step 6: Create a Service Manifest File of MongoDB

Create a file named mongo-svc.yml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongo
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017
  selector: mongo

This creates a NodePort service named mongo-svc for the MongoDB deployment.

Step 7: Create the Deployment Manifest File of the Flask App

Create a file named taskmaster.yml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: taskmaster
  namespace: microservices-k8s
  labels:
    app: taskmaster
spec:
  replicas: 1
  selector:
    matchLabels:
      app: taskmaster
  template:
    metadata:
      name: taskmaster
      namespace: microservices-k8s
      labels:
        app: taskmaster
    spec:
      containers:
        - name: taskmaster
          image: simbaa815/microservicespythonapp:latest
          ports:
            - containerPort: 5000
          imagePullPolicy: Always

Step 8: Create the Service Manifest File of the Flask App

Create a file named flask-app-svc.yml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: taskmaster-svc
  namespace: microservices-k8s
spec:
  selector:
    app: taskmaster
  ports:
    - protocol: TCP
      port: 80
      targetPort: 5000
      nodePort: 30007
  type: NodePort

This creates a NodePort service named taskmaster-service.yml for the Flask app deployment.

Step 9: Test the Application

Once you have applied all the manifests, use kubectl to monitor the status of your resources:

kubectl get pods
kubectl get deployments
kubectl get services

Make sure everything is running as expected.

In Minikube: How to verify this

kubectl port-forward pod/<pod_name> <local_port>:<target_port> -n <namespace>

Simply past to the IP in the web-browser that is shown in the first line

For further clarification and insights, please visit this

Congratulations on completing the deployment of your Microservices Project! 🚀🎉


Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps 🚀, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!

Feel free to connect with me on LinkedIn for more updates and discussions on DevOps and Kubernetes! 🚀

Happy Learning! Keep pushing those boundaries! 😊