Deploy a Reddit Clone on Kubernetes

Deploy a Reddit Clone on Kubernetes

ยท

2 min read

Step 1: Clone the source code

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/umer6921/reddit-clone-k8s.git

Step 2: Containerize the application using Docker

Write a Dockerfile with the following instructions


FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone

RUN npm install 

EXPOSE 3000

CMD ["npm","run","dev"]

Step 3: Build the Docker image

Build the image from the Dockerfile by using this command docker build . -t <image-name>

Step 4: Push the image to DockerHub

Once you have built the image, it's necessary to upload it to Dockerhub for it to become accessible to others.

  1. Login to your dockerhub by using docker login and give your username and password

  2. Use docker push <DockerHub_Username>/<image-name> for pushing to the DockerHub

๐Ÿ˜Š๐Ÿ˜‰ The code-building process has been finalized, and it's now time to engage with Kubernetes.

Step 5: Write a Kubernetes Manifest file

What is Manifest file?

A Kubernetes manifest file is a text file that defines the desired state of a Kubernetes resource or object, such as a pod, service, or deployment. It contains specifications for the resource, including its configuration settings, metadata, and how it should be managed by Kubernetes.

Manifest files are used to declare what you want Kubernetes to create or manage, and Kubernetes uses these files to ensure that your applications and services run as intended in a cluster. Essentially, it's a blueprint that Kubernetes follows to deploy and manage your applications.

  • Write Deployment.yml 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:  umer6921/reddit-clone
        ports:
        - containerPort: 3000
  • Write Service.yml file

apiVersion: v1
kind: Service
metadata:
  name: reddit-clone-service
  labels:
    app: reddit-clone
spec:
  type: NodePort
  selector:
    app: reddit-clone
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 31000

Step 6: Create the deployment and service from yaml file

You have created the deployment and service file. Now you need to apply them, first deployment.yml by using kubectl apply -f Deployment.yml then kubectl apply -f Service.yml for service.

You can check your deployment and service by this command kubectl get deployment & kubectl get services

Step 7: Expose the app

  1. When you are using the minikube, you need to expose the deployment by using kubectl expose deployment reddit-clone-deployment --type=NodePort

  2. You can now test your deployment using curl -L http://192.168.49.2:31000

  3. If you want to access your deployment in outside world, then you have to expose app service by using command kubectl port-forward svc reddit-clone-service 3000:3000 --address 0.0.0.0

ย