From 22cb3119a9dc5052b9a58709a9efa347b10bbcdc Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Mon, 12 Oct 2015 15:27:26 -0600 Subject: [PATCH] Add Consul support Add files to build Consul cluster as discovery backend for Docker Swarm cluster --- docker-swarm-ha/Vagrantfile | 61 +++++++++++++++++++++++++++++++++++++ docker-swarm-ha/consul.conf | 19 ++++++++++++ docker-swarm-ha/consul.sh | 53 ++++++++++++++++++++++++++++++++ docker-swarm-ha/server.json | 9 ++++++ docker-swarm-ha/servers.yml | 16 ++++++++++ 5 files changed, 158 insertions(+) create mode 100644 docker-swarm-ha/Vagrantfile create mode 100644 docker-swarm-ha/consul.conf create mode 100644 docker-swarm-ha/consul.sh create mode 100644 docker-swarm-ha/server.json create mode 100644 docker-swarm-ha/servers.yml diff --git a/docker-swarm-ha/Vagrantfile b/docker-swarm-ha/Vagrantfile new file mode 100644 index 0000000..e9fe790 --- /dev/null +++ b/docker-swarm-ha/Vagrantfile @@ -0,0 +1,61 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Specify Vagrant version, Vagrant API version, and Vagrant clone location +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'] = 'vmware_appcatalyst' + +# Require 'yaml' module +require 'yaml' + +# 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('servers.yml') + +# Create and configure the VMs +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + + # Always use Vagrant's default insecure key + 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| + + # 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['box'] + + # Assign an additional static private network + srv.vm.network 'private_network', ip: server['priv_ip'] + + # Specify default synced folder; requires VMware Tools + srv.vm.synced_folder '.', '/vagrant', disabled: true + + # Copy in the Consul configuration files + srv.vm.provision 'file', source: 'server.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' + + # Configure VMs with RAM and CPUs per settings in servers.yml + 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 + srv.vm.provider :vmware_appcatalyst do |vmw| + vmw.vmx['memsize'] = server['ram'] + vmw.vmx['numvcpus'] = server['vcpu'] + end # srv.vm.provider vmware_appcatalyst + end # config.vm.define + end # servers.each +end # Vagrant.configure diff --git a/docker-swarm-ha/consul.conf b/docker-swarm-ha/consul.conf new file mode 100644 index 0000000..d045021 --- /dev/null +++ b/docker-swarm-ha/consul.conf @@ -0,0 +1,19 @@ +description "Consul server process" + +start on runlevel [2345] +stop on runlevel [!2345] + +respawn + +script + if [ -f "/etc/service/consul" ]; then + . /etc/service/consul + fi + +export GOMAXPROCS=`nproc` + +exec /usr/local/bin/consul agent \ + -config-dir="/etc/consul.d/server" \ + ${CONSUL_FLAGS} \ + >>/var/log/consul.log 2>&1 +end script \ No newline at end of file diff --git a/docker-swarm-ha/consul.sh b/docker-swarm-ha/consul.sh new file mode 100644 index 0000000..7df0cad --- /dev/null +++ b/docker-swarm-ha/consul.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# Update list of packages +export DEBIAN_FRONTEND=noninteractive +sudo apt-get update + +# Install packages as needed +if [[ ! -e /usr/bin/curl ]]; then + apt-get -yqq install curl +fi + +if [[ ! -e /usr/bin/unzip ]]; then + apt-get -yqq install unzip +fi + +# Download and install Consul binary, if needed +if [[ ! -e /usr/local/bin/consul ]];then + + # Download Consul + curl -kLO https://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip + + # Decompress and remove Consul download + unzip 0.5.2_linux_amd64.zip + rm 0.5.2_linux_amd64.zip + + # Move Consul binary to location in path + sudo mv consul /usr/local/bin/ +fi + +# Create consul user, if it doesn't already exist +if [ -z "$(getent passwd consul)" ]; then + useradd -M -d /var/consul -r -s /usr/bin/nologin consul + else + echo "Consul user already created." + fi + +# Create and configure Consul working directory +sudo mkdir -p /var/consul +if [ -d /var/consul ]; then + sudo chown -R consul:consul /var/consul +fi + +# Create and configure Consul configuration directories +sudo mkdir -p /etc/consul.d/server +if [ -d /etc/consul.d ]; then + sudo chown -R root:consul /etc/consul.d +fi + +# Move files into the correct locations +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 diff --git a/docker-swarm-ha/server.json b/docker-swarm-ha/server.json new file mode 100644 index 0000000..2417bec --- /dev/null +++ b/docker-swarm-ha/server.json @@ -0,0 +1,9 @@ +{ + "bootstrap": false, + "server": true, + "datacenter": "dc1", + "data_dir": "/tmp", + "log_level": "INFO", + "enable_syslog": true, + "start_join": ["192.168.100.101", "192.168.100.102", "192.168.100.103"] +} \ No newline at end of file diff --git a/docker-swarm-ha/servers.yml b/docker-swarm-ha/servers.yml new file mode 100644 index 0000000..d15854f --- /dev/null +++ b/docker-swarm-ha/servers.yml @@ -0,0 +1,16 @@ +--- +- name: "consul-01" + box: "slowe/ubuntu-trusty-x64" + ram: "512" + vcpu: "1" + priv_ip: 192.168.100.101 +- name: "consul-02" + box: "slowe/ubuntu-trusty-x64" + ram: "512" + vcpu: "1" + priv_ip: 192.168.100.102 +- name: "consul-03" + box: "slowe/ubuntu-trusty-x64" + ram: "512" + vcpu: "1" + priv_ip: 192.168.100.103