From daafe0b70421534eb66141b138f7ad9c8e0e8af3 Mon Sep 17 00:00:00 2001 From: "Scott S. Lowe" Date: Mon, 26 Sep 2016 21:12:07 -0600 Subject: [PATCH] Update Docker Swarm HA environment Update Docker Swarm HA environment with changes from Docker Swarm environment --- docker-swarm-ha/Vagrantfile | 106 ++++++++++-------- docker-swarm-ha/config.json.erb | 20 ++-- docker-swarm-ha/consul.conf | 4 +- docker-swarm-ha/consul.sh | 6 +- docker-swarm-ha/{servers.yml => machines.yml} | 12 +- docker-swarm/README.md | 6 +- docker-swarm/config.json.erb | 18 +-- docker-swarm/consul.sh | 3 + 8 files changed, 95 insertions(+), 80 deletions(-) rename docker-swarm-ha/{servers.yml => machines.yml} (81%) diff --git a/docker-swarm-ha/Vagrantfile b/docker-swarm-ha/Vagrantfile index 85b381c..bbb14ab 100644 --- a/docker-swarm-ha/Vagrantfile +++ b/docker-swarm-ha/Vagrantfile @@ -1,7 +1,7 @@ # -*- 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' @@ -11,29 +11,29 @@ require 'fileutils' require 'erb' # 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')) # Use template to create server-specific configuration files template = File.join(File.dirname(__FILE__), 'config.json.erb') content = ERB.new File.new(template).read -# Populate cluster list -cluster_list = [] -servers.each do |server| - if server['role'] == 'consul' - cluster_list << "\"#{server['priv_ip']}\"" - end # if server['role'] -end # servers.each +# Build array of IP addresses for Consul cluster +consul_cluster_list = [] +machines.each do |machine| + if machine['role'] == 'consul' + consul_cluster_list << "\"#{machine['ip_addr']}\"" + end # if machine['role'] +end # machines.each -# Write out server-specific configuration files -servers.each do |server| - if server['role'] == 'consul' - ip = server['priv_ip'] - target = File.join(File.dirname(__FILE__), "#{server['name']}.config.json") +# Write out VM-specific configuration files +machines.each do |machine| + if machine['role'] == 'consul' + ip = machine['ip_addr'] + target = File.join(File.dirname(__FILE__), "#{machine['name']}.config.json") File.open(target, 'w') { |f| f.write(content.result(binding)) } - end # if server['role'] -end # servers.each + end # if machine['role'] +end # machines.each # Create and configure the VMs Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| @@ -42,50 +42,64 @@ 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 - # Specify hostname and Vagrant box - srv.vm.hostname = server['name'] - srv.vm.box = server['vmw_box'] + # Specify the hostname of the VM + srv.vm.hostname = machine['name'] - # Assign an additional static private network - srv.vm.network 'private_network', ip: server['priv_ip'] + # Specify the Vagrant box to use (use VMware box by default) + srv.vm.box = machine['vmw_box'] - # Specify default synced folder; requires VMware Tools - srv.vm.synced_folder '.', '/vagrant', disabled: true + # 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 with the 'consul' role assigned in machines.yml + if machine['role'] == 'consul' - # Configure VM based on "role" value from servers.yml - if server['role'] == 'consul' # Copy Consul configuration files - srv.vm.provision 'file', source: "#{server['name']}.config.json", destination: '/home/vagrant/config.json' + srv.vm.provision 'file', source: "#{machine['name']}.config.json", destination: '/home/vagrant/config.json' srv.vm.provision 'file', source: 'consul.conf', destination: '/home/vagrant/consul.conf' # Provision consul to the VMs srv.vm.provision 'shell', path: 'consul.sh' - end # if server['role'] + end # machine['role'] == 'consul' - if server['role'] == 'docker' + if machine['role'] == 'docker' srv.vm.provision 'docker', images: ['swarm'] srv.vm.provision 'shell', inline: 'echo DOCKER_OPTS=\"-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375\" >> /etc/default/docker', privileged: true srv.vm.provision 'shell', inline: 'service docker restart', privileged: true - end - - # 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, override| - vb.memory = server['ram'] - vb.cpus = server['vcpu'] - override.vm.box = server['vb_box'] - end # srv.vm.provider virtualbox + end # if machine['role'] == 'docker' end # config.vm.define - end # servers.each + end # machines.each end # Vagrant.configure diff --git a/docker-swarm-ha/config.json.erb b/docker-swarm-ha/config.json.erb index 0265a66..b52bc72 100644 --- a/docker-swarm-ha/config.json.erb +++ b/docker-swarm-ha/config.json.erb @@ -1,11 +1,11 @@ { - "bootstrap_expect": 3, - "client_addr": "0.0.0.0", - "advertise_addr": "<%= server['priv_ip'] %>", - "server": true, - "datacenter": "dc1", - "data_dir": "/var/consul", - "log_level": "INFO", - "enable_syslog": true, - "start_join": [<%= cluster_list.join(',') %>] -} \ No newline at end of file + "advertise_addr": "<%= machine['ip_addr'] %>", + "bootstrap_expect": 3, + "client_addr": "0.0.0.0", + "datacenter": "dc1", + "data_dir": "/var/consul", + "enable_syslog": true, + "log_level": "INFO", + "server": true, + "retry_join": [<%= consul_cluster_list.join(',') %>] +} diff --git a/docker-swarm-ha/consul.conf b/docker-swarm-ha/consul.conf index d045021..60fa820 100644 --- a/docker-swarm-ha/consul.conf +++ b/docker-swarm-ha/consul.conf @@ -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 \ No newline at end of file +end script diff --git a/docker-swarm-ha/consul.sh b/docker-swarm-ha/consul.sh index 02dd35d..132e9da 100644 --- a/docker-swarm-ha/consul.sh +++ b/docker-swarm-ha/consul.sh @@ -17,11 +17,11 @@ fi if [[ ! -e /usr/local/bin/consul ]];then # Download Consul - curl -kLO https://dl.bintray.com/mitchellh/consul/0.5.2_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 0.5.2_linux_amd64.zip - rm 0.5.2_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/ diff --git a/docker-swarm-ha/servers.yml b/docker-swarm-ha/machines.yml similarity index 81% rename from docker-swarm-ha/servers.yml rename to docker-swarm-ha/machines.yml index c501dd3..eff6b78 100644 --- a/docker-swarm-ha/servers.yml +++ b/docker-swarm-ha/machines.yml @@ -4,40 +4,40 @@ vb_box: "ubuntu/trusty64" ram: "512" vcpu: "1" - priv_ip: 192.168.100.101 + ip_addr: "192.168.100.101" role: "consul" - name: "consul-02" vmw_box: "slowe/ubuntu-trusty-x64" vb_box: "ubuntu/trusty64" ram: "512" vcpu: "1" - priv_ip: 192.168.100.102 + ip_addr: "192.168.100.102" role: "consul" - name: "consul-03" vmw_box: "slowe/ubuntu-trusty-x64" vb_box: "ubuntu/trusty64" ram: "512" vcpu: "1" - priv_ip: 192.168.100.103 + ip_addr: "192.168.100.103" role: "consul" - name: "docker-01" vmw_box: "slowe/ubuntu-trusty-x64" vb_box: "ubuntu/trusty64" ram: "512" vcpu: "1" - priv_ip: 192.168.100.104 + ip_addr: "192.168.100.104" role: "docker" - name: "docker-02" vmw_box: "slowe/ubuntu-trusty-x64" vb_box: "ubuntu/trusty64" ram: "512" vcpu: "1" - priv_ip: 192.168.100.105 + ip_addr: "192.168.100.105" role: "docker" - name: "docker-03" vmw_box: "slowe/ubuntu-trusty-x64" vb_box: "ubuntu/trusty64" ram: "512" vcpu: "1" - priv_ip: 192.168.100.106 + ip_addr: "192.168.100.106" role: "docker" diff --git a/docker-swarm/README.md b/docker-swarm/README.md index 3bccc54..cade286 100644 --- a/docker-swarm/README.md +++ b/docker-swarm/README.md @@ -32,11 +32,7 @@ These instructions assume you've already installed your virtualization provider 5. Once you have edited `machines.yml`, use `vagrant up` to bring up the 6 systems. Three VMs will run the Consul cluster; the other 3 VMs will be running CoreOS and will make up the Docker Swarm cluster. -6. Once Vagrant has finished bringing up the VMs, simply use `vagrant ssh consul-01` (where `consul-01` is the value assigned to the first VM from `machines.yml`) to connect to the VM. No password should be required; it should use the default (insecure) SSH key. Once you are logged into the first VM, start the Consul agent with this command: - - sudo service consul start - - Repeat this process with the other two Consul nodes (`consul-02` and `consul-03` if you are using the default values in `machines.yml`). Once Consul has been started on all three nodes, verify Consul is running correctly by running this command: +6. Once Vagrant has finished bringing up the VMs, simply use `vagrant ssh consul-01` (where `consul-01` is the value assigned to the first VM from `machines.yml`) to connect to the VM. No password should be required; it should use the default (insecure) SSH key. Once you are logged into the first VM, verify Consul is running correctly by running this command: consul members list diff --git a/docker-swarm/config.json.erb b/docker-swarm/config.json.erb index e149a68..b52bc72 100644 --- a/docker-swarm/config.json.erb +++ b/docker-swarm/config.json.erb @@ -1,11 +1,11 @@ { - "bootstrap_expect": 3, - "server": true, - "datacenter": "dc1", - "data_dir": "/var/consul", - "log_level": "INFO", - "enable_syslog": false, - "retry_join": [<%= consul_cluster_list.join(',') %>], - "client_addr": "0.0.0.0", - "advertise_addr": "<%= machine['ip_addr'] %>" + "advertise_addr": "<%= machine['ip_addr'] %>", + "bootstrap_expect": 3, + "client_addr": "0.0.0.0", + "datacenter": "dc1", + "data_dir": "/var/consul", + "enable_syslog": true, + "log_level": "INFO", + "server": true, + "retry_join": [<%= consul_cluster_list.join(',') %>] } diff --git a/docker-swarm/consul.sh b/docker-swarm/consul.sh index 8bdb82d..132e9da 100644 --- a/docker-swarm/consul.sh +++ b/docker-swarm/consul.sh @@ -51,3 +51,6 @@ sudo mv /home/vagrant/consul.conf /etc/init/consul.conf sudo chown root:root /etc/init/consul.conf sudo mv /home/vagrant/config.json /etc/consul.d/server/config.json sudo chown root:consul /etc/consul.d/server/config.json + +# Start Consul +sudo service consul start