Install Prometheus in Linux environment

To install Prometheus in a Linux environment, you can follow these steps. I'll assume you are using Ubuntu, but the process should be similar for other Linux distributions:

1. Download Prometheus:

Visit the Prometheus download page (https://prometheus.io/download) and get the latest version. You'll want the tarball with the Linux AMD64 architecture.

2. Extract the tarball:

Use the following command to extract the downloaded tarball (replace "prometheus-x.x.x.linux-amd64.tar.gz" with the actual filename):

tar xvfz prometheus-x.x.x.linux-amd64.tar.gz

3. Move the files:

Change to the extracted directory and move the binaries to the appropriate location. We'll move the Prometheus binary to "/usr/local/bin":

cd prometheus-x.x.x.linux-amd64
sudo mv prometheus /usr/local/bin/
sudo mv promtool /usr/local/bin/

4. Create a user (optional but recommended):

It's a good practice to run Prometheus as a non-root user for security reasons. Create a new user, for example "prometheus", and assign appropriate permissions to its data directory:

sudo useradd -M -r -s /bin/false prometheus
sudo mkdir /var/lib/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus

5. Configuration:

Create a configuration file for Prometheus. You can use the example configuration file provided (prometheus.yml) or create your custom one.

sudo vim /etc/prometheus/prometheus.yml

Add your desired configuration to this file. Here's a simple example that scrapes the local machine every 15 seconds:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

Save and close the file.

6. Service setup:

To manage Prometheus as a service, create a systemd service file for it:

sudo vim /etc/systemd/system/prometheus.service

Add the following content to the file:

[Unit]
Description=Prometheus Server
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus \
    --web.console.templates=/usr/local/bin/consoles \
    --web.console.libraries=/usr/local/bin/console_libraries

[Install]
WantedBy=multi-user.target

Save and close the file.

7. Enable and start the service:

Start the Prometheus service and enable it to start on system boot:

sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus

8. Access Prometheus UI:

Prometheus should now be running, and you can access its web interface by navigating to "http://localhost:9090" in your web browser.

That's it! Prometheus should now be installed and running on your Linux system. Remember to configure appropriate firewall rules and security measures to protect your Prometheus instance in a production environment.