---
# System prepended metadata

title: SQ7A

---

## installation von Prometheus

```bash
 docker run -d -p 9090:9090 --name prometheus prom/prometheus
```
![](https://hackmd.io/_uploads/BJFG4ubO2.png)

### Anpassung des Config File im docker container unter /etc/prometheus/prometheus.yml
![](https://hackmd.io/_uploads/B1YI3d-d2.png)

### Node expoprter Container installieren
![](https://hackmd.io/_uploads/S13y6_ZOn.png)

### Docker compose

Wir haben das docker compose file angepasst um promotheus und node-exporter einzubetten. Zur überwachung von einzelne Server sollte dann aber prometheus enfernt werden und die IP's im angepasst prometheus.yml angepasst werden.

```docker-compose
version: "3.8"

networks:
  210bridge:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/16

services:
  mysqldb:
    image: mysql:5.7
    restart: unless-stopped
    env_file: ./.env
    environment:
      - MYSQL_USER=$MYSQLDB_USER
      - MYSQL_PASSWORD=$MYSQLDB_PASSWORD
      - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
      - MYSQL_DATABASE=$MYSQLDB_DATABASE
    ports:
      - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
    volumes:
      - db:/var/lib/mysql
    networks:
      - 210bridge

  backend:
    depends_on:
      - mysqldb
    image: lubuwiss/lb210-backend:latest
    restart: on-failure
    env_file: ./.env
    ports:
      - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
    environment:
      SPRING_APPLICATION_JSON: '{
        "spring.datasource.url"  : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
        "spring.datasource.username" : "$ROOT",
        "spring.datasource.password" : "$MYSQLDB_PASSWORD",
        "spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQL5InnoDBDialect",
        "spring.jpa.hibernate.ddl-auto" : "create",
        "spring.datasource.initialization-mode" : "always"
      }'
    volumes:
      - .m2:/root/.m2
    stdin_open: true
    tty: true
    networks:
      - 210bridge

  frontend:
    image: lubuwiss/lb210-frontend:latest
    ports:
      - "3000"
    stdin_open: true
    tty: true
    networks:
      - 210bridge

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - frontend
    ports:
      - "4000:4000"
    networks:
      - 210bridge

  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    networks:
      - 210bridge

  node-exporter:
    image: quay.io/prometheus/node-exporter:latest
    restart: unless-stopped
    networks:
      210bridge:
        ipv4_address: 172.25.5.5

volumes:
  db:

```


```prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "server1"
    static_configs:
      - targets: ["172.25.5.5:9100"]

```

![](https://hackmd.io/_uploads/SJ07X5bd2.png)


Memstats des Servers:
![](https://hackmd.io/_uploads/S13LN9Wuh.png)


### Beispiel mit 3 Server

```prometheus.yml
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "server1"
    static_configs:
      - targets: ["172.25.5.5:9100"]

  - job_name: "server2"
    static_configs:
      - targets: ["172.25.5.6:9100"]

  - job_name: "server3"
    static_configs:
      - targets: ["172.25.5.7:9100"]
```

![](https://hackmd.io/_uploads/rkl4wqbun.png)


![](https://hackmd.io/_uploads/r13zD5bO3.png)


## Grafana Setup

Das Grafana image konnten wir direkt in das bestehende compose File einbauen. Hierzu haben wir, zur übesicht die Komponente Frontend, Backend, Ngynx und Mysql Datenbank entfernt.

```Docker-Compose
version: "3.8"

networks:
  210bridge:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/16

services:
  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    networks:
      - 210bridge

  node-exporter:
    image: quay.io/prometheus/node-exporter:latest
    restart: unless-stopped
    networks:
      210bridge:
        ipv4_address: 172.25.5.5

  node-exporter_2:
    image: quay.io/prometheus/node-exporter:latest
    restart: unless-stopped
    networks:
      210bridge:
        ipv4_address: 172.25.5.6

  node-exporter_3:
    image: quay.io/prometheus/node-exporter:latest
    restart: unless-stopped
    networks:
      210bridge:
        ipv4_address: 172.25.5.7

  grafana:
    image: grafana/grafana:latest
    ports:
      - 3003:3000
    volumes:
      - ./grafana.ini:/etc/grafana/grafana.ini
    networks:
      - 210bridge

volumes:
  db:

```

#### grafana.ini
```grafana.ini
[datasource.prometheus]
type = prometheus
url = http://172.25.0.2:9090
access = proxy
is_default = true
```

### Konfigurtion von Grafana
Nachdem die Docker Container gestarted und man sich bei Grafana mit dem standart Login admin:admin eingelogt hat, kann man unter Administration -> Data Source -> Add new data source Prometheus als Data Source hinzufügen.

![](https://hackmd.io/_uploads/HJVbMnZu3.png)

Danach unter Dashboard eine Visualizaion hinzufügen. Darauf kann man unten Query Metrics hinzufügen.

![](https://hackmd.io/_uploads/HJIS-3-_2.png)
