Prerequisites
Before you begin, you should have the following tools installed on your local machine:
Docker
Minikube cluster ( Running )
kubectl
Git
# Steps:-
# For Docker Installation
sudo apt-get update
#Install Required Packages
sudo apt install -y curl wget apt-transport-https
#Install Docker
sudo apt install -y docker.io
# Start and enable Docker.
sudo systemctl enable --now docker
#Add current user to docker group (To use docker without root)
sudo usermod -aG docker $USER && newgrp docker
# Install Minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
#Make it executable and move it into your path:
chmod +x minikube
sudo mv minikube /usr/local/bin/
#Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
#Make it executable and move it into your path:
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
#Start Minikube
minikube start --driver=docker
Installation
Follow these steps to install and run the Reddit clone app on your local machine:
Clone this repository to your local machine:
git clone
https://github.com/SanketShirke/Reddit-Clone-K8s-Ingress.git
Navigate to the project directory:
cd Reddit-Clone-K8s-Ingress
Build the Docker image for the Reddit clone app:
docker build -t reddit-clone-app .
Push the Image To DockerHub
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.You can use an existing docker image i.e.
trainwithshubham/reddit-clone
for deployment.
Write Deployment.yml file on the deployment server
Open the deployment server write deployment.yml, and service.yml and deploy it using the below commands ( deployments and service yml files are given in the above git repo)
Deploy the app to Kubernetes:
kubectl apply -f Deployment.yml
To check the running pods
kubectl get pods
Deploy the Service for deployment to Kubernetes:
kubectl apply -f Service.yml
If You want to check your deployment and service use the command
kubectl get deployment
&kubectl get services
Access the application using
minikube service reddit-clone-service --url
You can test your deployment using:
curl -L URL
Expose the app
First We need to expose our deployment so use
kubectl expose deployment reddit-clone-deployment --type=NodePort
command.Then We have to expose our app service
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
Add Inbound rules 3000 in the security group of your deployment server
Check the application on the server
EC2ServerIP:Port
Let's Configure Ingress
ingress is used to route external traffic to the Kubernetes endpoints by different URLs:
Minikube doesn't enable ingress by default; we have to enable it first using
minikube addons enable ingress
command.
Let's write ingress.yml and put the following code in it:
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
Now you can able to create ingress for your service. kubectl apply -f ingress.yml
use this command to apply ingress settings.
Test Ingress
Now It's time to test your ingress so use the curl -L domain.com/test
command in the terminal.
You have successfully Deployed a Reddit Copy on Kubernetes with Ingress Enabled.