feat: add worker nodes to docker swarm setup

This commit is contained in:
Alexander Navarro 2024-12-10 16:26:17 -03:00
parent 22d7e4a318
commit d318880600
10 changed files with 252 additions and 112 deletions

View file

@ -0,0 +1,20 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/refs/heads/main/src/ansiblelint/schemas/tasks.json
---
- name: Install Docker
ansible.builtin.package:
state: present
name:
- docker
- docker-cli-compose
- py3-yaml
- py3-pip
- py3-docker-py
- name: Copy openrc.sh to /etc/init.d/docker
copy:
src: files/openrc.sh
dest: /etc/init.d/docker
mode: '0755'
owner: root
group: root

View file

@ -1,94 +1,23 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/refs/heads/main/src/ansiblelint/schemas/tasks.json
---
- name: Create a user group named docker
ansible.builtin.group:
name: "docker"
- name: Add users to docker group
loop: "{{ users }}"
ansible.builtin.user:
state: present
append: true
name: "{{ item }}"
groups: "docker"
- name: Install docker in Alpine
import_tasks: docker_alpine.yaml
when: ansible_facts['os_family']|lower == 'alpine'
- name: Start docker service
ansible.builtin.service:
name: docker
state: started
enabled: true
- name: Setup Docker Swarm
when: docker_swarm_manager | bool
block:
- name: Enable Docker Swarm mode
community.docker.docker_swarm:
state: present
- name: Create Traefik network
community.docker.docker_network:
name: reverse-proxy
driver: overlay
attachable: true
- name: Deploy Traefik service
community.docker.docker_compose_v2:
remove_orphans: true
project_name: reverse-proxy
definition:
networks:
reverse-proxy:
external: true
services:
traefik:
container_name: traefix-proxy
image: 'traefik:latest'
restart: unless-stopped
networks:
- reverse-proxy
ports:
# listen on host ports without ingress network
- target: 80
published: 80
protocol: tcp
mode: host
- target: 443
published: 443
protocol: tcp
mode: host
- target: 8080
published: 8080
protocol: tcp
mode: host
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
healthcheck:
test: 'wget -qO- http://localhost:80/ping || exit 1'
interval: 4s
timeout: 2s
retries: 5
command:
- '--ping=true'
- '--ping.entrypoint=http'
- '--api.dashboard=true'
- '--api.insecure=true'
- '--entrypoints.http.address=:80'
- '--entryPoints.http.forwardedHeaders.trustedIPs=10.0.10.0/24'
- '--entrypoints.http.http.encodequerysemicolons=true'
- '--entryPoints.http.http2.maxConcurrentStreams=50'
# - "--providers.swarm.endpoint=tcp://{{ ansible_default_ipv4.address }}:2375"
- --providers.swarm.exposedByDefault=false
- --providers.swarm.network=reverse-proxy
deploy:
mode: global
placement:
constraints:
- node.role==manager
labels:
- traefik.enable=true
- traefik.http.routers.traefik.entrypoints=http
- traefik.http.routers.traefik.service=api@internal
- traefik.http.services.traefik.loadbalancer.server.port=8080
- name: Check if Docker context exists
local_action: ansible.builtin.command docker context inspect {{ ansible_hostname }}
register: context_exists
ignore_errors: true
- name: Create Docker context for each Swarm manager machine
local_action: >
ansible.builtin.command docker context create {{ ansible_hostname }} --docker "host=ssh://{{ ansible_default_ipv4.address }}"
when: context_exists.stderr != ''
- name: Join Docker Swarm as a worker
community.docker.docker_swarm:
state: join
join_token: "{{ hostvars['manager']['docker_swarm_worker_token'] }}"
remote_addrs: ["{{ hostvars['manager']['ansible_default_ipv4']['address'] }}"]
when: not docker_swarm_manager | bool

View file

@ -0,0 +1,81 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/refs/heads/main/src/ansiblelint/schemas/tasks.json
---
- name: Enable Docker Swarm mode
register: swarm_info
community.docker.docker_swarm:
state: present
- name: Create Traefik network
community.docker.docker_network:
name: reverse-proxy
driver: overlay
attachable: true
- name: Check if Docker context exists
local_action: ansible.builtin.command docker context inspect {{ ansible_hostname }}
register: context_exists
ignore_errors: true
- name: Create Docker context for each Swarm manager machine
local_action: >
ansible.builtin.command docker context create {{ ansible_hostname }} --docker "host=ssh://{{ ansible_default_ipv4.address }}"
when: context_exists.stderr != ''
- name: Deploy Traefik service
community.docker.docker_compose_v2:
remove_orphans: true
project_name: reverse-proxy
definition:
networks:
reverse-proxy:
external: true
services:
traefik:
container_name: traefix-proxy
image: 'traefik:latest'
restart: unless-stopped
networks:
- reverse-proxy
ports:
# listen on host ports without ingress network
- target: 80
published: 80
protocol: tcp
mode: host
- target: 443
published: 443
protocol: tcp
mode: host
- target: 8080
published: 8080
protocol: tcp
mode: host
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
healthcheck:
test: 'wget -qO- http://localhost:80/ping || exit 1'
interval: 4s
timeout: 2s
retries: 5
command:
- '--ping=true'
- '--ping.entrypoint=http'
- '--api.dashboard=true'
- '--api.insecure=true'
- '--entrypoints.http.address=:80'
- '--entryPoints.http.forwardedHeaders.trustedIPs=10.0.10.0/24'
- '--entrypoints.http.http.encodequerysemicolons=true'
- '--entryPoints.http.http2.maxConcurrentStreams=50'
# - "--providers.swarm.endpoint=tcp://{{ ansible_default_ipv4.address }}:2375"
- --providers.swarm.exposedByDefault=false
- --providers.swarm.network=reverse-proxy
deploy:
mode: global
placement:
constraints:
- node.role==manager
labels:
- traefik.enable=true
- traefik.http.routers.traefik.entrypoints=http
- traefik.http.routers.traefik.service=api@internal
- traefik.http.services.traefik.loadbalancer.server.port=8080

View file

@ -0,0 +1,9 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/refs/heads/main/src/ansiblelint/schemas/tasks.json
---
- name: Join Docker Swarm as a worker
vars:
key: "{{ groups[managers_group] | map('extract', hostvars, ['swarm_info', 'swarm_facts', 'JoinTokens', 'Worker']) | list | first }}"
community.docker.docker_swarm:
state: join
join_token: "{{ key }}"
remote_addrs: "{{ groups[managers_group] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) }}"