{"id":7060,"date":"2020-08-10T10:03:38","date_gmt":"2020-08-10T10:03:38","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=7060"},"modified":"2024-06-25T11:10:40","modified_gmt":"2024-06-25T11:10:40","slug":"learn-more-about-installation-and-configuration-of-prometheus-on-a-linux-server","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/learn-installation-and-configuration-of-prometheus-on-a-linux-server","title":{"rendered":"Learn Installation and Configuration of Prometheus On a Linux Server"},"content":{"rendered":"<p><strong>What Is Prometheus?\u00ad\u00ad\u00ad<\/strong><\/p>\n<p>Prometheus is an open-source tool with a metrics-based monitoring system. It has a simple yet powerful data model and a query language that lets you analyze how your applications and infrastructure are performing. It is very lightweight and has a good alerting mechanism. It is a cloud-agnostic tool so we can use it with any cloud provider. Many organizations prefer this tool because of its no cost, wide capabilities, and integrations with many third-party tools. In this blog, I have explained how to implement it on an AWS EC2 instance.<\/p>\n<h2>Architecture<\/h2>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7065\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_1.png\" alt=\"Prometheus On a Linux Server\" width=\"700\" height=\"504\" \/><\/a><\/p>\n<p>This diagram shows the architecture of Prometheus and certain elements of its ecosystem:<\/p>\n<p>Here you can understand the connectivity of different components of Prometheus with other exporters and tools better. These are the most commonly used exporters here, but to view a list of other exporters which can be integrated by Prometheus, <a href=\"https:\/\/prometheus.io\/docs\/instrumenting\/exporters\/\">click here<\/a>.<\/p>\n<p>Prometheus scrapes metrics from instrumented jobs directly. It keeps all scraped samples locally in its time-series database (TSDB) and runs rules over this data to either query and record new time series from existing data or generate alerts. Grafana or other API consumers can be used to visualize the collected data using Prometheus as a data source.<\/p>\n<p>Find below the server level architecture of Prometheus and Grafana to be set up in a cloud infrastructure for monitoring and alerting purposes.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7066\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_2.png\" alt=\"Prometheus On a Linux Server\" width=\"700\" height=\"419\" \/><\/a><\/p>\n<p>Here we have used two different servers for Prometheus and Grafana for optimized performance, but you can install both on the same server as well. You can install exporters like CloudWatch exporter on different servers also depending upon your requirement, and you just have to add that target into the configuration file of Prometheus under the exporter&#8217;s job.<\/p>\n<h2>Install and Configure Prometheus<\/h2>\n<p>Here we will learn how the latest Prometheus version can be installed and configured on a Linux machine.<\/p>\n<p><strong>Prerequisites<\/strong><\/p>\n<ol>\n<li>Make sure you have sudo access to the Linux server because it requires high privileges for the commands used in this blog.<\/li>\n<li>To download the Prometheus binary,\u00a0the server must have\u00a0access to the internet.<\/li>\n<li>The inbound firewall rules must be opened to the server to access Prometheus port 9090.<\/li>\n<\/ol>\n<h3><strong>Setup Prometheus Binaries<\/strong><\/h3>\n<p><strong>Step 1:<\/strong> To update the yum package repositories, use the below command.<\/p>\n<pre class=\"lang:default decode:true\">sudo yum update -y<\/pre>\n<p><strong>Step 2:<\/strong> Go to the official release page of Prometheus, and get the Linux binary download link.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7067\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_3.png\" alt=\"Prometheus On a Linux Server\" width=\"700\" height=\"221\" \/><\/a><\/p>\n<p><strong>Step 3:<\/strong> Create a user for Prometheus and required directories for Prometheus files. Then assign ownership of these directories to Prometheus user.<\/p>\n<pre class=\"lang:default decode:true\">sudo useradd --no-create-home --shell \/bin\/false prometheus\r\nsudo mkdir \/etc\/prometheus\r\nsudo mkdir \/var\/lib\/prometheus\r\nsudo chown prometheus:prometheus \/etc\/prometheus\r\nsudo chown prometheus:prometheus \/var\/lib\/prometheus\r\n<\/pre>\n<p><strong>Step 4:<\/strong> Using curl, download the Prometheus binary, un-tar it and change the extracted folder name to Prometheus-files.<\/p>\n<pre class=\"lang:default decode:true\">curl -LO https:\/\/github.com\/prometheus\/prometheus\/releases\/download\/v2.17.1\/prometheus-2.17.1.linux-amd64.tar.gz\r\ntar -xvf prometheus-2.17.1.linux-amd64.tar.gz\r\nmv prometheus-2.17.1.linux-amd64 prometheus-files<\/pre>\n<p><strong>Step 5: <\/strong>Now from Prometheus-files folder, copy Prometheus and promtool binary to \/usr\/local\/bin path and assign the ownership of copied Prometheus file to Prometheus user.<\/p>\n<pre class=\"lang:default decode:true\">sudo cp prometheus-files\/prometheus \/usr\/local\/bin\/\r\nsudo cp prometheus-files\/promtool \/usr\/local\/bin\/\r\nsudo chown prometheus:prometheus \/usr\/local\/bin\/prometheus<\/pre>\n<p><strong>Step 6:<\/strong> From Prometheus-files, move the consoles and console_libraries directories to \/etc\/prometheus folder and change the ownership of these directories as well to Prometheus user.<\/p>\n<pre class=\"lang:default decode:true\">sudo cp -r prometheus-files\/consoles \/etc\/prometheus\r\nsudo cp -r prometheus-files\/console_libraries \/etc\/prometheus\r\nsudo chown -R prometheus:prometheus \/etc\/prometheus\/consoles\r\nsudo chown -R prometheus:prometheus \/etc\/prometheus\/console_libraries<\/pre>\n<h3><strong>Setup Prometheus Configuration<\/strong><\/h3>\n<p>As we know that all the configuration files are in \/etc directory in Linux. Therefore, all the Prometheus configurations should be present in \/etc\/prometheus\/prometheus.yml file.<\/p>\n<p><strong>Step 1:<\/strong> Create the configuration file as prometheus.yml and open in the editor.<\/p>\n<pre class=\"lang:default decode:true\">sudo vi \/etc\/prometheus\/prometheus.yml<\/pre>\n<p><strong>Step 2:<\/strong> Copy the following contents to the prometheus.yml file. Here, we are creating a job name as Prometheus which will scrape metrics of Prometheus from the localhost. Then save the file.<\/p>\n<pre class=\"lang:default decode:true\">global:\r\n  scrape_interval: 10s\r\n\r\nscrape_configs:\r\n  - job_name: 'prometheus'\r\n    scrape_interval: 5s\r\n    static_configs:\r\n      - targets: ['localhost:9090']\r\n<\/pre>\n<p><strong>Step 3:<\/strong> Now, change the ownership of the prometheus.yml to Prometheus user.<\/p>\n<pre class=\"lang:default decode:true\">sudo chown prometheus:prometheus \/etc\/prometheus\/prometheus.yml<\/pre>\n<h3><strong>Setup Prometheus Service File<\/strong><\/h3>\n<p><strong>Step 1:<\/strong> Create a Prometheus service file.<\/p>\n<pre class=\"lang:default decode:true\">sudo vi \/etc\/systemd\/system\/prometheus.service<\/pre>\n<p><strong>Step 2:<\/strong> Edit the service file and add the below content in it.<\/p>\n<pre class=\"lang:default decode:true\">[Unit]\r\nDescription=Prometheus\r\nWants=network-online.target\r\nAfter=network-online.target\r\n\r\n [Service]\r\nUser=prometheus\r\nGroup=prometheus\r\nType=simple\r\nExecStart=\/usr\/local\/bin\/prometheus \\\r\n    --config.file \/etc\/prometheus\/prometheus.yml \\\r\n    --storage.tsdb.path \/var\/lib\/prometheus\/ \\\r\n    --web.console.templates=\/etc\/prometheus\/consoles \\\r\n    --web.console.libraries=\/etc\/prometheus\/console_libraries\r\n\r\n [Install]\r\nWantedBy=multi-user.target<\/pre>\n<p><strong>Step 3:<\/strong> Register the Prometheus service be reloading the system service then start the Prometheus service.<\/p>\n<pre class=\"lang:default decode:true\">sudo systemctl daemon-reload\r\nsudo systemctl start prometheus<\/pre>\n<p>Confirm if Prometheus service status is active using the below command.<\/p>\n<pre class=\"lang:default decode:true\">sudo systemctl status prometheus<\/pre>\n<p>The output should be as below.<\/p>\n<h3><strong>Access Prometheus Web UI<\/strong><\/h3>\n<p>You can now connect the Prometheus UI on the Prometheus server IP on port 9090.<\/p>\n<pre class=\"lang:default decode:true\">http:\/\/&lt;prometheus-ip&gt;:9090\/graph<\/pre>\n<p>The following UI should be visible as shown below.<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7068\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/prometheus_4.png\" alt=\"Prometheus On a Linux Server\" width=\"700\" height=\"310\" \/><\/a><\/p>\n<p>We only have installed Prometheus for now. The next step will be to register the targets for the exporters which you are going to use in the prometheus.yml file to get the metrics from the source systems.<\/p>\n<p>If you want to learn more about Prometheus go to our <a href=\"https:\/\/cloudthat.in\/?utm_source=blog-website&amp;utm-medium=text-link&amp;utm_campaign=training-website\" target=\"_blank\" rel=\"noopener\">website<\/a> and check more courses on DevOps.<\/p>\n<p>Please comment if you have any questions.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":229,"featured_media":7074,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3607],"user_email":"saurabhj@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/7060"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/229"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=7060"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/7060\/revisions"}],"predecessor-version":[{"id":42447,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/7060\/revisions\/42447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=7060"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=7060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}