This guide will walk you through deploying a monitoring stack, including Prometheus, Grafana, cAdvisor, and NGINX Proxy Manager, step by step. We’ll also highlight the purpose of each step so you can understand why it’s necessary.
From the provided structure:
- backend: Contains backend application Dockerfiles and configurations.
- frontend: Contains frontend application files and an
nginx.conffile. - monitoring: Contains configuration files for monitoring tools like Prometheus, Loki, and Promtail. Also contains the docker compose file for the spinning them up and ensuring they interact with fullstack application services.
- docker-compose.yml: Combines all services (monitoring, backend, and frontend) into a single deployment.
- README.md: Explains the purpose and usage of the project.
- LICENSE: Specifies licensing terms.
Before starting:
- Install Docker and Docker Compose on your system:
sudo apt update && sudo apt upgrade -y sudo apt install docker.io -y sudo apt install docker-compose-v2 -y sudo usermod -aG docker $USER
- Ensure your machine has at least:
- 4GB RAM
- Stable internet connection for pulling images.
- Backend and Frontend:
- Backend: Your application logic (e.g., API services).
- Frontend: The user interface served by an NGINX server.
- Monitoring Tools:
- Prometheus: Scrapes metrics from services.
- Grafana: Visualizes the metrics for better insights.
- cAdvisor: Collects container-specific metrics like CPU, memory, and disk usage.
- NGINX Proxy Manager:
- Acts as a reverse proxy for routing and securing traffic.
- Contains the backend application logic and Dockerfile for containerization.
- Purpose: Allows the backend to be run in a Docker container.
- Contains
nginx.conf, which configures how the frontend serves static files. - Purpose: Provides the user-facing portion of the application.
- Contains configuration files for Prometheus, Grafana, and other monitoring services.
- Purpose: To monitor the health, performance, and resource usage of your applications.
- Combines all services into one deployment.
- Purpose: Simplifies launching all services with one command.
Clone the repository to your local system:
git clone https://github.com/GideonIsBuilding/cv-challenge01.git
cd cv-challenge01Here, in the root folder, you will find the docker-compose.yml file. Enter and run the following command:
docker-compose -p cv-challenge -f docker-compose.yml up -dPrometheus needs a configuration file (prometheus.yml) to define which targets to scrape metrics from.
- Go to the monitoring folder.
- Ensure there is a
prometheus.ymlfile with the following contents:Why? This tells Prometheus to scrape metrics from itself (global: scrape_interval: 15s scrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"] - job_name: "cadvisor" static_configs: - targets: ["cadvisor:8080"]
localhost:9090) and cAdvisor (cadvisor:8080).
Grafana needs to know the root URL if it’s being accessed via a subpath (e.g., /grafana).
- Open the
docker-compose.ymlfile. - Under the Grafana service, add:
Why? This ensures Grafana correctly serves its dashboard when accessed at
environment: - GF_SERVER_ROOT_URL=http://localhost/grafana - GF_SERVER_SERVE_FROM_SUB_PATH=true
/grafana.
Run the following command to bring up all services:
docker-compose -p cv-challenge -f docker-compose.yml up -d-d: Runs the containers in detached mode.
Why? This starts all services in the background, allowing them to interact with each other.
If you’re using NGINX Proxy Manager, create a single proxy host for localhost, and add the following custom locations:
/grafana-> Forward tohttp://grafana:3000/prometheus-> Forward tohttp://prometheus:9090/api-> Forward tohttp://backend:8000/docs-> Forward tohttp://backend:8000
- Then click on the gear button besides the location fild. This opens up a field to enter the advanced configurations. Enter in the following:
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Why? This makes the services accessible through specific paths.
- Prometheus:
http://localhost/prometheus - Grafana:
http://localhost/grafana.. and so on.
-
Monitoring with Grafana and Prometheus:
- Add Prometheus as a data source in Grafana.
- Use dashboards to visualize container metrics (CPU, memory, disk usage).
-
Frontend and Backend Deployment:
- The backend API and frontend UI are deployed in separate containers.
-
Reverse Proxy with NGINX Proxy Manager:
- Centralized routing for easy access to services.
- Check if
/metricsis accessible:curl http://localhost:9090/metrics
- Fix the
prometheus.ymlfile or ensure no subpath issues exist.
- Verify
GF_SERVER_ROOT_URLandGF_SERVER_SERVE_FROM_SUB_PATHare set correctly.
- Run:
Check for error messages in the logs. Or just check if the firewall for that service is preventing the interacting. For example, while your cAdvisor may be configured well, you may get the
docker-compose logs <service-name>
context deadline exceedederror. Watch out for such errors!
-
Secure the Setup:
- Use SSL certificates with NGINX Proxy Manager.
- Restrict access to monitoring tools using basic authentication.
-
Expand Monitoring:
- Add Node Exporter for host-level metrics.
- Integrate Alertmanager with Prometheus for alerting.
-
Automate Deployment:
- Use CI/CD pipelines to automate deployments for the backend, frontend, and monitoring stack.
This guide simplifies deploying a monitoring stack for your containerized applications. If you encounter any issues, refer to the troubleshooting section or logs for insights. Let me know if you need further help! 😊