Docker and Kubernetes for Frontend

Docker and Kubernetes for Frontend

post-6.png

 

In this tutorial, we will show you a step-by-step guide on how to install Docker and Kubernetes for a React application. We will first create a Docker image of a React app, push it to a container registry and then deploy it locally using Kubernetes.

 

1. Create React App

Skip this step if you already have a running react app. Create react app by running the following command:

6-1.png

 

2. Set up Docker

ou can go to Docker website (https://www.docker.com) and download Docker for your operating system by following the installation wizard or instructions for your OS.

 

3. Dockerizing our React Application

You will need to navigate to your React application's root directory.
Create a Dockerfile in the root directory. This file will define how Docker builds your image.
Example Dockerfile:

6-2.png

After creating the Dockerfile, we need to build the Docker image by running the following command in the terminal, where <image-name> is the name you want to give to your Docker image:

6-3.png

 

4.Setup Kubernetes

You can run Kubernetes locally with Docker Desktop.
Open Docker Desktop ? Preferences ? Kubernetes ? Enable Kubernetes ? Apply & Restart
This will take several minutes for docker to restart

6-4.png

 

5.Push the Docker Image to a Container Registry

If you don’t have a Docker account, head to their official website and create one.
Log in to your container registry using the following command:

6-5.png

Tag your Docker image with the registry URL and version:

6-6.png

Example:

6-7.png

Push the image to the container registry:

6-8.png

 

6.Creating a Deployment in Kubernetes

Create a Kubernetes deployment configuration file (e.g., deployment.yaml) with the following content:

6-9.png
6-10.png

This command basically tells Kubernetes to create a Deployment based on the configuration details provided in the file created here.

 

7. Creating a Service in Kubernetes

We need then to create the service.yaml file contains the configuration for the Service.

6-11.png

We’re defining here that we want to have a load balancer service, so it won’t allow downtime while visiting the website.
To apply the file to our configuration, we can run:

6-12.png

After completing these steps, the React app should be running in a Docker container, accessible via Kubernetes service, and ready to scale as needed! 

Run the following command that will create an an exposure where we can get the IP

6-13.png

 

6-14.png

In this example, you can see that the service my-react-app-service has an EXTERNAL-IP of localhost and a PORT(S) mapping of 80:32308/TCP. This means that the service is accessible at localhost and port 32308 on your local machine (http://localhost:32308 from your browser).

Visit http://localhost:32000 and you will see your running React app

6-15.png

Hope you had fun!

Post Info

Category:

Tags:

Date:

24 Aug 2023
Blog

Related Post

Chat With Us

Got a brilliant idea or want to learn more about us? Whatsapp us now !

For Sales & Collaboration

For Job Enquiry