Add Consul support

Add files to build Consul cluster as discovery backend for Docker Swarm cluster
This commit is contained in:
Scott Lowe 2015-10-12 15:27:26 -06:00
parent e2aa3e2ba9
commit 22cb3119a9
5 changed files with 158 additions and 0 deletions

61
docker-swarm-ha/Vagrantfile vendored Normal file
View file

@ -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

View file

@ -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

53
docker-swarm-ha/consul.sh Normal file
View file

@ -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

View file

@ -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"]
}

View file

@ -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