🚀Deploying a Reddit Clone App using Kubernetes☸️Ingress and Ingress Controllers 🌐

🚀Deploying a Reddit Clone App using Kubernetes☸️Ingress and Ingress Controllers 🌐

Deploying a Reddit Clone using Kubernetes with Ingress Enabled

🎆 Introduction

In a Kubernetes environment, there are many different ways to expose your application to the outside world. One of the most popular methods is through the use of a load balancer or an ingress controller. In this blog post, we will learn about what is Ingress & Ingress Controller in K8's.

Also, we will deploy the Reddit Clone application using Ingress.

A Beginner's Guide to Ingress and Ingress Controllers in Kubernetes

🤔 What is Ingress?

Ingress is a Kubernetes resource that provides a way to expose HTTP and HTTPS routes from outside the cluster to services within the cluster. It acts as a reverse proxy, routing incoming traffic to the appropriate service based on the URL path and host specified in the request. In simpler terms, Ingress provides a way to manage external access to your services in a more intelligent and flexible way than a simple load balancer.

📍 Key features of Ingress

  • Host-Based Routing: Ingress allows you to route traffic based on the hostnames requested by clients. This enables hosting multiple services on the same cluster with different domain names.

  • Path-Based Routing: You can route traffic based on the path of the URL requested, allowing you to direct requests to specific services based on their paths.

  • TLS Termination: Ingress can also handle TLS termination, allowing you to encrypt traffic between clients and the Ingress Controller.

  • Custom routing: Some Ingress Controllers allow you to define custom routing rules using middleware. Middleware allows you to add additional functionality to your Ingress rules, such as rewriting URLs or modifying headers.

🤔 What is Ingress Controller?

An Ingress Controller is a Kubernetes resource that runs as a pod in the cluster and is responsible for implementing the rules defined in Ingress resources. In other words, the Ingress Controller is the component that handles the traffic routing based on the Ingress rules.

There are many different Ingress Controllers available for Kubernetes, each with its unique features and limitations. Some of the most popular Ingress Controllers include:

  • Nginx Ingress Controller: A popular and versatile Ingress Controller based on the Nginx web server. It offers a wide range of features and is well-documented.

  • Traefik: A modern, dynamic Ingress Controller that supports multiple backends and automatic configuration updates as Ingress resources change.

  • HAProxy Ingress: Based on HAProxy, it provides advanced routing and load-balancing capabilities.

  • Kubernetes Native Ingress: Starting from Kubernetes 1.19, there is also a Kubernetes Native Ingress (sometimes referred to as kube-native-ingress) that uses the Service resources as a basis for routing traffic.

💁 How to Use Ingress and Ingress Controllers

Here are the basic steps to set up and use Ingress and an Ingress Controller in your Kubernetes cluster:

  1. Choose an Ingress Controller: Depending on your requirements and preferences, select an Ingress Controller that suits your needs. Install it in your cluster.

  2. Create Ingress Resources: Define Ingress resources in YAML files. These resources specify routing rules, paths, hostnames, and TLS configurations. Here's a simple example:

     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: my-ingress
     spec:
       rules:
         - host: myapp.example.com
           http:
             paths:
               - path: /app
                 pathType: Prefix
                 backend:
                   service:
                     name: my-service
                     port:
                       number: 80
       tls:
         - hosts:
             - myapp.example.com
           secretName: my-tls-secret
    
  3. Apply Ingress Resources: Use kubectl apply to create the Ingress resources in your cluster:

     kubectl apply -f my-ingress.yaml
    
  4. Check Ingress Status: Verify that the Ingress Controller has applied the rules by checking the Ingress status:

     kubectl get ingress my-ingress
    
  5. Access Your Application: Once everything is set up, you can access your application externally using the defined hostnames and paths.

🚀 Deploying a Reddit Clone App with Ingress 🚀

Following are the steps Deploy the Reddit clone application using K8's Deployment, Service and Nginx Ingress controller.

🛠️ Prerequisites

First You have to install some Prerequisites for this deployment. Click here to set up the prerequisite.

🌐 Provision AWS Instances

  1. We will create the 1 EC2 instance(Ubuntu) with the instance type t2.medium as it is the minimum required to run the Kubernetes cluster.

📂 Clone the source code

  1. Clone the Reddit clone web application code into the machine.

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

🐳 Write a Dockerfile for the Project

  1. Create the Dockerfile to containerize the application using dockerfile. Here is the Dockerfile.

     FROM node:19-alpine3.15
    
     WORKDIR /reddit-clone
    
     COPY . /reddit-clone
     RUN npm install
    
     EXPOSE 3000
     CMD ["npm","run","dev"]
    

⚙️Build Docker Image

  1. Build the Docker image from the Dockerfile using the following command.

     docker build -t <dockerhub-username>/<dockerhub-repo-name> .
    

🔄Push Docker Image to DockerHub

  1. We will push our created application image into the docker hub repository.

  2. Make sure you have the DockerHub account created.

     docker login
     # Enter your username and password
     docker push <dockerhub-username>/<dockerhub-repo-name>:tagname
    

✍Write a Kubernetes Manifest File

  1. 📝Create Deployment YAML:

     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: deepak1603/reddit-clone-app
               ports:
                 - containerPort: 3000
    
  2. 📜Apply the Deployment File and Verify:

    Apply the Deployment Yaml file to create the deployment, pods, and replica set.

     kubectl apply -f <deployment-yaml-file>
    

  3. 📝Create Service YAML:

    We need to create a NodePort Service to access our application to the outside world.

     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
    
  4. 📜Apply the Service File and Verify:

    Apply the Service Yaml file to create the service.

     kubectl apply -f <service-yaml-file>
    

🚀 Access the Application

  1. You can now access your application by visiting the URL provided by Minikube:

     minikube service <service-name> -- url
    

🌍 Expose the Application to the Internet

  1. To make your application accessible from the internet, you'll need to configure port forwarding.

  2. Run the following command for port forwarding:

     kubectl port-forward svc/<service-name> <local-machine-port>:<service-port> --address 0.0.0.0 &
    
  3. Make sure port 3000 is open in your EC2 instance security group.

  4. Copy the EC2 instance public IP address and paste in the browser along with port 3000 like <EC2-IP>:3000

Congratulations! 🥇🎉 You've successfully deployed a Reddit clone web app on a Kubernetes cluster hosted on AWS.

🎮 Let's Configure NGINX Ingress ⚙️

Here we will use the NGINX Ingress controller.

  1. 🟢 Enable the ingress controller:

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

     minikube addons enable ingress
    

  2. Verify that the NGINX Ingress controller is running:

    Verify that the ingress resource is running correctly by using the command inside the ingress-nginx namespace.

     kubectl get pods -n ingress-nginx
    

  3. 📝 Create Ingress Resources:

    Define Ingress resources in YAML files. These resources specify routing rules, paths, hostnames, and TLS configurations. Here's an ingress resource file we will use for our Reddit application where we will redirect the traffic to reddit-clone-service:

     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
    
  4. 🗒️ Explanation:

    In the ingress resource file, we have specified the rules.

    • rules:: Defines the routing rules for incoming traffic.

    • host: Routes traffic for the exact hostname "domain.com" & Routes traffic for any subdomain of "domain.com".

    • http:: Specifies that the routing rules are for HTTP traffic.

    • paths:: Describes the specific paths to match under this host.

    • pathType: Prefix: Indicates that the path matching should be a prefix match.

    • path: "/test": Specifies the path prefix "/test".

    • backend:: Specifies the backend service to forward the matched traffic.

    • service:: Specifies the Kubernetes service to forward traffic to.

    • name: reddit-clone-service: Specifies the name of the Kubernetes service, which is "reddit-clone-service".

    • port:: Specifies the port of the service to which the traffic will be forwarded.

    • number: 3000: Specifies that traffic should be forwarded to port 3000 of the "reddit-clone-service".

  5. 📜Apply the Ingress Resource File and Verify:

     kubectl apply -f <ingress-sesource-yaml-file>
    

    Now we can see the host is domain.com,*.domain.com and it routed the traffic to the Ingress IP 192.168.49.2 .

🔎 Test ingress

  1. To test the application in the cluster using hostname use the command.

     curl -L domain.com/test
    

    Congratulations! 🥇🎉 You've successfully deployed a Reddit clone web app on a Kubernetes cluster hosted on AWS and we are also able to access the application in a cluster with the help of K8's Ingress and Ingress Controller using Hostname which is mapped with the our K8's Service and it will redirect the traffic to the Ingress IP address.

📒 Summary

In summary, Ingress and Ingress Controllers are essential tools for managing external access to services in Kubernetes. They provide a flexible and powerful way to route HTTP and HTTPS traffic to your applications, making it easier to manage and scale your containerized workloads.


Thank you🙏🙏... for taking the time to read this blog. I hope you found the information helpful and insightful. So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on

So, Stay in the loop and stay ahead in the world of DevOps!

Happy Learning !... Keep Learning ! 😊