diff --git a/consul/consul/README.md b/consul/consul/README.md index 1ba6104..67721d6 100644 --- a/consul/consul/README.md +++ b/consul/consul/README.md @@ -1,30 +1,30 @@ # Running a Consul Cluster in Vagrant -These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) quickly and relatively easily spin up a Consul ([http://www.consul.io](http://www.consul.io)) cluster. The configuration was tested using Vagrant 1.8 (both 1.8.1 and 1.8.5), VMware Fusion 8.1.0 (with the Vagrant VMware plugin), and VirtualBox 5.1. +These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) quickly and relatively easily spin up a Consul ([http://www.consul.io](http://www.consul.io)) cluster. The configuration was tested using Vagrant, VMware Fusion, VirtualBox, and Libvirt. ## Contents -* **consul.conf**: This Upstart configuration file allows you to use the Ubuntu `service` or `initctl` commands to start, stop, or check the status of the Consul agent running as a daemon. This configuration file is Ubuntu-specific. No edits should be necessary to this file. +* **consul.service**: This systemd unit file enables Consul to run as a daemon. No edits should be necessary to this file (the `consul.sh` makes a few in-place edits during provisioning). * **consul.sh**: This shell script is executed by the Vagrant shell provisioner to provision the Ubuntu base box with the Consul binary. This shell script was written for an Ubuntu system; edits will likely be necessary for use with a different Linux distribution. +* **machines.yml**: This YAML file contains a list of VM definitions. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs. You will need to edit this file to provide appropriate IP addresses and other VM configuration data (see "Instructions" below). If you edit the IP addresses in this file, you _must_ also edit `server.json` to supply matching IP addresses there as well. + * **README.md**: The file you're currently reading. * **server.json**: This Consul configuration file contains configuration directives to run the Consul agent as a server. This file is copied to `/etc/consul.d/server/config.json` by the Vagrant file provisioner. The IP addresses specified in this file _must_ match the IP address specified in `machines.yml`. -* **machines.yml**: This YAML file contains a list of VM definitions. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs. You will need to edit this file to provide appropriate IP addresses and other VM configuration data (see "Instructions" below). If you edit the IP addresses in this file, you _must_ also edit `server.json` to supply matching IP addresses there as well. - * **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 the virtualization provider (VMware Fusion or VirtualBox), Vagrant, and any necessary plugins (such as the Vagrant VMware plugin). Please refer to the documentation for those products for more information on installation or configuration. -1. Use `vagrant box add` to install an Ubuntu 14.04 x64 box for your virtualization provider. For a VMware-formatted box, the "bento/ubuntu-14.04" box is one good option; for VirtualBox, the "ubuntu/trusty64" box will work just fine. +1. Use `vagrant box add` to install an Ubuntu 16.04 x64 box for your virtualization provider. The `machines.yml` file contains some suggested boxes for VirtualBox, VMware, and Libvirt. -2. Place the files from the `consul` directory of this GitHub repository into a directory on your local system. You can clone the entire "learning-tools" repository (using `git clone`) or just download the specific files from the the `consul` folder. +2. If you decide to use boxes _other_ than those already listed in `machines.yml`, edit this file to specify the box being used. It may be necessary to edit the IP addresses being assigned, but otherwise no other edits should be needed. If you do edit the IP addresses, you _must_ edit `config.json` as well. -3. Edit the `machines.yml` file to provide the specific details on the VMs that Vagrant should create. The `Vagrantfile` expects 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); `vmw_box` (the name of an Ubuntu 14.04 base box for the "vmware_desktop" provider); `vb_box` (the name of an Ubuntu 14.04 base box for the VirtualBox provider); `ram` (the amount of memory to be assigned to the VM); `vcpu` (the number of vCPUs that should be assigned to the VM); and `priv_ip` (an IP address to be statically assigned to the VM and is used for Consul cluster communications). +3. Place the files from the `consul` directory of this GitHub repository into a directory on your local system. You can clone the entire "learning-tools" repository (using `git clone`) or just download the specific files from the the `consul` folder. 4. Once you have edited `machines.yml` (and `server.json`, if you changed the IP addresses in `machines.yml`), use `vagrant up` to bring up the 3 systems that will serve as your Consul cluster. diff --git a/consul/consul/Vagrantfile b/consul/consul/Vagrantfile index ce85764..6105703 100644 --- a/consul/consul/Vagrantfile +++ b/consul/consul/Vagrantfile @@ -40,8 +40,14 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| srv.vm.synced_folder '.', '/vagrant', disabled: true end #if machine['sync_disabled'] - # Assign an additional static private network - srv.vm.network 'private_network', ip: machine['priv_ip'] + # Iterate through networks as per settings in machines.yml + machine['nics'].each do |net| + if net['ip_addr'] == 'dhcp' + srv.vm.network net['type'], type: net['ip_addr'] + else + srv.vm.network net['type'], ip: net['ip_addr'] + end # if net['ip_addr'] + end # machine['nics'].each # Configure VMs with RAM and CPUs per settings in machines.yml (Fusion) srv.vm.provider 'vmware_fusion' do |vmw| @@ -59,9 +65,19 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| override.vm.box = machine['box']['vb'] end # srv.vm.provider 'virtualbox' + # Configure CPU & RAM per settings in machines.yml (Libvirt) + srv.vm.provider 'libvirt' do |lv,override| + lv.memory = machine['ram'] + lv.cpus = machine['vcpu'] + override.vm.box = machine['box']['lv'] + if machine['nested'] == true + lv.nested = true + end # if machine['nested'] + end # srv.vm.provider 'libvirt' + # 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' + srv.vm.provision 'file', source: 'consul.service', destination: '/home/vagrant/consul.service' # Provision consul to the VMs srv.vm.provision 'shell', path: 'consul.sh' diff --git a/consul/consul/consul.conf b/consul/consul/consul.conf deleted file mode 100644 index ff07906..0000000 --- a/consul/consul/consul.conf +++ /dev/null @@ -1,21 +0,0 @@ -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` -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 diff --git a/consul/consul/consul.service b/consul/consul/consul.service new file mode 100644 index 0000000..268fc4e --- /dev/null +++ b/consul/consul/consul.service @@ -0,0 +1,12 @@ +[Unit] +Description=Consul Server +Requires=network-online.target +After=network-online.target + +[Service] +User=consul +ExecStart=/usr/local/bin/consul agent -config-dir="/etc/consul.d/server" -bind=BINDADDR +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/consul/consul/consul.sh b/consul/consul/consul.sh index 979e762..661aeb6 100644 --- a/consul/consul/consul.sh +++ b/consul/consul/consul.sh @@ -17,11 +17,11 @@ fi if [[ ! -e /usr/local/bin/consul ]];then # Download Consul - curl -kLO https://releases.hashicorp.com/consul/0.7.1/consul_0.7.1_linux_amd64.zip + curl -kLOs https://releases.hashicorp.com/consul/1.2.3/consul_1.2.3_linux_amd64.zip # Decompress and remove Consul download - unzip consul_0.7.1_linux_amd64.zip - rm consul_0.7.1_linux_amd64.zip + unzip consul_1.2.3_linux_amd64.zip + rm consul_1.2.3_linux_amd64.zip # Move Consul binary to location in path sudo mv consul /usr/local/bin/ @@ -46,8 +46,25 @@ if [ -d /etc/consul.d ]; then sudo chown -R root:consul /etc/consul.d fi +# Customize the systemd unit file +sed -i s/BINDADDR/$(hostname -I | cut -f2 -d' ')/ /home/vagrant/consul.service + # 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/consul.service /etc/systemd/system/consul.service +sudo chown root:root /etc/systemd/system/consul.service sudo mv /home/vagrant/config.json /etc/consul.d/server/config.json sudo chown root:consul /etc/consul.d/server/config.json + +# Reload systemd, then enable and start Consul +sudo systemctl daemon-reload +if systemctl is-enabled --quiet consul; then + systemctl reenable consul +else + systemctl enable consul +fi + +if systemctl is-active --quiet consul; then + systemctl restart consul +else + systemctl start consul +fi diff --git a/consul/consul/machines.yml b/consul/consul/machines.yml index 182a7d1..87f273c 100644 --- a/consul/consul/machines.yml +++ b/consul/consul/machines.yml @@ -1,22 +1,37 @@ --- -- name: "consul-01" - box: - vmw: "bento/ubuntu-14.04" - vb: "ubuntu/trusty64" +- box: + lv: "generic/ubuntu1604" + vmw: "bento/ubuntu-16.04" + vb: "ubuntu/xenial64" + name: "consul-01" + nested: false + nics: + - type: "private_network" + ip_addr: "192.168.100.101" ram: "512" + sync_disabled: true vcpu: "1" - priv_ip: "192.168.100.101" -- name: "consul-02" - box: - vmw: "bento/ubuntu-14.04" - vb: "ubuntu/trusty64" +- box: + lv: "generic/ubuntu1604" + vmw: "bento/ubuntu-16.04" + vb: "ubuntu/xenial64" + name: "consul-02" + nested: false + nics: + - type: "private_network" + ip_addr: "192.168.100.102" ram: "512" + sync_disabled: true vcpu: "1" - priv_ip: "192.168.100.102" -- name: "consul-03" - box: - vmw: "bento/ubuntu-14.04" - vb: "ubuntu/trusty64" +- box: + lv: "generic/ubuntu1604" + vmw: "bento/ubuntu-16.04" + vb: "ubuntu/xenial64" + name: "consul-03" + nested: false + nics: + - type: "private_network" + ip_addr: "192.168.100.103" ram: "512" + sync_disabled: true vcpu: "1" - priv_ip: "192.168.100.103"