Let's Deploy a reddit-replica on kubernetes with ingress & push that replicate image of reddit to the dockerhub.

RAKESH DUTTARAKESH DUTTA
4 min read

In this reddit-replicate project we are going to see how to pick code from github and build a docker image then how and push that image on dockerhub from CI server which is running on AWS console with t2.micro type of instance and pull that image from dockerhub on Deployment Server which also running on AWS console with t2.xlarge type of instance and how to create our deployment.

Here is the architecture of this reddit-replica application runing on kubernetes with ingress.

Here are some prerequisites that we need to get through this deployment.

An AWS account and where are successfully running two different instances one on t2.micro for CI server and another on t2.xlarge for deployment server.

Installed Docker on both.

Installed Minikube.

Installed Kubectl.

Installed Git.

If they are not installed already on our AWS then lets begin the process.

Step 1 : Here we start with launching two different types of server based on AMI Ubuntu one is on t2.micro and other one is on t2.xlarge. And expose the port 3000 to anywhere.

Step 2 : Let's install Docker and setup our CI server.

# Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

Step 3 : Clone this source code for reddit-clone from Github and enter to this reddit-clone directory.

# Cloning from Github
git clone https://github.com/rksdutt/reddit-clone-k8s-ingress.git
cd reddit-clone-k8s-ingress

Step 4 : Here we are going to create Dockerfile to containerize the application.

# Write a Dockerfile

FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone

RUN npm install

EXPOSE 3000

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

Step 5 : Here are going to build Docker image through this Dockerfile and push this image to the dockerhub.

# Build Image & Push it on DockerHub
docker build . -t rakeshdutt/reddit-clone
docker images
docker login
docker push rakeshdutt/reddit-clone

Step 6 : Here we can see our Docker image pushed on Dockerhub successfully.

Step 7 : Now its time to setup our Deployment Server

# Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

Step 8 : Here we are going to install Kubectl and Minikube and set up our Deployment server.

# Minikube Installation
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube 
# Installation of Kubectl
sudo snap install kubectl --classic
# Start Minikube
minikube start --driver=docker

Step 9 : Here we are going to check if minikube is installed correctly.

# Check Minikube status
minikube status

Step 10 : Here we can see everything goes well till now..

Step 11 : So lets write the manifest files to to deploy the applicatiion. Lets make a folder and write the deployment.yml and service.yml & ingress.yml manifest files.

# Making a Folder
mkdir k8s
cd k8s/

Step 12 : Let's write the deployment.yml

# Write 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: rakeshdutt/reddit-clone
        ports:
        - containerPort: 3000

Step 13 : Lets write the service.yml

# Write service.yml
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 14 : Here we are going to deploy the application and service on kubernetes cluster.

# Deployment and Verify
kubectl apply -f deployment.yml
kubectl get deployment
kubectl apply -f service.yml
kubectl get services

Step 15 : Here we can see our application running successfully.

Step 16 : Here we are going to verify both our services are running or not.

# Verify the Deployment
minikube service reddit-clone-service --url

Step 17 : Here we can see everything going well as we expected.

Step 18 : Let see our application deployed or not with this command

# Verify the Apllication
curl -L http://192.168.49.2:31000

Step 19 : Here we are going apply ingress to route outside traffic to the application.

minikube addons enable ingress

Step 20 : Its time to write ingress.yml

# Write 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

Step 21 : Lets do the deployment and verify everything going well or not..

# Deployment and Verify and Expose the Application and Map the Expposed Port
kubectl apply -f ingress.yml
kubectl get ingress ingress-reddit-app
kubectl expose deployment reddit-clone-deployment --type=NodePort
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0
# If we Wanna keep the our Application Running just add & end of this
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
# Test the deplyment through ingress
curl -L domain.com/test

Step 22 : Here we can see our applicstion is deployed & running successfully.

We have successfully deployed a Reddit replicaton on a Kubernetes cluster with Ingress enabled. By following these steps we have containerized the application, deployed it to Kubernetes and exposed it via Ingress for outiside world.

0
Subscribe to my newsletter

Read articles from RAKESH DUTTA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RAKESH DUTTA
RAKESH DUTTA