chef, vagrant and friends

74
CHEF, VAGRANT AND FRIENDS AUTOMATING YOUR ENVIRONMENTS

Upload: ben-mcrae

Post on 07-May-2015

238 views

Category:

Software


2 download

DESCRIPTION

An introduction to using Chef and Vagrant for entry level users.

TRANSCRIPT

Page 1: Chef, Vagrant and Friends

CHEF, VAGRANT AND FRIENDS

AUTOMATING YOUR ENVIRONMENTS

Page 2: Chef, Vagrant and Friends

@benmcrae

• Software engineer

• CompareTheMarket.com

• Travelling & listening to music

• Coffee / real ale drinker

Page 3: Chef, Vagrant and Friends

YOU?

Page 4: Chef, Vagrant and Friends

MASTER CHEFS

Page 5: Chef, Vagrant and Friends

SOFTWARE!CONFIGURATION MANAGEMENT

Page 6: Chef, Vagrant and Friends
Page 7: Chef, Vagrant and Friends

Chef can automate how you configure, deploy and scale your servers and applications.

Page 8: Chef, Vagrant and Friends
Page 9: Chef, Vagrant and Friends

GETTING STARTED

Page 10: Chef, Vagrant and Friends

Installing Chef 11!

Chef Omnibus (Linux, OS X, Windows)

Chef DK (April 2014, v0.1.0) (Bundled software)

Gem (Ruby 1.9.3 recommended)

Page 11: Chef, Vagrant and Friends

Chef CLI tools

ohai (node attributes)

chef-apply (execute a single recipe from the command line)

chef-solo (execute run lists and cookbooks on a node)

chef-client (retrieves & executes run lists & cookbooks on nodes)

knife (interact with chef server)

Page 12: Chef, Vagrant and Friends

INFRASTRUCTURE AS CODE

Page 13: Chef, Vagrant and Friends

ResourcesResources represent a piece of the system and its desired state. Some resources available:!

• Directories

• Users

• Groups

• Services

• Packages

Page 14: Chef, Vagrant and Friends
Page 15: Chef, Vagrant and Friends

Resource Syntax

A resource is a Ruby block with four components:

• A type

• A name

• One (or more) attributes (with values)

• One (or more) actions

Page 16: Chef, Vagrant and Friends

RecipeRecipes are what you write to install and configure things on your machine.!

• Authored using a Ruby DSL

• Made from multiple resources

• Can include other recipes

• Single responsibility in purpose

• Belongs to a Cookbook

Page 17: Chef, Vagrant and Friends

Recipe DSL

A Ruby DSL, with specific methods to write chef recipes and resource blocks.

Common Ruby syntax can be used with the Recipe DSL methods. if / case statements…

Page 18: Chef, Vagrant and Friends

FIRST RECIPE

Page 19: Chef, Vagrant and Friends

Ingredients

• Using chef-apply and a single recipe

• Create a new developer user on the system

• Install Git using the OS package manager

• Create a .gitconfig file for the developer user

Page 20: Chef, Vagrant and Friends

stage-1https://github.com/benmcrae/ddd-chef-examples/tree/master/stage-1

Page 21: Chef, Vagrant and Friends

Chef Run

• Recipe loaded

• Resources are arranged in an ordered queue

• Each resource is mapped to a Provider!

• The node Converges by executing each provider

Page 22: Chef, Vagrant and Friends

Providers

Providers define steps that are needed to bring a piece of the system from its current

state into the desired state.

Page 23: Chef, Vagrant and Friends

Idempotent

A recipe can run multiple times on the same system and the results will always be

identical.

Page 24: Chef, Vagrant and Friends

RETROSPECTIVE

Page 25: Chef, Vagrant and Friends

Outcomes

• Poor single responsibility

• Use better suited resources e.g. template / file

• Fixed values could be swapped for attributes

Page 26: Chef, Vagrant and Friends

Next Steps

1. Create cookbook from existing recipe

2. Refactor outcomes from retrospective

Page 27: Chef, Vagrant and Friends

FIRST COOKBOOK

Page 28: Chef, Vagrant and Friends

Cookbook

A cookbook defines a scenario, such as everything needed to install and configure Apache and the resources that support it.

Page 29: Chef, Vagrant and Friends
Page 30: Chef, Vagrant and Friends

Cookbook Folders• attributes - attribute files, loaded in alphabetical order!

• files - stored files for file and directory resources!

• libraries - arbitrary ruby libraries, used in recipes!

• providers - custom providers (LWRP)!

• recipes - recipe files!

• resources - custom resources (LWRP)!

• templates - erb files for the template resource

Page 31: Chef, Vagrant and Friends

Cookbook Generators

• knife cookbook create ‘cookbook name’

• berks cookbook ‘cookbook name’

Page 32: Chef, Vagrant and Friends

Metadata File

• The metadata.rb sits in the cookbook root directory

• Defines cookbook name, version, and description

• Can declare dependencies on other cookbooks

• List supported operating systems

Page 33: Chef, Vagrant and Friends
Page 34: Chef, Vagrant and Friends

REFACTOR

Page 35: Chef, Vagrant and Friends

Template Resource

• Uses ERB (Embedded Ruby) files

• Supports variables and hashes in templates

• Multi nested folders designed to support distributing files across platforms

• Best practice: set variables using attributes

Page 36: Chef, Vagrant and Friends
Page 37: Chef, Vagrant and Friends

Node Object

• Attributes - An attribute is a specific piece of data about the node!

• Run list - A run-list is an ordered list of recipes and/or roles that are run in an exact order

Page 38: Chef, Vagrant and Friends

Attributes

!

• Attributes can be defined by the node, recipes, cookbooks, roles and environments!

• Node information. i.e. IP / MAC addresses, OS info

• Recipe information. i.e. directory paths, users, application data

Page 39: Chef, Vagrant and Friends
Page 40: Chef, Vagrant and Friends

Overriding Attributes

Page 41: Chef, Vagrant and Friends

Ohai

Ohai is a CLI tool that is used to detect attributes on a node!

• Platform details

• Network usage

• Memory usage

• Processor usage

Page 42: Chef, Vagrant and Friends

Run List

• A run-list defines all of the configuration settings that are necessary for a node to converge

• An ordered list of roles and/or recipes that are run in an exact order

Page 43: Chef, Vagrant and Friends

chef-solo

• chef-solo allows using cookbooks on nodes without using Chef server

• Cookbooks & dependencies must be on the node

• Limited in functionality compared to chef-server

• Requires configuration; run-list and attributes

Page 44: Chef, Vagrant and Friends

stage-2https://github.com/benmcrae/ddd-chef-examples/tree/master/stage-2

Page 45: Chef, Vagrant and Friends

Chef Run• Builds node object!

• Expands run-list

• Compiles Resources in an ordered queue

• Each resource is mapped to a Provider!

• The node Converges by executing each provider

Page 46: Chef, Vagrant and Friends

RETROSPECTIVE

Page 47: Chef, Vagrant and Friends

Outcomes

• A single cookbook to manage our developer user

• A cookbook that can be used with both chef-solo and chef-client (Chef Server)

Page 48: Chef, Vagrant and Friends

Next Steps

• Introduce community cookbooks and Berkshelf

• Install Ruby 2.1.2, using Berkshelf

Page 49: Chef, Vagrant and Friends

COMMUNITY COOKBOOKS

Page 50: Chef, Vagrant and Friends

Community Cookbooks• An online Open Source cookbook repository,

maintained and used by the chef community.

• Trusted cookbooks can be downloaded from - http://community.opscode.com

• Cookbook dependencies are not automatically downloaded. This must be done by looking through the cookbook metadata file, and manually downloading listed cookbooks.

Page 51: Chef, Vagrant and Friends

Berkshelf

• The cookbook dependency manager

• gem install berkshelf

• Used to maintain cookbooks on your Chef Server

• Written by Jamie Windsor, and Seth Vargo

Page 52: Chef, Vagrant and Friends

Berksfile• Lives in the root directory of the Cookbook

• Lists each cookbook name, and version (optional) which your cookbook depends on

• Ability to read cookbook dependencies from metadata.rb file

• Traverses over other cookbook dependencies

Page 53: Chef, Vagrant and Friends
Page 54: Chef, Vagrant and Friends

stage-3https://github.com/benmcrae/ddd-chef-examples/tree/master/stage-3

Page 55: Chef, Vagrant and Friends

RETROSPECTIVE

Page 56: Chef, Vagrant and Friends

Outcomes

• Implemented a community cookbook with the aid of Berkshelf.

• Applied our knowledge of attributes to other cookbooks.

Page 57: Chef, Vagrant and Friends

Next Steps

• Provision the node automatically with chef-solo and Vagrant.

• Create a new recipe to git clone our ruby app into the developer home directory.

Page 58: Chef, Vagrant and Friends
Page 59: Chef, Vagrant and Friends

• Create and configure lightweight, reproducible, and portable development environments.

• Vagrant stands on the shoulders of giants. Machines are provisioned on top of VirtualBox, VMware, AWS, or any other provider.

• Provision machines with, shell scripts, Chef, or Puppet.

Page 60: Chef, Vagrant and Friends

Commands• vagrant init hashicorp/precise32

• vagrant up

• vagrant provision

• vagrant halt

• vagrant destroy

Page 61: Chef, Vagrant and Friends
Page 62: Chef, Vagrant and Friends

Vagrant Provision

• Move the run list and attributes into the Vagrantfile

• Vagrant will run chef-solo on VM start, or by running the provision command

Page 63: Chef, Vagrant and Friends

Git Resource

• Manage source control resources that exist in a git repository

• Functionality for revision and branching control

• Offers both export and syncing abilities

Page 64: Chef, Vagrant and Friends

stage-4https://github.com/benmcrae/ddd-chef-examples/tree/master/stage-4

Page 65: Chef, Vagrant and Friends

RETROSPECTIVE

Page 66: Chef, Vagrant and Friends

Outcomes

• Used Vagrant to run chef-solo and setup our node

• Established our cookbook development workflow

• Finished cookbook

Page 67: Chef, Vagrant and Friends

VAGRANT CONT.

Page 68: Chef, Vagrant and Friends

More Features

• Full support for both Linux and Windows guest’s

• Mount directories using either NFS, SMB or Rsync

• Multi machine environments

• Network support - port forwarding, private networks

Page 69: Chef, Vagrant and Friends

Vagrant Cloud

• Share a link to your web server to a teammate across the country

• Community collection of fully baked box images

• Distribute versionable private environments to your team

Page 70: Chef, Vagrant and Friends

Share / Connect

• Share SSH access to other vagrant users

• Share the whole machine as a local entity to other vagrant users

Page 71: Chef, Vagrant and Friends

Vagrant Plugins

• vagrant plugin install <plugin_name>

• vagrant-sahara (operate in a sandbox environment)

• vagrant-proxy (define http and https proxies)

Page 72: Chef, Vagrant and Friends

Chef Reading• Test-Driven Infrastructure with Chef, 2nd Edition

- Steven Nelson-Smith

• Chef Infrastructure Automation Cookbook - Matthias Marschall

• Learning Chef (Released Sep 2014) - Seth Vargo, Mischa Taylor

• https://learnchef.opscode.com/

Page 73: Chef, Vagrant and Friends

Vagrant Reading

• Vagrant: Up and Running - Mitchell Hashimoto

• http://docs.vagrantup.com/

• http://www.vagrantup.com/blog.html

Page 74: Chef, Vagrant and Friends

THANK YOU