Deploy Reddit Clone with Kubernetes - DevOps Project

DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab
Prerequisites for Deploying a Reddit Clone on Kubernetes with Ingress
Before starting the project, ensure you have the following prerequisites installed:
EC2 (AMI: Ubuntu, Type: t2.medium)
Docker
Minikube
kubectl
Follow these steps to install them on Ubuntu AMI:
Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
Minikube & Kubectl Installation
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
Once these are set up, you're ready to proceed with the project.
Step 1: Clone the Source Code
Begin by cloning the app's source code using the following command:
git clone https://github.com/LondheShubham153/reddit-clone-k8s-ingress.git
Step 2: Containerize the Application with Docker
Create a Dockerfile with the following content:
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm","run","dev"]
Step 3: Build the Docker Image
Build the Docker image using this command:
docker build -t <DockerHub_Username>/<Imagename> .
Step 4: Push the Image to DockerHub
Push the Docker image to DockerHub so that the deployment file can pull this image and run the app in Kubernetes pods. First, log in to your DockerHub account using:
docker login
Enter your username and password when prompted.
First login to your DockerHub account using Command i.e
docker loginand give your username & password.Then use
docker push <DockerHub_Username>/<Imagename>for pushing to the DockerHub.You can use an existing docker image i.e
amitsinghs98/reddit-clone-k8s-ingress:v1for deployment.

Step 5: Write a Kubernetes Manifest File
You might wonder what a Kubernetes manifest file is. It's a file in YAML or JSON that describes Kubernetes resources like pods, deployments, etc. While you can create these resources via the command line, it's better to use a manifest file for version control and repeatability.
Write Deployment.yml file
Let's Create a Deployment File For our Application. Use the following code for the Deployment.yml 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: amitsinghs98/reddit-clone-k8s-ingress:v1
ports:
- containerPort: 3000
Write Service.yml file
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 to Kubernetes & Create 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 you need to run following the command:
kubectl apply -f Deployment.yml
kubectl apply -f Service.ymlIf You want to check your deployment & Service use the command
kubectl get deployment&kubectl get servicesBefore exposing check your pods: have started working like are they Ready?
kubectl get pods -o wide

We can run our app before ingress too:kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0



Step 6) Let's Configure Ingress
Ingress:
Pods and services in Kubernetes have their own IP; however, it is normally not the interface you'd provide to the external internet. Though there is a service with node IP configured, the port in the node IP can't be duplicated among the services.
It is cumbersome to decide which port to manage with which service. Furthermore, the node comes and goes, it wouldn't be clever to provide a static node IP to an external service. Ingress defines a set of rules that allows the inbound connection to access Kubernetes cluster services. It brings the traffic into the cluster at L7 and allocates and forwards a port on each VM to the service port.
This is shown in the following figure. We define a set of rules and post them as source type ingress to the API server.
When the traffic comes in, the ingress controller will then fulfill and route the ingress by the ingress rules. As shown in the following figure, ingress is used to route external traffic to the Kubernetes endpoints by different URLs:
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
Minikube doesn't enable ingress by default; we have to enable it first using
minikube addons enable ingressIf you want to check the current setting for addons in minikube use
minikube addons listkubectl apply -f ingress.ymluse this command to apply ingress settings.Verify that the ingress resource is running correctly by using
kubectl get ingress ingress-reddit-appcommand.
Step7: Expose the application
First We need to expose our deployment so use
kubectl expose deployment reddit-clone-deployment --type=NodePortcommand.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
Then We have to expose our app service
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
Test Ingress
curl -L domain/test command in the terminal.
Note:- Make sure you open the 3000 port in a security group of your Ec2 Instance.
You have successfully Deployed a Reddit Copy on Kubernetes with Ingress Enabled.


