NOTE: This article have associated with an article about Infrastructure as Code — Terraform on AWS.
Hello everyone. The first time, I used Prometheus and Grafana and hosted to Amazon EC2. Prometheus is open source monitoring system and time series database. Grafana is open source analytics and monitoring system.
When you have a web application, either CPU or memory passes 100% and your web application is crash but you don’t know how to fix this problem. The solution for this problem — is always to monitor your web application so that when having a problem and can fast fix it. Tools that can monitor — Prometheus and Grafana.
After creating an EC2 instance, install Grafana. Use Amazon Linux 2 for OS. You can choose Grafana open source or Grafana enterprise. For this tutorial, choose Grafana open source. Install Grafana from the YUM repository.
sudo nano /etc/yum.repos.d/grafana.repo
After sudo nano, copy and paste below this code.
[grafana] name=grafana baseurl=https://packages.grafana.com/oss/rpm repo_gpgcheck=1 enabled=1 gpgcheck=1 gpgkey=https://packages.grafana.com/gpg.key sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt
After copy-paste, press Ctrl + X then Y, and ENTER. Now can install Grafana. When asked to choose [y/N] — choose always y. After Grafana has already been installed, start Grafana and check Grafana status.
sudo yum install grafana sudo systemctl daemon-reload sudo systemctl start grafana-server sudo systemctl status grafana-server
After Grafana is now running, enable Grafana — sudo systemctl enable grafana-server. Then open EC2, search your EC2 instance with Grafana installed and copy public IPv4 address — publicIP:3000. But why 3000? Because for open Grafana need port 3000 based on this link.
Fill the username and password — admin and admin, then log in. Can skip the password or change the password. For this tutorial, skip the password. Welcome to Grafana finally.
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.37.5/prometheus-2.37.5.linux-amd64.tar.gz
After installing Prometheus, extract the Prometheus installed file.
tar -xvf prometheus-2.37.5.linux-amd64.tar.gz
Edit the prometheus.yml — sudo nano /etc/prometheus/prometheus.yml
When see targets, change localhost to a public IPv4 address on the EC2 instance that Prometheus installed.
After this, press Ctrl + X then Y and ENTER. Edit the prometheus.service — sudo nano /etc/systemd/system/prometheus.service
[Unit] Description=PromServer Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \n --config.file /etc/prometheus/prometheus.yml \n --storage.tsdb.path /var/lib/prometheus/ \n --web.console.templates=/etc/prometheus/consoles \n --web.console.libraries=/etc/prometheus/console_libraries [Install] WantedBy=multi-user.target
After this, press Ctrl + X then Y and ENTER. After Prometheus has already been installed, start Prometheus and check Prometheus status.
sudo systemctl daemon-reload sudo systemctl start prometheus sudo systemctl status prometheus
After Prometheus is now running, enable Prometheus — sudo systemctl enable prometheus. Then open EC2, search your EC2 instance with Prometheus installed and copy the public IPv4 address — publicIP:9090. But why 9090? Because for opening Prometheus need port 9090 based on this link. Then click Status and click Targets.
Go to the Prometheus link and show the result like below this.
Back to Grafana. Go to Configuration and click Data sources.
Click Add data source, search Prometheus, and click Prometheus to settings Prometheus.
Fill in the URL-based Prometheus link but delete /metrics like this screenshot.
Choose Prometheus and version 2.37.x because I install Prometheus version 2.37.5. Click Save & test. If successful, show a notification Data source is working. Then go to Explore section.
Explore means can focus on the query without thinking about visualization. Focus on the metrics you want to display. Try metric — prometheus_http_requests_total and click Run query.
If want create dashboard, go to dashboard section and click Import.
Importing a dashboard is fast step than creating a new dashboard, but instead, creating a new dashboard that can see the metrics that needs. Import dashboard can upload JSON files or via Grafana.com dashboard. Import via Grafana.com on this link, click Copy ID to the dashboard, and click Load.
Change name and folder if needed. Data source from Prometheus. Click Import and wait until the dashboard is showing.
After trying Prometheus and Grafana, I feel two tools are very useful for learning monitoring. Coming soon, I try Amazon Managed Service for Prometheus and Amazon Managed Service for Grafana with Amazon EKS. Stay tuned. Thank you very much :)