-
Notifications
You must be signed in to change notification settings - Fork 0
Setting up Monitoring with Prometheus and Grafana
This guide provides step-by-step instructions for setting up a comprehensive monitoring infrastructure using Node Exporter, Prometheus, and Grafana. This setup is designed to enable efficient system metric collection, storage, and visualization, with a focus on accommodating the needs of a Python DevOps team.
Key components covered in this documentation:
- Node Exporter: For collecting and exposing system metrics
- Prometheus: For scraping and storing time-series data
- Grafana: For visualizing metrics and setting up alerts
The guide is structured to walk you through the installation and configuration of each component, including:
- Detailed installation steps for each tool
- Creation of necessary system users and directories
- Configuration of systemd service files for automatic startup
- Basic setup of Prometheus to scrape Node Exporter metrics
- Initial Grafana configuration
Additionally, this document includes a section on Grafana user management and access control, ensuring that you can properly manage user access and permissions within your monitoring setup.
By following this guide, you'll establish a robust monitoring system capable of tracking system performance, visualizing key metrics, and setting up alerts for your DevOps environment.
Lets start with the installation of the node exporter
- Download and Install Node exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvfz node_exporter-1.8.2.linux-amd64.tar.gz
sudo mv node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin
rm -rf node_exporter-1.8.2.linux-amd64.tar.gz- Create a system user for Node Exporter:
sudo useradd -rs /bin/false node_exporterThe -rs flags in the useradd command serve the following purposes:
-r: This option adds a system account. System accounts are typically used for services and have user IDs lower than 1000.
-s /bin/false: This sets the user's login shell to /bin/false, preventing the user from logging in interactively.
- Create a systemd service file
sudo nano /etc/systemd/system/node_exporter.serviceAdd the following contents to the service file:
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetStart and enable the systemd service node_exporter.service
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporterNow that the node exporter has been installed, we will set up prometheus and grafana for the python-devops team: Prometheus setup
- Switch to the python-devops account:
sudo su - python-devops- Install Prometheus for the Python Devops team:
- Download and Install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.53.1/prometheus-2.53.1.linux-amd64.tar.gz
tar xvfz prometheus-2.53.1.linux-amd64.tar.gz
sudo mv prometheus-2.53.1.linux-amd64 /opt/prometheus-python
rm -rf prometheus-2.53.1.linux-amd64.tar.gz- Create a system user for Prometheus
sudo useradd -rs /bin/false prometheus- Create necessary directories and set permissions
sudo mkdir /etc/prometheus /var/lib/prometheus-python
sudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus-python
sudo chown -R prometheus:prometheus /opt/prometheus-python/The command sudo mkdir /etc/prometheus /var/lib/prometheus creates two directories:
/etc/prometheus: This is typically used for configuration files related to Prometheus. This is where your Prometheus configuration file (prometheus.yml) is placed.
/var/lib/prometheus: This directory is generally used for storing Prometheus data, such as time series data.
The command sudo chown prometheus:prometheus /etc/prometheus /var/lib/prometheus-python changes the ownership of the directories. This ensures that the prometheus user has the necessary permissions to read and write to these directories, which is essential for Prometheus to operate correctly when running under this specific user.
- Finally, create a configuration file in the
etc/prometheus
sudo nano /etc/prometheus/prometheus.ymlAdd the following to the configuration file:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']This configuration tells Prometheus scrape metrics from the Node Exporter instance on localhost:9091 every 15 seconds and also store the scraped metrics in the TSDB (time-series database) for 30 days.
- Create a systemd service file for the prometheus-python.service:
sudo nano /etc/systemd/system/prometheus-python.serviceAdd the following content:
[Unit]
Description=Prometheus for Python DevOps Team
After=network.target
[Service]
User=prometheus-python
Group=prometheus-python
Type=simple
ExecStart=/opt/prometheus-python/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus-python/ \
--web.console.templates=/opt/prometheus-python/consoles \
--web.console.libraries=/opt/prometheus-python/console_libraries \
--web.listen-address=:9091
--storage.tsdb.retention.time=1y
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetThis systemd service file that configures a Prometheus instance for the Python DevOps Team.
Service Description
- Starts a Prometheus instance specifically for the Python DevOps Team.
Service Configuration - Runs as the prometheususer and group.
- Uses the prometheus.yml configuration file located in /etc/prometheus/.
- Stores metrics in /var/lib/prometheus-python/.
- Serves the Prometheus web console on port 9091.
Service Management - Starts the service after the network target is reached.
- Enables the service to be started automatically on boot. The line
WantedBy=multi-user.targetin the [Install] section enables the service to be started automatically on boot.
Start and enable the service:
sudo systemctl daemon-reload
sudo systemctl start prometheus-python
sudo systemctl enable prometheus-pythonGrafana setup Install Grafana for the python-devops team
- Download and Install Grafana
wget https://dl.grafana.com/oss/release/grafana-11.1.3.linux-amd64.tar.gz
tar -zxvf grafana-11.1.3.linux-amd64.tar.gz
sudo mv grafana-v11.1.3 /opt/grafana-python
rm -rf grafana-11.1.3.linux-amd64.tar.gz- Create a system user for Grafana
sudo useradd -rs /bin/false grafana-python- Create necessary directories and set permissions
sudo mkdir /etc/grafana-python /var/lib/grafana-python
sudo chown grafana-python:grafana-python /etc/grafana-python /var/lib/grafana-python- Create a configuration file
sudo nano /etc/grafana-python/grafana.ini- Add the following content to the configuration file:
[server]
http_port = 3001
[paths]
data = /var/lib/grafana-python
logs = /var/log/grafana-python
plugins = /var/lib/grafana-python/plugins- Create a systemd service file:
sudo nano /etc/systemd/system/grafana-python.service- Add the following content:
[Unit]
Description=Grafana for Python DevOps Team
After=network.target
[Service]
User=grafana-python
Group=grafana-python
Type=simple
ExecStart=/opt/grafana-python/bin/grafana-server \
-config /etc/grafana-python/grafana.ini \
-homepath /opt/grafana-python
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target- Start and enable the service:
sudo systemctl daemon-reload
sudo systemctl start grafana-python
sudo systemctl enable grafana-python- Navigate to "Server Admin" > "Users"
- Click "Invite User" or "New User"
- Fill in required details:
- Name
- Username (if "New User")
- Password (if "New User")
- Set organization and role
- Click "Submit"
- Admin: Full access to all organizations
- Editor: Can edit and create dashboards
- Viewer: Can view dashboards
- Admin: Manage users and permissions within the organization
- Editor: Edit and create dashboards
- Viewer: View dashboards
- Go to "Configuration" > "Teams"
- Click "New Team"
- Add members and set permissions
- Navigate to the desired folder
- Click the gear icon > "Permissions"
- Add users, teams, or roles
- Set appropriate access levels
- Open the dashboard
- Click the gear icon > "Permissions"
- Add users, teams, or roles
- Set viewer, editor, or admin access
- Home
- CI CD Pipeline Configuration for the Python Application
- Deployment with Systemd
- NGINX Reverse Proxy Setup and SSL Configuration
- Setting up the remote server and installing prerequisites
(Content not available in the provided HTML)
(Content not available in the provided HTML)