Deploy a Reddit Clone with Kubernetes Ingress

Before we begin with the Project, we need to make sure we have the following prerequisites installed:

  1. EC2 ( AMI- Ubuntu, Type- t2.medium )

  2. Docker

  3. Minikube

  4. kubectl

You can Install all this by doing the below steps one by one. and these steps are for Ubuntu AMI.

COPY

Explain# Steps:-

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

Great! You're all set for the project. Your Minikube cluster is now prepared for deploying the Reddit clone application. For additional information on deployment,

Do clone the code=>Build=>push

clone it and build it

git clone https://github.com/shadeemerhi/reddit-clone-yt.git

make a Dockerfile here

# Use an appropriate Node.js base image
FROM node:16-alpine

# Set the working directory inside the container
WORKDIR /reddit-clone

# Copy package.json and package-lock.json separately to leverage Docker caching
COPY package.json package-lock.json ./

# Install dependencies
RUN npm install --legacy-peer-deps

# Copy the rest of the application code
COPY . .

# Expose the port that the app runs on
EXPOSE 3000

# Define the command to run the application
CMD ["npm", "run", "dev"]

Build It with repo name

docker build -t tyagisarthak/reditclone:latest .

docker login =>Enter id =>password

docker push tyagisarthak/reditclone:latest

Now go to master server(kubernetes) t2.medium

In CI Server You only need to take the github code and build then push

Yha Minikube or kubectl Install Nhi Krne Vo master Node(t2.medium ) pr krne hai

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

# 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
# check it is ready or not 
kubectl get nods

Make a Deployment file(Deployment.yml) For Scalability

mkdir k8s
cd k8s
vim 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: tyagisarthak/reditclone:latest
        ports:
        - containerPort: 3000

Now I Want to Deploy It

kubectl apply -f Deployment.yml

For Check Your App Deployed or Not

kubectl get deployment

Now Create Service.yml Which make a All pods in one 1 ip by which you can access your application

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 You have to start it

kubectl apply -f service.yml
#check it is running or not 
minikube service reddit-clone-service --url
it'll provide you one ip address

Now You have to Expose it and See in the browser then Expose it

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

Now you have to do Port forwarding to access the world for it

You have to open 3000 port in your ec2 Instance

kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0

Check it is running or not Just Copy you ip address:3000 paste in url and ENter

If You Want to run it BAckgroud Then type & in the last

kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &

Ingress (Let's Start){Just 5 Min}

What is Ingress = We have a lots of pods and a lots of IPs then ingress help us to make integrate all ip and make a cluster ip by which we can access the Web app

Service.yml{Front-end } Bound ip with Domain name is Rules(sarthak.com)

Service.yml{Back-end} Rules(sarthak.com/api)

You have to enable ingress in minikube

minikube addons enable ingress

Let's Create ingress.yml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-reddit-app
spec:
  rules:
  - host: "domain.com"
    http:
      paths:
      - pathType: Prefix
        path: "/test"
        backend:
          service:
            name: reddit-clone-service
            port:
              number: 3000
  - host: "*.sarthak.store"
    http:
      paths:
      - pathType: Prefix
        path: "/test"
        backend:
          service:
            name: reddit-clone-service
            port:
              number: 3000

Apply it

kubectl apply -f ingress.yml