In today's tech world, applications need to be scalable, reliable, and easy to manage. Kubernetes provides the necessary tools to achieve these goals, allowing businesses to deploy applications faster, manage them more efficiently, and reduce downtime. Kubernetes is a game-changer for modern software development and operations.
Before we begin with the Project, we need to make sure we have the following prerequisites installed:
EC2 (AMI-Ubuntu, 2-instances (CI-t3.micro) (Deployment-t3.large))
Docker
Minikube
kubectl
You can install all this by doing the below steps one by one. and these steps are for Ubuntu AMI.
# Steps:-
# For Docker Installation (for CI Instance)
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
# For Minikube & Kubectl (for Deployment Instance)
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
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 (CI-Instance)
The first step is to clone the source code for the app. You can do this by using this command.
git clone https://github.com/LondheShubham153/reddit-clone-k8s-ingress.git
2 . Write a Dockerfile with the following code:
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm","run","dev"]
Step 2: Building Docker-Image
Now it's time to build Docker Image from this Dockerfile.
# to build docker image
docker build -t <DockerHub_Username>/<Imagename>
Step 3: Push the Image To DockerHub
To push the image to Dockerhub. We want to login to your DockerHub account using a command.Then use for pushing command to the push image to the DockerHub.
# to login with dockerhub
docker login
# to push docker image to dockerhub
docker push <DockerHub_Username>/<Imagename>
Step 4: Write Deployment.yml file (in deployment instance)
Let's Create a Deployment File For our Application. Use the following code for the deployment.yml file.
# to create and view the deployment.yml file
vi deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: reddit-clone-deployment
labels:
app: reddit-clone
spec:
replicas: 4
selector:
matchLabels:
app: reddit-clone
template:
metadata:
labels:
app: reddit-clone
spec:
containers:
- name: reddit-clone
image: yashisrani05/reddit-clone
ports:
- containerPort: 3000
Write Service.yml file
# to create and view service.yml file
vi 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
Step 5: Deploy the app to Kubernetes & Create a Service For It.
To deploy the app you need to run following the command :
# to deploy deployment.yml file
kubectl apply -f deployment.yml
# to deploy service.yml file
kubectl apply -f service.yml
If You want to check your deployment & Service use the command:
# to check deployment
kubectl get deployment
#to check service
kubectl get services
Step 8) Expose the app
First We need to expose our deployment. so use below command:
kubectl expose deployment reddit-clone-deployment --type=NodePort
You can test your deployment, use below command:
curl -L http://<public-IP>:3000
Then We have to expose our app service.
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &