Ultimate Kubernetes Project Using Ingress

Harshit SahuHarshit Sahu
4 min read

In this guide, we will walk through the steps to deploy a Reddit clone application on a Kubernetes cluster with ingress enabled for external access.

STEPS TO BE DONE BEFORE STARTING THE PROJECT:

Launch Two Instance in AWS, one with t2.micro and other with t2.large and install docker and Kubernetes.

You can Install all this by doing the below steps one by one. and these steps are for Ubuntu AMI.

# For Docker Installation
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER && newgrp docker
sudo chmod 777 /var/run/docker.sock

# 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

Step 1: Clone the Source Code

Here do this all step in Instance with t2.micro configuration.

Start by cloning the source code for the Reddit clone. You can do this with the following command:

git clone https://github.com/harshitsahu2311/reddit-clone-kubernetes-projects

Step 2: Containerize the Application with Docker

Next, create a Dockerfile to containerize the application. Use the following content for the Dockerfile:

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

Now, build the Docker image by running the command below. Replace <DockerHub_Username> and <Imagename> with your DockerHub username and the image name you want to assign:

docker build -t <DockerHub_Username>/<Imagename> .

Step 4: Push the Image to DockerHub

Once the image is built, push it to DockerHub:

  1. Log in to DockerHub:

     docker login -u <DockerHub_Username>
    
  2. Push the image:

     docker push <DockerHub_Username>/<Imagename>
    

Alternatively, you can use the existing Docker image: harshitsahu2311/reddit-clone-app.

Step 5: Write Kubernetes Manifest Files

Kubernetes acquires high memory and also needs more CPUs that’s why we will do all this steps in our Instance with t2.medium configuration.

Deployment.yml

Create a deployment file (Deployment.yml) to define the pods for the application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: Reddit-clone-deployment
  labels:
    app: kuber-project
spec:
  replicas: 2
  selector:
    matchLabels:
      app: kuber-project
  template:
    metadata:
      labels: 
        app: kuber-project

    spec:
      containers:
      - name: result-app
        image: harshitsahu2311/reddit-clone-app
        ports:
        - containerPort: 3000

Service.yml

Create a service file (Service.yml) to expose the application:

apiVersion: apps/v1
kind: Service
metadata:
  name: Reddit-clone-service
  labels:
    app: kuber-project
spec:
  type: NodePort
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 31000
  selector:
    app: kuber-project

Step 6: Deploy the Application to Kubernetes

Now that the manifest files are ready, you can deploy the application to Kubernetes. Run the following commands:

kubectl apply -f Deployment.yml
kubectl apply -f Service.yml

Check the status of your deployment and service:

kubectl get deploy,svc

You can check it now also by doing Step 8. Directly and accessing it with <public_ip>:3000 but I am doing it with ingress only.

Step 7: Configure Ingress

Ingress in Kubernetes allows external traffic to access services within the cluster by defining routing rules. Let's configure ingress for the Reddit clone.

First, enable ingress in Minikube (if you're using Minikube):

minikube addons enable ingress

Next, create an ingress.yml file:

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

Apply the ingress configuration:

kubectl apply -f ingress.yml

Verify the ingress setup:

kubectl get ingress ingress-reddit-app

Step 8: Expose and Test the Application

To expose your deployment, run:

kubectl expose deployment reddit-clone-deployment --type=NodePort
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &

You can now access the application via the Minikube IP and the NodePort (31000):

curl -L http://<minikube_ip>:31000

To test the ingress, use:

curl -L domain.com/test

You can also access the deployed application via the EC2 instance's IP address on port 3000 (ensure port 3000 is open in your EC2 security group).

Congratulations! You have successfully deployed a Reddit clone on Kubernetes with ingress enabled.

Congratulations Buddy Congrats GIF - Congratulations Buddy Congrats ...

0
Subscribe to my newsletter

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

Written by

Harshit Sahu
Harshit Sahu

Enthusiastic about DevOps tools like Docker, Kubernetes, Maven, Nagios, Chef, and Ansible and currently learning and gaining experience by doing some hands-on projects on these tools. Also, started learning about AWS and GCP (Cloud Computing Platforms).