πReddit Clone with Kubernetes Ingress
Table of contents
- πFlow of the Project
- Step 1: Prerequisites
- Step 2: Installation in first Instance
- Step 3: Build Dockerfile
- Step 4: Push the Image to DockerHub
- Step 5: Now, Installation in Second Instance
- Step 6: Deploy Reddit Clone Application
- Step 7: Application works or not
- Step 8: Expose the Application
- Step 9: Let's Configure Ingress
πFlow of the Project
Step 1: Prerequisites
GitHub
2 EC2 instances with minimum t2.medium instance type.
Docker & DockerHub
Minikube - It is a lightweight version of Kubernetes that runs a single-node cluster on a local machine.
Kubectl - It is a command line tool that interacts with Kubernetes clusters.
Step 2: Installation in first Instance
- Install Docker in the First Instance (CI Server)
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
- After Installation, Clone the GitHub Repository in First Instance (CI Server)
git clone https://github.com/mandgepratik/reddit-clone-k8s-ingress.git
- Check, whether Repository clone or not
ls # To check Reddis Clone Repository
Step 3: Build Dockerfile
- Here is a Dockerfile,
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm","run","dev"]
- To build the Dockerfile, use Command:
docker build . -t andropython/reddit-clone #<DockerHub_Username>/<Imagename>
Step 4: Push the Image to DockerHub
After build, push this Docker Image to DockerHub so our Deployment file can pull this image & run the app in Kubernetes pods.
First login to your DockerHub account using Command i.e
docker login
and give your username & password.After Login Succeeded, Push your Docker Image to DockerHub using command:
docker push andropython/reddit-clone:latest #Here is a latest docker image name
Now, Go to your DockerHub account your image will be push in DockerHub:
Step 5: Now, Installation in Second Instance
Install Docker in the Second Instance (Deployment Server)
sudo apt-get update sudo apt-get install docker.io -y sudo usermod -aG docker $USER && newgrp docker
After Installing Docker, Install the Minikube in the Second Instance (Deployment Server)
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 6: Deploy Reddit Clone Application
For Deploy the Docker Image, You should make one directory in that, you need to write Deployment.yml file:
apiVersion: apps/v1 kind: Deployment metadata: name: reddit-clone-deployment labels: app: reddit-clone spec: replicas: 2 # For Scaling, You can increase replicas as per your requirement selector: matchLabels: app: reddit-clone template: metadata: labels: app: reddit-clone spec: containers: - name: reddit-clone image: andropython/reddit-clone ports: - containerPort: 3000
Now, Write Service.yml file.
apiVersion: v1 kind: Service metadata: name: reddit-clone-service labels: app: reddit-clone spec: type: NodePort #type of service ports: - port: 3000 targetPort: 3000 #App runs on 3000 port. nodePort: 31000 selector: app: reddit-clone
Now, use command to deploy the Deployment.yml and Service.yml file:
kubectl apply -f Deployment.yml #it configured the deployment kubectl apply -f Service.yml #it creates the services
To check, pods are ready or not use command:
kubectl get deployment
Step 7: Application works or not
To access your application, use command:
minikube service reddit-clone-service --url # you'll get URL
To run your application in local, use command:
curl -L above generated url #Paste here above url, your app will runs locally.
Step 8: Expose the Application
First we need to expose our deployment so, use command:
kubectl expose deployment reddit-clone-deployment --type=NodePort #Your service will get expose
Now, go to your Instance (Deployment Server), Open Security group->inbound rules->Create new inbound rule for 3000 port:
Now, you need to port forwarding to expose application:
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 & # & - means your application runs in background
Step 9: Let's Configure Ingress
Ingress - In Kubernetes, an ingress is an API object that controls how internet traffic reaches services within a Kubernetes cluster. It consists of an ingress API object and an ingress controller, which acts as a reverse proxy and load balancer. The ingress controller receives configuration requests from the ingress and adjusts its configuration to fulfill the request.
To enable ingress in minikube, use command:
minikube addons enable ingress #You'll get ingress is enable
Now, write 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
Now, Apply ingress.yml using command:
kubectl apply -f ingress.yml #You'll get ingress is created.
To check, app runs or not enter:
curl -L domain.com/test #Your app running locally
- You can also see the deployed application on YourIP:3000
πFinally,You have successfully Deployed a Reddit Copy on Kubernetes with Ingress Enabled.
I believe this blog will be really helpful, giving you fresh perspectives and teaching you something new and interesting. π
π Enjoy learning!
πCopyright Β© Pratik R. Mandge, 2024. All rights reserved.
This article and its content are protected by copyright law. Reproduction or distribution of this article without the author's permission is prohibited. For inquiries, please contactpratikmandge021@gmail.com
Subscribe to my newsletter
Read articles from Pratik R. Mandge directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pratik R. Mandge
Pratik R. Mandge
Hey there! π I'm Pratik R. Mandge, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently on a learning adventure, I'm here to share my journey and Blog's in the world of cloud and DevOps. π οΈ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space. π Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology! Follow me on LinkedIn: https://www.linkedin.com/in/pratik-mandge363