How to Install Prometheus Server on Ubuntu 20.04 Monitoring Tutorial

How to Install Prometheus Server on Ubuntu 20.04 Open Source Monitoring.  The best growing companies rely on metrics to monitor and understand the performance of their applications and infrastructure. With that in mind telemetry data and time series databases (TSDB) have exploded in popularity over the past several years. The Prometheus monitoring platform is one of the most popular TSDB available today.

What is Prometheus

Prometheus is one of the prominent free, open source monitoring software hosted by the CNCF (Cloud Native Computing Foundation). It is used as an event monitoring solution and alerting toolkit. It seamlessly integrates with variety of systems via plugins or native integration.

Prometheus was designed to provide monitoring and alerting functionality for cloud-native environments . It is quite a popular monitoring solution used today for Kubernetes Engine, which is also another project managed by CNCF.

It gathers metrics related to your IT infrastructure and applications. These metrics signify the information about an event along with its time, date and a definitive value. These metrics are not like ‘logs’ containing heaps of data. Instead, it comprises only specific information about a wide range of events to help you identify the operating status of your stack.

Prometheus Features

  • Prometheus 2. 0 (better storage and staleness handling).
  • Written in Go, has source code available on GitHub and licensed under the Apache 2 License.
  • Multi dimensional data model that generates time series data that can be recognized by metric name and ‘labels’ i.e., descriptive key pairs.
  • Uses PromQL a powerful query language.
  • Leverages a pull model over HTTP to collect time series data.
  • Autonomous single server nodes (does not rely upon network storage or any other remote services).
  • Supports a push gateway to push time series.
  • Scalable data collection.
  • Supports multiple modes of dashboards and graphs.
  • Target endpoints are found using static configuration or service discovery.
  • Simplified operation.

Prometheus overview

Prometheus collects metrics from exposed HTTP endpoints. For such application integration, it leverages reams of client libraries in the building stage. For an exposed endpoint, Prometheus scrapes numerical metric and stores it in the form of time series at a local database.

Further, it can produce temporary time series through queries. These time series are identified by their metric name and ‘labels’. PromQL (Prometheus Query Language) generates queries and helps users choose and collect real time series metrics. With PromQL, you can configure alert settings to get notifications at other sources as well such as email, Slack or PagerDuty. In Prometheus web interface, you can visualize this data in the form of tables or graphs. Or, you can even use other display tools like Grafana through API integrations.

Follow this post, we will show you how to install Prometheus server on Ubuntu 20.04.

Install Prometheus Server on Ubuntu 20.04

Open Source Monitoring

Before starting, you will need to create a data and configuration directory for Prometheus. You can create them with the following command:

				
					mkdir /var/lib/prometheus
mkdir -p /etc/prometheus/rules /etc/prometheus/rules.d /etc/prometheus/files_sd

				
			

Next, download the latest version of Prometheus using the following command:

				
					curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -
				
			

Once the download is completed, extract the downloaded file using the following command:

				
					tar xvf prometheus*.tar.gz
				
			

Change the directory to the extracted directory and copy Prometheus binary to the system directory:

				
					cd prometheus*/
mv prometheus promtool /usr/local/bin/
				
			

Verify the Prometheus version with the following command:

				
					prometheus --version
				
			

You will get the following output:

				
					prometheus, version 2.35.0 (branch: HEAD, revision: 6656cd29fe6ac92bab91ecec0fe162ef0f187654)
  build user:       root@cf6852b14d68
  build date:       20220421-09:53:42
  go version:       go1.18.1
  platform:         linux/amd64

				
			

You can also verify the Promotool version using the following command:

				
					promtool --version
				
			

You will get the following output:

				
					promtool, version 2.35.0 (branch: HEAD, revision: 6656cd29fe6ac92bab91ecec0fe162ef0f187654)
  build user:       root@cf6852b14d68
  build date:       20220421-09:53:42
  go version:       go1.18.1
  platform:         linux/amd64

				
			

Next, copy required configuration file and directory to the /etc/prometheus directory:

				
					mv prometheus.yml /etc/prometheus/prometheus.yml
mv consoles/ console_libraries/ /etc/prometheus/
				
			

Enable Password Authentication

By default, Prometheus can be accessed without any password. For security reasons, it is recommended to create password access the Prometheus.

First, install the Python Bcrypt utility to generate a secure password:

				
					apt install python3-bcrypt gnupg2 -y
				
			

Please create a Python script to generate a password:

				
					nano gen-pass.py
				
			

Add the following code:

				
					import getpass
import bcrypt

password = getpass.getpass("password: ")
hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
print(hashed_password.decode())

				
			

Save and close the file then run the script to generate a password:

				
					python3 gen-pass.py
				
			

You will get the generated password in the following output:

				
					password: 
$2b$12$/h1CtP5CL4nvPBXIIexmYONqGoG0wfYRrkwLppqmwHDRF89CouFfW

				
			

Next, create a web.yml configuration file and define your password:

				
					nano /etc/prometheus/web.yml
				
			

Add the following lines:

				
					basic_auth_users:
       admin: '$2b$12$/h1CtP5CL4nvPBXIIexmYONqGoG0wfYRrkwLppqmwHDRF89CouFfW'

				
			

Verify the web.yml file using the following command:

				
					promtool check web-config /etc/prometheus/web.yml
				
			

If everything is fine, you will get the following output:

				
					/etc/prometheus/web.yml SUCCESS
				
			

Create a System User and Group

It is a good idea to create a dedicated user and group for Prometheus. First, create a Prometheus group with the following command:

				
					groupadd --system prometheus
				
			

Next, create a Prometheus user with the following command:

				
					useradd -s /sbin/nologin --system -g prometheus prometheus
				
			

Here setup directory permission and ownership using the following command:

				
					chown -R prometheus:prometheus /etc/prometheus
chmod -R 775 /etc/prometheus/
chown -R prometheus:prometheus /var/lib/prometheus/
				
			

Create a Service File For Prometheus

At this point you will need to create a Prometheus service file to manage the Prometheus service via systemd. You can create it with the following command:

				
					nano /etc/systemd/system/prometheus.service
				
			

Add the following lines:

				
					[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP \$MAINPID
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus \
  --web.config.file=/etc/prometheus/web.yml \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries \
  --web.listen-address=0.0.0.0:9090 \
  --web.external-url=

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target

				
			

Save and close the file when you are finished. Then, reload the systemd daemon to apply the changes:

				
					systemctl daemon-reload
				
			

Next, start the Prometheus service and enable it to start at system reboot using the following command:

				
					systemctl start prometheus
systemctl enable prometheus
				
			

You can now check the status of the Prometheus using the following command:

				
					systemctl status prometheus
				
			

You will get the following output:

				
					● prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-27 13:35:21 UTC; 6s ago
       Docs: https://prometheus.io/docs/introduction/overview/
   Main PID: 1686 (prometheus)
      Tasks: 7 (limit: 4686)
     Memory: 14.5M
     CGroup: /system.slice/prometheus.service
             └─1686 /usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.conf>

Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.971Z caller=head.go:493 level=info component=tsdb msg="Replaying on-disk >
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.971Z caller=head.go:536 level=info component=tsdb msg="On-disk memory map>
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.971Z caller=head.go:542 level=info component=tsdb msg="Replaying WAL, thi>
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.971Z caller=head.go:613 level=info component=tsdb msg="WAL segment loaded>
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.971Z caller=head.go:619 level=info component=tsdb msg="WAL replay complet>
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.973Z caller=main.go:978 level=info fs_type=EXT4_SUPER_MAGIC
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.973Z caller=main.go:981 level=info msg="TSDB started"
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.973Z caller=main.go:1162 level=info msg="Loading configuration file" file>
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.974Z caller=main.go:1199 level=info msg="Completed loading of configurati>
Apr 27 13:35:21 ubuntu2004 prometheus[1686]: ts=2022-04-27T13:35:21.974Z caller=main.go:930 level=info msg="Server is ready to receive web re>

				
			

Configure Nginx as a Reverse Proxy for Prometheus

It is also recommended to configure Nginx as a reverse proxy for Prometheus. First, install the Nginx web server using the following command:

				
					apt-get install nginx -y
				
			

Once the Nginx package is installed, create an Nginx virtual host configuration file with the following command:

				
					nano /etc/nginx/conf.d/prometheus.conf
				
			

Add the following lines:

				
					server {
    listen 80;
    server_name  208.117.84.116;
    location / {
        proxy_pass           http://localhost:9090/;
    }
}

				
			

Save and close the file then verify the Nginx configuration file with the following command:

				
					nginx -t
				
			

You will get the following output:

				
					nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
				
			

Next, restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

To check the Nginx running status, run the following command:

				
					systemctl status nginx
				
			

You will get the following output:

				
					● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-27 13:37:37 UTC; 5s ago
       Docs: man:nginx(8)
    Process: 2216 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 2217 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2233 (nginx)
      Tasks: 3 (limit: 4686)
     Memory: 3.5M
     CGroup: /system.slice/nginx.service
             ├─2233 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─2234 nginx: worker process
             └─2235 nginx: worker process

Apr 27 13:37:37 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 27 13:37:37 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.

				
			

Access Prometheus Dashboard

Now, open your web browser and access the Prometheus dashboard using the URL http://your-server-ip. You will be asked to authenticate with your username and password as shown below:

Username and Password Authentication

Provide your admin username, password and click on the Sign in button. Once you are logged in, you should see the Prometheus dashboard on the following screen:

Great Job! We have learned how to Install Prometheus Server on Ubuntu 20.04 Open Source Monitoring.

How to Install Prometheus Server on Ubuntu 20.04 Conclusion

In this guide, we explained how to install Prometheus on Debian 11 server. We also explained how to enable the authentication and configure Nginx as a reverse proxy for Prometheus. You can now start monitoring your infrastructure metrics via the Prometheus dashboard.

While monitoring has seen a shift toward tools including Prometheus in the past few years, the dominant solution are Nagios server , Grafana or Zabbix server

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x