Skip to content

Commit

Permalink
Review setup of swarm
Browse files Browse the repository at this point in the history
  • Loading branch information
drmalex07 committed Sep 12, 2017
1 parent eac1667 commit 69aac31
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/.vagrant

/secrets/

/hosts.conf
/hosts-*.conf
/hosts.yml
Expand All @@ -8,3 +10,6 @@
/pyenv

/play.retry

/scratch.yml
/scratch.retry
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

## 0. Prerequisites ##

### 0.1 Ansible Environment ###

You must install `Ansible` on the control machine, preferably in a virtual Python environment:

virtualenv pyenv
. pyenv/bin/activate
pip install ansible==2.2 netaddr

### 0.2 Keys ###

Place your PEM-formatted private key under `secrets/id_sa`. Ensure the key file has proper permissions (`0600`).

## 1. Prepare your inventory ##

An single inventory file should be created at `hosts.yml`. Both `vagrant` and `ansible` will use this. An example inventory file can be found [here](hosts.yml.example).
An single inventory file should be created at `hosts.yml`. Both `vagrant` and `ansible` will use this same inventory.
An example inventory file can be found [here](hosts.yml.example).

## 2.1 Setup with Vagrant and Ansible ##

If we want a full Vagrant environment (of course we will also need `vagrant` installed), then:

vagrant up

In this case, `vagrant` will provide the virtual machines (via virtualbox) and setup the private network,
In this case, `vagrant` will provide the virtual machines (via virtualbox), will setup the private network,
and then will delegate to an `ansible` playbook to actually setup the swarm nodes.

## 2.2 Setup with Ansible only ##
Expand Down
32 changes: 18 additions & 14 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,24 @@ inventory_groups = inventory['all']['children']
#

Vagrant.configure(2) do |config|

config.vm.box = "debian/jessie64"
config.vm.box_check_update = false

config.vm.synced_folder ".", "/vagrant", type: "rsync"

inventory_groups['workers']['hosts'].keys.each do |worker_name|
config.vm.define worker_name do |worker|
h = inventory_groups['workers']['hosts'][worker_name]

worker.vm.network "private_network", ip: h['ipv4_address']
worker.vm.provider "virtualbox" do |vb|
vb.name = h['hostname']
vb.memory = 512
end
end
end

config.vm.define "manager" do |manager|
h = inventory_groups['manager']['hosts']['manager']

Expand All @@ -43,25 +55,17 @@ Vagrant.configure(2) do |config|
end
end

inventory_groups['workers']['hosts'].keys.each do |worker_name|
config.vm.define worker_name do |worker|
h = inventory_groups['workers']['hosts'][worker_name]

worker.vm.network "private_network", ip: h['ipv4_address']
worker.vm.provider "virtualbox" do |vb|
vb.name = h['hostname']
vb.memory = 768
end
end
end

# Define common provisioning tasks

config.vm.provision "file", source: "secrets/id_rsa", destination: ".ssh/id_rsa"
config.vm.provision "shell", path: "copy-key.sh", privileged: false

config.vm.provision "file", source: "profile", destination: ".profile"
config.vm.provision "file", source: "bashrc", destination: ".bashrc"

config.vm.provision "file", source: "~/.vimrc", destination: ".vimrc"
config.vm.provision "file", source: "~/.vim/", destination: "."
#config.vm.provision "file", source: "~/.vimrc", destination: ".vimrc"
#config.vm.provision "file", source: "~/.vim/", destination: "."

#config.vm.provision "shell", path: "configure-apt-proxy.sh"

Expand Down
2 changes: 2 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[defaults]

private_key_file = secrets/id_rsa

inventory = hosts.yml
hostfile = hosts.yml

Expand Down
9 changes: 9 additions & 0 deletions copy-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

cd ~

test ! -f .ssh/id_rsa.pub && ssh-keygen -y -f .ssh/id_rsa > .ssh/id_rsa.pub

if [ -z "$(grep -Fx -f .ssh/id_rsa.pub .ssh/authorized_keys)" ]; then
cat .ssh/id_rsa.pub >> .ssh/authorized_keys
fi
34 changes: 32 additions & 2 deletions play.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
set_fact:
hostname: '{{ansible_host}}'
when: not (hostname is defined)

- set_fact:
hostname_s: '{{hostname.split(".") | first}}'

vars:

Expand All @@ -54,10 +57,37 @@
insertafter: '^127\.0\.1\.1 .*'
with_items: '{{play_hosts}}'
sudo: yes

#
# 2. Reboot all hosts
#

- hosts: all

tasks:

- name: Reboot for group membership to be re-evaluated
shell: reboot
sudo: yes
async: 30
poll: 0
ignore_errors: true

- name: Wait for play hosts to be up again
wait_for:
host: '{{hostvars[item].ansible_host}}'
port: 22
state: started
delay: 10
timeout: 40
with_items: '{{play_hosts}}'
connection: local

- name:
shell: uptime

#
# 2. Initialize manager as a swarm manager node
# 3. Initialize manager as a swarm manager node
#

- hosts: manager
Expand All @@ -78,7 +108,7 @@
msg: The join token is {{swarm_join_token}}

#
# 3. Join worker nodes into swarm
# 4. Join worker nodes into swarm
#

- hosts: workers
Expand Down

0 comments on commit 69aac31

Please sign in to comment.