Prometheus and Grafana are popular tools in the DevOps and monitoring ecosystems. Prometheus is a powerful, open-source system for monitoring and alerting, primarily used to scrape and store time-series data. Grafana is a flexible open-source platform for data visualization, providing real-time interactive dashboards for displaying data from a variety of sources, including Prometheus.
In this guide, we will walk through the steps to install and set up Prometheus and Grafana on a Linux server. Once set up, these tools will work together to provide a robust monitoring and visualization system for your infrastructure.
Installing Prometheus
Step 1: Install Prometheus
Prometheus can be easily installed via the package manager on a Linux system. Use the following command to install it:
sudo apt-get install prometheus
This command installs the Prometheus server, its default configuration, and necessary dependencies.
Step 2: Start Prometheus
After the installation is complete, you need to start the Prometheus service:
sudo systemctl start prometheus
This command starts Prometheus immediately so that it can begin scraping metrics from its targets.
Step 3: Enable Prometheus on Startup
To ensure that Prometheus starts automatically when the server reboots, enable the service:
sudo systemctl enable prometheus
By enabling the service, you ensure the Prometheus monitoring system runs continuously without manual intervention after a server restart.
Installing Grafana
Grafana allows you to create beautiful dashboards for visualizing the data collected by Prometheus. Follow these steps to install and configure Grafana on your system.
Step 1: Install Grafana
First, install the Grafana package using the package manager:
sudo apt-get install grafana
This command installs Grafana and its dependencies, providing you with all the necessary tools to create and manage dashboards.
Step 2: Add Grafana’s APT Repository
Grafana is continually updated with new features and bug fixes. To ensure you have the latest version, it’s a good idea to add the official Grafana repository:
sudo apt-get install software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
This adds the official Grafana repository to your system’s package manager.
Step 3: Import Grafana’s GPG Key
Before you can use the newly added repository, you need to import Grafana’s GPG key to verify the integrity of packages:
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
This command downloads and adds the GPG key, ensuring the packages you install from Grafana’s repository are authentic.
Step 4: Update Package Lists
Next, update your package list to include the newly added Grafana repository:
sudo apt-get update
This ensures that when you install or upgrade Grafana, you will get the latest version from the official repository.
Step 5: Start Grafana Server
After installing Grafana, start the Grafana server with the following command:
sudo systemctl start grafana-server
This command launches the Grafana server, enabling you to access it through your web browser.
Step 6: Enable Grafana on Startup
Similar to Prometheus, we want to make sure Grafana starts automatically when the server reboots:
sudo systemctl enable grafana-server
By enabling this service, you ensure that Grafana is always running after a reboot, making the monitoring dashboards available at all times.
Accessing Grafana
Once Grafana is installed and running, you can access it by navigating to http://<your-server-ip>:3000
in your web browser. The default login credentials are:
- Username:
admin
- Password:
admin
After the first login, Grafana will prompt you to change the password for security purposes.
Connecting Grafana to Prometheus
With both Prometheus and Grafana installed, the next step is to connect Grafana to Prometheus so that you can visualize the data being scraped.
- Add Prometheus as a Data Source in Grafana:
- Log in to Grafana.
- Navigate to Configuration -> Data Sources -> Add data source.
- Select Prometheus as the data source type.
- In the URL field, enter
http://localhost:9090
, which is the default URL for Prometheus. - Click Save & Test to verify the connection.
- Create Dashboards: Once Prometheus is added as a data source, you can start creating dashboards in Grafana by selecting metrics from Prometheus and visualizing them in various chart types (graphs, heatmaps, etc.).
Conclusion
By following this guide, you’ve successfully installed and configured both Prometheus and Grafana on a Linux server. Prometheus will continuously scrape metrics from your applications and systems, while Grafana provides you with the tools to visualize those metrics in custom dashboards.
This powerful combination of tools allows you to monitor your system’s health, track performance over time, and get alerted in case of any issues. Make sure to explore Grafana’s extensive visualization options and customize your dashboards to fit your specific needs.
With this setup in place, you’re well on your way to building a robust monitoring system that provides valuable insights into your infrastructure.
This setup forms the foundation of a strong observability stack, ideal for scaling applications and tracking performance in real-time.