Container orchestration with Kubernetes has become a standard for deploying and managing applications efficiently. In this detailed guide, we will walk you through the process of deploying a Reddit clone web application onto a Kubernetes cluster hosted on AWS. By following these steps, you will not only learn the deployment process but also gain insights into the power of Kubernetes for managing containerized applications.
This Reddit clone web application deployed on a Kubernetes cluster has several potential use cases with minimal modifications:
-
Multi-Tenant Web Hosting: Host multiple instances of the Reddit clone for different users or organizations within the same Kubernetes cluster.
-
Load Balancing: Easily scale your Reddit clone horizontally by adjusting the number of replicas in the Deployment, ensuring high availability and improved performance.
-
Microservices Architecture: Extend this project by breaking down your Reddit clone into microservices, each running in its own container, and manage them efficiently with Kubernetes.
-
API Gateway: Integrate an API gateway to manage and secure access to different services within your application, making it more robust and secure.
Begin by accessing the AWS console and creating two instances with specific configurations:
Deployment Server:
- AMI: Ubuntu
- Type: t2.medium
Ensure that you name these instances accordingly for easy reference.
Clone the codebase of your Reddit clone web application.
On Deployment Server:
- Update the server:
sudo apt-get update
- Install Docker:
sudo apt-get install docker.io -y
- Give permission to Docker:
sudo usermod -aG docker $USER && newgrp docker
- Install Minikube and 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
Create a Dockerfile for your Reddit clone project with the following content:
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm","run","dev"]
Build the Docker image from the Dockerfile using the following command:
docker build . -t sumanprasad007/reddit-clone
After building the image, push it to Docker Hub by running:
docker login
# Enter your username and password
docker push sumanprasad007/reddit-clone
SSH into the Deployment Server.
Ensure Minikube and Kubectl are installed on the Deployment Server.
Now, it's time to deploy the Docker image from Docker Hub onto your Kubernetes cluster.
Create a folder named "K8s" on the Deployment Server.
Inside the "K8s" folder, create a file named "Deployment.yml" and add the following content:
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: trainwithshubham/reddit-clone
ports:
- containerPort: 3000
Apply the Deployment file to create pods:
kubectl apply -f Deployment.yml
To ensure that the deployments are successful, use the following command:
kubectl get deployments
Now, you need to create a Service to provide an IP address for your application within the cluster.
Inside the "K8s" folder, 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 to create a service:
kubectl apply -f Service.yml
To verify that the service is created successfully, run:
kubectl get services
You can now access your application by visiting the URL provided by Minikube:
minikube service reddit-clone-service — url
To make your application accessible from the internet, you'll need to configure port forwarding.
Run the following command for port forwarding:
kubectl port-forward svc/reddit-clone-service 3000:3000 –address 0.0.0.0
Visit your Reddit Clone web app at the provided domain or IP address, and it should now be accessible to the world.
Congratulations! 🎉 You've successfully deployed a Reddit clone web app on a Kubernetes cluster hosted on AWS. This guide has covered the entire process, from provisioning AWS instances to advanced routing using Ingress. Kubernetes offers powerful capabilities for container orchestration, making it an essential tool for modern application deployment and management. Explore the various use cases to adapt this project to your specific needs.
This README.md file provides a comprehensive step-by-step guide for deploying a Reddit Clone Web App on a Kubernetes cluster hosted on AWS, including the additional steps you requested.