Project: Deployment of a Microservice Application on Kubernetes.

Project: Deployment of a Microservice Application on Kubernetes.

curl http://192.168.49.2:30009/tasksIn this project microservice application was built using Flask and deployed on Kubernetes. It is designed to demonstrate how to build and deploy microservices on a Kubernetes cluster.

Step 1: Clone the repository from GitHub to your local machine.

link: https://github.com/LondheShubham153/microservices-k8s

git clone <github repository link>

Step 2:

Create one Dockerfile and build the image. Create a container for microservices.

# Dockerfile for microservice flask app 
FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENV PORT 5000
EXPOSE 5000
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]
docker build . -t microservice
docker run -d -p 5000:5000 microservice:latest

Step 3:

login to docker hub. tag the docker container with the docker hub ID name. Push the microservice container to the docker hub.

docker login
docker tag achyut197/microservice:latest
docker push achyut197/microservice:latest

Step 4:

deploy this docker container using Kubernetes. Create one microappdeployment.yml for the microservice container.

minikube start
vi microappdeployment.yml

microappdeployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: microservice
  labels:
    app: microservice
spec:
  replicas: 1
  selector:
    matchLabels:
      app: microservice
  template:
    metadata:
      labels:
        app: microservice
    spec:
      containers:
        - name: microservice
          image: achyut197/microservice:latest
          ports:
            - containerPort: 5000
          imagePullPolicy: Always

From this file, the microservice container was searched from the docker hub, and the deployment process was executed in Kubernetes. One pod was created.

kubectl apply -f microappdeployment.yml

If you want to scale the deployment command is -

kubectl scale --replicas=3 deployment/microservice
# see all pods 
kubectl get pods

so three pods were created automatically. ( scale replicas=3)

To access the deployment from outside the cluster create a service file.

microappservice.yml:

apiVersion: v1
kind: Service
metadata:
  name: microappservice
spec:
  selector:
    app: microservice
  ports:
    - port: 80
      targetPort: 5000
      nodePort: 30009
  type: NodePort

In Kubernetes, three types of services were used that is node port, ClusterIP, and LoadBalancer. Here in microappservice.yml port was mapped target port and the port was assigned with node port 30009 for microservice. Create the service using the command-

kubectl apply -f microappservice.yml

Step 5:

Create a MongoDB database. To store the data it requires one persistent volume.

Create persistent volume:

mongo-persistentvolume.yml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-persistentvolume
spec:
  capacity:
    storage: 280Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/db

In this file, I have assigned 280 MB for MongoDB to store the data with a temporary path /tmp/db.

Create the volume capacity by applying this command -

kubectl apply -f mongo-persistentvolume.yml

To claim the persistent volume create mongo-persistentvolumeclaim.yml file-

mongo-persistentvolumeclaim.yml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 280Mi

Create the PersistentVolumeClaim by applying this command-

kubectl apply -f mongo-persistentvolumeclaim.yml

Create MongoDB by using MongoDB image-

mongodb.yml:

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

deploy the MongoDB database by using this command-

kubectl apply -f mongodb.yml

volume was assigned to the Mongodb database successfully and one pod was created for Mongodb.

To access MongoDB outside of the cluster create a service file.

mongodbservice.yml:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: mongo
  name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
      nodePort: 30010
  type: NodePort
  selector:
    app: mongo
kubectl apply -f mongodbservice.yml

so two pods were created i.e. microservice flask app and another MongoDB.

to get all deployments and service details:

kubectl get deployments,services

To access this deployment using the URL in the browser command is:

#to get ip address for deployment name microservice-:
minikube service microservice --url
#to get ip address for deployment name mongo
minikube service mongo --url
url -L http://<ip>:<nodeport>

type that URL in the browser.

Step 6:

To insert data in Mongodb perform this command -:

 curl http://192.168.49.2:30009/task
add data in URL:
curl -d '{"task":"hi my name is achyut das."}' -H "Content-Type: application/json" -X POST http://192.168.49.2:30009/task
curl -d '{"task":"i have done this project sucessfully"}' -H "Content-Type: application/json" -X POST http://192.168.49.2:30009/task

now all the data was inserted in MongoDB.

to see the data in the browser:

http://192.168.49.2:30009/tasks

Now I have done this project successfully.

Did you find this article valuable?

Support Achyut Das by becoming a sponsor. Any amount is appreciated!