This is the simple architecture we are going to follow for our deployment:
Points:
CI Server: t2.micro
DockerHub: Create a repository
Deployment Server: t2.medium
Prerequisite:
Install Docker on CI Server
Minikube and Kubectl on Deployment Server
# For Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
# 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
Login into your CI Server Ec2 instance:
Steps:
Clone the GitHub repo: https://github.com/LondheShubham153/reddit-clone-k8s-ingress.git
Write a DockerFile:
FROM node:19-alpine3.15 WORKDIR /reddit-clone COPY . /reddit-clone RUN npm install EXPOSE 3000 CMD ["npm","run","dev"]
Build a Docker Image:
docker build -t <name-of-your-docker-repo> . docker build -t hiphopsid/reddit .
Login into the docker repo:
docker login #Login with your DockerHub Credentials docker push <imageName>
Login into your Deployment Server:
Steps:
Install Minikube and kubectl
- Check "kubectl get pods" if minikube is running.
Create a deployment file:
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: hiphopsid/reddit
ports:
- containerPort: 3000
Apply the deployment file:
kubectl apply -f <deploymentFileName>
Now, to provide access for the application we will create a Service.yaml file:
#Node Port Service:
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
Expose Deployment:
First We need to expose our deployment so use
kubectl expose deployment reddit-clone-deployment --type=NodePort
command.You can test your deployment using curl -L http://192.168.49.2:31000. 192.168.49.2 is a minikube ip & Port 31000 is defined in Service.yml
Then We have to expose our app service
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
- The difference b/w both is now the port will be exposed in the background and you can continue with your work.