The Docker is changing so rapidly that most of the tutorials get outdated shortly after publication. It’s about to change with the release of Docker-CE and Docker-EE with the second aiming at stabilization. Here I will document the creation of docker swarm using Ubuntu 14.04.05 or 16.04 (trusty or xenial) cloud images.

Firs we need a hosts file or DNS entries for our swarm nodes. I assume all nodes are spawned in single network and are named docker-NN. Once the swarm nodes are reachable over the network by their names we can configure ansible hosts file to access them:

cat /etc/ansible/hosts

[swarm]
docker-07
docker-08
docker-09
docker-10

[swarm-master]
docker-07

[swarm-nodes]
docker-08
docker-09
docker-10

Let’s make sure the nodes are available:

# ansible swarm -f5 -m ping

docker-07 | success >> {
    "changed": false,
    "ping": "pong"
}

docker-10 | success >> {
    "changed": false,
    "ping": "pong"
}

docker-08 | success >> {
    "changed": false,
    "ping": "pong"
}

docker-09 | success >> {
    "changed": false,
    "ping": "pong"
}

We can optionally copy our hosts file and make sure the communication works:

ansible swarm -f5 -m copy -a ‘src=/etc/hosts dest=/etc/hosts’

ansible swarm -f5 -m shell -a “ping docker-10 -c1”

 

Following step is required on 14.04 trusty to update/upgrade nodes and install required components:

# Update & upgrade
ansible swarm -m apt -a "upgrade=safe update_cache=yes" --sudo

# AUFS kernel for ubintu(?)
ansible swarm -f5 -m shell -a "sudo apt-get -y install \
 linux-image-extra-$(uname -r) \
 linux-image-extra-virtual"

# Use new kernel
ansible swarm -f5 -m shell -a "reboot"

Deployment of docker swarm is now easy as pie, just run the playbook:

ansible-playbook ./docker-install.yml

Just make sure we have following playbook :) Note that the line that starts with “repo: “deb [arch=amd64]” mentions trusty. Just replace it with xenial if you like.

cat docker-install.yml
# Upgrade
- name: Update & Upgrade
 hosts: swarm-master:swarm-nodes
 sudo: true
 tasks:
 - apt: upgrade=safe update_cache=yes

- name: Install Docker Prereqs
 hosts: swarm-master:swarm-nodes
 gather_facts: yes
 sudo: true
 tasks:
 - apt:
 name: "{{item}}"
 state: latest
 with_items:
 - apt-transport-https
 - ca-certificates
 - curl
 - software-properties-common
 - shell: curl -s https://download.docker.com/linux/ubuntu/gpg | apt-key add -
 - apt_repository:
 repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty stable"
 state: present

- name: Install Docker
 hosts: swarm-master:swarm-nodes
 gather_facts: yes
 sudo: true
 tasks:
 - apt:
 name: "docker-ce"
 state: latest
 - user:
 name: "{{ ansible_ssh_user }}"
 groups: docker
 append: yes

- name: Initialize Swarm Master
 hosts: swarm-master
 gather_facts: yes
 sudo: true
 tasks:
 - command: "docker swarm init --advertise-addr eth0"
 - command: "docker swarm join-token -q worker"
 register: swarm_token
 - set_fact: swarmtoken="{{swarm_token.stdout}}"

- name: Join Swarm Nodes
 hosts: swarm-nodes
 gather_facts: yes
 sudo: true
 tasks:
 - command: "docker swarm join --advertise-addr eth0 --token {{hostvars[groups['swarm-master'][0]].swarmtoken}} {{hostvars[groups['swarm-master'][0]].inventory_hostname}}:2377"

Accessing the swarm master to rule the swarm i now within a reach of a palm:

ssh docker-07

docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
3t79s1etvsg6g784gyky4mzw1 docker-10 Ready Active
dojsfrowv17tmcj6msfoeryyb docker-08 Ready Active
ioasnnina1c58q6groj53q5em docker-09 Ready Active
pzsnk96d6u3pt3q9vxf5r5wxx * docker-07 Ready Active Leader

p.s. Playbook credits go to https://rsmitty.github.io/Multi-Cloud-Swarm/

Advertisements