🚀Deploying a Reddit Clone on Kubernetes with Ingress Enabled 🔥

🚀Deploying a Reddit Clone on Kubernetes with Ingress Enabled 🔥

·

5 min read

Prerequisites:

  1. EC2

  2. Docker

  3. MiniKube

  4. Kubectl

Some important points you should know before moving forward

  1. Pod:

    The smallest unit in Kubernetes, a Pod is a group of one or more containers. Think of it like an apartment in an apartment building.

  2. Service:

    This is like a phone directory for Pods. Since Pods can come and go, a Service provides a stable "address" so that other parts of your application can find them.

  3. Namespace:

    A way to divide cluster resources among multiple users or teams. Think of it as having different folders on a shared computer, where each team can only see their own folder.

  4. Ingress:

    Think of this as the "front door" for external access to your applications, controlling how HTTP and HTTPS traffic should be routed to your services.

  5. Minikube:

    Local Kubernetes cluster tool for development and testing, provides a single-node cluster on your machine.

  6. kubectl:

    Kubernetes command-line tool, used to interact with and manage Kubernetes clusters, deploy applications, and perform cluster operations.

Step 1: Launch EC2 Instances (Servers)

First of all launch two ec2 instances (AMI- Ubuntu)

  1. CI-Server = simple free tier t2-micro server

  2. Deployment-server = t2-medium (for 2 CPU and 4 GB RAM)

Setup Docker on CI-Server

# For Docker Installation (on both CI-Server)
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

sudo usermod -aG docker $USER: This command adds your current user to the "docker" group.
The usermod command is used to modify user account settings, and the -aG flags specify that you want to append the user to the specified groups. In this case, it's adding your user to the "docker" group.
newgrp docker: This command starts a new shell with the "docker" group as the primary group. This is used to apply the group changes without requiring a system restart or user logout/login.

Step 2: Clone the source code from Github

The first step is to clone the source code in the CI-Server from the below link
github.com/shadeemerhi/reddit-clone-yt

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

Step 3: Create and Build Dockerfile

FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone
RUN npm install

EXPOSE 3000
CMD ["npm","run","dev"]

Now Build the Dockerfile

 docker build -t omkar4141/reddit-clone .

Step 3: Push the 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 the below Command and give your username & password.

  docker login

Now push your image to dockerhub.

docker push omkar4141/reddit-clone:latest

We can see pushed image on dockerhub.

Step 4: Setup Deployment-server

# Steps:-

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

# For Minikube & Kubectl (on Deployment-Server)
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

Note: kubectl is configured to use "minikube" cluster and "default" namespace by default.

Check on your deployment instance if Kubernetes is installed, Check it with the command.

kubectl get pods

Now, A Kubernetes Deployment YAML specifies the configuration for a Deployment object. this is a Kubernetes object that can create and update a set of identical pods. Each pod runs specific containers, which are defined in the spec. template field of the YAML configuration.

Step 5: Write Deployment.yml file

Let's Create a Deployment File For our Application.

Deployment.yml file is a Kubernetes configuration file used to define a Deployment for a containerized application

Deployment.yml file is a blueprint for Kubernetes to deploy and manage instances of a containerized application (in this case, a Reddit clone). It specifies the desired state, the number of replicas, and the container image to use.

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

b) Write Service.yml file

a Service.yml file is used to define a Service, which is an abstraction that provides network access to a set of pods. The purpose of a Kubernetes Service is to expose a stable endpoint (IP address and port) that can be used by other services or applications to communicate with the pods.

It enables other services within the cluster or external entities to communicate with the pods through a consistent and reliable interface, facilitating seamless networking in a dynamic containerized environment.

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 on Kubernetes & apply a Service For It

Now, 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 the below command

kubectl apply -f Deployment.yml

Just Like this create a Service using below command

kubectl apply -f service.yml

We have made 2 pods for our deployment and we will use Service.yml to create IP of our cluster(Deployment)
If You want to check your deployment & Service use the below command

kubectl get deployment 

kubectl get services

Step 6: Expose the app

First We need to expose our deployment so use below command.

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

The service will allocate a port on each node in the cluster and forward traffic to the pods managed by the specified deployment.

kubectl expose: This is the command to expose a Kubernetes resource.

  • deployment reddit-clone-deployment: This specifies the deployment that you want to create a service for. Replace reddit-clone-deployment with the actual name of your deployment.

  • --type=NodePort: This flag sets the service type to NodePort, which means that the service will be accessible on a port on each node in the cluster. Kubernetes will automatically allocate a port within a specific range (usually 30000-32767) for this service.

We need to do port forwarding to expose our app service

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

Note: Ensure you added security group for port 3000

Â