5.1 What is Grafana
Grafana is a powerful tool for visualizing metrics, widely used for creating customizable dashboards and analyzing data in real-time. In this lecture, we’ll look at how to set up Grafana to work with Prometheus and how to create dashboards for visualizing Docker container metrics.
5.2 Installing Grafana on Windows
To install Grafana on Windows, follow these steps:
1. Download the installer
Go to the official Grafana website and download the installer for Windows: https://grafana.com/grafana/download.
# The installer file is usually named grafana-installer-x.x.x.exe
2. Install Grafana
Run the installer and follow the instructions. You can choose to install it as a Windows service for automatic startup.
3. Start Grafana
After the installation is complete, you can start Grafana as a service or using the desktop shortcut. Open your browser and go to http://localhost:3000
.
5.3 Installing Grafana on macOS
To install Grafana on macOS, follow these steps:
1. Installing Homebrew
If Homebrew is not installed yet, run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
2. Installing Grafana
brew update
brew install grafana
3. Starting Grafana
Run Grafana as a service:
brew services start grafana
Open your browser and go to http://localhost:3000
.
5.4 Installing Grafana on Linux
To install Grafana on Linux, follow these steps:
1. Adding the Grafana Repository
Add the official Grafana repository:
sudo apt-get install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo apt-get update
2. Installing Grafana
sudo apt-get install grafana
3. Starting Grafana
Start and enable Grafana auto-start:
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
Go to the browser and visit http://localhost:3000
.
5.5 Setting Up Grafana
1. Accessing the Grafana Web Interface
Open your web browser and go to http://localhost:3000. Log in using the default credentials: (admin/admin).
2. Adding a Prometheus Data Source
- Navigate to "Configuration" → "Data Sources".
- Click "Add data source".
- Select "Prometheus".
- Enter the URL of your Prometheus server (e.g., http://localhost:9090).
- Click "Save & Test" to check the connection.
5.6 Creating Dashboards in Grafana
1. Creating a New Dashboard
- Go to "Create" → "Dashboard".
- Click "Add new panel".
2. Adding a Panel
- In the "Query" section, select the data source Prometheus.
- Enter a PromQL query to get the metrics. For example, to use CPU:
- Select the type of graph (e.g., "Graph").
- Customize the panel (e.g., title, legend, axes, etc.).
- Click "Apply" to save the panel.
rate(container_cpu_usage_seconds_total[1m])
3. Creating Additional Panels
Repeat the steps to create additional panels for other metrics, such as memory, network, and disk. Example queries:
Memory Usage:
container_memory_usage_bytes
Network Traffic:
rate(container_network_receive_bytes_total[1m])
Disk Operations:
rate(container_fs_reads_total[1m])
5.7 Examples of Creating Panels
Example 1: CPU Panel
Query:
rate(container_cpu_usage_seconds_total[1m])
Graph Type: Graph
Panel Settings: Set the title to "CPU Usage" and configure the axes.
Example 2: Memory Panel
Query:
container_memory_usage_bytes
Graph Type: Graph
Panel Settings: Set the title to "Memory Usage" and configure the axes.
Example 3: Network Traffic Panel
Query:
rate(container_network_receive_bytes_total[1m])
Graph Type: Graph
Panel Settings: Set the title to "Network Traffic" and configure the axes.
5.8 Setting Up Dashboards
1. Setting Time Ranges
At the top of your dashboard, select the time range for displaying metrics (e.g., last 5 minutes, last hour, etc.).
2. Saving the Dashboard
- Click on the "Save dashboard" button in the top menu.
- Specify the name of the dashboard and, if needed, provide a description.
- Click "Save".
3. Organizing Panels
You can drag and drop panels, resize them, and organize them on the dashboard however you like. This will help you create a more user-friendly and informative dashboard.
Example of a Full Dashboard
Creating a dashboard that includes panels for CPU, memory, network traffic, and disk operations will give you a complete view of the state of your Docker containers.
- Create a new dashboard and add panels for all key metrics.
- Configure each graph with the appropriate PromQL queries and display settings.
- Organize panels on the dashboard so that important metrics are easily accessible.
GO TO FULL VERSION