<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jobin Biju</title>
    <description>The latest articles on Forem by Jobin Biju (@jobinbiju).</description>
    <link>https://forem.com/jobinbiju</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F524214%2F476b20ba-68fd-4ebf-8e6d-29edc9c2cf39.JPG</url>
      <title>Forem: Jobin Biju</title>
      <link>https://forem.com/jobinbiju</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jobinbiju"/>
    <language>en</language>
    <item>
      <title>Monitoring Your Server with Prometheus, Node Exporter, and Grafana Using Docker</title>
      <dc:creator>Jobin Biju</dc:creator>
      <pubDate>Tue, 04 Jun 2024 08:35:30 +0000</pubDate>
      <link>https://forem.com/jobinbiju/monitoring-your-server-with-prometheus-node-exporter-and-grafana-using-docker-1ae7</link>
      <guid>https://forem.com/jobinbiju/monitoring-your-server-with-prometheus-node-exporter-and-grafana-using-docker-1ae7</guid>
      <description>&lt;p&gt;In this tutorial, you’ll learn how to set up a comprehensive monitoring stack for your server using Prometheus, Node Exporter, and Grafana. We’ll use Docker to simplify the deployment and Nginx as a reverse proxy. Let’s dive in!&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An Ubuntu server (we're using a Lightsail instance).&lt;/li&gt;
&lt;li&gt;Docker installed.&lt;/li&gt;
&lt;li&gt;Nginx installed and configured as a reverse proxy for your application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1: Setting Up Docker Compose
&lt;/h3&gt;

&lt;p&gt;First, create a directory for your monitoring setup:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mkdir -p ~/monitoring
cd ~/monitoring


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a network for Prometheus and Grafana to communicate:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker network create monitoring


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a Docker Compose file (docker-compose.yml) with the following content:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

version: '3'

services:
  node-exporter:
    image: prom/node-exporter
    container_name: node-exporter
    ports:
      - 9100:9100
    restart: unless-stopped
    networks:
      - monitoring

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
    restart: unless-stopped
    networks:
      - monitoring

  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - 3000:3000
    environment:
      - GF_SERVER_ROOT_URL=%(protocol)s://%(domain)s:%(http_port)s/grafana/
      - GF_SERVER_SERVE_FROM_SUB_PATH=false
    restart: unless-stopped
    networks:
      - monitoring

networks:
  monitoring:
    driver: bridge


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, create the Prometheus configuration file (prometheus.yml):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node-exporter:9100']


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  2: Launch the Monitoring Stack
&lt;/h3&gt;

&lt;p&gt;Run the Docker Compose stack:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

docker-compose up -d


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  3: Configure Nginx as a Reverse Proxy
&lt;/h3&gt;

&lt;p&gt;Open your Nginx configuration file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo nano /etc/nginx/sites-available/default #use your domain instead of default if required.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the following server blocks for Prometheus and Grafana:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

server {
    listen 80;

    server_name your-domain.com; # Replace with your domain

    location / {
        proxy_pass http://localhost:8080; # Your application
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /grafana/ { #subpath to use grafana
        proxy_pass http://localhost:3000/; 
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Save and close the file, then test the Nginx configuration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

sudo nginx -t


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Reload Nginx to apply the changes:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;sudo systemctl reload nginx&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  4: Access Grafana&lt;br&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open your browser and go to &lt;a href="http://your-domain.com/grafana/" rel="noopener noreferrer"&gt;http://your-domain.com/grafana/&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Log in with the default credentials (user: admin, password: admin).&lt;/li&gt;
&lt;li&gt;Make sure to change the password when prompted.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Add Prometheus as a Data Source
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Go to Connections &amp;gt; Data Sources.&lt;/li&gt;
&lt;li&gt;Click on "Add data source" and select Prometheus.&lt;/li&gt;
&lt;li&gt;Set the URL to &lt;a href="http://prometheus:9090" rel="noopener noreferrer"&gt;http://prometheus:9090&lt;/a&gt; and click "Save &amp;amp; Test".&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Create Dashboards
&lt;/h4&gt;

&lt;p&gt;You can now import pre-built dashboards or create your own. A good starting point is the &lt;a href="https://grafana.com/grafana/dashboards/1860-node-exporter-full/" rel="noopener noreferrer"&gt;Node Exporter Full&lt;/a&gt; dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabm61of0dn9wab84261f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabm61of0dn9wab84261f.png" alt="Node Exporter Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congratulations! You have successfully set up a powerful monitoring stack with Prometheus, Node Exporter, and Grafana using Docker, integrated with Nginx as a reverse proxy. Now you can monitor your server's performance and health in real-time. Happy monitoring!&lt;/p&gt;

</description>
      <category>grafana</category>
      <category>prometheus</category>
      <category>aws</category>
      <category>nginx</category>
    </item>
  </channel>
</rss>
