Three-Tier App Deployment on Docker with Observability using Prometheus & Grafana
Prometheus and Grafana are powerful tools that work together to help you monitor and visualize your systems.
Prometheus is an open-source tool that collects data about your systems, like servers, apps, and databases, at regular intervals. It stores this data in a time-series database, which means it keeps track of data over time. You can then query and analyze this data to see how well your systems are performing.
Grafana is a platform that helps you create dashboards to display the data collected by Prometheus. These dashboards can include graphs, charts, and tables to help you easily understand the performance of your systems and spot any issues
Key benefits of using Prometheus and Grafana together
Why Observability : Because of over application running in carton way
1 Step : Create Your Instance
Update your system : sudo apt-get update && sudo apt-get upgrade -y
Install Docker and Docker Compose : sudo apt-get install docker.io -y sudo apt-get install docker-compose-v2 -y
Clone This Repository
The prometheus service section in your docker-compose.yml file defines how the Prometheus container is configured to run. Here's a breakdown of the key parts of the Prometheus section and what the command does
Prometheus Service Configuration:
image: prom/prometheus:latest
Specifies the Docker image to use for Prometheus. It pulls the latest Prometheus image from Docker Hub.
container_name: prometheus
Sets the name of the container as prometheus to make it easier to reference when running Docker commands.
restart: unless-stopped
This policy tells Docker to automatically restart the container unless it is explicitly stopped. If the container stops unexpectedly, it will automatically restart.
networks: - fullstack-chatapp
Connects the Prometheus container to the fullstack-chatapp network defined elsewhere in the docker-compose.yml file. This allows Prometheus to communicate with other services (e.g., your backend or frontend).
volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml
This mounts the prometheus.yml configuration file from your local filesystem (relative to the directory where the docker-compose.yml file is located) to the container’s /etc/prometheus/prometheus.yml. The container uses this configuration file to scrape metrics from the defined targets.
command section:
This part specifies the custom arguments that will be passed when starting the Prometheus container. They control the configuration and behavior of Prometheus:
ports: - "9090:9090"
Exposes port 9090 on the host machine and maps it to port 9090 inside the container. This is the default port that Prometheus uses to serve its web interface
Code Reference : https://sulkurl.com/ftx
Add New yml file : prometheus.yml
global:
scrape_interval: 1m
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 1m
static_configs:
- targets: ['localhost:9090']
- job_name: 'docker'
scrape_interval: 1m
static_configs:
- targets: ['localhost:80']
Your prometheus.yml configuration file looks almost correct for basic Prometheus setup. However, here are some key points and adjustments you might consider to ensure it works effectively
Issues and Recommendations
1 Indentation Issue :
2 Target Addresses:
3 Scrape Interval
4 Adding Labels :
Consider adding labels to your targets for better identification in Prometheus metrics
static_configs:
- targets: ['localhost:9090']
labels:
service: prometheus
5 Verify Prometheus Access
Corrected and Enhanced Configuration
6 Applying and Testing
Docker Compose Up
Prometheus metrics are a standardized way of collecting and storing data about system and application performance. These metrics are numerical data points collected over time, which are used for monitoring, alerting, and analysis
The error indicates that Prometheus is attempting to scrape metrics from http://localhost:80/metrics, but it cannot connect to this endpoint because no service is running or exposing /metrics on port 80 of the Prometheus container
Solution: Use the Container Name
In Docker Compose, services in the same network can communicate using their service names. Replace localhost:80 with the correct service name from your docker-compose.yml file
Why Use cAdvisor?
cAdvisor (Container Advisor) is a tool from Google designed to collect, monitor, and export resource usage and performance metrics for containers. It works well with Prometheus for container-specific monitoring
How to Use cAdvisor with Prometheus some Changes in Docker Compose
If you are actually using it then check that Jenkins is not already installed there.
Recommended by LinkedIn
Now My cAdvisor is running on port 8080
Now Click on Docker Containers
Now Move to solve this problem
cAdvisor (Container Advisor)
Metrics Provided:
Node Exporter
Now Add Node Exporter code on docker-compose.yml file
node-exporter:
image: prom/node-exporter:latest
container_name: node-exporter
restart: unless-stopped
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
command:
- '--path.procfs=/host/proc'
- '--path.rootfs=/rootfs'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
ports:
- "9100:9100"
networks:
- fullstack-chatapp
Prometheus scrape job for collecting metrics from a Node Exporter instance
- job_name: 'node'
scrape_interval: 1m
static_configs:
- targets: ['node-exporter:9100']
Run Promethous : docker-compose up --no-deps --build prometheus
again Docker Compose up
All Server Metrics
after the change all code need to check on localhost:9090/targets to check logs
sum by(mode)(irate(node_cpu_seconds_total{mode!="idle"}[1h]))
Integration of Grafana with Prometheus and other services
Grafana is widely used for visualizing metrics collected by Prometheus and other monitoring tools. Here's how to set up and integrate Grafana into your environment.
Step 1: Add Grafana to Your docker-compose.yml
version: '3.8'
services:
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- grafana_data:/var/lib/grafana
networks:
- fullstack-chatapp
networks:
fullstack-chatapp:
driver: bridge
volumes:
grafana_data:
Step 2: Start the Services
Run the following command to start Grafana along with other services
Step 3: Access Grafana
Step 4: Add Prometheus as a Data Source
Step 5: Create Dashboards and Panels
Step 7: Additional Integrations
You can integrate Grafana with other monitoring tools like:
Note if your Grafana dashboard not work then check your Prometheus is working or not if it not working then use the command
Click on New Connection : type Promethous select Prometheus
Grafana Templates Theme : link
Copy Dashboard ID
Good work !!
Well done Burhan
Hi