Deploying Reddit Clone Web App with ingress on Kubernetes Cluster

Deploying Reddit Clone Web App with ingress on Kubernetes Cluster

Β·

4 min read

Step 1: Provision AWS Instances and some prerequisite

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

create two instances with specific configurations:

  • Deployment Server:

    • AMI: Ubuntu

    • Type: t2.medium

  • CI Server:

    • AMI: Ubuntu

    • Type: t2.microEC2 ( AMI- Ubuntu, Type- t2.medium )

now install docker, minikube and kubectl --

# Steps:-

# 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

Step 2: Clone the Code

clone the reddit app code from github On Ci-server

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

Step 4: Write a Dockerfile for the Project 🐳

Create a Dockerfile for your Reddit clone project with the following content:

FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone

RUN npm install

EXPOSE 3000

CMD [β€œnpm”,”run”,”dev”]

Step 5: Build and Push Docker Image

Build the Docker image from the Dockerfile using the following command:

docker build . -t krishpauri/reddit-clone

After building the image, push it to Docker Hub by running:

docker login
# Enter your username and password
docker push krishpauri/reddit-clone:latest

Step 6: Connect to Deployment Server

SSH into the Deployment Server.

Step 7: Install Minikube and Kubectl

Ensure Minikube and Kubectl are installed on the Deployment Server.

Step 8: Create a Kubernetes Folder

Create a folder named "K8s" on the Deployment Server.

Step 9: Create a Deployment YAML File πŸ“

Inside the "K8s" folder, create a file named "Deployment.yml" and add the following content:

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

Step 11: Apply the Deployment File πŸ“œ

Apply the Deployment file to create pods:

kubectl apply -f Deployment.yml

Step 12: Verify Deployments

To ensure that the deployments are successful, use the following command:

kubectl get deployments

Step 13: Create a Kubernetes Service 🌐

Now, you need to create a Service to provide an IP address for your application within the cluster.

Step 14: Create a Service YAML File πŸ“

Inside the "K8s" folder, create a file named "Service.yml" and add the following content

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 15: Apply the Service File πŸ“œ

Apply the Service file to create a service:

kubectl apply -f Service.yml

Step 16: Check the Service 🌐

To verify that the service is created successfully, run:

kubectl get services

Step 17: Access the Application πŸš€

You can now access your application by visiting the URL provided by Minikube:

minikube service reddit-clone-service β€” url

Step 18: Expose the Application to the Internet 🌍

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

  2. You can test your deployment using curl -L http://192.168.49.2:31000. 192.168.49.2 is a minikube ip & Port 31000 is defined in Service.yml

  3. Then We have to expose our app service kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &

Note:- Make sure you open the 3000 port in a security group of your Ec2 Instance.

Step 20: Access Your Live Reddit Clone

Visit your Reddit Clone web app at the provided domain or IP address, and it should now be accessible to the world.

Step 20: configure ingress

Let's write ingress.yml and put the following code in it:

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: "*.domain.com"
    http:
      paths:
      - pathType: Prefix
        path: "/test"
        backend:
          service:
            name: reddit-clone-service
            port:
              number: 3000

Minikube doesn't enable ingress by default; we have to enable it first using minikube addons enable ingress command.

Now you can able to create ingress for your service. kubectl apply -f ingress.yml use this command to apply ingress settings.

  • Verify that the ingress resource is running correctly by using kubectl get ingress ingress-reddit-app command.

happy learning :) Thanks for reading...https://github.com/krishkprawat/reddit-clone-02

Β