<p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Working with cloud-based tools and services is made easy with the help of service providers like Amazon Web Services (AWS). It becomes very easy to run your applications on the cloud and manage large amounts of data efficiently. Amazon Elastic Compute Cloud, popularly known as EC2, is one of the most widely used AWS services. With the help of EC2 and its instances, you can host your web applications on virtual servers without worrying about the expenditure on infrastructure.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">In a cloud-based environment that hosts many applications and resources, monitoring data and other metrics is very important for developers. Prometheus is an application that helps organize tasks such as monitoring and alerting.</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">What is Prometheus?</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Be it any cloud-based servers and resources, constant monitoring and surveillance of performance and a few other metrics are crucial to acquiring feedback. Prometheus is a tool that helps in monitoring - an open-source toolkit developed in 2012 for monitoring and alerting a wide range of systems; the core function of Prometheus is acting as a time-series database to handle time-series data and use it for analyzing metrics, among other things.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Prometheus stores its data in key-value pairs like most of the databases do. The data stored by the tool are time-series sets, meaning they are data points collected over equal intervals. The data points have a metric name and also carry labels. These data sets can be very useful in monitoring and tracking metrics like memory consumption, error rates, etc.</span></p><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Installing Prometheus on EC2 Instance: Step-by-Step Guide</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">The process of configuring Prometheus on an EC2 instance can be a little bit tricky for those who are doing it for the first time. Follow the steps given below carefully to install Prometheus on your instances.</span></p><h3 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Creating an EC2 Instance</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 1:</strong> Open the Amazon EC2 console and launch an instance.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 2:</strong> For the Amazon Machine Image (AMI) option, select Ubuntu.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 3:</strong> Now, choose an instance type. For this example, we will be using ‘t2.micro’.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 4:</strong> Next, move to configuring the instance details. Choose the required network and subnet, and then click the “Add Storage” option. Add 8GB storage, as this should be enough for this purpose.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 5:</strong> Add a name tag to the EC2 instance. You can name something related to your project to identify it easily. </span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 6:</strong> This step is all about configuring the security rules. You can opt for 9090 for Prometheus and move to the next step.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 7:</strong> You can download a new key pair if you need to. Once this is created, you can review your instance and launch it.</span></p><h3 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Installing Prometheus on EC2</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">It is suggested that Prometheus be used by a different user other than the root user as it can add better security. By creating a new user, we can separate login and other personal details, use Prometheus as a service, and add protection to the system.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 1:</strong> The first step is to create a new user and to create two directories - one for Prometheus configuration and another for data. </span></p><pre><code class="language-plaintext">sudo useradd --no-create-home prometheus sudo mkdir /etc/prometheus sudo mkdir /var/lib/prometheus </code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 2:</strong>Now that the directory is set up, it is time to install Prometheus. You can use the below commands to download the tar file and unzip it. The commands also help in installing the toolkit in the system. </span></p><pre><code class="language-plaintext">wget https://github.com/prometheus/prometheus/releases/download/v2.19.0/prometheus-2.19.0.linux-amd64.tar.gz tar xvfz prometheus-2.19.0.linux-amd64.tar.gz sudo cp prometheus-2.19.0.linux-amd64/prometheus /usr/local/bin sudo cp prometheus-2.19.0.linux-amd64/promtool /usr/local/bin/ sudo cp -r prometheus-2.19.0.linux-amd64/consoles /etc/prometheus sudo cp -r prometheus-2.19.0.linux-amd64/console_libraries /etc/prometheus sudo cp prometheus-2.19.0.linux-amd64/promtool /usr/local/bin/ rm -rf prometheus-2.19.0.linux-amd64.tar.gz prometheus-2.19.0.linux-amd64</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 3:</strong> Now, add a prometheus.yml file to configure Prometheus to monitor.</span></p><pre><code class="language-plaintext">sudo vi /etc/prometheus/prometheus.yml</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 4:</strong> Now that we are in the newly created prometheus.yml, let us add the configurations.</span></p><pre><code class="language-plaintext">global: scrape_interval: 15s external_labels: monitor: 'prometheus' scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 5:</strong> Once the configuration is figured out, we can add a service file that will define Prometheus as a service and how it will be managed.</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">First, create the following command:</span></p><pre><code class="language-plaintext">/etc/systemd/system/prometheus.service </code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Now, add it to the below content:</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">[Unit]</span></p><pre><code class="language-plaintext">Description=Prometheus Wants=network-online.target After=network-online.target</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">[Service]</span></p><pre><code class="language-plaintext">User=prometheus Group=prometheus</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Type=simple</span></p><pre><code class="language-plaintext">ExecStart=/usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">[Install]</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">WantedBy=multi-user.target</span></p><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;"><strong>Step 6:</strong> Once the installation is done, you can change the permissions of the files and directories, and configure it again.</span></p><pre><code class="language-plaintext">sudo chown prometheus:prometheus /etc/prometheus sudo chown prometheus:prometheus /usr/local/bin/prometheus sudo chown prometheus:prometheus /usr/local/bin/promtool sudo chown -R prometheus:prometheus /etc/prometheus/consoles sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries sudo chown -R prometheus:prometheus /var/lib/prometheus sudo systemctl daemon-reload sudo systemctl enable prometheus sudo systemctl start prometheus</code></pre><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">With this, you have successfully installed and configured Prometheus on your EC2 instance. But this is not the end of the process. To link the metrics to your Prometheus instance, you have to set up a node exporter.</span></p><h3 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Why Install Prometheus on EC2 Instance?</span></h3><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">When handling cloud-based servers, applications, and resources, monitoring and receiving feedback on their performance is crucial. Prometheus not only makes it easier, but it also integrates smoothly with AWS EC2 instances. Here are a few other reasons why you should configure Prometheus in your EC2 instances and gain valuable insights.</span></p><ul><li><span style="font-family:Arial,sans-serif;">Prometheus can offer you centralized monitoring in case you have multiple EC2 instances.</span></li><li><span style="font-family:Arial,sans-serif;">With the inputs and insights you receive from the toolkit, you can work on immediate alterations needed in the servers or applications. </span></li><li><span style="font-family:Arial,sans-serif;">Based on the monitoring results, you can scale your EC2 instances accordingly.</span></li><li><span style="font-family:Arial,sans-serif;">Prometheus also tracks containerized applications and databases, making it a crucial integration in the AWS environment.</span></li></ul><h2 style="text-align:justify;"><span style="font-family:Arial,sans-serif;">The Bottom Line</span></h2><p style="text-align:justify;"><span style="font-family:Arial,sans-serif;">Configuring Prometheus on your EC2 instance can make monitoring your web servers and applications easier. While setting up Prometheus on your EC2 can be a bit complicated for beginners, it is a very useful configuration that can be of great use to developers, as the insights received from Prometheus can help improve your application significantly.</span></p><p style="text-align:justify;"><span style="font-family:Arial, sans-serif;">Read More</span></p><p style="text-align:justify;"><a href="https://devopsden.io/article/how-to-install-docker-on-an-ec2-instance">https://devopsden.io/article/how-to-install-docker-on-an-ec2-instance</a></p><p style="text-align:justify;"><span style="font-family:Arial, sans-serif;">Follow us on</span></p><p style="text-align:justify;"><a href="https://www.linkedin.com/company/devopsden/">https://www.linkedin.com/company/devopsden/</a></p>