Deployment of Reddit-Clone application on Kubernetes with Ingress enabled

Deployment of Reddit-Clone application on Kubernetes with Ingress enabled

To deploy the reddit-clone application we need 2 EC-2 Servers:

  • CI server

  • Deployment server

  • Let's setup first CI Server.

Install Docker and give permission to Docker user by following commands:

sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker

  • Clone the source code from Git Hub Repository to CI server

git clonehttps://github.com/rushikesh-rawool10/reddit-clone-k8s-ingress.git

git clone <reddit-app url>

Now go to reddit-clone-k8s folder

cd reddit-clone-k8s-ingress

  • Create a Dockerfile using command: vim Dockerfile
FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone
RUN npm install

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

Build the Dockerfile

create an image and push it to Dockerhub

Use following commands:

docker build . -t reddit-clone
docker tag reddit-clone rushidevops10/reddit-clone:latest
docker login -u "username" -p "password" docker.io
docker push rushidevops10/reddit-clone:latest

Image successfully pushed to Docker Hub

  • CI server set-up has been done, Now configure the Deployment server.

  • Install the following prerequisites on the Deployment server:

    1. EC2 ( AMI- Ubuntu, Type- t2.medium )

    2. Docker

    3. Minikube

    4. kubectl

 # Steps:-

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

 # 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

  • Write a Deployment file for the application 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: rushidevops10/reddit-clone:latest
        ports:
        - containerPort: 3000

You can see your deployments by following the command:

  kubectl apply -f Deployment.yml
  kubectl get Deployment

  • Now create a Service file by following commands:

  • Write Service file 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

Please make sure you have opened port no. 31000 of deployment server

Create URL link for reddit app to access:

      minikube service reddit-clone-service --url
      curl -L http://192.168.49.2:31000

Let's Configure Ingress

In Kubernetes, an Ingress is a resource that manages external access to services within the cluster. It acts as a layer 7 (application layer) load balancer, allowing you to define rules for routing traffic to different services based on factors such as hostnames, paths, and more. Ingress is particularly useful for exposing HTTP and HTTPS services to the outside world.

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
  • To use Ingress in your Kubernetes cluster, you'll need to:

    1. Deploy an Ingress Controller of your choice.

    2. Create Ingress resources to define the routing rules for your services.

    3. Ensure DNS records or host file entries point to the appropriate IP address for your Ingress Controller.

    4. Secure your services with TLS if needed by configuring TLS secrets in your Ingress resource.

In summary, Ingress services in Kubernetes provide a way to manage external access to services in your cluster, making it easier to expose applications and services to the internet while providing routing and security features.

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

Now you can able to create ingress for your service. kubectl apply -f ingress.yml

  • To check ingress service status by using

    kubectl get ingress ingress-reddit-app command.

Now expose the app

Expose the deployment by using

kubectl expose deployment reddit-clone-deployment --type=NodePort command.

You can test your deployment using curl -http://http://13.212.49.44/:31000. 13.212.49.44 is a deployment server (minikube)ip & Port 31000 is defined in Service.yml

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

Test Ingress

Now let's test our ingress so use the curl -L domain.com/test command in the terminal.

You can also see the deployed application on Ec2_ip:31000

Please Make sure you have opened the 31000 port in a security group of your Ec2 Instance.

Finally, we have successfully deployed a Reddit Clone app on Kubernetes with Ingress Enabled!

Happy Learning!