{"id":5929,"date":"2020-05-29T12:15:00","date_gmt":"2020-05-29T09:15:00","guid":{"rendered":"https:\/\/kifarunix.com\/?p=5929"},"modified":"2024-03-14T20:30:40","modified_gmt":"2024-03-14T17:30:40","slug":"install-and-setup-prometheus-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-setup-prometheus-on-ubuntu-20-04\/","title":{"rendered":"Install and Setup Prometheus on Ubuntu 20.04"},"content":{"rendered":"\n
In this guide, we are going to learn how to install Prometheus on Ubuntu 20.04. Prometheus<\/a> is an open-source systems and service monitoring system. It collects metrics from configured targets via HTTP calls at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some conditions are met.<\/p>\n\n\n\n Some of the main features of Prometheus include;<\/p>\n\n\n\n Prometheus is made up various components;<\/p>\n\n\n\n Prometheus is available on the default Ubuntu 20.04 repos;<\/p>\n\n\n\n However, as you might have noted, the Prometheus release versions provided by the default focal repos is not up-to-date. The version 2.18 is current stable release version of Prometheus.<\/p>\n\n\n\n To ensure that you are installing the latest release version of Prometheus, use the pre-compiled<\/strong> binaries which can be downloaded directly from Prometheus downloads section<\/a>.<\/p>\n\n\n\n Before you can go ahead and install Prometheus using pre-compiled binaries on Ubuntu 20.04;<\/p>\n\n\n\n Run the command below to create Prometheus system user and group;<\/p>\n\n\n\n Next, you need to create the directories that will be used to store Prometheus configurations files and other data.<\/p>\n\n\n\n Next, navigate to Prometheus downloads section<\/a> and grab the latest version of Prometheus. You simply use wget to download the Prometheus binary for Linux as shown below;<\/p>\n\n\n\n Verify the integrity of the binary file downloaded by calculating the SHA256 hash.<\/p>\n\n\n\n Compare the resulting hash with the hash provided on the downloads page. Ensure that they match.<\/p>\n\n\n\n Extract the downloaded Prometheus binary;<\/p>\n\n\n\n Copy the After copying, set the user and group ownership of these binaries to Next, copy the A sample Prometheus configuration file is available on the extracted archive folder. To make our work easier, just copy it to Prometheus configuration directory.<\/p>\n\n\n\n Modify the configuration file to suit your needs. In this case, we just go with the defaults.<\/p>\n\n\n\n In the default configuration there is only a single job, called prometheus<\/strong> , which scrapes the time series data exposed<\/strong> by the Prometheus<\/strong> server. The job contains a single, statically configured, target, the localhost on port 9090<\/strong>.<\/p>\n\n\n\n Next, set the user and group ownership of Prometheus configuration directory, \/etc\/prometheus<\/strong> to prometheus<\/strong>.<\/p>\n\n\n\n Once that is done, similarly set the user and group ownership of Prometheus data directory, \/var\/lib\/prometheus\/<\/strong> to prometheus<\/strong>.<\/p>\n\n\n\n At the very least, Prometheus is now set and is ready to run. However, at this point we do not have the Prometheus service configuration file and hence, we can run it as shown below;<\/p>\n\n\n\n Open Prometheus port on firewall (UFW) if it is running. By default, it listens on TCP port 9090.<\/p>\n\n\n\n Prometheus is now ready to receive web requests. You can access it from the browser using the address, To check the status of your node, navigate to Status<\/strong> > Targets<\/strong>.<\/p>\n\n\n\n To view scraped metrics, navigate to, http:\/\/<server_IP>:9090\/metrics<\/strong><\/p>\n\n\n\n To check memory statistics, for example, free memory available, select the To view the graph for the memory statistics, click Graph<\/strong> tab.<\/p>\n\n\n\n To run Prometheus as a service, you can create a systemd service configuration file as shown below;<\/p>\n\n\n\n Next, reload systemd configuration files and start and enable Prometheus to run on system boot.<\/p>\n\n\n\n To check the status of Prometheus service;<\/p>\n\n\n\n Prometheus Getting Started<\/a><\/p>\n\n\n\n Monitor Linux System Metrics with Prometheus Node Exporter<\/a><\/p>\n\n\n\n Install Graylog 3.0 on CentOS 7<\/a><\/p>\n\n\n\n Monitor Squid Access Logs with Graylog Server<\/a><\/p>\n\n\n\n\n
Installing Prometheus on Ubuntu 20.04<\/h2>\n\n\n\n
\n
apt-cache policy prometheus<\/code><\/pre>\n\n\n\n
prometheus:\n Installed: (none)\n Candidate: 2.15.2+ds-2<\/strong>\n Version table:\n 2.15.2+ds-2 500\n 500 http:\/\/ke.archive.ubuntu.com\/ubuntu focal\/universe amd64 Packages<\/code><\/pre>\n\n\n\n
Install Prometheus Using Pre-compiled Binaries on Ubuntu 20.04<\/h3>\n\n\n\n
Prerequisites<\/h4>\n\n\n\n
Create Prometheus System User and Group<\/h5>\n\n\n\n
useradd -M -r -s \/bin\/false prometheus<\/code><\/pre>\n\n\n\n
Create Prometheus Directories<\/h5>\n\n\n\n
mkdir \/etc\/prometheus \/var\/lib\/prometheus<\/code><\/pre>\n\n\n\n
Download Prometheus Binary<\/h5>\n\n\n\n
wget https:\/\/github.com\/prometheus\/prometheus\/releases\/download\/v2.18.1\/prometheus-2.18.1.linux-amd64.tar.gz<\/code><\/pre>\n\n\n\n
sha256sum prometheus-2.18.1.linux-amd64.tar.gz<\/code><\/pre>\n\n\n\n
5fcc35b78bd0a1b84afae6de94248a4bea3cdb4daf0d54a37b5491cb86b014d7 prometheus-2.18.1.linux-amd64.tar.gz<\/code><\/pre>\n\n\n\n
Install Prometheus on Ubuntu 20.04<\/h4>\n\n\n\n
tar xzf prometheus-2.18.1.linux-amd64.tar.gz<\/code><\/pre>\n\n\n\n
prometheus<\/code><\/strong> and
promtool<\/code><\/strong> binaries under the extracted Prometheus archive folder to
\/usr\/local\/bin<\/code> directory.<\/p>\n\n\n\n
cp prometheus-2.18.1.linux-amd64\/{prometheus,promtool} \/usr\/local\/bin\/<\/code><\/pre>\n\n\n\n
prometheus<\/code><\/strong>.<\/p>\n\n\n\n
chown prometheus:prometheus \/usr\/local\/bin\/{prometheus,promtool}<\/code><\/pre>\n\n\n\n
consoles<\/code> and
console_libraries<\/code> directories to Prometheus configuration directory,
\/etc\/prometheus<\/code>.<\/p>\n\n\n\n
cp -r prometheus-2.18.1.linux-amd64\/{consoles,console_libraries} \/etc\/prometheus\/<\/code><\/pre>\n\n\n\n
Create Prometheus Configuration file<\/h4>\n\n\n\n
cp prometheus-2.18.1.linux-amd64\/prometheus.yml \/etc\/prometheus\/<\/code><\/pre>\n\n\n\n
vim \/etc\/prometheus\/prometheus.yml<\/code><\/pre>\n\n\n\n
# my global config\nglobal:\n scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.\n evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.\n # scrape_timeout is set to the global default (10s).\n\n# Alertmanager configuration\nalerting:\n alertmanagers:\n - static_configs:\n - targets:\n # - alertmanager:9093\n\n# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.\nrule_files:\n # - \"first_rules.yml\"\n # - \"second_rules.yml\"\n\n# A scrape configuration containing exactly one endpoint to scrape:\n# Here it's Prometheus itself.\nscrape_configs:\n # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.\n - job_name: 'prometheus'\n\n # metrics_path defaults to '\/metrics'\n # scheme defaults to 'http'.\n\n static_configs:\n - targets: ['localhost:9090']<\/code><\/pre>\n\n\n\n
chown -R prometheus:prometheus \/etc\/prometheus<\/code><\/pre>\n\n\n\n
chown prometheus:prometheus \/var\/lib\/prometheus<\/code><\/pre>\n\n\n\n
Running Prometheus<\/h4>\n\n\n\n
prometheus --config.file=\/etc\/prometheus\/prometheus.yml<\/code><\/pre>\n\n\n\n
...\nlevel=info ts=2020-05-29T08:32:47.317Z caller=main.go:678 msg=\"Starting TSDB \u2026\"\nlevel=info ts=2020-05-29T08:32:47.320Z caller=head.go:575 component=tsdb msg=\"Replaying WAL, this may take awhile\"\nlevel=info ts=2020-05-29T08:32:47.320Z caller=web.go:523 component=web msg=\"Start listening for connections\" address=0.0.0.0:9090<\/strong>\n...\nlevel=info ts=2020-05-29T08:32:47.326Z caller=main.go:695 msg=\"TSDB started\"\nlevel=info ts=2020-05-29T08:32:47.326Z caller=main.go:799 msg=\"Loading configuration file\" filename=\/etc\/prometheus\/prometheus.yml\nlevel=info ts=2020-05-29T08:32:47.731Z caller=main.go:827 msg=\"Completed loading of configuration file\" filename=\/etc\/prometheus\/prometheus.yml\nlevel=info ts=2020-05-29T08:32:47.731Z caller=main.go:646 msg=\"Server is ready to receive web requests.\"<\/code><\/pre>\n\n\n\n
Accessing Prometheus from Web Interface<\/h3>\n\n\n\n
ufw allow 9090\/tcp<\/code><\/pre>\n\n\n\n
http:\/\/server-IP-or-Hostname:9090<\/strong><\/code>.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
go_memstats_frees_total<\/strong><\/code> query and click execute and see the result on the console<\/strong> tab.<\/p>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
Create Prometheus Systemd Service File<\/h4>\n\n\n\n
vim \/etc\/systemd\/system\/prometheus.service<\/code><\/pre>\n\n\n\n
[Unit]\nDescription=Prometheus Time Series Collection and Processing Server\nWants=network-online.target\nAfter=network-online.target\n\n[Service]\nUser=prometheus\nGroup=prometheus\nType=simple\nExecStart=\/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\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre>\n\n\n\n
systemctl daemon-reload\nsystemctl enable --now prometheus<\/code><\/pre>\n\n\n\n
systemctl status prometheus<\/code><\/pre>\n\n\n\n
\u25cf prometheus.service - Prometheus Time Series Collection and Processing Server\n Loaded: loaded (\/etc\/systemd\/system\/prometheus.service; enabled; vendor preset: enabled)\n Active: active (running) since Fri 2020-05-29 08:56:32 UTC; 18s ago\n Main PID: 105588 (prometheus)\n Tasks: 6 (limit: 2281)\n Memory: 17.1M\n CGroup: \/system.slice\/prometheus.service\n \u2514\u2500105588 \/usr\/local\/bin\/prometheus --config.file \/etc\/prometheus\/prometheus.yml --storage.tsdb.path \/var\/lib\/prometheus\/ --web.console.templates=\/etc\/promethe>\n\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.655Z caller=main.go:678 msg=\"Starting TSDB ...\"\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.657Z caller=head.go:575 component=tsdb msg=\"Replaying WAL, this may tak>\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.657Z caller=web.go:523 component=web msg=\"Start listening for connectio>\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.660Z caller=head.go:624 component=tsdb msg=\"WAL segment loaded\" segment>\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.660Z caller=head.go:627 component=tsdb msg=\"WAL replay completed\" durat>\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.662Z caller=main.go:694 fs_type=EXT4_SUPER_MAGIC\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.662Z caller=main.go:695 msg=\"TSDB started\"\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.662Z caller=main.go:799 msg=\"Loading configuration file\" filename=\/etc\/>\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.710Z caller=main.go:827 msg=\"Completed loading of configuration file\" f>\nMay 29 08:56:33 freeradius.kifarunix-demo.com prometheus[105588]: level=info ts=2020-05-29T08:56:33.711Z caller=main.go:646 msg=\"Server is ready to receive web requests.\"<\/code><\/pre>\n\n\n\n
Further Reading;<\/h3>\n\n\n\n
Other Related Tutorials<\/h3>\n\n\n\n