sentenz / devops

A service for DevOps operations.
Apache License 2.0
1 stars 3 forks source link

Monitoring and Logging #110

Open sentenz opened 11 months ago

sentenz commented 11 months ago

Prometheus & Grafana

Prometheus and Grafana are versatile and can be used to monitor a variety of applications and systems. They're commonly applied to monitor web servers, databases (like MySQL or PostgreSQL), container orchestration platforms (such as Kubernetes), and even custom applications. Their flexibility makes them handy for diverse monitoring needs.

Below is a simple example of using Prometheus and Grafana to monitor the metrics of all containers in a Docker environment:

  1. Docker Compose File (docker-compose.yml):

    version: '3'
    services:
     prometheus:
       image: prom/prometheus:latest
       volumes:
         - ./prometheus:/etc/prometheus
       command:
         - '--config.file=/etc/prometheus/prometheus.yml'
       ports:
         - '9090:9090'
     grafana:
       image: grafana/grafana:latest
       environment:
         - GF_SECURITY_ADMIN_PASSWORD=admin
       ports:
         - '3000:3000'
     node-exporter:
       image: prom/node-exporter:latest
       volumes:
         - /proc:/host/proc:ro
         - /sys:/host/sys:ro
         - /:/rootfs:ro
       command:
         - '--path.procfs=/host/proc'
         - '--path.sysfs=/host/sys'
         - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
  2. Prometheus Configuration (prometheus/prometheus.yml):

    global:
     scrape_interval: 15s
    scrape_configs:
     - job_name: 'docker-containers'
       static_configs:
         - targets: ['node-exporter:9100']
  3. Run Docker Compose:

    docker-compose up -d
  4. Access Grafana:

    Open your browser and go to http://localhost:3000. Log in with username admin and password admin. Add Prometheus as a data source and import dashboards for Docker and Node Exporter.

This setup includes Prometheus, Grafana, and Node Exporter. Node Exporter collects system metrics, and Prometheus scrapes those metrics. Grafana visualizes the data using pre-built Docker and Node Exporter dashboards. Adjust configurations based on your specific needs.

sentenz commented 11 months ago

Ansible

Certainly! Below is an Ansible playbook example to set up the mentioned Prometheus, Grafana, and Node Exporter stack:

---
- hosts: localhost
  become: true
  tasks:
    - name: Create Docker Compose file
      copy:
        content: |
          version: '3'
          services:
            prometheus:
              image: prom/prometheus:latest
              volumes:
                - ./prometheus:/etc/prometheus
              command:
                - '--config.file=/etc/prometheus/prometheus.yml'
              ports:
                - '9090:9090'
            grafana:
              image: grafana/grafana:latest
              environment:
                - GF_SECURITY_ADMIN_PASSWORD=admin
              ports:
                - '3000:3000'
              depends_on:
                - prometheus
            node-exporter:
              image: prom/node-exporter:latest
              volumes:
                - /proc:/host/proc:ro
                - /sys:/host/sys:ro
                - /:/rootfs:ro
              command:
                - '--path.procfs=/host/proc'
                - '--path.sysfs=/host/sys'
                - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
      dest: ~/docker-compose.yml

    - name: Create Prometheus Configuration
      copy:
        content: |
          global:
            scrape_interval: 15s
          scrape_configs:
            - job_name: 'docker-containers'
              static_configs:
                - targets: ['node-exporter:9100']
      dest: ~/prometheus/prometheus.yml

    - name: Run Docker Compose
      command: docker-compose up -d
      args:
        chdir: ~

    - name: Wait for services to start
      wait_for:
        host: localhost
        port: 3000

This Ansible playbook does the following:

  1. Copies the Docker Compose file to the target machine.
  2. Copies the Prometheus configuration to the target machine.
  3. Runs docker-compose up -d.
  4. Waits for Grafana to start.

Make sure Docker and Ansible are installed on the target machine, and adjust the paths and configurations as needed.