Project 6: Deploying Reddit Clone App on Kubernetes Cluster

Ashmi SinhaAshmi Sinha
4 min read

Introduction

Automating the Delivery of Highly Scalable Reddit Clone Application using CI/CD Pipeline with Docker, Docker Hub, and Kubernetes Minikube ► Description: As part of a software development project, I designed and implemented a robust continuous integration (CI) and continuous deployment (CD) pipeline for a Reddit clone application. The pipeline incorporated Docker, Docker Hub, and Kubernetes Minikube technologies to achieve a seamless, automated flow from code changes to deployment.

Prerequisites

  1. Provision EC2 instance ( AMI- Ubuntu, Type- t2.medium ) - Deployment Server

  2. Provision EC2 instance ( AMI- Ubuntu, Type- t2.micro ) - CI Server

  3. Docker

  4. Minikube

  5. kubectl

If not installed already, follow below steps to install Docker, Minikube and kubectl.

Install Docker on both CI-Server and Deployment-Server:

# For Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

Install Minikube & Kubectl on Deployment-Server:

# For Minikube & Kubectl
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube 

sudo snap install kubectl --classic
minikube start --driver=docker

Your Minikube cluster is now prepared for deploying the Reddit clone application.

Great! We are all set for the project. EXCITED?

(Step 1 - Step 4 will be performed on CI-Server)

Step 1: Clone the source code

The first step is to clone the source code for the app. You can do this by using this command

git clone https://github.com/ashmisinha/reddit-clone-k8s-ingress.git

We have cloned the repository:

Step 2: Containerize the Application using Docker

Write a Dockerfile with the following code:

FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install 
EXPOSE 3000
CMD ["npm","run","dev"]

Step 3: Building Docker Image

Build a Docker Image from this Dockerfile using this command:

docker build . -t ashmisinha/reddit-clone

Step 4: Push this Image To DockerHub

Now push this Docker Image to DockerHub so our Deployment file can pull this image & run the app in Kubernetes pods.

  • First login to your DockerHub account using docker login and give your credentials.

  • Use docker push ashmisinha/reddit-clone:latest for pushing to the DockerHub.

To check if the image is pushed to the DockerHub Repository, we can use the docker search command or we can directly login to the dockerHub and navigate to 'Repositories' tab.

(Let's work on the Deployment-Server now :: Step 5 onwards)

Step 5: Write a Deployment Manifest File for K8s

  • Let's Create a Deployment Manifest File for our Application:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: reddit-clone-deployment
  labels:
    app: reddit-clone
spec:
  replicas: 2
  selector:
    matchLabels:
      app: reddit-clone
  template:
    metadata:
      labels:
        app: reddit-clone
    spec:
      containers:
      - name: reddit-clone
        image: ashmisinha/reddit-clone
        ports:
        - containerPort: 3000
  • Write Service Manifest file:
apiVersion: v1
kind: Service
metadata:
  name: reddit-clone-service
  labels:
    app: reddit-clone
spec:
  type: NodePort
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 31000
  selector:
    app: reddit-clone

Step 6: Deploy the app to Kubernetes and create a Service for it

We have a deployment file for our app and we have a running Kubernetes cluster, we can deploy the app to Kubernetes.

To deploy the app, run: kubectl apply -f Deployment.yml

Let's verify the deployment and pods created: kubectl get deployment

Let's create a Service using the manifest file kubectl apply -f Service.yml to access the Pods. Verify the service: kubectl get svc

Step 7: Access the App

Use below command to create an URL to access the app:

minikube service reddit-clone-service --url

Let's test our app using curl -L http://192.168.49.2:31000 .

192.168.49.2 is a minikube ip & Port 31000 is defined in our Service manifest file.

Perfect! It's running just fine.

But, I want to see it in the Browser, so let's expose our Reddit-Clone App so that we can run it globally.

Step 8: Expose the app to the Internet

  1. First We need to expose our deployment so use kubectl expose deployment reddit-clone-deployment --type=NodePort command.

    Note: Add the Port 3000 in the Deployment-Server instance in the inbound rule.

  2. Let's expose our app service kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0

    Here you go!

In case you want this app to be running in background, you can use kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 & .

It was so exciting to do this project! 🤓

One of the issues I faced was my instance going unresponsive and minikube getting stopped. So, I had to restart the instance, and manually start the minikube, using minikube start .

Happy Deploying! :)

0
Subscribe to my newsletter

Read articles from Ashmi Sinha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashmi Sinha
Ashmi Sinha