In this project, we shall clone the code from GitHub, build an image of it through docker, push it to the docker repository and then deploy and scale it through k8s
Pre-requisites:
CI server- AWS t2.medium instance: AMI-Ubuntu
CD server-AWS t2.medium instance: AMI- Ubuntu
Docker installed and running
Dockerhub account
Code from GitHub
kubeadm installed
I am using a kubeadm setup wherein the CI process which involves code clone, build and pushing to the docker hub happens in the worker node, hereafter CI server
And we will deploy the application in the k8s master node, here after the deployment server.
On CI-server(worker node)
- Let us first clone the Reddit clone project from GitHub
git clone https://github.com/sowmya-bm/reddit-clone-k8s-ingress
- Create a docker file
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm","run","dev"]
- Build a docker image and push it to DockerHub
docker build . tag <username>/<imagename>
docker login
docker push <username>/<imagename>:latest
The the CI part of deployment is done. We shall move to deploy this Reddit clone through k8s cluster
On Deployment-server(Master node)
- Create a deployment file, let us keep the no of replicas 2
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
- Run the following command
kubectl apply -f deployment.yml
- Create a 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
- Run the following command
kubectl apply -f service.yml
- Expose the deployment and service
kubectl expose deployment reddit-clone-deployment --type=NodePort
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
- Open port 3000 in the deployment server security group and VOILAA your app will be running on port 3000 of the Deployment server IP address