Skip to main content

Command Palette

Search for a command to run...

Deploying a Reddit Clone Web App on Kubernetes Cluster on AWS

Published
6 min read
Deploying a Reddit Clone Web App on Kubernetes Cluster on AWS
V

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!

👋Introduction

Kubernetes has emerged as the industry standard for orchestrating containers and optimizing application management.

In this comprehensive guide, we will take you through the intricate steps of deploying a web application resembling Reddit on an AWS-hosted Kubernetes cluster.

By following these instructions, you will not only grasp the deployment intricacies but also discover the robust capabilities of Kubernetes in efficiently handling containerized applications.

📚Prerequisites

Before you begin, ensure you have the following tools installed on your local machine:

  1. Deployment Server:

    • AMI: Ubuntu

    • Type: t2.medium

  2. Docker: To containerize our Reddit clone app.

     sudo apt-get update  #Update the server
     sudo apt-get install docker.io -y #Install Docker
     sudo usermod -aG docker $USER && newgrp docker #Give permission to Docker
     sudo docker --version #Docker Version
    

  3. Minikube Cluster: Make sure it's up and running.

  4. kubectl: For interacting with your Kubernetes cluster

     #Download the latest Minikube binary.
     curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 
     #Install Minikube
     sudo install minikube-linux-amd64 /usr/local/bin/minikube
     #Install kubectl via Snap package manager
     sudo snap install kubectl --classic
     # Start Minikube using the Docker driver
     minikube start --driver=docker
    

  5. Git: To clone the necessary repository.

     git clone https://github.com/VishalPhadnis/reddit-clone-k8s-ingress.git
    

🐳Dockerfile for the Project

Create a Dockerfile for your Reddit clone project with the following content

# Use the official Node.js image with version 19 based on Alpine Linux 3.15 as the base image.
FROM node:19-alpine3.15

# Set the working directory inside the container to /reddit-clone.
WORKDIR /reddit-clone

# Copy the contents of the current directory into the container's /reddit-clone directory.
COPY . /reddit-clone

# Install the Node.js application's dependencies using npm.
RUN npm install

# Expose port 3000 to allow external access to the Node.js application.
EXPOSE 3000

# Define the default command to run when the container starts.
CMD ["npm", "run", "dev"]

This Dockerfile is used to create a Docker image that sets up an environment for running a Node.js Reddit clone application.

It starts with an official Node.js base image, sets the working directory, copies the application code into the container, installs dependencies using npm, exposes port 3000 for external access, and specifies the default command to run the application (npm run dev)

🚀Build and Push Docker Image

Build the Docker image from the Dockerfile using the following command:

docker build -t vishalphadnis/reddit-clone-app .

After building the image, push it to Docker Hub by running:

# Enter your dockerhub username and password
docker login
docker push vishalphadnis/reddit-clone-app

📝Create a Deployment YAML File

Create a file named "deployment.yml" and add the following content & create the namespace

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: vishalphadnis/reddit-clone-app
          ports:
            - containerPort: 3000

📜Apply the Deployment File

Apply the Deployment file to create pods:

kubectl apply -f deployment.yml -n reddit-app

🔍Verify Deployments

To ensure that the deployments are successful, use the following command:

kubectl get deployment -n reddit-app

🌐Create a Kubernetes Service

Now, you need to create a Service to provide an IP address for your application within the cluster.

📝Create a Service YAML File

Create a file named "service.yml" and add the following content:

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

📜Apply the Service File

Apply the Service file to create a service:

kubectl apply -f service.yml -n reddit-app

🌐Check the Service

To verify that the service is created successfully, run:

kubectl get services

🚀Access the Application

You can now access your application by visiting the URL provided by Minikube:

minikube service reddit-clone-service -n reddit-app created --url

🌍Expose the Application to the Internet

To make your application accessible from the internet, you'll need to configure port forwarding.

kubectl expose deployment reddit-clone-deployment -n reddit-app --type=NodePort

This command creates a new service and exposes it as a NodePort service in the reddit-app namespace.

🔄Port Forwarding

Run the following command for port forwarding:

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

🌐Access Your Live Reddit Clone

Visit your Reddit Clone web app at the provided domain or IP address, and it should now be accessible to the world.

📍Enable Ingress in Minikube

In Kubernetes, Pods and services have their internal IPs, which are typically not exposed directly to the external internet. Even though there may be a service with a node IP configured, the port on the node IP cannot be shared across multiple services, making it challenging to manage which port corresponds to which service.

Additionally, nodes in a Kubernetes cluster can come and go, so it's not practical to assign a static node IP to an external service. Ingress provides a solution by defining a set of rules that allow inbound connections to access services within the Kubernetes cluster. It acts as an entry point for external traffic at the application layer (Layer 7) and allocates and forwards traffic to service ports on each virtual machine (VM).

📃Create 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

This YAML defines an Ingress resource that directs traffic based on the hostnames "domain.com" and "*.domain.com" to the "reddit-clone-service" with a path prefix of "/test" and port number 3000.

The first rule applies to "domain.com," while the second rule is a wildcard rule for subdomains of "domain.com."

📚To enable Ingress in Minikube

minikube addons enable ingress

❄Current settings of addons in Minikube

To display the list of enabled and disabled Minikube addons, you can use the following command When you run this command.

It will show you a list of available addons and their current status (enabled or disabled) in your Minikube cluster.

minikube addons list

📃Create and verify an Ingress resource

This command will first apply the ingress configuration from the ingress.yml file to the "reddit-app" namespace and then immediately display the created ingress resources using kubectl get ingress.

kubectl apply -f ingress.yml -n reddit-app 
kubectl get ingress

📖Test ingress

Use the following command

curl -L domain.com/test

💥Conclusion

We've accomplished the deployment of a Reddit clone web application on an AWS-hosted Kubernetes cluster.

This comprehensive guide has walked you through the entire journey, starting from AWS instance provisioning to advanced routing with Ingress. Kubernetes delivers robust container orchestration capabilities, establishing itself as a vital resource for contemporary application deployment and administration.

Delve into the multitude of use cases to tailor this project to your unique requirements and objectives.

🚀 Stay tuned for more insightful Kubernetes blogs in the days ahead! 🎉

Thank you for joining us on completing this assignment in the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.

So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashcode.dev

LinkedIn: linkedin.com/in/vishalphadnis

A

This article was very much helpful whenever I got stucked .

Thank you!

1
V

Thank you.. Happy learning