Introduction: Monitoring with Prometheus on AWS EC2
Monitoring is critical to maintaining any system’s health and performance. Prometheus, an open-source monitoring and alerting toolkit, is an excellent choice for collecting metrics and providing insights into the state of your applications and infrastructure. This guide will walk you through installing and configuring Prometheus on an Amazon EC2 instance.
Prerequisites: Preparing Your EC2 Instance
Before we dive into the installation and configuration, ensure you have the following prerequisites in place:
- Amazon EC2 Instance: Launch an EC2 instance with a Linux-based operating system. For this guide, we will use Amazon Linux 2.
- Security Groups: Ensure your instance’s security group allows inbound traffic on port 9090 (Prometheus default port).
- SSH Access: Ensure you have SSH access to your EC2 instance.
Installation: Downloading and Extracting Prometheus
- Connect to Your EC2 Instance:
ssh -i your-key-pair.pem ec2-user@your-ec2-instance-public-dns
- Update the System:
sudo yum update -y
- Download Prometheus: Visit the Prometheus download page and copy the link to the latest stable release. Then, download and extract Prometheus using the following commands:
wget https://github.com/prometheus/prometheus/releases/download/v2.x.x/prometheus-2.x.x.linux-amd64.tar.gz
tar -xvf prometheus-2.x.x.linux-amd64.tar.gz
- Move Prometheus Files:
sudo mv prometheus-2.x.x.linux-amd64 /usr/local/prometheus
cd /usr/local/prometheus
Configuration: Setting Up the Prometheus User and Service
- Create a Prometheus User:
sudo useradd –no-create-home –shell /bin/false prometheus
- Set Permissions:
sudo chown -R prometheus:prometheus /usr/local/prometheus
- Create a Prometheus Service File:
sudo nano /etc/systemd/system/prometheus.service
Add the following content to the file:
[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/prometheus/prometheus –config.file=/usr/local/prometheus/prometheus.yml –storage.tsdb.path=/usr/local/prometheus/data
[Install]
WantedBy=multi-user.target
- Reload Systemd:
sudo systemctl daemon-reload
Running Prometheus: Starting and Enabling the Service with Systemd
- Start Prometheus:
sudo systemctl start prometheus
- Enable Prometheus to Start on Boot:
sudo systemctl enable prometheus
Verification: Checking the Service Status and Accessing the Prometheus UI
- Check Prometheus Service Status:
sudo systemctl status prometheus
You should see an output indicating that Prometheus is active and running.
- Access the Prometheus UI: Open your web browser and navigate to http://your-ec2-instance-public-dns:9090. You should see the Prometheus web interface.
Following these steps, you have successfully installed and configured Prometheus on an Amazon EC2 instance. You can now start exploring Prometheus and setting up monitoring for your applications and infrastructure.
References
Set up and configure Prometheus metrics collection on Amazon EC2 instances