As we have discussed in the previous post on how to build a Mern stack app on GCP GKE, Mern Stack Developers need to be aware of the deployment procedure after building the application. Deploying the application on the Google Cloud Platform (GKE) is very different from deploying on other platforms. Due to digitization, most businesses prefer GCP as a platform to deploy their applications. It is a bit complicated, but we are here to make it easy as we break down the procedure and explain it to you as simply and easily as possible.
Deployment procedure in detail:
It is a seven-step process. To understand this process in detail, every Mern Stack developer should be familiar with the fundamentals of Mern Stack technology, Kubernetes, Docker, etc. If you find difficulty understanding, it is advisable to seek professional help from experienced experts from either the Mern Stack app development companies or Mern Stack platforms, as their experience helps you to understand what is required to understand the deployment procedure easily.
- Dockerize your web application:
Before you start the deployment procedure, you need to ensure that the web application is dockerized, as Google GKE helps you run K8 on GCP with ease, for which this step is important. Building images with Docker files is essential. Docker images enable you to download applications faster and with fewer attacks. Steps for building Docker images are explained step by step for easy understanding.
- UI build stage:
Node:14-slim is the base image that is used for building Docker images. You need to add the node.js server and react server package.json files to a docker file system. Install the necessary dependencies required for this stage. Once you install every dependency file, copy the source file. Then run npm run build to build the React app. All necessary files or components are found in the build folder inside the UI folder.
- API build stage:
Again, start from the base image. Then copy only node.js package.json files into the /.api folder. Install the necessary dependencies. At last, copy the server.js file into the same folder.
- Packaging the app:
Start with the base image. Copy the UI build files and API build files. Then use this command node api.bundle.js, for packaging the application.
- Building docker image:
Use docker build -t mern-image for building images. Once images are created, use Docker images to check whether you built the correct images or not. If not, repeat the same steps until you build the correct Docker images.
- Running web app on Docker:
Once the Docker image is built, the next step is to run your web application on Docker. Simply run the Docker image using the below command:
// run the container
docker run -d -p 80:80 –name mern-stack mern-image
// list the container
docker logs mern-stack
// exec into a running container
docker exec -it mern-stack /bin/sh
Most Mern Stack developers face difficulties while running the Docker images. Perform this step with care and resolve any bugs found while running the web application. Once you run the above commands, your web application is available at https://localhost/.
- Pushing image to container registry:
A container registry is one platform where you can manage all the Docker images easily. You can even decide who has access to your files. Let us focus on how to push to the GCP container registry. The only prerequisite is to have a relevant API on GCP. To check whether the API is enabled or not, use the below command after logging into GCP.
gcloud services enable containerregistry.googleapis.com.
Then use cloud auth configure-docker to configure the docker with the GCP container registry. Once the docker image is built, you need to add tags for that image, which can be done by running the docker tag web app gcr.io/staticweb-test/webapp:v1 command. Here, static web-test is the name of the sample project, the web app is the Docker image, and v1 is the image tag. Once you run docker push gcr.io/staticweb-test/webapp:v1, you can push the Docker image into the GCP container registry. This step becomes easy for mern stack developers if they practice it with dummy images before pushing the actual Docker image used for their projects.
- Creating GKE cluster:
The next step is to create the cluster, which is done by running the gcloud container clusters create my-cluster command. You can also create a cluster by logging into the GCP console and following the below steps:
- Click the “create” button to start the process.
- Two options are displayed: GKE standard and Autopilot. Always choose the GKE standard for creating clusters.
- Fill in a few basic details about the cluster, and then click on the “create cluster” button.
- A new cluster is created with three nodes. To list the clusters, use the gcloud container clusters list command.
Configure Kuebctl With the GKE Cluster:
It is very easy to configure using the GCP console. Kuebctl is the command utility line for K8 used in a Mern stack web application. Before configuring it, you have to install it. Once installed, click on the connect button displayed on the GCP console. A command is created that helps you configure Kuebctl with the GKE cluster. Copy the command and run it on your terminal to configure.
gCloud container clusters get-credentials frontend-cluster –zone us-central1-c –project static web-test is a sample command created based on details filled in while creating a new cluster using the GCP console.
- Deploying K8 objects on the GKE cluster:
It is time to deploy the K8 objects, which is done by creating deployment and service objects. Then use the image from the GCP container registry that was created earlier. To proceed further, get the image console URL details from the details section of the container registry where your image or repository resides. After copying the details, it is time to create service objects by using the kubectl create -f manifest.yml command. Once the objects are created, use the below commands to check whether the objects are in the desired state or not:
// list the deployment
kubectl get deploy
// list the pods
kubectl get pod
// list the service
kubectl get svc
The number of pods running will tell you how many replicas were created for the deployment process.
- Deleting the cluster:
Once the application is deployed, you can access the web application from the browser. One way to access the application is to create an external IP from the load balancer. Once you notice that there are no changes to be made, you can delete the cluster by using the gcloud container clusters delete [CLUSTER_NAME] command.
Every Mern Stack developer should be aware of this process, as most businesses prefer deploying applications on GCP or GKE clusters because of its popularity and high demand. The best way to stay ahead of your competition is to learn this process effectively under the professional guidance of experienced Mern Stack Developers. Their knowledge helps to understand things better.
BrainFuel is a one-stop solution for mern-stack development. Most businesses utilize the expertise of our experienced mern stack developers to use this technology successfully without any issues.