Host & Container Monitoring with Prometheus

Monitoring your systems and containers is essential for maintaining a reliable homelab or home server. A popular setup involves Prometheus, Node Exporter, and cAdvisor for collecting metrics, combined with Grafana for creating insightful dashboards.

In this guide, we’ll set up a complete monitoring solution by:

  1. Configuring Prometheus to scrape metrics from Node Exporter and cAdvisor.
  2. Using Grafana to visualize the data with intuitive dashboards.

Let’s dive in and build a robust monitoring stack!

Setup Node Exporter & Cadvisor

To organize and store configuration files for monitoring, create dedicated folders for Node Exporter and cAdvisor.

Run the following commands to create the required directories:

BASH
1mkdir nodeexporter
2mkdir cadvisor
Click to expand and view more

Next, we’ll set up a docker-compose.yml` file in each folder to configure the respective services.

Node Exporter

Open a new docker-compose.yml file for editing:

BASH
1nano nodeexporter/docker-compose.yml
Click to expand and view more

Paste the following content into the file:

docker-compose.yml
 1services:
 2  nodeexporter:
 3    image: prom/node-exporter
 4    container_name: nodeexporter
 5    volumes:
 6      - /proc:/host/proc:ro
 7      - /sys:/host/sys:ro
 8      - /:/rootfs:ro
 9    command:
10      - '--path.procfs=/host/proc'
11      - '--path.rootfs=/rootfs'
12      - '--path.sysfs=/host/sys'
13      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
14    restart: unless-stopped
15    network_mode: host
Click to expand and view more

The network_mode: host setting allows Node Exporter to access the host network interfaces, enabling it to collect networking metrics.

cAdvisor

Open a new docker-compose.yml file for editing:

BASH
1nano cadvisor/docker-compose.yml
Click to expand and view more

Paste the following content into the file:

docker-compose.yml
 1services:
 2  cadvisor:
 3    image: gcr.io/cadvisor/cadvisor
 4    container_name: cadvisor
 5    privileged: true
 6    devices:
 7      - /dev/kmsg:/dev/kmsg
 8    environment:
 9      - TZ=Europe/Amsterdam
10    volumes:
11      - /:/rootfs:ro
12      - /var/run:/var/run:ro
13      - /sys:/sys:ro
14      - /var/lib/docker:/var/lib/docker:ro
15      - /cgroup:/cgroup:ro
16    command: 
17      - '--housekeeping_interval=15s'
18      - '--docker_only=true'
19    restart: unless-stopped
20    networks:
21      - backend
22networks:
23  backend:
24    name: backend
Click to expand and view more

Now that we have the configurations in place, we can start Node Exporter and cAdvisor by running the following commands:

BASH
1docker compose -f nodeexporter/docker-compose.yml up -d
2docker compose -f cadvisor/docker-compose.yml up -d
Click to expand and view more

Setup Prometheus

To collect the metrics from Node Exporter and cAdvisor, we’ll create a dedicated directory for Prometheus to store its configuration and Docker Compose files.

First, create the Prometheus folder:

BASH
1mkdir prometheus
Click to expand and view more

Next, create a docker-compose.yml file for Prometheus:

BASH
1nano prometheus/docker-compose.yml
Click to expand and view more

Add the following configuration to the file:

docker-compose.yml
 1services:
 2  prometheus:
 3    image: prom/prometheus
 4    container_name: prometheus
 5    environment:
 6      - TZ=Europe/Amsterdam
 7    volumes:
 8      - ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
 9      - prometheus:/prometheus
10    command:
11      - '--config.file=/etc/prometheus/prometheus.yml'
12      - '--storage.tsdb.path=/prometheus'
13      - '--web.console.libraries=/etc/prometheus/console_libraries'
14      - '--web.console.templates=/etc/prometheus/consoles'
15      - '--storage.tsdb.retention.size=100GB'
16      - '--web.enable-lifecycle'
17    restart: unless-stopped
18    expose:
19      - 9090
20    networks:
21      - backend
22    extra_hosts:       
23      - "host.docker.internal:host-gateway"
24networks:
25  backend:
26    name: backend
27volumes:
28    prometheus:
29      name: prometheus
Click to expand and view more

Prometheus requires a configuration file to define which services to scrape for metrics. Create the configuration file:

BASH
1nano prometheus/prometheus.yml
Click to expand and view more

Add the following configuration to the file:

prometheus.yml
 1global:
 2  scrape_interval:     15s
 3  evaluation_interval: 15s
 4scrape_configs:
 5  - job_name: 'cadvisor'
 6    scrape_interval: 10s
 7    static_configs:
 8      - targets: ['cadvisor:8080']
 9  - job_name: 'prometheus'
10    scrape_interval: 10s
11    static_configs:
12      - targets: ['localhost:9090']
13  - job_name: 'nodeexporter'
14    scrape_interval: 10s
15    static_configs:
16      - targets: ['hostip:9100']
17    metric_relabel_configs:
18      - source_labels: [nodename]
19        target_label: "instance"
20        action: "replace"
Click to expand and view more

Since Node Exporter is using the host network, you need to replace hostip with your actual host IP address. The metric_relabel_configs will help in relabeling the hostip:9100 to the actual hostname, making it easier to identify.

Now that you have configured Prometheus, you can start it with the following command:

BASH
1docker compose -f prometheus/docker-compose.yml up -d
Click to expand and view more

This command starts the Prometheus container, which will begin collecting metrics from both Node Exporter and cAdvisor, providing comprehensive monitoring for your systems and containers.

Setup Grafana

To finalize your monitoring setup, we’ll create a directory for Grafana to store its Docker Compose and configuration files.

First, create the Grafana folder:

BASH
1mkdir grafana
Click to expand and view more

Next, create a docker-compose.yml file for Grafana:

BASH
1nano grafana/docker-compose.yml
Click to expand and view more

Add the following configuration to the file:

docker-compose.yml
 1services:
 2  grafana:
 3    image: grafana/grafana
 4    container_name: grafana
 5    environment:
 6      - TZ=Europe/Amsterdam
 7    volumes:
 8      - grafana_data:/var/lib/grafana
 9    restart: unless-stopped
10    ports:
11      - 3000:3000
12    networks:
13      - backend
14networks:
15  backend:
16    name: backend
17volumes:
18    grafana_data:
19      name: grafana_data
Click to expand and view more

Now you can start Grafana by running:

BASH
1docker compose -f grafana/docker-compose.yml up -d
Click to expand and view more

Once Grafana is running, open your browser and navigate to: http://<HOST_IP>:3000

Datasource

To visualize the data collected by Prometheus, you need to add it as a data source in Grafana:

  1. Click Connections in the left-side menu.
  2. Search for Prometheus
  3. Click Add new Datasource
  4. Enter the name prometheus
  5. Fill in the Prometheus server URL http://prometheus:9090

Dashboards

To see all the metrics we need to dashboards. You can make your own dashboards or use mine as a starter:

Conclusion

Congratulations! You have successfully set up host and container monitoring with Prometheus and Grafana. Your monitoring system is now capable of visualizing the metrics from your applications and self-hosting.

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/host-container-monitoring-with-prometheus/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut