From 2ce25ecc08833133978a5c7056a5c3145bbfad8e Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 6 Apr 2023 19:36:57 -0700 Subject: [PATCH] use ansible instead of docker compose Mainly so everything can be on the same Docker network --- lotus-land-story/caddy.yml | 46 ++++++------- lotus-land-story/docker.yml | 10 ++- lotus-land-story/grafana.yml | 43 ++++++------ lotus-land-story/loki.yml | 93 +++++++++++++++++++------- lotus-land-story/main.yml | 1 + lotus-land-story/miniflux.yml | 41 ++++++------ lotus-land-story/prometheus.yml | 46 +++++++------ lotus-land-story/templates/Caddyfile | 14 ++-- lotus-land-story/templates/daemon.json | 6 +- 9 files changed, 173 insertions(+), 127 deletions(-) diff --git a/lotus-land-story/caddy.yml b/lotus-land-story/caddy.yml index 7429f56..2445d12 100644 --- a/lotus-land-story/caddy.yml +++ b/lotus-land-story/caddy.yml @@ -19,29 +19,29 @@ dest: /mnt/lotus-land-story/caddy/Caddyfile mode: "0644" - - name: Run caddy - community.docker.docker_compose: - project_name: caddy - remove_orphans: true - definition: - version: "3.3" - services: - caddy: - image: caddy:2 - container_name: caddy - ports: - - "80:80" - - "443:443" - - "2019:2019" - volumes: - - /mnt/lotus-land-story/caddy/Caddyfile:/etc/caddy/Caddyfile - - /mnt/lotus-land-story/caddy/data:/data - - caddy-config:/config - restart: unless-stopped - extra_hosts: - - "host.docker.internal:host-gateway" - volumes: - caddy-config: + - name: Create Caddy volume + community.docker.docker_volume: + name: caddy + + - name: Run Caddy + community.docker.docker_container: + # recreate: true + # restart: true + name: caddy + image: caddy:2 + ports: + - "80:80" + - "443:443" + - "2019:2019" + volumes: + - /mnt/lotus-land-story/caddy/Caddyfile:/etc/caddy/Caddyfile + - /mnt/lotus-land-story/caddy/data:/data + - caddy-config:/config + restart_policy: unless-stopped + networks: + - name: lotus_land_story + etc_hosts: + host.docker.internal: host-gateway handlers: - name: Import restarts diff --git a/lotus-land-story/docker.yml b/lotus-land-story/docker.yml index d39d7b5..79028ee 100644 --- a/lotus-land-story/docker.yml +++ b/lotus-land-story/docker.yml @@ -45,12 +45,6 @@ - docker-compose-plugin update_cache: true - - name: Install Loki Docker driver - community.docker.docker_plugin: - plugin_name: grafana/loki-docker-driver - alias: loki - state: enable - - name: Create Docker volume location ansible.builtin.file: path: /mnt/lotus-land-story/docker @@ -77,6 +71,10 @@ mode: "0644" notify: Restart docker + - name: Create docker network + community.docker.docker_network: + name: lotus_land_story + handlers: - name: Import restarts ansible.builtin.import_tasks: restarts.yml diff --git a/lotus-land-story/grafana.yml b/lotus-land-story/grafana.yml index 1c250a3..6fa52ca 100644 --- a/lotus-land-story/grafana.yml +++ b/lotus-land-story/grafana.yml @@ -1,5 +1,7 @@ - name: Set up Grafana hosts: lotus-land-story + vars_files: + - vars.yml tasks: - name: Create Grafana dir @@ -32,7 +34,7 @@ type: prometheus # Access mode - proxy (server in the UI) or direct (browser in the UI). access: proxy - url: http://host.docker.internal:9090 + url: http://prometheus:9090 jsonData: httpMethod: POST manageAlerts: true @@ -40,25 +42,26 @@ prometheusVersion: 2.37.0 mode: "0644" + - name: Create Grafana volume + community.docker.docker_volume: + name: grafana + - name: Run Grafana - community.docker.docker_compose: - project_name: grafana - remove_orphans: true - definition: - version: "3.3" - services: - grafana: - image: grafana/grafana-oss:latest - ports: - - "3000:3000" - volumes: - - /mnt/lotus-land-story/grafana/grafana.ini:/etc/grafana/grafana.ini - - /mnt/lotus-land-story/grafana/provisioning:/etc/grafana/provisioning - - grafana:/var/lib/grafana - restart: unless-stopped - extra_hosts: - - "host.docker.internal:host-gateway" - volumes: - grafana: + community.docker.docker_container: + # recreate: true + # restart: true + name: grafana + image: grafana/grafana-oss:9.2.15 + ports: + - "3000:3000" + volumes: + - /mnt/lotus-land-story/grafana/grafana.ini:/etc/grafana/grafana.ini + - /mnt/lotus-land-story/grafana/provisioning:/etc/grafana/provisioning + - grafana:/var/lib/grafana + restart_policy: unless-stopped + networks: + - name: lotus_land_story + etc_hosts: + host.docker.internal: host-gateway # vim: ft=yaml.ansible diff --git a/lotus-land-story/loki.yml b/lotus-land-story/loki.yml index a455092..b382c16 100644 --- a/lotus-land-story/loki.yml +++ b/lotus-land-story/loki.yml @@ -15,13 +15,15 @@ - name: Loki type: loki access: proxy - url: http://host.docker.internal:3100 + url: http://loki:3100 mode: "0644" - name: Create Loki config dirs ansible.builtin.file: path: /mnt/lotus-land-story/loki/{{ item }} state: directory + owner: 10001 + group: 10001 mode: "0755" loop: - config @@ -29,13 +31,11 @@ - name: Configure Loki ansible.builtin.copy: - dest: /mnt/lotus-land-story/loki/config/config.yml + dest: /mnt/lotus-land-story/loki/config/loki.yml content: | auth_enabled: false - server: http_listen_port: 3100 - common: path_prefix: /loki storage: @@ -46,7 +46,6 @@ ring: kvstore: store: inmemory - schema_config: configs: - from: 2020-10-24 @@ -56,32 +55,78 @@ index: prefix: index_ period: 24h - storage_config: filesystem: directory: /data - ruler: alertmanager_url: http://localhost:9093 + owner: 10001 + group: 10001 + mode: "0644" + + # https://github.com/grafana/loki/issues/2361 + - name: Configure Promtail + ansible.builtin.copy: + dest: /mnt/lotus-land-story/loki/config/promtail.yml + content: | + server: + http_listen_port: 9080 + grpc_listen_port: 0 + positions: + filename: /tmp/positions.yaml + clients: + - url: http://loki:3100/loki/api/v1/push + scrape_configs: + - job_name: system + static_configs: + - targets: + - localhost + labels: + job: varlogs + __path__: /var/log/*log + - job_name: docker + docker_sd_configs: + - host: unix:///var/run/docker.sock + refresh_interval: 5s + relabel_configs: + - source_labels: ['__meta_docker_container_name'] + regex: '/(.*)' + target_label: 'container' mode: "0644" - name: Run Loki - community.docker.docker_compose: - project_name: loki - remove_orphans: true - definition: - version: "3.3" - services: - loki: - image: grafana/loki:2.8.0 - ports: - - "3100:3100" - command: -config.file=/mnt/config/config.yml - volumes: - - /mnt/lotus-land-story/loki/config:/mnt/config - - /mnt/lotus-land-story/loki/data:/data - restart: unless-stopped - extra_hosts: - - "host.docker.internal:host-gateway" + community.docker.docker_container: + # recreate: true + # restart: true + name: loki + image: grafana/loki:2.8.0 + ports: + - "3100:3100" + command: -config.file=/mnt/config/loki.yml + volumes: + - /mnt/lotus-land-story/loki/config:/mnt/config + - /mnt/lotus-land-story/loki/data:/data + restart_policy: unless-stopped + networks: + - name: lotus_land_story + etc_hosts: + host.docker.internal: host-gateway + + - name: Run Promtail + community.docker.docker_container: + # recreate: true + # restart: true + name: promtail + image: grafana/promtail:2.8.0 + command: -config.file=/mnt/config/promtail.yml + volumes: + - /var/log:/var/log + - /var/run/docker.sock:/var/run/docker.sock + - /mnt/lotus-land-story/loki/config:/mnt/config + restart_policy: unless-stopped + networks: + - name: lotus_land_story + etc_hosts: + host.docker.internal: host-gateway # vim: ft=yaml.ansible diff --git a/lotus-land-story/main.yml b/lotus-land-story/main.yml index 6050cfa..a8aa556 100644 --- a/lotus-land-story/main.yml +++ b/lotus-land-story/main.yml @@ -89,6 +89,7 @@ when: "'prometheus' not in postgres_info.roles" - import_playbook: grafana.yml # noqa: name[play] +- import_playbook: loki.yml # noqa: name[play] - import_playbook: miniflux.yml # noqa: name[play] - import_playbook: caddy.yml # noqa: name[play] diff --git a/lotus-land-story/miniflux.yml b/lotus-land-story/miniflux.yml index 8df8271..fd10d69 100644 --- a/lotus-land-story/miniflux.yml +++ b/lotus-land-story/miniflux.yml @@ -46,26 +46,25 @@ - vars.yml tasks: - - name: Run miniflux - community.docker.docker_compose: - project_name: miniflux - remove_orphans: true - definition: - version: "3.3" - services: - miniflux: - image: miniflux/miniflux:latest - ports: - - "8080:8080" - environment: - - DATABASE_URL=postgres://miniflux:{{ miniflux_db_password }}@host.docker.internal/miniflux - - RUN_MIGRATIONS=1 - - CREATE_ADMIN=1 - - ADMIN_USERNAME=alpha - - ADMIN_PASSWORD={{ miniflux_password }} - - BASE_URL=https://rss.{{ domain }} - restart: unless-stopped - extra_hosts: - - "host.docker.internal:host-gateway" + - name: Run Miniflux + community.docker.docker_container: + # recreate: true + # restart: true + name: miniflux + image: miniflux/miniflux:2.0.43 + ports: + - "8080:8080" + env: + DATABASE_URL: postgres://miniflux:{{ miniflux_db_password }}@host.docker.internal/miniflux + RUN_MIGRATIONS: "1" + CREATE_ADMIN: "1" + ADMIN_USERNAME: alpha + ADMIN_PASSWORD: "{{ miniflux_password }}" + BASE_URL: https://rss.{{ domain }} + restart_policy: unless-stopped + networks: + - name: lotus_land_story + etc_hosts: + host.docker.internal: host-gateway # vim: ft=yaml.ansible diff --git a/lotus-land-story/prometheus.yml b/lotus-land-story/prometheus.yml index d6d5670..3632f21 100644 --- a/lotus-land-story/prometheus.yml +++ b/lotus-land-story/prometheus.yml @@ -34,7 +34,7 @@ monitor: 'codelab-monitor' scrape_configs: - - job_name: 'prometheus' + - job_name: prometheus static_configs: - targets: ['localhost:9090'] @@ -42,38 +42,42 @@ static_configs: - targets: ['host.docker.internal:9100'] - - job_name: 'docker' + - job_name: docker static_configs: - targets: ['host.docker.internal:9323'] - job_name: caddy static_configs: - - targets: ['host.docker.internal:2019'] + - targets: ['caddy:2019'] - - job_name: 'grafana' + - job_name: grafana static_configs: - - targets: ['host.docker.internal:3000'] + - targets: ['grafana:3000'] - - job_name: 'postgres' + - job_name: postgres static_configs: - targets: ['host.docker.internal:9187'] mode: "0644" + - name: Create Prometheus volume + community.docker.docker_volume: + name: prometheus + - name: Run Prometheus - community.docker.docker_compose: - project_name: prometheus - remove_orphans: true - definition: - version: "3.3" - services: - prometheus: - image: prom/prometheus:latest - ports: - - "9090:9090" - volumes: - - /mnt/lotus-land-story/prometheus:/etc/prometheus - restart: unless-stopped - extra_hosts: - - "host.docker.internal:host-gateway" + community.docker.docker_container: + # recreate: true + # restart: true + name: prometheus + image: prom/prometheus:v2.43.0 + ports: + - "9090:9090" + volumes: + - /mnt/lotus-land-story/prometheus:/etc/prometheus + - prometheus:/prometheus + restart_policy: unless-stopped + networks: + - name: lotus_land_story + etc_hosts: + host.docker.internal: host-gateway # vim: ft=yaml.ansible diff --git a/lotus-land-story/templates/Caddyfile b/lotus-land-story/templates/Caddyfile index 77fd166..bd26f53 100644 --- a/lotus-land-story/templates/Caddyfile +++ b/lotus-land-story/templates/Caddyfile @@ -2,18 +2,18 @@ metrics } +loki.{{ domain }} { + reverse_proxy loki:3100 +} + rss.{{ domain }} { - reverse_proxy host.docker.internal:8080 + reverse_proxy miniflux:8080 } prometheus.{{ domain }} { - reverse_proxy host.docker.internal:9090 + reverse_proxy prometheus:9090 } grafana.{{ domain }} { - reverse_proxy host.docker.internal:3000 -} - -loki.{{ domain }} { - reverse_proxy host.docker.internal:3100 + reverse_proxy grafana:3000 } diff --git a/lotus-land-story/templates/daemon.json b/lotus-land-story/templates/daemon.json index 8c88120..ba28dd9 100644 --- a/lotus-land-story/templates/daemon.json +++ b/lotus-land-story/templates/daemon.json @@ -1,8 +1,4 @@ { "data-root": "/mnt/lotus-land-story/docker", - "metrics-addr": "{{ docker_ip.address }}:9323", - "log-driver": "loki", - "log-opts": { - "loki-url": "http://localhost:3100/loki/api/v1/push" - } + "metrics-addr": "{{ docker_ip.address }}:9323" }