create disposable test environments with vagrant and puppet

33
© Copyright 2015 Coveros, Inc. All rights reserved. Creating Disposable Test Environments with Vagrant and Puppet Gene Gotimer, Senior Architect [email protected]

Upload: gene-gotimer

Post on 14-Apr-2017

497 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Create Disposable Test Environments with Vagrant and Puppet

© Copyright 2015 Coveros, Inc. All rights reserved.

Creating Disposable Test Environments with Vagrant and Puppet

Gene Gotimer, Senior Architect

[email protected]

Page 2: Create Disposable Test Environments with Vagrant and Puppet

2© Copyright 2015 Coveros, Inc. All rights reserved.

Coveros helps organizations accelerate the delivery of business value through secure, reliable software

About Coveros

Page 3: Create Disposable Test Environments with Vagrant and Puppet

3© Copyright 2015 Coveros, Inc. All rights reserved.

Why Disposable Test Environments?

Destructive testing

Known baseline

Available on-demand

Not shared

No vested interest in keeping them long-term

Always up-to-date

Page 4: Create Disposable Test Environments with Vagrant and Puppet

4© Copyright 2015 Coveros, Inc. All rights reserved.

Tools Involved

VirtualBox– virtualization software

Vagrant– virtualization automation

Puppet– configuration management and automation– Chef, Ansible, or SaltStack would work equally well

Packer– machine image automation

Page 5: Create Disposable Test Environments with Vagrant and Puppet

5© Copyright 2015 Coveros, Inc. All rights reserved.

VirtualBox

Page 6: Create Disposable Test Environments with Vagrant and Puppet

6© Copyright 2015 Coveros, Inc. All rights reserved.

Oracle VM VirtualBox

Virtualization software from Oracle

Free

Runs on Windows, Mac, Linux

Runs as an application

Allows us to use local VMs

Easy to install

Works well with Vagrant

https://www.virtualbox.org/

Page 7: Create Disposable Test Environments with Vagrant and Puppet

7© Copyright 2015 Coveros, Inc. All rights reserved.

Vagrant

Page 8: Create Disposable Test Environments with Vagrant and Puppet

8© Copyright 2015 Coveros, Inc. All rights reserved.

Vagrant

Virtualization workflow software from HashiCorp

Free, open-source

Runs on Windows, Mac, Linux

Easy to install

Works well with Puppet, Chef, Shell– many other provisioners

Works well with VirtualBox, VMware, Amazon Web Services– many other providers

https://www.vagrantup.com/

Page 9: Create Disposable Test Environments with Vagrant and Puppet

9© Copyright 2015 Coveros, Inc. All rights reserved.

Creating a Vagrant Box

To create a VM:– mkdir starcanada‐vagrant– cd starcanada‐vagrant– vagrant box add hashicorp/precise64– vagrant init hashicorp/precise64– vagrant up

vagrant box add– downloads a “base box”– boxes at https://atlas.hashicorp.com/search

vagrant init– builds a Vagrantfile with the base box

vagrant up– starts the VM

Page 10: Create Disposable Test Environments with Vagrant and Puppet

10© Copyright 2015 Coveros, Inc. All rights reserved.

Vagrantfile

Vagrantfile– lots of comments by default– stock Vagrantfile without comments is:

Vagrant.configure(2) do |config|config.vm.box = "hashicorp/precise64"

end

Page 11: Create Disposable Test Environments with Vagrant and Puppet

11© Copyright 2015 Coveros, Inc. All rights reserved.

vagrant up

vagrant up– imports the base box to VirtualBox– makes sure the base box is up to date– sets a unique name for the VM– sets up networking (just NAT by default)– sets up port forwarding (just SSH by default)– boots VM– replaces known, insecure SSH key with a new random key– makes sure VirtualBox Guest Additions are installed– mounts shared folders (/vagrant by default on the VM)– provisions software (nothing by default)

Page 12: Create Disposable Test Environments with Vagrant and Puppet

12© Copyright 2015 Coveros, Inc. All rights reserved.

Access Vagrant Box

To access a VM:– vagrant ssh

vagrant ssh– connects to the VM via the forwarded SSH port

requires an SSH client installed– Git (https://msysgit.github.io/)– openssh on Cygwin (http://www.cygwin.com/)– PuTTY (http://www.chiark.greenend.org.uk/~sgtatham/putty/)

requires converting the key format

Page 13: Create Disposable Test Environments with Vagrant and Puppet

13© Copyright 2015 Coveros, Inc. All rights reserved.

Rebuild Vagrant Box

To rebuild a VM:– vagrant destroy– vagrant up

vagrant destroy– deletes a VM

vagrant up– starts the VM

Page 14: Create Disposable Test Environments with Vagrant and Puppet

14© Copyright 2015 Coveros, Inc. All rights reserved.

Puppet

Page 15: Create Disposable Test Environments with Vagrant and Puppet

15© Copyright 2015 Coveros, Inc. All rights reserved.

Puppet

Configuration management software from PuppetLabs

Vaguely Ruby-based, domain-specific language

Free, open-source

Runs on Windows, Mac, Linux

Easy to install

Works well with Vagrant

Similar to Chef, Ansible, SaltStack

https://puppetlabs.com/

Page 16: Create Disposable Test Environments with Vagrant and Puppet

16© Copyright 2015 Coveros, Inc. All rights reserved.

Install Apache with Puppet

Modify the Vagrantfile:Vagrant.configure(2) do |config|

config.vm.box = "hashicorp/precise64"config.vm.network "private_network", ip: "192.168.33.10"config.puppet_install.puppet_version = '3.8.1'config.vm.provision "shell", inline: <<‐SHELLsudo puppet module install puppetlabs‐apache

SHELLconfig.vm.provision "puppet" do |puppet|puppet.manifests_path = "manifests"puppet.manifest_file = "site.pp"puppet.module_path = "modules"

endend

Page 17: Create Disposable Test Environments with Vagrant and Puppet

17© Copyright 2015 Coveros, Inc. All rights reserved.

Vagrant Networking

config.vm.network "private_network", ip: "192.168.33.10"– sets up a new network interface on the box– private_network = host-only

only this box and other VMs on this box can reach it

Page 18: Create Disposable Test Environments with Vagrant and Puppet

18© Copyright 2015 Coveros, Inc. All rights reserved.

Vagrant Modules

config.puppet_install.puppet_version = '3.8.1'– Vagrant module from

https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Plugins– vagrant‐puppet‐install

installs Puppet version 3.8.1 could have been :latest, but I want control

Page 19: Create Disposable Test Environments with Vagrant and Puppet

19© Copyright 2015 Coveros, Inc. All rights reserved.

Shell Provisioning

config.vm.provision "shell", inline: <<‐SHELLsudo puppet module install puppetlabs‐apacheSHELL

– here-doc that runs all the commands until SHELL– this command installs a Puppet module from

https://forge.puppetlabs.com/puppetlabs

Page 20: Create Disposable Test Environments with Vagrant and Puppet

20© Copyright 2015 Coveros, Inc. All rights reserved.

Puppet Provisioning

config.vm.provision "puppet" do |puppet|puppet.manifests_path = "manifests"puppet.manifest_file = "site.pp"puppet.module_path = "modules"end

– sets up a standard Puppet layout– commands in manifests/site.pp– reusable modules in modules

Page 21: Create Disposable Test Environments with Vagrant and Puppet

21© Copyright 2015 Coveros, Inc. All rights reserved.

Example Puppet Code

Example init.pp file in the modules/website/manifests directory:

class website {class { 'apache': }apache::vhost { "${::fqdn}":vhost_name => '*',default_vhost => true,port          => '80',docroot => '/var/www',

}file { '/var/www/index.html':ensure  => 'file',content => template('website/index.html.erb'),owner   => 'root',group   => 'www‐data',mode    => '0640',require => Class['apache'],

}}

Page 22: Create Disposable Test Environments with Vagrant and Puppet

22© Copyright 2015 Coveros, Inc. All rights reserved.

Installing Apache httpd

class { 'apache:' } – installs Apache httpd server– sets up default configuration

Page 23: Create Disposable Test Environments with Vagrant and Puppet

23© Copyright 2015 Coveros, Inc. All rights reserved.

Configuring Apache httpd

apache::vhost { "${::fqdn}":vhost_name => '*',default_vhost => true,port          => '80',docroot => '/var/www',

}– sets up default virtual host– listening on port 80– document root is /var/www

Page 24: Create Disposable Test Environments with Vagrant and Puppet

24© Copyright 2015 Coveros, Inc. All rights reserved.

Installing Templated Content

file { '/var/www/index.html':ensure  => 'file',content => template('website/index.html.erb'),owner   => 'root',group   => 'www‐data',mode    => '0640',require => Class['apache'],

}– copies file from host box– sets owner, group, and permissions

Page 25: Create Disposable Test Environments with Vagrant and Puppet

25© Copyright 2015 Coveros, Inc. All rights reserved.

Automation Advantages

Deploy is now automated

Automated = repeatable, easy, quick

Test on the system, make any changes we want, then destroy it, recreate it in a pristine condition

Reuse the deployment scripts in all environments– including production– especially production

Page 26: Create Disposable Test Environments with Vagrant and Puppet

26© Copyright 2015 Coveros, Inc. All rights reserved.

Other Possibilities

Template files

Variable substitution/Configuration database– YAML– JSON– Encrypted

Multiple machines

Different providers– Managed– VMware– Amazon Web Services (AWS)

Chef, Ansible, or SaltStack

Page 27: Create Disposable Test Environments with Vagrant and Puppet

27© Copyright 2015 Coveros, Inc. All rights reserved.

Packer

Page 28: Create Disposable Test Environments with Vagrant and Puppet

28© Copyright 2015 Coveros, Inc. All rights reserved.

Packer

Machine image automation from HashiCorp

Free, open-source

Runs on Windows, Mac, Linux

Easy to install

Works well with Puppet, Chef, Shell– many other provisioners

Works well with VirtualBox, VMware, Amazon Web Services– many other providers

https://packer.io/

Page 29: Create Disposable Test Environments with Vagrant and Puppet

29© Copyright 2015 Coveros, Inc. All rights reserved.

Packer Templates

Packer templates on GitHub from Shiguredo, Inc.

Templates for– CentOS Linux 6.4, 6.5, 6.6, 7.0, 7.1– Scientific Linux 6.4, 6.5, 7.0– Ubuntu Linux 12.04, 14.04

Fork and edit to create you own base boxes

https://github.com/shiguredo/packer-templates

Page 30: Create Disposable Test Environments with Vagrant and Puppet

30© Copyright 2015 Coveros, Inc. All rights reserved.

Wrap-Up

Page 31: Create Disposable Test Environments with Vagrant and Puppet

31© Copyright 2015 Coveros, Inc. All rights reserved.

Tools Recap

VirtualBox– virtualization software– https://www.virtualbox.org/

Vagrant– virtualization automation– https://www.vagrantup.com/– Boxes: https://atlas.hashicorp.com/search– Plugins:

https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Plugins

Page 32: Create Disposable Test Environments with Vagrant and Puppet

32© Copyright 2015 Coveros, Inc. All rights reserved.

Tools Recap

Puppet– configuration management and automation– https://puppetlabs.com/– Modules: https://forge.puppetlabs.com/puppetlabs

Packer– machine image automation– https://packer.io/– Templates: https://github.com/shiguredo/packer-

templates

Page 33: Create Disposable Test Environments with Vagrant and Puppet

33© Copyright 2015 Coveros, Inc. All rights reserved.

Questions?

Gene [email protected]://www.coveros.com@CoverosGene