Kubernetes Deployment of Reddit Clone

Kubernetes Deployment of Reddit Clone

With Ingress Enabled

Pre-requisites

1. EC2 Instance 1 -CI-Server( AMI- Ubuntu, Type- t2.micro, port 22, 443, 80 should be enabled.)

2. Ec2 Instance 2 - Deployment Server (Ami-Ubuntu, Type-t2.medium, port 22, 80, 443,3000 should be enabled. )

3. Dockerhub Account

4. GitHub Account

5. Docker installed on both (CI-Server and deployment server)

6. Minikube and kubectl Installed on the deployment server.

Introduction

In the current high-speed software development landscape, possessing a streamlined Continuous Integration and Continuous Deployment (CI/CD) pipeline is crucial. This pipeline automates the procedure of constructing, examining, and launching software, empowering developers to swiftly introduce new functionalities and enhancements. In this guide, we will guide you through the stages of configuring a CI/CD pipeline for a Reddit clone application utilizing AWS EC2 instances, Docker, Kubernetes, and Minikube.

Step 1: Setup the CI-Server

1. Launch an EC2 Instance (AMI- ubuntu, Type-t2.micro, port 22 and 80 should be enabled)

2. Update the System & Install Docker

Keep your server up-to-date by running & Install Docker to containerize your application

# Steps:-
# For Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

3. Clone the Reddit clone application source code from GitHub and Navigate to the project directory.

Fork this URL

https://github.com/msnehabawane/reddit-clone-k8s-ingress.git

4. Create the Docker image from the Docker file.

5. Once the image has been created check the docker images.

6. After creating a Docker image push it to the DokcerHub. First, log in to the DockerHub using CLI.

Before pushing the image to the DockerHub tag the image first using command

docker tag reddit-clone-image nehabawane/reddit-clone-image

7. Once the image has been pushed to DockerHub check it out in the DockerHub Dashboard.

Step 2: Setup the Deployment Server

1. Launch an another EC2 Instance (AMI- ubuntu, Type-t2.medium, port 22,80,443, 3000 should be enabled)

2. In Deployment Server, Update the system and install Docker.

# Steps:-
# For Docker Installation
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
docker -v

3. Installation of Minikube & Kubectl on Deployment Server.

# 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

4. Let’s create a Directory Name with k8-project to write a required Manifest file.

5. Create Namespace for Reddit-App.

6. Let's Create a Deployment File For our Application. (Deployment.yml)

Now, You might be wondering what this manifest file in Kubernetes is. Don't worry, I'll tell you in brief.

When you are going to deploy to Kubernetes or create Kubernetes resources like a pod, replica-set, config map, secret, deployment, etc., you need to write a file called manifest that describes that object and its attributes either in YAML or JSON. Just like you do in the Ansible playbook.

Of course, you can create those objects by using just the command line, but the recommended way is to write a file so that you can version control it and use it in a repeatable way.

create Deployment.yml file and open it in vim editor

vim Deployment.yml

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: nehabawane/reddit-clone-image
        ports:
        - containerPort: 3000

7. Run kubectl apply -f Deployment.yml to create the deployment object in K8s. You should see an output similar to this.

8. Let's dive into what is Kubernetes Service manifest file, It defines how network traffic should be routed to a set of Pods. Services provide stable endpoints to access your application running in a Kubernetes cluster. Below is an example of a Kubernetes Service manifest file:

create service.yml file and open it in vim editor.

vim 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

9. Run kubectl apply -f service.yml to create this service in K8s. Run Kubectl get svc to check if the service has been created, the output should look like this

10. Get the URL: minikube service reddit-clone-service --url

Step 3: Expose the App

before exposing the app ensure that the 3000 Port on the Deployment Server Security Group should be enabled.

  1. First We need to expose our deployment so use kubectl expose deployment reddit-clone-deployment --type=NodePort command.

  2. 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

  3. Then We have to expose our app service kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &

You can see the deployed application on [Ec2_instance_public_ip]:3000

43.204.142.186:3000

You have successfully Deployed a Reddit Clone App on Kubernetes with Ingress Enabled.

Step 4: Let's Configure Ingress

In Kubernetes, an Ingress is like a gatekeeper for incoming network traffic. It helps manage how external users access services inside the cluster. With Ingress, you can set rules to direct traffic based on things like web addresses (hostnames) or specific paths in the URL. This means you can easily route requests to different parts of your application. Ingress also takes care of secure communication by handling SSL/TLS certificates for you. It's a handy tool for efficiently managing external access to your Kubernetes services.

1. Minikube doesn't enable ingress by default; we have to enable it first using the minikube addons enable ingress command.

2. Once enabled the ingress create the ingress for your service and verify it. Let's write ingress.yml and put the following code in it

vim ingress.yml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-reddit-app
spec:
  rules:
  - host: "domain.com"
    http:
      paths:
      - pathType: Prefix
        path: "/test"
        backend:
          service:
            name: reddit-clone-service
            port:
              number: 3000
  - host: "*.domain.com"
    http:
      paths:
      - pathType: Prefix
        path: "/test"
        backend:
          service:
            name: reddit-clone-service
            port:
              number: 3000
  • If you want to check the current setting for addons in minikube use minikube addons list command.

  • Now you can able to create ingress for your service. kubectl apply -f ingress.yml uses this command to apply ingress settings.

  • Verify that the ingress resource is running correctly by using kubectl get ingress ingress-reddit-app command

Conclusion

Setting up your Reddit clone application involves configuring a CI server, creating Docker images, and deploying your app using Kubernetes and Minikube. With this pipeline in place, you can streamline your development and deployment processes, making it easier to deliver new features and updates to your users.

Happy Learning

Thanks For Reading!

Neha Bawane