# Set up Prometheus on a Linux machine (EC2 Instance)

In the realm of monitoring and observability, Prometheus stands tall as an open-source system designed for capturing and analyzing real-time metrics. Its journey began in 2012 at SoundCloud and has since become a cornerstone in the world of cloud-native computing. This blog post will delve into the core features and architecture that make Prometheus an indispensable tool for ensuring the health and performance of your systems.

Operating on a pull-based system, Prometheus initiates an HTTP request, commonly known as a "scrape," following the configurations outlined in the prometheus.yml file, as discussed below. The resulting response from this scrape request is then stored and parsed in the storage, alongside the metrics associated with the scrape operation.

Key Terminology:

1. **Prometheus Server:** The central server responsible for scraping and storing metrics in a time series database.
    
2. **Scrape:** The method employed by the Prometheus server to pull and retrieve metrics.
    
3. **Target:** The clients of Prometheus servers from which it fetches information.
    
4. **Exporter:** Libraries associated with targets, converting and exporting metrics into the Prometheus format.
    
5. **Alert Manager:** The component tasked with the management and handling of alerts.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703398901071/c69f81da-d6af-4734-a5c3-d6dcefce1360.png align="center")

Let's try it out by performing some simple commands:

### Create an ec2 instance in AWS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703381919144/dc062d60-6cd0-4d95-84bf-1ccf3daf2baf.png align="center")

### connect to the ec2 instance via SSH protocol

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703382388736/c15ad178-ad0f-4a58-9174-3bfd44f9478f.png align="center")

Before installing anything you have to update your machine **“sudo apt-get update”.**

```plaintext
sudo apt-get update -y
sudo apt-get upgrade -y
```

Now let's install the docker

```plaintext
sudo apt-get install docker.io
```

Pull the Prometheus docker image

```plaintext
sudo docker pull prom/prometheus
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703383961924/303787dd-5d86-4ac5-bd00-afb7e27e2bad.png align="center")

Define the configuration file for Prometheus

We pulled the Prometheus image from the docker hub. Let's create the configuration file for Prometheus at ***“/etc/prometheus/prometheus.yml”***.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703384656406/6a0dd456-87ac-4dae-aa11-41a3a72d4fa6.png align="center")

There are two targets for the Prometheus server. First is your **localhost** and second is your **Ubuntu machine public IP**.

### **Run Prometheus as a Docker container**

```plaintext
docker run -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703385463072/d6e7c88b-7cb4-4464-b85f-f5d48359fc73.png align="center")

The Prometheus server will run on port number 9090, which is binded to our Ubuntu machine port 9090.

**Configure your security group of AWS EC2 instance.**

Select your running Ubuntu machine go to the security group-&gt;edit inbound rules-&gt;Add Rule. Allow the following ports as shown below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703385609655/55200307-2c0b-4723-8ad8-863651112769.png align="center")

**Launch the Prometheus Dashboard**

Open your browser and type- [***http://***](http://54.167.231.125:9090/targets)[18.212.27.132/](http://18.212.27.132/)[***:9090/targets***](http://54.167.231.125:9090/targets)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703385709299/8d2941cc-dcf9-44d8-a98c-b93c08360eab.png align="center")

Congratulations, we have completely set up the Prometheus in our ec2 instance, and is easily accessible at port 9090.

### Setting Up Grafana with Prometheus

To connect Prometheus (running on port 9090) with Grafana (running on port 3000) on the same EC2 instance, you can follow these steps:

**Configure Prometheus as a Data Source in Grafana:**

* Open the Grafana web UI by navigating to [`http://your-ec2-instance-ip:3000`](http://your-ec2-instance-ip:3000) in your browser.
    
* Log in to Grafana (default credentials are usually admin/admin).
    
* In Grafana, go to the "+" icon on the left sidebar and select "Data Sources."
    
* Click on "Add your first data source" or "Add data source."
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703396004456/13d26f1f-4a7b-4680-82ce-8d823a91ab92.jpeg align="center")
    
* Choose "Prometheus" from the list of available data sources.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703396154763/c2f04ecb-6090-4490-80f2-ace5948dc86c.jpeg align="center")
    
* In the HTTP section, set the URL to [`http://ec2-ip:9090`](http://localhost:9090) since Prometheus is running on the same machine. If your Grafana and Prometheus are on different machines, replace them with the IP address or DNS name of the Prometheus server.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703396369768/33762f98-02f4-4875-a98a-2e6724ec449f.jpeg align="center")
    
* Click "Save & Test" to verify the connection. If successful, you'll see a green "Data source is working" message.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703396420779/daff46ba-ab7a-4355-9363-83bcb7606892.png align="center")
    

After creating the dashboard, you have the option to incorporate various visualization tools for in-depth data analysis. In the image below, you'll notice that no data is visualized at the moment due to the absence of a query being passed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703396792500/54d47e0b-88be-4f85-8e34-f4006f724054.png align="center")

Awesome! we did it. We did the whole setup with very easy steps. And we finally connected the Prometheus with Grafana.

In the upcoming blogs, we will try out the real implementation of Prometheus, Grafana with real time data visualization

Happy Learning!
