Deploying a Reddit Clone App on Kubernetes with Ingress and Minikube
Table of contents
No headings in the article.
Essentials: First You have to install some essential packages for this deployment. You can find the essential here.
Step 1: Clone the source code
To begin, clone the source code for the application by executing the following command:
git clone https://github.com/BSatishSutar/reddit-clone-k8s-ingress.git
Step 2: Containerize the Application using Docker
Create a Dockerfile with the following sophisticated code:
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "dev"]
Step 3) Building Docker-Image
Next, build the Docker image from the Dockerfile using the command:
docker build -t <DockerHub_Username>/<Imagename> .
Ensure you replace <DockerHub_Username>
and <Imagename>
with appropriate values.
Step 4) Push the Image to DockerHub
Push the Docker image to DockerHub so that the Deployment file can pull the image and run the app in Kubernetes pods. Follow these steps:
Log in to your DockerHub account using the command:
docker login
and provide your username and password.Push the image to DockerHub using the command:
docker push <DockerHub_Username>/<Imagename>
Alternatively, you can use an existing Docker image, such as satishsutar1111/reddit-clone:tagname
, for deployment.
Step 5) Write a Kubernetes Manifest File
Now, let's create a Kubernetes manifest file that describes the deployment and service for the application. The manifest file enables version control and ensures a repeatable deployment process.
Create a Deployment.yml
file with the following sophisticated code:
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: satishsutar1111/reddit-clone
ports:
- containerPort: 3000
Create a Service.yml
file with the following code:
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
Step 6) Deploy the app to Kubernetes & Create a Service
With the deployment and service manifest files ready, you can now deploy the app to Kubernetes. Execute the following commands:
kubectl apply -f Deployment.yml
kubectl apply -f Service.yml
To verify the deployment and service, you can use the commands kubectl get deployment
and kubectl get services
.
Step 7) Configure Ingress
Ingress provides rules for inbound connections to access Kubernetes cluster services. To configure Ingress, create an ingress.yml
file and add the following sophisticated code:
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
By executing the command minikube addons enable ingress
, you can enable Ingress on Minikube. Use kubectl apply -f ingress.yml
to apply the Ingress settings. To verify the Ingress resource, use kubectl get ingress ingress-reddit-app
.
Step 8) Expose the app
To expose the deployment, use the command kubectl expose deployment reddit-clone-deployment --type=NodePort
. You can test the deployment by executing curl -L
http://192.168.49.2:31000
, where 192.168.49.2
is the Minikube IP and 31000
is the defined port in Service.yml
.
Next, expose the app service using kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0
.
Test Ingress
To test the Ingress configuration, use the command curl -L domain/test
in the terminal. Additionally, you can access the deployed application using "publicip:3000".
Note: Ensure that port 3000 is open in the security group of your EC2 instance.
Congratulations!
You have successfully deployed a Reddit clone on Kubernetes with Ingress enabled.