Docker: Manage a swarm.

This commit is contained in:
Andrea Dell'Amico 2019-09-24 18:16:04 +02:00
parent ba0a801b25
commit 751d24740c
6 changed files with 140 additions and 18 deletions

View File

@ -1,15 +1,29 @@
---
docker_install: False
docker_swarm: False
docker_repo_key: '58118E89F3A912897C070ADBF76221572C52609D'
docker_repo_keyserver: 'hkp://ha.pool.sks-keyservers.net:80'
docker_repo_key_url: 'https://download.docker.com/linux/ubuntu/gpg'
# Set to the Debian distribution name if not Ubuntu
docker_distribution: '{{ ansible_distribution_release | lower }}'
docker_repo: 'deb https://apt.dockerproject.org/repo {{ ansible_distribution | lower }}-{{ docker_distribution }} main'
docker_repo_old: 'deb https://apt.dockerproject.org/repo {{ ansible_distribution | lower }}-{{ docker_distribution }} main'
docker_repo: 'deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable'
# Swarm
docker_swarm: False
docker_swarm_manager: False
docker_swarm_manager_availability: 'drain'
docker_swarm_node: False
docker_swarm_node_availability: 'active'
docker_swarm_allowed_hosts:
- '0.0.0.0/0'
# Set this to one of the manager hostnames to be able to initialize the cluster
docker_swarm_manager_main_node_hostname: localhost
# Set this on one of the masters
docker_swarm_manager_main_node: False
docker_pkg_status: latest
docker_packages:
- docker-engine
- docker-ce
- docker-ce-cli
docker_run_as_docker_user: True
docker_user_home: /home/docker
@ -17,3 +31,4 @@ docker_defaults_file: /etc/default/docker
docker_enable_tcp_socket: False
docker_tcp_socket_port: 2375
docker_tcp_socket_host: 127.0.0.1

View File

@ -1,8 +1,8 @@
---
- import_tasks: pkgs.yml
- name: Enable Docker TCP on local bridge (for docker swarm)
action: configfile path=/etc/default/docker syntax=shell key=DOCKER_OPTS value="\"-H tcp://172.17.42.1:2375 -H unix:///var/run/docker.sock\""
notify: Restart docker
when: docker_swarm
tags: docker-conf
- import_tasks: swarm_mgr.yml
when: docker_swarm | bool
- import_tasks: swarm_node.yml
when: docker_swarm | bool

View File

@ -1,7 +1,11 @@
---
- block:
- name: Add the Docker repository key
apt_key: keyserver={{ docker_repo_keyserver }} id={{ docker_repo_key }} state=present
#apt_key: keyserver={{ docker_repo_keyserver }} id={{ docker_repo_key }} state=present
apt_key: url={{ docker_repo_key_url }} state=present
- name: Remove the obsolete docker repository
apt_repository: repo='{{ docker_repo_old }}' state=absent update_cache=yes
- name: Add the Docker repository
apt_repository: repo='{{ docker_repo }}' update_cache=yes
@ -11,8 +15,7 @@
when: docker_run_as_docker_user
- name: Install Docker
apt: pkg={{ item }} state={{ docker_pkg_status }} update_cache=yes cache_valid_time=3600
with_items: '{{ docker_packages }}'
apt: pkg={{ docker_packages }} state={{ docker_pkg_status }} update_cache=yes cache_valid_time=3600
- name: Install the Docker default options
template: src=docker-default.j2 dest={{ docker_defaults_file }} owner=root group=root mode=0644
@ -21,16 +24,16 @@
- name: Ensure that the service is started and enabled
service: name=docker state=started enabled=yes
when: docker_install
when: docker_install | bool
tags: docker
- block:
- name: Ensure that the service is stopped and disabled
service: name=docker state=stopped enabled=no
ignore_errors: True
- name: Remove the Docker package
apt: pkg={{ item }} state=absent
with_items: '{{ docker_packages }}'
apt: pkg={{ docker_packages }} state=absent
- name: Remove the docker user
user: name=docker state=absent
@ -41,5 +44,5 @@
- name: Remove the Docker repository
apt_repository: repo='{{ docker_repo }}' state=absent update_cache=yes
when: not docker_install
when: not docker_install | bool
tags: docker

View File

@ -0,0 +1,53 @@
---
- block:
- debug:
msg: "Tasks that setup the Swarm Manager nodes"
when: docker_swarm_manager | bool
tags: [ 'docker', 'docker_swarm' ]
- block:
- name: Initialize the cluster
docker_swarm:
state: present
when:
- docker_swarm_manager | bool
- docker_swarm_manager_main_node | bool
tags: [ 'docker', 'docker_swarm', 'docker_swarm_init' ]
- block:
- name: Get swarm status information
docker_swarm:
state: inspect
register: swarm_info_data
- name: Get the swarm manager token
set_fact:
docker_swarm_mgr_token: '{{ swarm_info_data.swarm_facts.JoinTokens.Manager }}'
cacheable: True
when: docker_swarm_manager
delegate_to: '{{ docker_swarm_manager_main_node_hostname }}'
tags: [ 'docker', 'docker_swarm' ]
- block:
- name: Add manager nodes to the docker swarm cluster
docker_swarm:
state: join
advertise_addr: '{{ ansible_default_ipv4.address }}'
join_token: '{{ docker_swarm_mgr_token }}'
remote_addrs: [ '{{ docker_swarm_manager_main_node_hostname }}:2377' ]
when:
- docker_swarm_manager | bool
- not docker_swarm_manager_main_node | bool
tags: [ 'docker', 'docker_swarm', 'docker_swarm_mgr_nodes' ]
- block:
- name: Set the swarm manager nodes availability. Default 'drain'
docker_node:
hostname: '{{ ansible_hostname }}'
availability: '{{ docker_swarm_manager_availability }}'
when: docker_swarm_manager | bool
tags: [ 'docker', 'docker_swarm', 'docker_swarm_mgr_nodes', 'docker_swarm_mgr_availability' ]

View File

@ -0,0 +1,43 @@
---
- block:
- debug:
msg: "Tasks that setup the Swarm worker nodes"
when: docker_swarm_node | bool
tags: [ 'docker', 'docker_swarm' ]
- block:
- name: Get swarm status information
docker_swarm:
state: inspect
register: swarm_info_data
- name: Get the swarm worker node token
set_fact:
docker_swarm_worker_token: '{{ swarm_info_data.swarm_facts.JoinTokens.Worker }}'
cacheable: True
when: docker_swarm_node | bool
delegate_to: '{{ docker_swarm_manager_main_node_hostname }}'
tags: [ 'docker', 'docker_swarm' ]
- block:
- name: Add worker nodes to the docker swarm cluster
docker_swarm:
state: join
advertise_addr: '{{ ansible_default_ipv4.address }}'
join_token: '{{ docker_swarm_worker_token }}'
remote_addrs: [ '{{ docker_swarm_manager_main_node_hostname }}:2377' ]
when: docker_swarm_node | bool
tags: [ 'docker', 'docker_swarm', 'docker_swarm_worker_nodes' ]
- block:
- name: Set the swarm nodes availability. Default 'active'
docker_node:
hostname: '{{ ansible_hostname }}'
availability: '{{ docker_swarm_node_availability }}'
when: docker_swarm_node | bool
delegate_to: '{{ docker_swarm_manager_main_node_hostname }}'
tags: [ 'docker', 'docker_swarm', 'docker_swarm_worker_nodes', 'docker_swarm_worker_nodes_availability' ]

View File

@ -1,12 +1,20 @@
# Customize location of Docker binary (especially for development testing).
#DOCKERD="/usr/local/bin/dockerd"
{% if docker_swarm %}
# Docker swarm mode have precedence on docker_enable_tcp_socket
CUSTOM_DOCKER_SOCKET="-H tcp://0.0.0.0:{{ docker_tcp_socket_port }} -H unix:///var/run/docker.sock"
{% else %}
CUSTOM_DOCKER_SOCKET="-H tcp://{{ docker_tcp_socket_host }}:{{ docker_tcp_socket_port }} -H unix:///var/run/docker.sock"
{% endif %}
#CUSTOM_DOCKER_DNS_SERVERS="--dns 8.8.8.8 --dns 8.8.4.4"
# Use DOCKER_OPTS to modify the daemon startup options.
{% if docker_swarm %}
DOCKER_OPTS=" $CUSTOM_DOCKER_SOCKET "
{% else %}
DOCKER_OPTS="{% if docker_enable_tcp_socket %}$CUSTOM_DOCKER_SOCKET {% endif %}"
{% endif %}
# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"