Deploying a Reddit Clone using Kubernetes

Deploying a Reddit Clone using Kubernetes

Introduction

Kubernetes is a container orchestration platform that makes it easy to deploy and manage containerized applications. Ingress is a Kubernetes API object that provides HTTP and HTTPS routing to services based on defined rules.

In this blog post, you will learn how to deploy a Reddit clone on Kubernetes with Ingress enabled. This will allow you to expose the Reddit clone application to the internet and make it accessible to users.

What is Ingress?

In Kubernetes, an Ingress is an API object that provides HTTP and HTTPS routing to services based on defined rules. It allows external users to access services in a Kubernetes cluster.

Key Features of Ingress:

Sneaker Culture, Designer Apparel + Lifestyle – Feature

  1. URL-Based Routing:

    • Ingress allows you to route traffic based on the URL path. For example, you can send requests to different services based on the path in the URL (e.g., /app, /api, etc.).
  2. Domain-Based Routing:

    • You can also route traffic based on the domain name in the request. This is known as virtual hosting, where different services can be associated with different domain names.
  3. TLS Termination:

    • Ingress can handle SSL/TLS termination, allowing you to manage encryption for your services.
  4. Load Balancing:

    • Ingress can distribute traffic across multiple pods within a service. It supports various load-balancing algorithms, which can be specified in the service configuration.
  5. Resource Efficiency:

    • Ingress allows you to efficiently manage external access through a single controller.

Ingress Controller:

  • While Ingress is a Kubernetes API object, it requires an Ingress Controller to function. The controller is a piece of software that reads the Ingress resource and enforces the rules specified in it.

  • Ingress controllers can be implemented by different providers, such as NGINX, Traefik, HAProxy, and more. These controllers manage the actual traffic routing based on the rules defined in the Ingress resource.

  • The Ingress controller in Kubernetes dynamically generates and manages proxy configurations based on changes to Ingress resources in the cluster. It continuously monitors the Kubernetes API server for updates, and when a new Ingress resource is created or an existing one is modified, the controller processes the changes and updates its internal routing rules.

Prerequisite:

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

  1. Docker:

    • Install Docker by following the instructions on the official website: Get Docker.
  2. Minikube:

    • Install Minikube by following the instructions on the official website: Install Minikube.
  3. kubectl:

    • Install kubectl, the command-line tool for interacting with Kubernetes clusters, by following the instructions on the official website: Install kubectl.

Instead, Use these commands for easy installation

# 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

also, you have GitHub and Docker Hub Accounts:

  1. GitHub:

    • If you don't already have a GitHub account, go to GitHub and sign up for a new account.
  2. Docker Hub:

    • If you don't already have a Docker Hub account, go to Docker Hub and sign up for a new account.

Steps:

Step 1: Clone the source code

Clone this to your system

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

Step 3: Navigate to the project directory and create a namespace named reddit

  • Navigate

  • Create Namespace

       kubectl create namespace reddit
    

Step 4: Containerize the Application using Docker

FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone
RUN npm install

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

Step 5: Building Docker-Image

docker build -t <DockerHub_Username>/<Imagename> .

Step 6: Push the Image To DockerHub

docker push <dockerHub_username>/<images_name>

Step 7: Write a Kubernetes Manifest File

  • Write Deployment.yml file

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: reddit-clone-deployment
        namespace: reddit
        labels:
          app: reddit-clone
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: reddit-clone
        template:
          metadata:
            namespace: reddit
            labels:
              app: reddit-clone
          spec:
            containers:
            - name: reddit-clone
              image: simbaa815/reddit-clone
              ports:
              - containerPort: 3000
    

  • Write Service.yml file

      apiVersion: v1
      kind: Service
      metadata:
        name: reddit-clone-service
        namespace: reddit
      spec:
        selector:
          app: reddit-clone
        ports:
        - protocol: TCP
          port: 3000
          targetPort: 3000
          nodePort: 31000
        type: NodePort
    

Step 8: Applying the Configurations

  1. Start by applying the deployment file with the following command.

      kubectl apply -f deployment.yaml
    

    To check the deployment configuration

     kubectl get pods -n <namespace>
     #in my case namespace is reddit
    

  2. Next, apply the service file so you can access it through your web browser.

      kubectl apply -f service.yaml
    

    To check the service configuration

     kubectl get svc -n <namespace>
     #in my case namespace is reddit
    

Step 9: Configure and Test Ingress

  • Ingress:

      apiVersion: networking.k8s.io/v1
      kind: Ingress
      metadata:
        name: ingress-reddit-app
        namespace: reddit
      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
    

  • Test Ingress

    • Enable the ingress controller

        minikube addons enable ingress
      

  • IP

      minikube service <service_name> --url -n <namespace>
      #in my case service_name is service-clone-service and namespace is reddit
    

  • Test the rule

      curl -L <ip>/rule
      #ip is shown in the above command
    

Step 10: Access Your Live Reddit Clone

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

Simply paste your local_IP:3000 on your browser and then see the magic

Congratulation🎉

You have successfully Deployed a Reddit Copy on Kubernetes with Ingress Enabled.

Free and customizable congratulations templates


Feel free to connect with me on LinkedIn for more updates and discussions on DevOps.

Happy Learning! Keep pushing those boundaries! 😊