Enhancing User Experience: Deploying a Reddit Clone Web App on a Kubernetes Cluster with Ingress
Table of contents
Kubernetes
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.
The name Kubernetes originates from Greek, meaning helmsman or pilot. K8s as an abbreviation results from counting the eight letters between the "K" and the "s". Google open-sourced the Kubernetes project in 2014. Kubernetes combines over 15 years of Google's experience running production workloads at scale with best-of-breed ideas and practices from the community.
Ingress
Ingress is a combination of Nginx & HA proxy. Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.
Pods and services in Kubernetes have their own IP; however, it is normally not the interface you'd provide to the external internet. Though there is a service with node IP configured, the port in the node IP can't be duplicated among the services. It is cumbersome to decide which port to manage with which service. Furthermore, the node comes and goes, it wouldn't be clever to provide a static node IP to an external service. Ingress defines a set of rules that allows the inbound connection to access Kubernetes cluster services. It brings the traffic into the cluster at L7 and allocates and forwards a port on each VM to the service port. This is shown in the following figure. We define a set of rules and post them as source type ingress to the API server. When the traffic comes in, the ingress controller will then fulfill and route the ingress by the ingress rules. As shown in the following figure, ingress is used to route external traffic to the Kubernetes endpoints by different URLs.
Prerequisites
Before we begin with the Project, we need to make sure we have the following prerequisites installed:
Two EC2 instances
Instance1: ( AMI- Ubuntu, Type- t2.micro )
Instance2: ( AMI- Ubuntu, Type- t2.xlarge )
Docker
Minikube
Kubectl
Install all this by doing the below steps one by one and these steps are for Ubuntu AMI.
# 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
Step-by-Step Implementation
Step 1: Launch an instance CI-server having Ubuntu AMI, t2.micro instance type(1CPU, 1GiB RAM).
Launch an instance Deployment-server having Ubuntu AMI, t2.xlarge instance type(4CPU, 16GiB RAM).
[Note: Here we have used instance type t2.xlarge for production or large-scale purposes, while we can use instance type t2.medium for small-scale purposes.]
Step 2: Connect the CI server instance and install Docker in it using the command.
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER && newgrp docker
Check its version via the docker --version
command.
Then, clone the repository from the developer's side and get the code ready on this server, as shown below.
Containerize the Application by using Dockerfile.
Step 3: Now build a docker image by using the docker build -t <DockerHub_username>/<image-name>
command.
Push the image to the DockerHub profile in this manner. First login to DockerHub using docker login
command then docker push <DockerHub_username>/<Image-name>:latest
Here in the DockerHub, we get the Docker image immediately.
Step 4: Connect another instance of the Deployment-server to perform the Kubernetes deployment portion of this project.
As we know, Minikube needs docker as a driver, install docker as well in this server.
Fire the commands on this server as shown below.
# 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
Verify the Docker and Minikube status in this way.
Step 5: Create a deployment manifest file to ensure auto-healing and continuous availability of our application, even if scalability limits increase, without any crashes.
Step 6: Create a service manifest file to access this application's cluster through its Cluster IP. Here we have used the "NodePort" service type.
Step 7: Check the application internally on this server by executing the curl command.
Follow the commands below to expose the application externally on a browser.
kubectl expose deployment reddit-clone-deployment --type=NodePort
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
Also, make sure to add port 3000 to its security group inbound rules.
Step 8: We can also see the deployed application on PublicIP:3000
Now let's learn how to configure it with Ingress.
Step 9: By default, Minikube does not enable ingress, so we need to enable it first using the command minikube addons enable ingress
.
We can also list addons in minikube by using minikube addons list
command.
Step 10: Create an ingress.yml manifest file with the code shown below, and apply it using the kubectl apply -f ingress.yml
command. To view this ingress, use the kubectl get ingress
command.
Step 11: After running the curl -L domain.com/test
command, we finally have our application running successfully.
Well done, we have successfully deployed a Reddit Copy on Kubernetes with Ingress Enabled.
Reference: click here Video to know more about this amazing project.
Thanks for reading the blog. Hope it helps!
Keep learning and Keep upskilling to fulfill your dream.