Deployed a Reddit Copy on Kubernetes with Ingress Enabled
Table of contents
- Table of contents
- Step 1: Clone the source code
- Step 2: Containerize the Application using Docker
- Step 3) Building Docker-Image
- Step 4) Load the Docker Image into Kind Cluster
- Step 5) Write a Kubernetes Manifest File
- Step 5) Deploy the app to Kubernetes & Create a Service For It
- Step 6) Expose the App
- Step 8) Expose the app
Table of contents
Show less
Step 1: Clone the source code
The first 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 2: Containerize the Application using Docker
- Write a Dockerfile with the following code:
Copy
Copy
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
Now it's time to build Docker Image from this Dockerfile. docker build -t <DockerHub_Username>/<Imagename> .
use this command to build a docker image.
Step 4) Load the Docker Image into Kind Cluster
Since Kind creates a Kubernetes cluster inside Docker, you'll need to load your Docker image into the Kind cluster:
This is the command for load the docker image : kind load docker-image reddit-clone:latest
You can use an existing docker image i.e trainwithshubham/reddit-clone for deployment.
Step 5) 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.
Copy
Copy
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: trainwithshubham/reddit-clone
ports:
- containerPort: 3000
Write Service.yml file
Copy
Copy
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 5) 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 using kubectl apply -f Service.yml
If You want to check your deployment & Service use the command kubectl get deployment
& kubectl get services
Step 6) Expose the App
To make your application accessible outside the Kubernetes cluster, you'll use the kubectl expose
command. This command creates a service that allows external traffic to reach your deployment. There are different types of services you can use, such as NodePort
, LoadBalancer
, ClusterIP
, etc. In this example, we will use NodePort
.
A NodePort
service exposes a deployment by assigning a static port on each node. The service will be accessible on every node's IP address at that port, in addition to the Kubernetes default networking (e.g., the internal ClusterIP
):
kubectl expose deployment --type=NodePort --name=
Let's write ingress.yml and put the following code in it:
Copy
Copy
Step 8) Expose the app
kubectl port-forward
is a powerful command that lets you forward local traffic to a specific port on a pod within the Kubernetes cluster. This is useful when you want to access a service running inside a pod from your local machine without exposing it to the public network.svc/reddit-clone-service
: Specifies the service that you want to forward the traffic to. Here,reddit-clone-service
is the name of the Kubernetes service that you exposed in the previous step.3000:3000
: This part of the command specifies the local port to forward and the target port on the pod:3000
: Local port.3000
: Target port inside the pod (the port your application listens on).
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.
Thanks all. Good luck out there!
Follow for more such amazing content :)
Happy Learning ๐