diff --git a/ipvs-docker/README.md b/ipvs-docker/README.md new file mode 100644 index 0000000..41fa329 --- /dev/null +++ b/ipvs-docker/README.md @@ -0,0 +1,44 @@ +# Load Balancing Docker Containers with IPVS + +These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) to build an environment to test using IPVS for load balancing Docker containers. This configuration was tested using Vagrant 1.8.1, VMware Fusion 8.1.0, and version 4.0.5 of the Vagrant VMware plugin. + + +## Contents + +* **machines.yml**: This is a YAML file containing the configuration data used by Vagrant when creating and provisioning VMs. This particular Vagrant environment requires six (6) values in this file for each VM: `name` (name to be assigned to the box as well as used for hostname), `box` (the name of the Vagrant box), `ram` (desired RAM), `vcpu` (number of virtual CPUs), `ip_addr` (the private IP address to be assigned to the second network interface; also controls whether a second interface is provisioned), and `docker` (set to "true" or "false"; controls whether Vagrant provisions Docker onto the VM). + +* **README.md**: The file you're currently reading. + +* **Vagrantfile**: This file is used by Vagrant to spin up the virtual machines for this environment. No changes need to be made to this document, as all the configuration data is found in other files (like `machines.yml`). However, if you are using a virtualization solution _other_ than VMware Fusion, you might need to make changes to this file. + +## Instructions + +These instructions assume you've already installed VMware Fusion, Vagrant, and the Vagrant VMware plugin. Please refer to the documentation for those products for more information on installation or configuration. Note that Internet access is required when using `vagrant up` to create this environment. + +1. Use `vagrant box add` to install an Ubuntu 14.04 x86_64 Vagrant box. I have a base box you can use for this purpose; to use my Ubuntu 14.04 x64 base box, add the box with `vagrant box add slowe/ubuntu-trusty-x64`. + +2. Place the files from the `ipvs-docker` 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 `ipvs-docker` folder. + +3. If necessary, edit `machines.yml` to specify the name of the Vagrant box downloaded in step 1. You may also make changes to the private IP addresses or the RAM/CPU settings, although no changes are typically required. + +4. Run `vagrant up` to instantiate the learning environment. This will spin up three (3) VMs based on the Ubuntu 14.04 base box you downloaded in step 1 and specified in `machines.yml` in step 3. Vagrant will also appropriately configure each VM and start the necessary services. Depending on the speed of your system and your Internet connection, this may take a few minutes. + +5. Configure the virtual IP (VIP) you'll use to connect to the Docker containers. Run `vagrant ssh lvs-01` to connect to the VM named `lvs-01`, then run this command: + + ip addr add 192.168.100.10/24 dev eth1 + +6. Log into the first Docker VM using `vagrant ssh docker-01` and launch an Nginx container with this command line: + + docker run -d -p 80:80 nginx:latest + + Repeat this command on the other Docker VM (the VM named `docker-02`). + +7. Use `vagrant ssh lvs-01` to log back into the IPVS load balancer and run these commands to set up the virtual server: + + sudo ipvsadm -A -t 192.168.100.10:80 -s rr + sudo ipvsadm -a -t 192.168.100.10:80 -r 192.168.100.151:80 -g + sudo ipvsadm -a -t 192.168.100.10:80 -r 192.168.100.152:80 -g + +8. Connect to "http://192.168.100.10" to see the load balanced VIP in front of the Docker containers. + +Have fun! diff --git a/ipvs-docker/Vagrantfile b/ipvs-docker/Vagrantfile new file mode 100644 index 0000000..4f00501 --- /dev/null +++ b/ipvs-docker/Vagrantfile @@ -0,0 +1,74 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Specify Vagrant version, Vagrant API version, and desired clone location +Vagrant.require_version '>= 1.6.0' +VAGRANTFILE_API_VERSION = '2' +ENV['VAGRANT_VMWARE_CLONE_DIRECTORY'] = '~/.vagrant' + +# Require 'yaml' module +require 'yaml' + +# Read YAML file with VM details (box, CPU, RAM, IP addresses) +machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.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 + machines.each do |machine| + config.vm.define machine['name'] do |srv| + + # Don't check for box updates + srv.vm.box_check_update = false + srv.vm.hostname = machine['name'] + + # Use VMware box by default (override later for VirtualBox) + srv.vm.box = machine['vmw_box'] + + # Configure default synced folder (disable by default) + if machine['sync_disabled'] != nil + srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled'] + else + srv.vm.synced_folder '.', '/vagrant', disabled: true + end #if machine['sync_disabled'] + + # Assign additional private network + if machine['ip_addr'] != nil + srv.vm.network 'private_network', ip: machine['ip_addr'] + end # if machine['ip_addr'] + + # Configure CPU & RAM per settings in machines.yml (Fusion) + srv.vm.provider :vmware_fusion do |vmw| + vmw.vmx['memsize'] = machine['ram'] + vmw.vmx['numvcpus'] = machine['vcpu'] + if machine['nested'] == true + vmw.vmx['vhv.enable'] = 'TRUE' + end #if machine['nested'] + end # srv.vm.provider vmware_fusion + + # Configure CPU & RAM per settings in machines.yml (VirtualBox) + srv.vm.provider :virtualbox do |vb, override| + vb.memory = machine['ram'] + vb.cpus = machine['vcpu'] + override.vm.box = machine['vb_box'] + end # srv.vm.provider virtualbox + + # Provision Docker on appropriate VMs + if machine['docker'] == true + srv.vm.provision 'docker', images: ['nginx:latest'] + srv.vm.provision 'shell', + inline: 'iptables -t nat -A PREROUTING -d 192.168.100.10 -j REDIRECT', + privileged: true + end # if machine['docker'] + + # Provision LVS VM + if machine['name'] == 'lvs-01' + srv.vm.provision 'shell', path: 'provision.sh', privileged: true + end # if machine['name'] + end # config.vm.define + end # machines.each +end # Vagrant.configure diff --git a/ipvs-docker/machines.yml b/ipvs-docker/machines.yml new file mode 100644 index 0000000..9b3937b --- /dev/null +++ b/ipvs-docker/machines.yml @@ -0,0 +1,22 @@ +--- +- name: docker-01 + vmw_box: slowe/ubuntu-trusty-x64 + vb_box: ubuntu/trusty64 + ram: 512 + vcpu: 1 + ip_addr: 192.168.100.151 + docker: true +- name: docker-02 + vmw_box: slowe/ubuntu-trusty-x64 + vb_box: ubuntu/trusty64 + ram: 512 + vcpu: 1 + ip_addr: 192.168.100.152 + docker: true +- name: lvs-01 + vmw_box: slowe/ubuntu-trusty-x64 + vb_box: ubuntu/trusty64 + ram: 512 + vcpu: 1 + ip_addr: 192.168.100.101 + docker: false diff --git a/ipvs-docker/provision.sh b/ipvs-docker/provision.sh new file mode 100644 index 0000000..9c091d4 --- /dev/null +++ b/ipvs-docker/provision.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Initial housekeepting +export DEBIAN_FRONTEND=noninteractive + +# Update package list +sudo apt-get update + +# Install ipvsadm if not already installed +if [[ ! -e /sbin/ipvsadm ]]; then + sudo apt-get -yq install ipvsadm +fi + +# Configure /etc/default/ipvsadm +# Set ipvsadm to start automatically +sudo sed -i 's/^AUTO=\"false\"/AUTO=\"true\"/' /etc/default/ipvsadm + +# Set ipvsadm to master mode +sudo sed -i 's/^DAEMON=\"none\"/DAEMON=\"master\"/' /etc/default/ipvsadm + +# Set ipvsadm interface to eth1 +sudo sed -i 's/^IFACE=\"eth0\"/IFACE=\"eth1\"/' /etc/default/ipvsadm + +# Restart the ipvsadm service +sudo service ipvsadm restart