Update the etcd-backed Docker Swarm environment with new methods/techniques learned in building the Vagrant environment. This involves changes to the external YAML file (renamed to "machines.yml") and the Vagrantfile. Only minor changes were made to the ERB template and the provisioning script (to eliminate a dependency on shared folders). Signed-off-by: Scott S. Lowe <scott.lowe@scottlowe.org> |
||
|---|---|---|
| .. | ||
| .gitignore | ||
| etcd.conf | ||
| etcd.defaults.erb | ||
| machines.yml | ||
| provision.sh | ||
| README.md | ||
| user-data | ||
| Vagrantfile | ||
Running an etcd-Backed Docker Swarm Cluster in Vagrant
These files were created to allow users to use Vagrant (http://www.vagrantup.com) quickly and relatively easily spin up a Docker Swarm (http://docs.docker.com/swarm/) cluster backed by etcd (https://github.com/coreos/etcd/). The configuration was tested using Vagrant 1.8.6, VMware Fusion 8.1.0 with the Vagrant VMware plugin, and VirtualBox 5.1.
Contents
-
etcd.conf: This is an Upstart script (written for Ubuntu) to start etcd. No modifications to this file should be necessary. This file is installed by the provisioning script called in
Vagrantfile. -
etcd.defaults.erb: A template used by Vagrant to create machine-specific service defaults files for each node in the etcd cluster. The appropriate node-specific file is installed as
etcdin the/etc/defaultdirectory on each node by the provisioning script called inVagrantfile. These files are generated for each VM whose "etcd" value inmachines.ymlis set to "true". -
provision.sh: This provisioning script is called by the shell provisioner in
Vagrantfile. It downloads the etcd 2.3.7 release from GitHub, expands it, creates necessary directories, and places files in the appropriate locations. Finally it starts etcd (or restarts it if already running). -
README.md: This file you're currently reading.
-
machines.yml: This YAML file contains a list of VM definitions and associated configuration data. It is referenced by
Vagrantfilewhen Vagrant instantiates the VMs. You might need to edit this file to change the IP addresses (the default IP addresses may not work in all environments). -
user_data: This file is used by the CoreOS cloud-init process to customize the CoreOS VMs upon instantiation. This file disables etcd and fleet, and configures Docker to listen on TCP port 2375.
-
Vagrantfile: This file is used by Vagrant to spin up the virtual machines. This file is fairly extensively commented to help explain what's happening. You should be able to use this file unchanged; all the VM configuration options are stored outside this file.
Instructions
These instructions assume you've already installed your virtualization provider (VMware Fusion/Workstation or VirtualBox), Vagrant, and any applicable Vagrant plugins (like the Vagrant VMware plugin). Please refer to the documentation for those products for more information on installation or configuration.
-
Use
vagrant box addto install an Ubuntu 14.04 x64 box for your virtualization provider. If you are using VMware Fusion/Workstation, I have a base box you can use for this purpose; to use my Ubuntu 14.04 x64 base box, add the box withvagrant box add slowe/ubuntu-trusty-x64. -
Use
vagrant box addto install a CoreOS base box. TheVagrantfileassumes you are using the Stable release of CoreOS (the box namedcoreos-stable). If the box name is different, you'll need to edit bothmachines.ymland theVagrantfileto specify the correct box name. You'll need to be sure to use a version of CoreOS that comes with Docker 1.4.0 or later, as Swarm requires Docker => 1.4.0. Note that CoreOS Stable 557.2.0 comes with Docker 1.4.1. -
Place the files from the
docker-swarm-etcddirectory of this GitHub repository into a directory on your local system. You can clone the entire "learning-tools" repository (usinggit clone) or just download the specific files from the thedocker-swarm-etcdfolder. -
Edit the
machines.ymlfile to make sure the box names specified in this file match the names of the boxes you added to your system in steps 1 and 2. Generally speaking, no other changes to this file should be necessary. TheVagrantfileexpects six values for each VM:name(the user-friendly name of the VM, which will also be used as the hostname for the guest OS inside the VM);box(two subvalues which provide the name of the Vagrant box to use for the VMware and VirtualBox providers);ram(the amount of memory to be assigned to the VM);vcpu(the number of vCPUs that should be assigned to the VM);ip_addr(an IP address to be statically assigned to the VM and is used for Consul cluster communications); andetcd(set to "true" for a node to be included in the etcd cluster configuration; any other value will exclude it from the etcd cluster configuration). -
Edit
Vagrantfileto make sure the box names referenced in this file (on line 90) match the names of the boxes you added to your system in steps 1 and 2. -
Once you have edited
machines.yml(andVagrantfile, if necessary), usevagrant upto bring up the 6 systems. Three VMs will run the etcd cluster; the other 3 VMs will be running CoreOS and will make up the Docker Swarm cluster. -
Once Vagrant has finished bringing up the VMs, simply use
vagrant ssh etcd-01(whereetcd-01is the value assigned to the first VM frommachines.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 the operation of etcd with this command:etcdctl member listThis command should return a list of three members, which are the first three VMs (based on an Ubuntu 14.04 box) specified in
machines.yml. If the command does not return a list of three members, then you'll need to troubleshoot etcd. -
Use
vagrant ssh coreos-01to log into the first CoreOS system (coreos-01is the default name supplied inmachines.yml; if you've changed the default name, modify your command appropriately). Add it as the first node to a new Docker Swarm cluster using this command:docker run -d swarm join --addr=192.168.100.111:2375 etcd://192.168.100.101:2379/swarmMake sure the IP address referenced by the
--addr=parameter matches the IP address assigned to the CoreOS VM (which is pulled from the "ip_addr" key inmachines.yml). Further, make sure the IP address in the "etcd://" URL matches the IP address assigned to one of the Ubuntu-based etcd cluster nodes. -
Log out of the first CoreOS system and use
vagrant sshto log into the second and third CoreOS systems, repeating step 8 on each system. Be sure to change values for the--addr=parameter on each system. The default values frommachines.ymluse the addresses 192.168.100.111 (coreos-01), 192.168.100.112 (coreos-02), and 192.168.100.113 (coreos-03). -
On the first CoreOS system (the one named
coreos-01by default), run a Docker container that will serve as the manager of the Swarm cluster. Launch the Swarm manager with this command:docker run -d -p 8333:2375 swarm manage etcd://192.168.100.101:2379/swarmThe IP address specified in the "etcd://" URL should correspond to the IP address assigned to one of the Ubuntu-based etcd cluster nodes. Make note of the port exposed by the
-pcommand and the IP address of the node on which you're running the Swarm manager. This will be the endpoint against which you will run Docker commands against the cluster. -
Verify the operation of the Swarm cluster by running this command (from any system that has connectivity to the CoreOS system running the Swarm manager container launched in the previous step):
docker -H tcp://192.168.100.111:8333 infoDocker should return information indicating that there are 4 containers running across 3 nodes, and then provide more information about each node and the containers running on that node.
-
Launch an Nginx container somewhere on the cluster with this command:
docker -H tcp://192.168.100.111:8333 run -d --name www -p 80:80 nginxIf everything is working as expected, Docker will launch an Nginx container on one of the CoreOS nodes in the Swarm cluster. Re-running the command from step 10 can help track it down, as well as using this command:
docker -H tcp://192.168.100.111:8333 ps
Enjoy!