In this article, we’ll explore how to monitor Redis databases using two powerful tools: Prometheus and Grafana. Together, they give you deep insights into your database’s performance, health, and resource usage. Monitoring Redis is essential for spotting issues early, improving performance, and keeping your Redis setup running smoothly. With Prometheus collecting data and Grafana visualizing it, you can easily create custom dashboards to see important metrics in real-time. This helps you stay on top of your infrastructure and take proactive steps to keep it healthy.
Table of Contents
Prerequisites
- AWS Account with Ubuntu 22.04 LTS EC2 Instance.
- Basic knowledge of AWS services, Prometheus and Grafana.
Step #1:Install Redis on Ubuntu
Update the system before starting the installation.
sudo apt update
After this add the repository to the apt
index and then update it.
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update
After updating the repository, install it.
sudo apt-get install redis
Step #2:Install Prometheus on Ubuntu
Download the latest version of Prometheus from the GitHub repository using the following command.
wget https://github.com/prometheus/prometheus/releases/download/v2.30.0/prometheus-2.30.0.linux-amd64.tar.gz
extract the downloaded archives.
tar xvfz prometheus-2.30.0.linux-amd64.tar.gz
The command extracts the contents of the file prometheus-2.30.0.linux-amd64.tar.gz
now navigate to the extracted directory using following command.
cd prometheus-2.30.0.linux-amd64
navigate to the /etc/systemd/system, this is where typically systemd unit files are located, which are used for managing services on Linux systems.
cd /etc/systemd/system
now lets create a service for Prometheus named prometheus.service.
sudo vi prometheus.service
add the following content into it.
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/home/ubuntu/prometheus-2.30.0.linux-amd64/prometheus --config.file=/home/ubuntu/prometheus-2.30.0.linux-amd64/prometheus.yml
Restart=always
[Install]
WantedBy=default.target
Now our prometheus.service is created.
After this
Reload the daemon service, enable, and start the Prometheus service:
sudo systemctl daemon-reload
sudo systemctl enable prometheus.service
sudo systemctl start prometheus.service
now Verify the status of the Prometheus service using following command:
sudo systemctl status prometheus.service
If your service is running properly then you can run prometheus by running
Public IP:9090
which is default port for prometheus in url.
Step #3:Install Grafana on Ubuntu
First import the GPG key used by the Grafana package.
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
then add the Grafana repository to the APT sources.
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
After adding the repository update the package lists
sudo apt update
then install the grafana.
sudo apt install grafana
Start and enable the Grafana service.
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
and if everything works fine and your service is running properly then run grafana by running your Public IP:3000
which is default port of grafana in searchbar.
you will see the login page of grafana (UI) user interface
Grafana has admin
as default username and password.
it will ask for changing the password you can change it or skip it.
then you will see the welcome page of grafana.
Step #4:Create User Group and User for Prometheus
Now after the installation of Prometheus and grafana is done lets create User Group and User for Prometheus.
sudo groupadd --system prometheussudo useradd -s /sbin/nologin --system -g prometheus prometheus
Monitor Redis with Prometheus and Grafana
Step #5:Download and Install Redis Exporter
Install the redis exporter using following command.
curl -s https://api.github.com/repos/oliver006/redis_exporter/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
Extract the downloaded archive file
tar xvf redis_exporter-*.linux-amd64.tar.gz
Move the binary to the appropriate directory.
sudo mv redis_exporter-*.linux-amd64/redis_exporter /usr/local/bin/
Confirm the installation with following command.
redis_exporter --version
Step #6:Configuring Redis Exporter Service
Create a service for the Redis exporter.
sudo vim /etc/systemd/system/redis_exporter.service
add the folllowing content
[Unit]
Description=Prometheus
Documentation=https://github.com/oliver006/redis_exporter
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/redis_exporter \
--log-format=txt \
--namespace=redis \
--web.listen-address=:9121 \
--web.telemetry-path=/metrics
SyslogIdentifier=redis_exporter
Restart=always
[Install]
WantedBy=multi-user.target
save the file and to verify our configuration file is correct and reload the daemon service.
Then enable and start the service.
sudo systemctl daemon-reload
sudo systemctl enable redis_exporter
sudo systemctl start redis_exporter
Confirm the service status.
systemctl status redis_exporter
Step #7:Configuring Redis Exporter Endpoint in Prometheus Configuration
Navigate to the directory containing the Prometheus configuration file.
cd prometheus-2.30.0.linux-amd64
Edit the prometheus.yml file
.
nano prometheus.yml
add the block into it.
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
#Redis Servers
- job_name: "redis"
static_configs:
- targets: ["localhost:9121"]
save the file and close it.
restart the Prometheus service.
sudo systemctl restart prometheus.service
Now go to Prometheus dashboard and click on status, select target, you can see our exporter are up and running.
You can run the Public IP:9121 To see redis web page
click on the metric and you will see the following output.
Step #8:Setting up Grafana Dashboards for Redis Metrics
Now go back to the Grafana welcome page. Go to the Home
in the left top corner.
Then go to the Connections
and select the Data sources
option.
Search for Prometheus in the search bar and select it.
In connection, in Prometheus server URL, give the server url on which our prometheus is running.
after this click on save and test
button. You will see the message for prometheus being successfully queried.
now go to the ‘+
‘ icon from up-right corner and select the New dashboard
option
Here you can start your own new dashboard by adding a visualization.
So click on + Add visualization
option button.
Here you can also import dashboards.
Select the Prometheus as Data source.
You will see the edit panel page
Now in the Query section add the A query
- go_gc_duration_seconds_sum
- instance: localhost:9090
then add the B query
- Metric: redis_cpu_user_seconds_total
- job: redis
Click on run queries
You will see the following output.
It’s in the Gaugevisualization, but there are many visualization option like time series, stats, bar graph etc.
Conclusion:
In conclusion, implementing a monitoring solution for Redis databases using Prometheus and Grafana is essential for maintaining the health, performance, and reliability of your Redis environment. By following the steps outlined in this guide, you can effectively set up and configure Prometheus to collect metrics from Redis instances, visualize these metrics in Grafana dashboards, and proactively manage your Redis infrastructure.
Related Articles:
How to Monitor MySQL with Prometheus and Grafana
Reference: