Deployment of Reddit-Clone application on Kubernetes with Ingress enabled.

Passionate AWS Developer | DevOps Engineer with a strong background in cloud architecture and solutions engineering. Leveraging the power of Amazon Web Services (AWS), knowledge of the AWS global infrastructure, design and implement robust cloud-based solutions that align with clients' specific needs.
To deploy the reddit-clone application we need two servers:
CI server
Deployment server

- First, we do the CI Server setup.
Install Docker and give permission to Docker user by following commands:
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker


- Clone the source code to CI server.
git clone https://github.com/akarsha-prasad/reddit-clone-k8s-ingress.git
git clone <reddit-clone-app-url>

Navigate to the project directory:
cd reddit-clone-k8s-ingressCreate a Dockerfile using command:
vim Dockerfile

Dockerfile:
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone RUN npm install
EXPOSE 3000 CMD ["npm","run","dev"]
Build the Dockerfile to create an image and push it to Dockerhub by following the command:
docker build . -t 291019955/reddit-clone


- Login to Dockerhub and push the image.
docker login
<enter username and password>
docker push reddit-clone <username>/reddit-clone:latest


Here we can see the pushed reddit-clone app on Dockerhub

CI server set-up has been done, Now configure the Deployment server.
Install the following prerequisite on the Deployment server:
EC2 ( AMI- Ubuntu, Type- t2.medium )
Docker
Minikube
kubectl
# 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



Write a Kubernetes Manifest file: Manifest file describe the objects and its attributes in yaml or json format. we need to write Manifest file to create pod, replica-set, deployment, services etc in Kubernetes.
Write a Deployment file for the application
Deployment.ymlapiVersion: 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: 291019955/reddit-clone ports: - containerPort: 3000

Create a deployment for the app and see by following the command:
kubectl apply -f Deployment.yml kubectl get Deployment

Now, we have deployed our app on Kubernetes.
Now create a Service file by following commands:
Write Service file
vim Service.ymlkubectl apply -f 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

Create URL link for reddit app to access:
minikube service reddit-clone-service --url curl -L http://192.168.49.2:31000

- Expose the deployment and service to outside world
kubectl expose deployment reddit-clone-deployment --type=NodePort
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &

We can also see the deployed application on Ec2_ip:3000
Copy the public-ip of deloyment server:3000 and paste to browser
Make sure to add port 3000 to security group of deployment server.
http://100.26.160.146:3000/ - Check on the browser
We can see the reddit clone app running .


Configure Ingress, 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.
Write ingress file- 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
- Enable Ingres- enable it first using
minikube addons enable ingresscommand.

create ingress for your service.
kubectl apply -f ingress.ymluse this command to apply ingress settings.Test the ingress so use the curl -L domain/test command in the terminal.



Here we can see that the reddit-clone app is successfully deployed on the kubernetes with Ingress enabled.




