Reddit clone deployment through k8s

Reddit clone deployment through k8s

·

2 min read

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:

  1. CI server- AWS t2.medium instance: AMI-Ubuntu

  2. CD server-AWS t2.medium instance: AMI- Ubuntu

  3. Docker installed and running

  4. Dockerhub account

  5. Code from GitHub

  6. 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)

  1. Let us first clone the Reddit clone project from GitHub
git clone https://github.com/sowmya-bm/reddit-clone-k8s-ingress
  1. Create a docker file
FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone
RUN npm install

EXPOSE 3000
CMD ["npm","run","dev"]
  1. 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)

  1. 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
  1. Run the following command
kubectl apply -f deployment.yml
  1. 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
  1. Run the following command
kubectl apply -f service.yml
  1. 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 &
  1. 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