Project : Reddit Clone App on Kubernetes with Ingress

Project : Reddit Clone App on Kubernetes with Ingress

Project

Automating the Delivery of Highly Scalable Reddit Clone Application using CI/CD Pipeline with Docker, Docker Hub, and Kubernetes Minikube.

Key technologies used: Docker, Docker Hub, Kubernetes, Minikube, CI/CD, Containerization, Automation, Monitoring, Logging, Reddit Clone Application.

This project demonstrates how to deploy a Reddit clone app on Kubernetes with Ingress and expose it to the world using Minikube as the cluster.

CI part

  1. In your EC2 instance make sure you install docker.

# For Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
  1. Clone the below repo in your CI server.

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

  2. Build

    Lets build the docker file using below command.

     docker build . -t aasifa/reddit-clone
    

  3. Push

#to login to dockerhub, give usrname and pwd
docker login
#To view images created
docker images 
#push to dockerhub
docker push aasifa/reddit-clone:latest

Deployment part

Install minikube and kubectl to run kubernetes

# 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

We need to deploy the image we pushed in dockerhub.

For that we need a deployment file.

Why deployment file?

For scalability, if suppose there are so many people who are accessing this application, it may face downtime. So we create replica for the pods so that it can auto heal the application.

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: trainwithshubham/reddit-clone
        ports:
        - containerPort: 3000
kubectl apply -f Deployment.yml
kubectl get deployment

service.yaml

To access this application we use service file.

We will be using nodeport service.

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

The difference between the port 3000 and 31000 is that port 3000 is the port that the service is listening on inside the Kubernetes cluster, while port 31000 is the port that the service is exposed on outside of the Kubernetes cluster, accessible from the internet or other external networks.

kubectl apply -f Service.yml
# Access the application using 
 minikube service reddit-clone-service --url

curl -L http://192.168.49.2:31000

You can see our app is running in the cluster

Expose the deployment to run in browser

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

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

Our port id forwarded. Add 3000 to your instance IP address.

Ingress

Enable ingress in minikube

minikube addons enable ingress

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

curl -L domain.com/test

Finally our app is deployed using domain name.

--Thank you for your time--