Skip to main content

Command Palette

Search for a command to run...

Reddit Clone App deployment on Kubernetes

Published
3 min read
A

I am a DevOps Engineer with an aim to learn and contribute to the Tech World!!

📝 Blogging and Sharing: On this platform, I'll be sharing insightful articles, tutorials, and tips on all things DevOps. Expect deep dives into CI/CD best practices, IaC patterns, containerization strategies, cloud optimization, and much more. Let's learn and grow together!

📌Project Architecture Diagram :

📌Tools and Technologies Used for Project :

  • Git / Git Hub

  • AWS EC2

  • Docker

  • Docker Hub

  • Yaml

  • Minikube for provisioning Kubernetes cluster

📌Prerequisite for this Project :

  • Firstly we need to have two ec2 servers one of t2.micro type and the other of t2.medium type, t2.micro will be our CI server which will have docker installed on it where we will build our docker image from the docker file

  • On t2.medium type which will be our Deployment server there, we will Install Kubernetes so that we can scale and deploy our Reddit application

📌Steps for Project :

  • 🎇 In step1 ) we will pull the code from Git Hub by using the below command
$git clone https://github.com/1706017/reddit-clone-k8s-deploy-ingress-enabled.git

  • 🎇In step2 we will containerize the application using Docker by writing the Dockerfile below
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install 
EXPOSE 3000
CMD ["npm","run","dev"]
  • 🎇In step3 we will build a docker image from the Dockerfile by using the below command
$docker build . -t amrit1997/reddit-clone
  • 🎇 In step4 will push this docker image to the docker Hub so that from docker hub this image can be easily accessed by the Kubernetes via manifest file/config file
$docker login 
#then you will be asked for username and password for dockerhub 

$docker push amrit1997/reddit-clone

  • Now we can also verify if the image gets pushed into DockerHub as screenshot attached below

  • 🎇In step5 Write a Manifest file for deployment

Now the question is what is a manifest file and why it is required so the answer is very simple manifest file is also known as a configuration file that we are writing in YAML and it is required because in Kubernetes every resource like pods, deployment, service, config map, secrets, persistent volume, persistent volume claim etc all these resources are written as a manifest or config file which then gets applied for Kubernetes cluster via command line / kubectl commands

Below is the config file for Deployment.yml

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: amrit1997/reddit-clone
        ports:
        - containerPort: 3000

Now we will apply the Deployment.yml using below command

$kubectl apply -f Deployment.yml

Then we can verify if the deployment and 2 pods are created for which we have created 2 replicas in Deployment.yml or not by using below command

$kubectl get pods

  • 🎇 In step6 we will write a Kubernetes manifest file for the Service

Now the question is what is Service in Kubernetes so the simple answer is that Service is the manifest file in Kubernetes which allows our application to get accessed by the external world via a web browser

File name Service.yml

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

Now will apply the Service.yml using below command

$kubectl apply -f Service.yml

  • 🎇 In Step7 Expose the app:

A)First We need to expose our deployment so use below command

$kubectl expose deployment reddit-clone-deployment --type=NodePort

B) Then we have to expose our service

C) Now we need to add port 3000 in our deployment-server security group in inbound rules so that this application can be accessed in web browser

Now in order to access the app deployed in kubernetes in webbrowser

Just hit below url in webbrowser

100.26.121.201:3000

Where

100.26.121.201 -> This is the public port for our deployment server

3000 -> is the port for our application

Now our application is successfully deployed on Kubernetes

Thanks and Regards,

Amrit Manash

Thanks for reading till now this Project 😊😊