Open In App

Deploying a React Application in Kubernetes

Kubernetes is an open-source free manager for your computer program. These programs can be in the containers. Each container holds a program and everything it needs to run. To Keep track of all these containers that contain your application this is where the Kubernetes role comes in. Kubernetes does this for us. It can automate, scale, and manage the deployment of our application. Some Terms are there you need to go through it. So during deployment, you can understand how it helps.

So, if you have built your awesome React app, and now you want to share it with the world. With the help of Kubernetes you can easily deploy it. In this guide we will break down how to take your react app and deploy it into Kubernetes, making it accessible to anyone with an internet connection.



Deployment in Kubernetes

Let’s see the following terms what they are and how they help us to deploy our app on Kubernetes.

Steps To Build & Deploy React App on Kubernetes

Lets begin deploying it locally on kubernetes using docker desktop. Follow each steps carefully.



Step 1: Create a simple react application by following command. if you have yours then you can skip this step.This will create our simple react app and lets try to run it whether it is running successfully.

npx create-react-app react-kubernetes

cd react-kubernetes # change your directory

npm install

npm start

Step 2: Create a Dockerfile inside projects root directory and add below content to it.

FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Step 3: Open Docker Desktop in background. and hit following command to build the docker image using above created Dockerfile.

docker build -t my-simple-app <space> .

You can check if the docker image has created successfully or not . by entering docker images command in the terminal.At this stage, we have dockerized our application, and you can check if it running by hitting this command . this will run the created docker image specified port.

docker run -d -p 3000:3000 my-simple-app .

Step 4: Deploying docker image to kubernetes:

Now, we have containerized our application with required dependencies itself. At this step, we have to provide details about deployment such as on which port our application should listen? how many containers should run? which docker image to deploy ? and some other configuration data that needed for more complex applications.

Create Deployment.yaml file in root directory with following content.

apiVersion: apps/v1
kind: Deployment
metadata:
name: react-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: react-app
template:
metadata:
labels:
app: react-app
spec:
containers:
- name: react-app-container
image: my-simple-app # Use the image name and tag you built earlier
ports:
- containerPort: 3000 # Adjust the port as needed

Create a service.yaml file in projects root directory.

apiVersion: v1
kind: Service
metadata:
name: react-app-service
spec:
selector:
app: react-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer

Enable Kubernetes from Docker Desktop.

Enter Following commands in the terminal to start cluster and apply the configuration. it will deploy and run our application in that cluster with the configuration specified in deployment.yaml and service.yaml files.

kubectl start

kubectl config use-context docker-desktop

kubectl apply -f deployment.yaml

kubectl apply -f service.yaml

This is how you can deploy the React application on kubernetes cluster. Some doubts about react deployment have been covered in below questions.

FAQs On Deploying a React Application in Kubernetes

1. What is the difference between Docker and Kubernetes ?

Docker is a platform that packages our application with the required dependencies to run application. Kubernetes uses docker container in deployment process.

2. How kubernetes differs from GCP, AWS, Azure Deployment ?

AWS, GCP, Azure are the cloud service providers they provide various services in the cloud. we can use kubernetes with these cloud services to run and manage containerized apps on these cloud platforms.

3. Why should we containerized our app for deployment ?

Containerizing applications means it is portable, means you can run it accross different environment. So to avoid any issues we should containerized the application.


Article Tags :