From cd5777402e461402d8802a95f1df359ee6aaef4b Mon Sep 17 00:00:00 2001 From: Jacob Cody Wimer Date: Tue, 9 Oct 2018 15:00:01 -0400 Subject: [PATCH] Headed in a new direction using swarm --- .gitignore | 2 + group_vars/{all => all.example} | 11 +- hosts.example | 10 + multi-instance-hosts | 0 playbooks/multi-instance.yml | 13 +- roles/startup-infrastructure/tasks/main.yml | 6 + .../templates/docker-compose.yml.j2 | 179 ++++++++++++++++++ supporting-scripts/deploy.sh | 4 + supporting-scripts/multi-instance-deploy.sh | 4 - 9 files changed, 212 insertions(+), 17 deletions(-) create mode 100644 .gitignore rename group_vars/{all => all.example} (65%) create mode 100644 hosts.example delete mode 100644 multi-instance-hosts create mode 100644 roles/startup-infrastructure/tasks/main.yml create mode 100644 roles/startup-infrastructure/templates/docker-compose.yml.j2 create mode 100644 supporting-scripts/deploy.sh delete mode 100644 supporting-scripts/multi-instance-deploy.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa5f6cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +group_vars/all +hosts \ No newline at end of file diff --git a/group_vars/all b/group_vars/all.example similarity index 65% rename from group_vars/all rename to group_vars/all.example index beda6e4..cbc6329 100644 --- a/group_vars/all +++ b/group_vars/all.example @@ -1,18 +1,23 @@ --- # Variables listed here are applicable to all host groups -# Software versions +### Software versions docker_compose_version_to_install: 1.18.0 docker_ce_version_to_install: 17.09.1~ce-0~ubuntu -# User stuff +### User stuff default_pub_key: https://raw.githubusercontent.com/jcwimer/ubuntu-template/master/post/id_rsa.pub standard_user: cody git_user: "Jacob Cody Wimer" git_email: "jacob.wimer@gmail.com" chosen_timezone: "America/New_York" +# root domain for all services. You should have an A record for *.root_domain. For example, if your domain is test.com you should have an A record for *.test.com pointing to your node. +# this will allow automatic dns for for things like dokuwiki.test.com and portainer.test.com +root_domain: test.com -# Persistent storage if you are doing a single machine deploy, local is an option. If you are doing multi instance deploy, choose one of the following: + + +### Persistent storage if you are doing a single machine deploy, local is an option. If you are doing multi instance deploy, choose one of the following: # nfs # digitalocean # gcp diff --git a/hosts.example b/hosts.example new file mode 100644 index 0000000..add8dcf --- /dev/null +++ b/hosts.example @@ -0,0 +1,10 @@ +# Node where config files get copied to and docker swarm gets initiated +# replace localhost with the ip of your bootstrap node +[bootstrap] +localhost + +# nodes that will be swarm managers (note these will not host services) +[non-bootstrap-managers] + +# nodes that will be swarm workers (note these will need to have more resources than managers) +[workers] \ No newline at end of file diff --git a/multi-instance-hosts b/multi-instance-hosts deleted file mode 100644 index e69de29..0000000 diff --git a/playbooks/multi-instance.yml b/playbooks/multi-instance.yml index 4d12294..b9ba743 100644 --- a/playbooks/multi-instance.yml +++ b/playbooks/multi-instance.yml @@ -19,16 +19,9 @@ tasks: - include: ../roles/common/tasks/main.yml -- name: Deploy gitea - hosts: gitea +- name: Deploy startup-infrastructure swarm stack + hosts: bootstrap user: root serial: 100% tasks: - - include: ../roles/gitea/tasks/main.yml - -- name: Deploy dokuwiki - hosts: dokuwiki - user: root - serial: 100% - tasks: - - include: ../roles/dokuwiki/tasks/main.yml \ No newline at end of file + - include: ../roles/startup-infrastructure/tasks/main.yml \ No newline at end of file diff --git a/roles/startup-infrastructure/tasks/main.yml b/roles/startup-infrastructure/tasks/main.yml new file mode 100644 index 0000000..5005f8e --- /dev/null +++ b/roles/startup-infrastructure/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: Place the compose file + template: src=../roles/startup-infrastructure/templates/docker-compose.yml.j2 dest=/data/startup-infrastructure.yml + +- name: Run stack deploy + shell: cd /data && docker stack deploy -c startup-infrastructure.yml startup-infrastructure \ No newline at end of file diff --git a/roles/startup-infrastructure/templates/docker-compose.yml.j2 b/roles/startup-infrastructure/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..d44904f --- /dev/null +++ b/roles/startup-infrastructure/templates/docker-compose.yml.j2 @@ -0,0 +1,179 @@ +version: '3.1' +networks: + appnet: + wekan: + driver: bridge + +services: + traefik: + image: traefik:1.6.4 + ports: + - "80:80" + - "443:443" + - "8081:8080" + networks: + - appnet + volumes: + - ./traefik.toml:/etc/traefik/traefik.toml + - /var/run/docker.sock:/var/run/docker.sock:ro + deploy: + mode: replicated + replicas: 1 + placement: + constraints: + - node.role == manager + + portainer: + image: portainer/portainer + networks: + - appnet + volumes: + - portainer_data:/data + - /var/run/docker.sock:/var/run/docker.sock:ro + deploy: + labels: + - "traefik.frontend.entryPoints=http" + - "traefik.protocol=http" + - "traefik.backend=portainer" + - "traefik.port=9000" + - "traefik.docker.network=appnet" + - "traefik.frontend.rule=Host:portainer.{{ root_domain }}" + mode: replicated + replicas: 1 + {% if {{ groups['workers'] | length }} > 0 %} + placement: + constraints: + - node.role == worker + {% endif %} + + bitwarden: + image: mprasil/bitwarden + networks: + - appnet + volumes: + - bitwarden_data:/data + deploy: + labels: + - "traefik.frontend.entryPoints=http" + - "traefik.protocol=http" + - "traefik.backend=bitwarden" + - "traefik.port=80" + - "traefik.docker.network=appnet" + - "traefik.frontend.rule=Host:bitwarden.{{ root_domain }}" + mode: replicated + replicas: 1 + {% if {{ groups['workers'] | length }} > 0 %} + placement: + constraints: + - node.role == worker + {% endif %} + + gitea: + image: gitea/gitea:latest + environment: + - USER_UID=1000 + - USER_GID=1000 + networks: + - appnet + volumes: + - gitea_data:/data + ports: + - "2222:22" + deploy: + labels: + - "traefik.frontend.entryPoints=http" + - "traefik.protocol=http" + - "traefik.backend=git" + - "traefik.port=3000" + - "traefik.docker.network=appnet" + - "traefik.frontend.rule=Host:git.{{ root_domain }}" + mode: replicated + replicas: 1 + {% if {{ groups['workers'] | length }} > 0 %} + placement: + constraints: + - node.role == worker + {% endif %} + + dokuwiki: + image: mprasil/dokuwiki + networks: + - appnet + volumes: + - dokuwiki_data:/dokuwiki + deploy: + labels: + - "traefik.frontend.entryPoints=http" + - "traefik.protocol=http" + - "traefik.backend=dokuwiki" + - "traefik.port=80" + - "traefik.docker.network=appnet" + - "traefik.frontend.rule=Host:dokuwiki.{{ root_domain }}" + mode: replicated + replicas: 1 + {% if {{ groups['workers'] | length }} > 0 %} + placement: + constraints: + - node.role == worker + {% endif %} + + wekandb: + # All Wekan data is stored in MongoDB. For backup and restore, see: + # https://github.com/wekan/wekan/wiki/Export-Docker-Mongo-Data + image: mongo:3.2.21 + command: mongod --smallfiles --oplogSize 128 + networks: + - wekan + volumes: + - wekan-db:/data/db + - wekan-db-dump:/dump + deploy: + mode: replicated + replicas: 1 + {% if {{ groups['workers'] | length }} > 0 %} + placement: + constraints: + - node.role == worker + {% endif %} + + wekan: + image: quay.io/wekan/wekan + networks: + - wekan + - appnet + environment: + - ROOT_URL=http://{{ root_domain }} + - MONGO_URL=mongodb://wekandb:27017/wekan + #- MAIL_URL=smtp://user:pass@mailserver.example.com:25/ + #- MAIL_FROM='Example Wekan Support ' + - WITH_API=true + deploy: + labels: + - "traefik.frontend.entryPoints=http" + - "traefik.protocol=http" + - "traefik.backend=wekan" + - "traefik.port=8080" + - "traefik.docker.network=appnet" + - "traefik.frontend.rule=Host:wekan.{{ root_domain }}" + mode: replicated + replicas: 1 + {% if {{ groups['workers'] | length }} > 0 %} + placement: + constraints: + - node.role == worker + {% endif %} + +{% set docker_volumes = ['portainer_data','bitwarden_data','gitea_data','dokuwiki_data','wekan-db','wekan-db-dump'] %} +volumes: +{% for volume in docker_volumes %} + {{ volume }}: + {% if storage_type == 'nfs' %} + driver: local + driver_opts: + type: nfs + o: "addr={{ nfs_address }},soft,nolock,rw" + device: ":{{ nfs_root_path }}/{{ volume }}" + {% elif storage_type == 'local' %} + driver: local + {% endif %} +{% endfor %} \ No newline at end of file diff --git a/supporting-scripts/deploy.sh b/supporting-scripts/deploy.sh new file mode 100644 index 0000000..0963fee --- /dev/null +++ b/supporting-scripts/deploy.sh @@ -0,0 +1,4 @@ +#!/bin/bash +#keep adding dirname's to go up more directories. +project_dir="$(dirname $( dirname $(readlink -f ${BASH_SOURCE[0]})))" +ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook --private-key ${PRIVATE_KEY} -i ${project_dir}/hosts ${project_dir}/playbooks/multi-instance.yml \ No newline at end of file diff --git a/supporting-scripts/multi-instance-deploy.sh b/supporting-scripts/multi-instance-deploy.sh deleted file mode 100644 index 88479e0..0000000 --- a/supporting-scripts/multi-instance-deploy.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -#keep adding dirname's to go up more directories. -project_dir="$(dirname $( dirname $(readlink -f ${BASH_SOURCE[0]})))" -ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i ${project_dir}/multi-instance-hosts ${project_dir}/playbooks/multi-instance.yml \ No newline at end of file