Swarm Consul

Installs clusters with Docker Swarm + Consul

Example to orchestrate three nodes with Docker Swarm:
mac: 0.7.1
description: Infrastructure demo
name: demo
version: '1.0'
roles:
swarm:
instance create:
configuration: swarm_consul
environment:
- MEMBERS_IP: swarm.PRIVATE_IP
infrastructures:
swarm-master:
name: 'master'
provider: gce
hardware: https://www.googleapis.com/compute/v1/projects/manageacloud-instances/zones/us-central1-a/machineTypes/g1-small
location: us-central1-c
role: swarm
environment:
- MANAGEMENT: 1
- PRIVATE_IP: swarm-master.PRIVATE_IP
- PUBLIC_IP: swarm-master.PUBLIC_IP
swarm01:
name: 'swarm01'
provider: gce
hardware: https://www.googleapis.com/compute/v1/projects/manageacloud-instances/zones/us-central1-a/machineTypes/g1-small
location: us-central1-c
role: swarm
environment:
- PRIVATE_IP: swarm01.PRIVATE_IP
- PUBLIC_IP: swarm01.PUBLIC_IP
swarm02:
name: 'swarm02'
provider: gce
hardware: https://www.googleapis.com/compute/v1/projects/manageacloud-instances/zones/us-central1-a/machineTypes/g1-small
location: us-central1-c
role: swarm
environment:
- PRIVATE_IP: swarm02.PRIVATE_IP
- PUBLIC_IP: swarm02.PUBLIC_IP

To test the configuration, save the file as swarm-consul.macfile and execute the following command:
mac infrastructure macfile swarm-consul.macfile

The previous configuration won't work with the cloud supplier smallest instances.

You can install mac cli with the following command:
curl -sSL https://manageacloud.com/mac | bash

Configuration summary

Ubuntu Trusty Tahr 14.04 Ubuntu Trusty Tahr 14.04
Bootstrap code
#!/bin/bash
#
# INPUT PARAMETERS:
# - MEMBERS_IP: String with the private IP of all members
# - PRIVATE_IP: Private IP
# - PUBLIC_IP: Public IP
# - MANAGEMENT: If this server in the cluster is the master or not

set -x

PRIVATE_IP=${PRIVATE_IP:-127.0.0.1}
PUBLIC_IP=${PUBLIC_IP:-127.0.0.1}
MEMBERS_IP=${MEMBERS_IP:-127.0.0.1}
MANAGEMENT=${MANAGEMENT:-0}
MEMBERS_IPS=($MEMBERS_IP)

# TRIM
PRIVATE_IP=$(echo $PRIVATE_IP | xargs)
PUBLIC_IP=$(echo $PUBLIC_IP | xargs)

apt-get update -qq

#
# install and configure consul
#
mkdir -p /etc/consul.d/server
mkdir -p /var/consul
mkdir -p /var/www/html

curl -O -J -L https://dl.bintray.com/mitchellh/consul/0.5.2_linux_amd64.zip
apt-get install unzip && unzip 0.5.2_linux_amd64.zip && mv consul /usr/local/bin/

STR_MEMBERS='['
COMMA=0
for IP in "${MEMBERS_IPS[@]}"
do
        if [ "$COMMA" == "1" ]; then
          STR_MEMBERS="$STR_MEMBERS,\"$IP\""
        else
          STR_MEMBERS="$STR_MEMBERS\"$IP\""
        fi
        COMMA=1
done
STR_MEMBERS="$STR_MEMBERS]"

cat >/etc/consul.d/server/config.json <<EOL
{
	"bootstrap_expect": 2,
	"server": true,
	"data_dir": "/var/consul",
	"log_level": "INFO",
	"enable_syslog": false,
	"retry_join": $STR_MEMBERS,
	"client_addr": "0.0.0.0"
}
EOL

# TODO create init script
# TODO consul service to run as a not priviledge user
nohup /usr/local/bin/consul agent -config-dir="/etc/consul.d/server/config.json" -ui-dir="/var/www/html" -advertise=$PUBLIC_IP -bind=$PRIVATE_IP >>/var/log/consul.log 2>&1 &

sleep 2

#
# add 512Mb extra swap
#
dd if=/dev/zero of=/swapfile bs=1024 count=512k
mkswap /swapfile

echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
echo 10 | sudo tee /proc/sys/vm/swappiness

chown root:root /swapfile
chmod 0600 /swapfile

echo " /swapfile       none    swap    sw      0       0 " >> /etc/fstab

swapon /swapfile


#
# install docker 
#
curl -sSL https://get.docker.io/ubuntu/ | sh

# configure docker
cat >/etc/default/docker <<EOL
DOCKER_OPTS="-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375"
EOL

service docker restart

sleep 20

#
# Join to Swarm cluster
#
docker run -d swarm join --addr=$PRIVATE_IP:2375 consul://$PRIVATE_IP:8500/swarm

if [ "$MANAGEMENT" == "1" ]; then
    echo "Starting Swarm Manager"
    docker run -d -p 3333:2375 swarm manage consul://$PRIVATE_IP:8500/swarm
fi;

# Add the registration service
docker run -d --name reg`hostname` -h `hostname` -v /var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator consul://$PRIVATE_IP:8500


















Quick Deployment
Advanced Deployment