This project consists of a 3-tier web application developed using the MERN stack (MongoDB, Express, React, Node.js). We utilize Docker to create containers for the MongoDB database, as well as the frontend and backend components of the application. To achieve this, we write Dockerfiles for each part and follow best practices for containerization. Additionally, we use Docker Compose to deploy the entire application and set up a private Docker image registry.
In the second part of the project, our focus is on deploying the application in a Kubernetes cluster. For this purpose, we create Kubernetes manifests to define and manage the application's resources within the cluster. We also set up various probes to monitor the health of the application and implement an Ingress to provide external access to the application.
Furthermore, we enhance the application's observability by implementing monitoring and logging. We utilize Prometheus and Grafana for monitoring various metrics and performance data, while the EFK stack (Elasticsearch, Fluentd, Kibana) is employed for log aggregation and analysis.
Before diving into the project, make sure you have the following tools installed:
- Docker
- Kubernetes (e.g., Minikube, MicroK8s)
To containerize the MongoDB database, we'll use Docker and make the storage persistent. by executing this command :
docker run –d --name mongodb-service –v mongo:/data/db –p 27017:27017 mongoWe'll create a Docker network using an appropriate driver and connect the mongodb-service container to it.
docker network create –-driver bridge <network_name>
docker network connect <network_name> mongodb-service- Create a Dockerfile for all parts of the application. This file will define the necessary steps to build the backend image:
docker build -t <image name> .To follow the best practices used in the creation of Dockerfiles, you can refer to this post: : https://www.linkedin.com/posts/asmae-elazrak_docker-devops-dockerimage-activity-7073970892988370944-Kqyk?utm_source=share&utm_medium=member_desktop
- Scan the image for vulnerabilities:
docker scan <container name>- Publish the image to Docker Hub:
docker login
docker tag <image name> <docker_hub_username>/<image name>:<tag>
docker push <docker_hub_username>/<image name>:<tag>- Instantiate the image as a local container, sharing the same network with the MongoDB container:
docker run -d --name <container name> --network <network_name> <image name>- Inspect the container:
docker inspect <container name>- Display logs related to the container:
docker logs <container name>Instead of executing all these commands, you can simply apply the Docker Compose file by using the command :
docker compose upPlease remember that 'docker-compose-app' file uses an image from a private registry, while the 'docker-compose' file uses an image from a public repository.
All the necessary Kubernetes YAML files (manifests) to deploy the application within a Kubernetes cluster under the namespace 'exam' can be applied by using the following command:
kubectl apply -f <path_to_yaml_files_directory>For the directory called "k8s-file" located in the current working directory . The command to apply those files to the Kubernetes cluster under the 'exam' namespace would be:
kubectl apply -f k8s-fileYou can verify the application pods and services are running:
kubectl get all -n=examOpen your browser and access the application using: http://asmae-k8s.com
-
Prometheus and Grafana are set up for monitoring. Access Grafana dashboard at http://localhost:9070.
-
The EFK stack is used for logging. Access Kibana dashboard at http://localhost:5601.
🎉 Congratulations! You now have the fully containerized MERN stack application running in Kubernetes, monitored, and logged effectively. Enjoy exploring and developing further! 😊
🔍 For more in-depth information about each task and detailed steps, please refer to the project repository and the provided code in the respective directories.
Thank you for going through this comprehensive guide for deploying the MERN Stack Web Application using Docker and Kubernetes. We hope you find it helpful! If you encounter any issues or have questions, feel free to reach out or raise an issue in the respective repositories.
Happy Deploying! 😄🚀