Skip to main content

Command Palette

Search for a command to run...

Kubernetes Assignment 2

Deployment of Reddit-Clone app

Published
4 min readView as Markdown
Kubernetes Assignment 2

This is an Assignment No. 2 for Batch 4 on Kubernetes. Let's complete this assignment...

About the project:-

In this assignment, we'll deploy a Reddit copy on the K8s cluster(Minikube) with ingress Enabled.

Steps:-

  1. Clone the code

  2. Containerize the application

  3. Build image

  4. Push the image to DockerHub

  5. Write a Deployment Manifest files

  6. Write a Service Manifest files

  7. Create deployment and service in the K8s cluster

  8. Configure Ingress

  9. Expose the app

Prerequisite:-

  • Docker with appropriate user permissions

  • Minikube

  • Kubectl

1- Clone the code

First, we have to clone the code for the Reddit app to run. You can clone from the repository URL given below.

Repository URL:- https://github.com/amitvpawar/reddit-clone-k8s-ingress.git

Use git clone https://github.com/amitvpawar/reddit-clone-k8s-ingress.git command to clone in the local repository.

2- Containerize the application

The Dockerfile should be as follows

FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone
RUN npm install 

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

3- Build Image

To build an image use the above Dockerfile. Use docker build -t amitvpawar/redditclone . command. (I am tagging the image as my DockerHub Username at the time of building the image to avoid further tagging.) Check the created image by docker images command

4- Push the image to DockerHub

We have to push the created image to the remote repository (DockerHub).

  • First login to DockerHub. Use the docker login command. Enter your credentials or it will authenticate with existing credentials.

  • After successful login use docker push amitvpawar/redditclone command.

I am going to use an existing docker image for deployment. trainwithshubham/reddit-clone

We have to create two manifest files for Deployment and Service objects.

5- Write a Deployment Manifest file

Create deployment.yml as shown below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: reddit-deploy
  labels:
    app: redditclone
spec:
  replicas: 2
  selector:
    matchLabels:
      app: redditclone
  template:
    metadata:
      labels:
        app: redditclone
    spec:
      containers:
        - name: redditclone-cont
          image: trainwithshubham/reddit-clone
          ports:
            - containerPort: 3000

6- Write a Service Manifest files

Create service.yml as shown below

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

7- Create deployment and service in the K8s cluster

To create a deployment using the deployment.yml file use kubectl apply -f deployment.yml and for service to create use kubectl apply -f service.yml command. To check deployment and service use kubectl get deployment and kubectl get svc commands resp.

You can access your Reddit Clone by using the Minikube IP and Port given

i.e. http://192.168.49.2:31000

8- Configure Ingress

What is Ingress:- Ingress is a K8s object that allows external users to access services running inside a K8s cluster. It acts as a gateway or entry point for incoming traffic to reach the services running inside the cluster.

Purpose:- As ingress allows external users to access services running inside a K8s cluster. It provides a way to route traffice to different services based on the URL path or domain name and also enables the use of SSL/TSL encryption for secure communication.

Let's configure Ingress.

First, enable ingress addons in Minikube using minikube addons enable ingress command and create ingress by using kubectl appy -f ingress.yml command.

If you use curl -L domain.com/test command it gives an error.

9- Expose the app

To use this application with local system IP and port 3000 do forward or expose 3000 port as follows. Use kubectl port-forward svc/reddit-service 3000:3000 --address 0.0.0.0 & command.

Now access the Reddit clone on localIP:3000

This is how we can deploy Reddit clones on Minikube.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Thank you so much Shubham Londhe Bhaiyya for this assignment.

Happy Learning :)