Deployment of a Reddit Copy on Kubernetes with Ingress Enabled
1. What Is Reddit?
Reddit is a community-determined aggregator of content.
Reddit is a social news site where users create and share content.
It is a social platform where users submit posts that other users ‘upvote’ or ‘downvote’ based on whether they like it. If a post gets lots of upvotes it moves up the Reddit rankings so that more people can see it. If it gets downvoted it quickly falls and disappears from most people’s view.
2. Deploy a Reddit Based Application Using K8's
Step 1:
Before we begin with the Project, we need to make sure we have the following prerequisites installed:
EC2 ( AMI- Ubuntu, Type- t2.medium )
Docker
Minikube
kubectl
You can Install all this by doing the below steps one by one. and these steps are for Ubuntu AMI.
Great! You're all set for the project. Your Minikube cluster is now prepared for deploying the Reddit clone application.
Step 2: Clone the source code
The next step is to clone the source code for the app. You can do this by using this command git clone
https://github.com/LondheShubham153/reddit-clone-k8s-ingress.git
Step 3:Containerize the Application using Docker
Write a Dockerfile with the following code:
Step 4: Building Docker-Image
Now it's time to build a Docker Image from this Dockerfile. docker build -t <DockerHub_Username>/<Imagename> .
use this command to build a docker image.
Step 5: Push the Image To DockerHub
Now push this Docker Image to DockerHub so our Deployment file can pull this image & run the app in Kubernetes pods.
First login to your DockerHub account using Command i.e
docker login
and give your username & password.Then use
docker push <DockerHub_Username>/<Imagename>
for pushing to the DockerHub.
Step 6: Write a Kubernetes Manifest File
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.
Write Deployment.yml file
Let's Create a Deployment File For our Application. Use the following code for the Deployment.yml file.
Pod's Selector labels and Replica Set labels should remain the same to identify the pod.
Write Service.yml file
Now it's time to create a Service yaml file.
The service's selector should be the same as the pod's from the deployment file as the service should know for which pod it will be created.
An important point is that this service file is a type of Nodeport to access it from the internet and a node-port range is also defined here as 31000.
Step 7: Deploy the app to Kubernetes & Create a Service For It
Now, we have a deployment file for our app and we have a running Kubernetes cluster, we can deploy the app to Kubernetes. To deploy the app you need to run following the command:
kubectl apply -f deployment.yml
Just Like this create a Service usingkubectl apply -f service.yml
If You want to check your deployment & Service use the command
kubectl get deployment
&kubectl get services
Step 8: Let's Configure Ingress
Ingress:
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:
Let's write ingress.yml and put the following code in it:
Minikube doesn't enable ingress by default; we have to enable it first using
minikube addons enable ingress
command.- If you want to check the current setting for addons in Minikube use
minikube addons list
command.
- If you want to check the current setting for addons in Minikube use
Now you can able to create ingress for your service. kubectl apply -f ingress.yml
use this command to apply ingress settings.
- Verify that the ingress resource is running correctly by using
kubectl get ingress ingress-reddit-app
command.
Step 8: Expose the app
First We need to expose our deployment so use
kubectl expose deployment reddit-clone-deployment --type=NodePort
command.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
Then We have to expose our app service
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
Test Ingress
Now It's time to test your ingress so use the curl -L domain/test command in the terminal.
You can also see the deployed application on Ec2_ip:3000
Note:- Make sure you open the 3000 port in a security group of your Ec2 Instance.
You have successfully Deployed a Reddit Copy on Kubernetes with Ingress Enabled.