Update Docker Swarm+Consul environment

Update learning environment for running a Consul-backed Docker Swarm cluster
This commit is contained in:
Scott S. Lowe 2016-09-25 14:02:14 -06:00
parent 64cac4ee8c
commit a4178a889b
6 changed files with 112 additions and 80 deletions

View file

@ -1,12 +1,9 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify Vagrant version, Vagrant API version, and Vagrant clone location
# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
ENV['VAGRANT_VMWARE_CLONE_DIRECTORY'] = '~/.vagrant'
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion'
#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
# Require 'yaml', 'fileutils', and 'erb' modules
require 'yaml'
@ -18,26 +15,26 @@ require 'erb'
USER_DATA = File.join(File.dirname(__FILE__), 'user-data')
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Be sure to edit servers.yml to provide correct IP addresses
servers = YAML.load_file(File.join(File.dirname(__FILE__), 'servers.yml'))
# Edit machines.yml to change VM configuration details
machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml'))
# Build array of IP addresses for Consul cluster
consul_cluster_list = []
servers.each do |server|
if server['box'] == 'slowe/ubuntu-trusty-x64'
consul_cluster_list << "\"#{server['priv_ip']}\""
end # if server['box']
end # servers.each
machines.each do |machine|
if machine['role'] == 'consul'
consul_cluster_list << "\"#{machine['ip_addr']}\""
end # if machine['role']
end # machines.each
# Build a Consul configuration file from ERB template
template = File.join(File.dirname(__FILE__), 'server.json.erb')
template = File.join(File.dirname(__FILE__), 'config.json.erb')
content = ERB.new File.new(template).read
servers.each do |server|
if server['box'] == 'slowe/ubuntu-trusty-x64'
target = File.join(File.dirname(__FILE__), "#{server['name']}.json")
machines.each do |machine|
if machine['role'] == 'consul'
target = File.join(File.dirname(__FILE__), "#{machine['name']}.json")
File.open(target, 'w') { |f| f.write(content.result(binding)) }
end # if server['box']
end # servers.each
end # if machine['role']
end # machines.each
# Create and configure the VMs
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
@ -46,47 +43,68 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.ssh.insert_key = false
# Iterate through entries in YAML file to create VMs
servers.each do |server|
config.vm.define server['name'] do |srv|
machines.each do |machine|
# Configure the VMs per details in machines.yml
config.vm.define machine['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
srv.vm.hostname = server['name']
srv.vm.box = server['box']
# Assign an additional static private network
srv.vm.network 'private_network', ip: server['priv_ip']
# Specify the hostname of the VM
srv.vm.hostname = machine['name']
# Specify the Vagrant box to use (use VMware box by default)
srv.vm.box = machine['vmw_box']
# Configure default synced folder (disable by default)
if machine['sync_disabled'] != nil
srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled']
else
srv.vm.synced_folder '.', '/vagrant', disabled: true
end #if machine['sync_disabled']
# Assign additional private network
if machine['ip_addr'] != nil
srv.vm.network 'private_network', ip: machine['ip_addr']
end # if machine['ip_addr']
# Configure CPU & RAM per settings in machines.yml (Fusion)
srv.vm.provider 'vmware_fusion' do |vmw|
vmw.vmx['memsize'] = machine['ram']
vmw.vmx['numvcpus'] = machine['vcpu']
if machine['nested'] == true
vmw.vmx['vhv.enable'] = 'TRUE'
end #if machine['nested']
end # srv.vm.provider 'vmware_fusion'
# Configure CPU & RAM per settings in machines.yml (VirtualBox)
srv.vm.provider 'virtualbox' do |vb, override|
vb.memory = machine['ram']
vb.cpus = machine['vcpu']
override.vm.box = machine['vb_box']
end # srv.vm.provider 'virtualbox'
# Configure VMs based on CoreOS box
if srv.vm.box == 'coreos-stable'
# Disable default synced folder for CoreOS VMs
srv.vm.synced_folder '.', '/vagrant', disabled: true
# Copy user_data file into CoreOS VM
srv.vm.provision 'file', source: "#{USER_DATA}", destination: '/tmp/vagrantfile-user-data'
# Move user_data to correct location to be processed by cloud-init
srv.vm.provision 'shell', inline: 'mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/', privileged: true
end # srv.vm.box == 'coreos-stable'
# Configure VMs based on Ubuntu box
if srv.vm.box == 'slowe/ubuntu-trusty-x64'
# Enable default synced folder
srv.vm.synced_folder '.', '/vagrant'
# Configure VMs with the 'consul' role assigned in machines.yml
if machine['role'] == 'consul'
# Copy files into the VM
srv.vm.provision 'file', source: "#{server['name']}.json", destination: '/home/vagrant/config.json'
srv.vm.provision 'file', source: "#{machine['name']}.json", destination: '/home/vagrant/config.json'
srv.vm.provision 'file', source: 'consul.conf', destination: '/home/vagrant/consul.conf'
# Run final provisioning script
srv.vm.provision 'shell', path: 'consul.sh'
end # srv.vm.box == 'ubuntu-trusty-x64'
# Configure VMs with RAM and CPUs per settings in servers.yml (Fusion)
srv.vm.provider :vmware_fusion do |vmw|
vmw.vmx['memsize'] = server['ram']
vmw.vmx['numvcpus'] = server['vcpu']
end # srv.vm.provider vmware_fusion
# Configure VMs with RAM and CPUs per settings in servers.yml (VirtualBox)
srv.vm.provider :virtualbox do |vb|
vb.memory = server['ram']
vb.cpus = server['vcpu']
end # srv.vm.provider virtualbox
end # machine['role'] == 'consul'
end # config.vm.define
end # servers.each
end # machines.each
end # Vagrant.configure

View file

@ -7,5 +7,5 @@
"enable_syslog": false,
"retry_join": [<%= consul_cluster_list.join(',') %>],
"client_addr": "0.0.0.0",
"advertise_addr": "<%= server['priv_ip'] %>"
"advertise_addr": "<%= machine['ip_addr'] %>"
}

View file

@ -11,9 +11,11 @@ script
fi
export GOMAXPROCS=`nproc`
BIND_ADDR=`hostname -I | cut -f2 -d' '`
exec /usr/local/bin/consul agent \
-config-dir="/etc/consul.d/server" \
-bind $BIND_ADDR \
${CONSUL_FLAGS} \
>>/var/log/consul.log 2>&1
end script
end script

View file

@ -17,11 +17,11 @@ fi
if [[ ! -e /usr/local/bin/consul ]];then
# Download Consul
curl -kLO https://releases.hashicorp.com/consul/0.6.3/consul_0.6.3_linux_amd64.zip
curl -kLO https://releases.hashicorp.com/consul/0.7.0/consul_0.7.0_linux_amd64.zip
# Decompress and remove Consul download
unzip consul_0.6.3_linux_amd64.zip
rm consul_0.6.3_linux_amd64.zip
unzip consul_0.7.0_linux_amd64.zip
rm consul_0.7.0_linux_amd64.zip
# Move Consul binary to location in path
sudo mv consul /usr/local/bin/

43
docker-swarm/machines.yml Normal file
View file

@ -0,0 +1,43 @@
---
- name: "consul-01"
vmw_box: "slowe/ubuntu-trusty-x64"
vb_box: "ubuntu/trusty64"
ram: "512"
vcpu: "1"
ip_addr: "192.168.1.101"
role: "consul"
- name: "consul-02"
vmw_box: "slowe/ubuntu-trusty-x64"
vb_box: "ubuntu/trusty64"
ram: "512"
vcpu: "1"
ip_addr: "192.168.1.102"
role: "consul"
- name: "consul-03"
vmw_box: "slowe/ubuntu-trusty-x64"
vb_box: "ubuntu/trusty64"
ram: "512"
vcpu: "1"
ip_addr: "192.168.1.103"
role: "consul"
- name: "coreos-01"
vmw_box: "coreos-stable"
vb_box: "coreos-stable"
ram: "512"
vcpu: "1"
ip_addr: "192.168.1.104"
role: "docker"
- name: "coreos-02"
vmw_box: "coreos-stable"
vb_box: "coreos-stable"
ram: "512"
vcpu: "1"
ip_addr: "192.168.1.105"
role: "docker"
- name: "coreos-03"
vmw_box: "coreos-stable"
vb_box: "coreos-stable"
ram: "512"
vcpu: "1"
ip_addr: "192.168.1.106"
role: "docker"

View file

@ -1,31 +0,0 @@
---
- name: consul-01
box: slowe/ubuntu-trusty-x64
ram: 512
vcpu: 1
priv_ip: 192.168.1.101
- name: consul-02
box: slowe/ubuntu-trusty-x64
ram: 512
vcpu: 1
priv_ip: 192.168.1.102
- name: consul-03
box: slowe/ubuntu-trusty-x64
ram: 512
vcpu: 1
priv_ip: 192.168.1.103
- name: coreos-01
box: coreos-stable
ram: 512
vcpu: 1
priv_ip: 192.168.1.104
- name: coreos-02
box: coreos-stable
ram: 512
vcpu: 1
priv_ip: 192.168.1.105
- name: coreos-03
box: coreos-stable
ram: 512
vcpu: 1
priv_ip: 192.168.1.106