Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
Signed-off-by: Larry Smith Jr <mrlesmithjr@gmail.com>
  • Loading branch information
mrlesmithjr committed Aug 30, 2017
1 parent 53f024c commit 4deb69e
Show file tree
Hide file tree
Showing 10 changed files with 802 additions and 342 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,6 +1,6 @@
.vagrant
host_vars/
group_vars/
# group_vars/
*.retry
.galaxy_install_info
*cloudimg-console.log
130 changes: 77 additions & 53 deletions README.md
@@ -1,44 +1,66 @@
Purpose
=======
Spins up a [Docker] Swarm mode (1.12) cluster within a [Vagrant] environment
and uses [Ansible] to provision the whole environment.

Requirements
------------
- [Ansible]
- [VirtualBox]
- [Vagrant]

Information
-----------
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Purpose](#purpose)
- [Requirements](#requirements)
- [Information](#information)
- [Usage](#usage)
- [License](#license)
- [Author Information](#author-information)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Purpose

Spins up a [Docker](https://www.docker.com) Swarm mode cluster within a
[Vagrant](https://www.vagrantup.com) environment and uses
[Ansible](https://www.ansible.com) to provision the whole environment.

## Requirements

- [Ansible](https://www.ansible.com)
- [VirtualBox](https://www.virtualbox.org)
- [Vagrant](https://www.vagrantup.com)

## Information

This environment will consist of the following:
- 7 [Ubuntu] 16.04 nodes
- 3 [Docker] Swarm Managers (node0-node2)
- 4 [Docker] Swarm Workers (node3-node6)

- 7 [Ubuntu](https://www.ubuntu.com) `16.04` nodes
- 3 [Docker](https://www.docker.com) Swarm Managers (node0-node2)
- 4 [Docker](https://www.docker.com) Swarm Workers (node3-node6)

I have also included a [Python] script `docker-management.py` that will be
worked on over time to do some initial various things but will have more
functionality over time.

Usage
-----
Easily spin up the [Vagrant] environment.
```
## Usage

Easily spin up the [Vagrant](https://www.vagrantup.com) environment.

```bash
vagrant up
```
Once everything provisions you are now ready to begin using your [Docker] Swarm

Once everything provisions you are now ready to begin using your [Docker](https://www.docker.com) Swarm
mode cluster.

Connect to one of the [Docker] Swarm Managers to begin creating services and etc.
```
Connect to one of the [Docker](https://www.docker.com) Swarm Managers to begin creating services and etc.

```bash
vagrant ssh node0
```
Validate that the [Docker] Swarm cluster is functional:
```

Validate that the [Docker](https://www.docker.com) Swarm cluster is functional:

```bash
sudo docker node ls
```

You should something similar to below:
```

```bash
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
0bp91mlgswsl19chkudasptxt node3 Ready Active
0uyl5ms4lb543d6284vrycuh1 node5 Ready Active
Expand All @@ -48,39 +70,51 @@ ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
60qm8mohj517vovv7id5p7l6s node2 Ready Active Reachable
cwnkijv7f8gzsvwltnomgmmea node1 Ready Active Reachable
```
Create a new [Docker] service:
```

Create a new [Docker](https://www.docker.com) service:

```bash
sudo docker service create --name web --publish 8080:80 --replicas 1 mrlesmithjr/nginx
```
List the current [Docker] services:
```

List the current [Docker](https://www.docker.com) services:

```bash
sudo docker service ls
...
ID NAME REPLICAS IMAGE COMMAND
016psrb2tb7y web 1/1 mrlesmithjr/nginx
```

List the tasks of a service:
```

```bash
sudo docker service ps web
...
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
9n3yq6k2ig71kkldzrs2zfqb3 web.1 mrlesmithjr/nginx node0 Running Running about a minute ago
```

Scale the service to increase the number of replicas:
```

```bash
sudo docker service scale web=4
...
web scaled to 4
```
Now list the current [Docker] services:
```

Now list the current [Docker](https://www.docker.com) services:

```bash
sudo docker service ls
...
ID NAME REPLICAS IMAGE COMMAND
016psrb2tb7y web 4/4 mrlesmithjr/nginx
```

Now list the tasks of the service:
```

```bash
sudo docker service ps web
...
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
Expand All @@ -90,27 +124,17 @@ bisd4qcaxsx66u6zeuomykkr3 web.2 mrlesmithjr/nginx node3 Running Runni
dhjjumf7auf6s5k8uqwjo6wbx web.4 mrlesmithjr/nginx node1 Running Running 40 seconds ago
```

Now go and enjoy your [Docker] Swarm mode cluster and do some learning.
Now go and enjoy your [Docker](https://www.docker.com) Swarm mode cluster and do some learning.

License
-------
## License

BSD

Author Information
------------------
## Author Information

Larry Smith Jr.
- [@mrlesmithjr]
- [everythingshouldbevirtual.com]
- [mrlesmithjr@gmail.com]


[Ansible]: <https://www.ansible.com/>
[Docker]: <https://www.docker.com>
[@mrlesmithjr]: <https://twitter.com/mrlesmithjr>
[everythingshouldbevirtual.com]: <http://everythingshouldbevirtual.com>
[mrlesmithjr@gmail.com]: <mailto:mrlesmithjr@gmail.com>
[Ubuntu]: <https://www.ubuntu.com/>
[Vagrant]: <https://www.vagrantup.com/>
[VirtualBox]: <https://www.virtualbox.org/>

- [@mrlesmithjr](https://www.twitter.com/mrlesmithjr)
- [EverythingShouldBeVirtual](http://everythingshouldbevirtual.com)
- [mrlesmithjr](http://mrlesmithjr.com)
- mrlesmithjr [at] gmail.com
121 changes: 58 additions & 63 deletions Vagrantfile
Expand Up @@ -15,16 +15,13 @@ nodes = YAML.load_file(File.join(File.dirname(__FILE__), 'nodes.yml'))

# Define global variables
#
# Define if provisioners should run (true|false)
provision_nodes = true


Vagrant.configure(2) do |config|
# Iterate over nodes to get a count
# Define as 0 for counting the number of nodes to create from nodes.yml
groups = [] # Define array to hold ansible groups
num_nodes = 0
populated_ansible_groups = Hash.new # Create hash to contain iterated groups
populated_ansible_groups = {} # Create hash to contain iterated groups

# Create array of Ansible Groups from iterated nodes
nodes.each do |node|
Expand All @@ -44,9 +41,7 @@ Vagrant.configure(2) do |config|
nodes.each do |node|
node['ansible_groups'].each do |nodegroup|
# Check if node is a member of iterated group
if nodegroup == group
group_nodes.push(node['name'])
end
group_nodes.push(node['name']) if nodegroup == group
end
populated_ansible_groups[group] = group_nodes
end
Expand All @@ -66,110 +61,110 @@ Vagrant.configure(2) do |config|
# "docker-swarm-workers" => ["node[5:6]"]
# }

#Iterate over nodes
# Iterate over nodes
nodes.each do |node_id|
# Below is needed if not using Guest Additions
# config.vm.synced_folder ".", "/vagrant", type: "rsync",
# rsync__exclude: "hosts"
config.vm.define node_id['name'] do |node|
unless node_id['synced_folder'].nil?
unless node_id['synced_folder']['type'].nil?
config.vm.synced_folder '.', '/vagrant', type: node_id['synced_folder']['type']
end
end
node.vm.box = node_id['box']
node.vm.hostname = node_id['name']
node.vm.provider "virtualbox" do |vb|
node.vm.provider 'virtualbox' do |vb|
vb.memory = node_id['mem']
vb.cpus = node_id['vcpu']

# Setup desktop environment
if not node_id['desktop'].nil?
unless node_id['desktop'].nil?
if node_id['desktop']
vb.gui = true
vb.customize ["modifyvm", :id, "--graphicscontroller", "vboxvga"]
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
vb.customize ["modifyvm", :id, "--ioapic", "on"]
vb.customize ["modifyvm", :id, "--vram", "128"]
vb.customize ["modifyvm", :id, "--hwvirtex", "on"]
vb.customize ['modifyvm', :id, '--graphicscontroller', 'vboxvga']
vb.customize ['modifyvm', :id, '--accelerate3d', 'on']
vb.customize ['modifyvm', :id, '--ioapic', 'on']
vb.customize ['modifyvm', :id, '--vram', '128']
vb.customize ['modifyvm', :id, '--hwvirtex', 'on']
end
end

# Add additional disk(s)
if not node_id['disks'].nil?
unless node_id['disks'].nil?
dnum = 0
node_id['disks'].each do |disk_num|
dnum = (dnum.to_i + 1)
ddev = ("#{node_id['name']}_Disk#{dnum}.vdi")
ddev = "#{node_id['name']}_Disk#{dnum}.vdi"
dsize = disk_num['size'].to_i * 1024
unless File.exist?("#{ddev}")
vb.customize ['createhd', '--filename', ("#{ddev}"), \
'--variant', 'Fixed', '--size', dsize]
unless File.exist?(ddev.to_s)
vb.customize ['createhd', '--filename', ddev.to_s, \
'--variant', 'Fixed', '--size', dsize]
end
vb.customize ['storageattach', :id, '--storagectl', \
"#{disk_num['controller']}", '--port', dnum, '--device', 0, \
'--type', 'hdd', '--medium', "#{ddev}"]
vb.customize ['storageattach', :id, '--storagectl', \
(disk_num['controller']).to_s, '--port', dnum, '--device', 0, \
'--type', 'hdd', '--medium', ddev.to_s]
end
end
end

# Provision network interfaces
if not node_id['interfaces'].nil?
unless node_id['interfaces'].nil?
node_id['interfaces'].each do |int|
if int['method'] == 'dhcp'
if int['network_name'] == "None"
if int['network_name'] == 'None'
node.vm.network :private_network, \
type: "dhcp"
type: 'dhcp'
end
if int['network_name'] != "None"
if int['network_name'] != 'None'
node.vm.network :private_network, \
virtualbox__intnet: int['network_name'], \
type: "dhcp"
virtualbox__intnet: int['network_name'], \
type: 'dhcp'
end
end
if int['method'] == "static"
if int['network_name'] == "None"
node.vm.network :private_network, \
ip: int['ip'], \
auto_config: int['auto_config']
end
if int['network_name'] != "None"
node.vm.network :private_network, \
virtualbox__intnet: int['network_name'], \
ip: int['ip'], \
auto_config: int['auto_config']
end
next unless int['method'] == 'static'
if int['network_name'] == 'None'
node.vm.network :private_network, \
ip: int['ip'], \
auto_config: int['auto_config']
end
next unless int['network_name'] != 'None'
node.vm.network :private_network, \
virtualbox__intnet: int['network_name'], \
ip: int['ip'], \
auto_config: int['auto_config']
end
end

# Port Forwards
if not node_id['port_forwards'].nil?
unless node_id['port_forwards'].nil?
node_id['port_forwards'].each do |pf|
node.vm.network :forwarded_port, \
guest: pf['guest'], \
host: pf['host']
guest: pf['guest'], \
host: pf['host']
end
end

# Provisioners
if provision_nodes
if node_id == num_nodes
node.vm.provision "ansible" do |ansible|
ansible.limit = "all"
#runs bootstrap Ansible playbook
ansible.playbook = "bootstrap.yml"
end
node.vm.provision "ansible" do |ansible|
ansible.limit = "all"
#runs Ansible playbook for installing roles/executing tasks
ansible.playbook = "playbook.yml"
ansible.groups = ansible_groups
unless node_id['provision'].nil?
if node_id['provision']
# runs initial shell script
config.vm.provision :shell, path: 'bootstrap.sh', keep_color: 'true'
if node_id == num_nodes
node.vm.provision 'ansible' do |ansible|
ansible.limit = 'all'
# runs bootstrap Ansible playbook
ansible.playbook = 'bootstrap.yml'
end
node.vm.provision 'ansible' do |ansible|
ansible.limit = 'all'
# runs Ansible playbook for installing roles/executing tasks
ansible.playbook = 'playbook.yml'
ansible.groups = ansible_groups
end
end
end
end

end
end

#runs initial shell script
if provision_nodes
config.vm.provision :shell, path: "bootstrap.sh", keep_color: "true"
end

end

0 comments on commit 4deb69e

Please sign in to comment.