Deploy a Reddit Clone with Kubernetes

This is the simple architecture we are going to follow for our deployment:

Points:

  1. CI Server: t2.micro

  2. DockerHub: Create a repository

  3. Deployment Server: t2.medium

Prerequisite:

  1. Install Docker on CI Server

  2. Minikube and Kubectl on Deployment Server

# 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

Login into your CI Server Ec2 instance:

Steps:

  1. Clone the GitHub repo: https://github.com/LondheShubham153/reddit-clone-k8s-ingress.git

  2. Write a DockerFile:

     FROM node:19-alpine3.15
    
     WORKDIR /reddit-clone
    
     COPY . /reddit-clone
     RUN npm install
    
     EXPOSE 3000
     CMD ["npm","run","dev"]
    
  3. Build a Docker Image:

     docker build -t <name-of-your-docker-repo> .
    
     docker build -t hiphopsid/reddit .
    
  4. Login into the docker repo:

     docker login
    
     #Login with your DockerHub Credentials
    
     docker push <imageName>
    

Login into your Deployment Server:

Steps:

  1. Install Minikube and kubectl

    1. Check "kubectl get pods" if minikube is running.
  2. Create a deployment file:

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: hiphopsid/reddit
        ports:
        - containerPort: 3000
  1. Apply the deployment file:

     kubectl apply -f <deploymentFileName>
    

  2. Now, to provide access for the application we will create a Service.yaml file:

#Node Port 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

Expose Deployment:

  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

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

  1. The difference b/w both is now the port will be exposed in the background and you can continue with your work.