Download - Cooking with Chef, Knife, Vagrant and Co

Transcript
Page 1: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Torben Knerr

Cooking with Chef, Knife, Vagrant & Co 4 Ingredients for tasty “Infrastructure-As-Code”

27. September 2013 Slide 1

Page 2: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

With typical components:

• Application Servers

• Database Servers

• Caching Servers

• Load Balancer / Reverse Proxy

• Clustering & Failover

A Typical Web App Project…

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

nginx

Tomcat Tomcat

DB Master

DB Slave

App App

memcached

27. September 2013 Slide 2

Page 3: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

With a typical Deployment Pipeline:

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

Local DEV TEST PROD

A Typical Web App Project…

27. September 2013 Slide 3

Page 4: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

With typical (?) problems:

• All environments are somehow different

• Nobody knows the differences in detail

• Setting up a new environment takes weeks

• And again it looks different from the others

• Totally no Dev/Prod Parity

• And last but not least…

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

A Typical Web App Project…

27. September 2013 Slide 5

Page 5: Cooking with Chef, Knife, Vagrant and Co
Page 6: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

First Approach: Hey, let’s build VMs!

This already helps:

• Use VMs for a consistent work environment

• Manually set up a local developer VM with Tomcat, PostgreSQL, nginx, etc…

• Carefully hand-crafted with love!

• Distribute it to the team

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 7

Page 7: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

But…

• Manual management of VMs is cumbersome

• How do you version a 40GB VM?

• How do you take care of updates?

• Can you diff the changes?

• Once it works locally, how can you apply this to the rest of the deployment pipeline?

• How do you parameterize such VMs?

Better than nothing, but still far from perfect!

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 8

First Approach: Hey, let’s build VMs!

Page 8: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Why Scripting / Automation?

• Lightweight scripts vs. fat VMs – Allows for proper versioning and diffing! – Easy to share

• Infrastructure can be reliably recreated – Minimize manual (= boring & error prone) tasks – Basis for “clean” infrastructure deployments – Anywhere in the deployment pipeline

• Explicit design of infrastructure – Described as an executable specification – Not formed “by accident”

Treat your infrastructure like code!

Better Solution: Automate your VMs!

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 9

Page 9: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

Welcome to the DevOps Kitchen! Infrastructure á la Carte

27. September 2013 Slide 10

Page 10: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Goal: set up a simple server infrastructure, fully automated

• For example on a developer machine

• Separate VMs for app server and database server

• Anywhere reproducible („clean setup“)

Our Example Scenario

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

App Server VM

Tomcat

Database Server VM

PostgreSQL

.war

27. September 2013 Slide 11

Page 11: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

Ingredient #1 We need VMs

27. September 2013 Slide 12

Page 12: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #1: we need VMs Enter Vagrant What is Vagrant?

• Vagrant is “automation for VMs”

• Different VM providers: VirtualBox, aws, rackspace, etc..

• The configuration of VMs is described in a Vagrantfile

• Simple commands for interacting with VMs: – vagrant up – vagrant ssh – vagrant halt – …

• http://vagrantup.com

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 13

Page 13: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – A simple Vagrantfile

Page 14: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – A simple Vagrantfile

Basebox

Page 15: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – A simple Vagrantfile

App Server VM

Page 16: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – A simple Vagrantfile

Database Server VM

Page 17: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – Interacting with Vagrant VMs

vagrant up

Page 18: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – Interacting with Vagrant VMs

vagrant ssh

Page 19: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #1: we need VMs A quick look at the VirtualBox GUI

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

Is this the App Server VM? Or the Database Server VM?

27. September 2013 Slide 20

Page 20: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – More VM configuration

Page 21: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – More VM configuration

Page 22: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – More VM configuration

Name, CPUs, RAM, etc…

Page 23: Cooking with Chef, Knife, Vagrant and Co

Ingredient #1: we need VMs – More VM configuration

vagrant reload

Page 24: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #1: we need VMs Looking at the VirtualBox GUI again

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

and the VM Parameters are applied too

Better names

27. September 2013 Slide 25

Page 25: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

Ingredient #2: Provisioning

27. September 2013 Slide 26

Page 26: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #2: Provisioning Back to our Example Scenario

“Naked” VMs are not enough

• On the database server VM we need PostgreSQL

• On the app server VM we need Tomcat

We need a tool (“provisioner”) for installing and configuring software components on top of naked VMs

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 27

Page 27: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #2: Provisioning Enter Chef

What is Chef?

• Chef is a “Configuration Management” system

• Installs & configures software on nodes

• Provides a DSL for describing the configuration of a node in a readable and platform-independent way

More information:

• http://www.opscode.com/chef/

Vagrant supports provisioning of VMs via Chef out-of-the-box!

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 28

Page 28: Cooking with Chef, Knife, Vagrant and Co

Ingredient #2: Provisioning – Installing PostgreSQL

Page 29: Cooking with Chef, Knife, Vagrant and Co

Ingredient #2: Provisioning – Installing PostgreSQL

run the PostgreSQL Server recipe

Page 30: Cooking with Chef, Knife, Vagrant and Co

Ingredient #2: Provisioning – Installing PostgreSQL

configuration parameters

Page 31: Cooking with Chef, Knife, Vagrant and Co

vagrant up db

Page 32: Cooking with Chef, Knife, Vagrant and Co

What’s inside the PostgreSQL Recipe?

Page 33: Cooking with Chef, Knife, Vagrant and Co
Page 34: Cooking with Chef, Knife, Vagrant and Co

install the “postgresql“ package

Page 35: Cooking with Chef, Knife, Vagrant and Co

create the config file

Page 36: Cooking with Chef, Knife, Vagrant and Co

Erzeuge die Konfig-Datei

Page 37: Cooking with Chef, Knife, Vagrant and Co

create another config file

Page 38: Cooking with Chef, Knife, Vagrant and Co

start the „postgresql“ service

Page 39: Cooking with Chef, Knife, Vagrant and Co

we also need the postgresql client…

Page 40: Cooking with Chef, Knife, Vagrant and Co
Page 41: Cooking with Chef, Knife, Vagrant and Co

Ingredient #2: Provisioning – Installing Tomcat

Page 42: Cooking with Chef, Knife, Vagrant and Co

Ingredient #2: Provisioning – Installing Tomcat

run the Tomcat default recipe (short for “tomcat::default“)

Page 43: Cooking with Chef, Knife, Vagrant and Co

Ingredient #2: Provisioning – Installing Tomcat

tomcat specific configuration

Page 44: Cooking with Chef, Knife, Vagrant and Co

vagrant up web

Page 45: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Chef Philosophy

Specification of the target state

• Idempotence

• Convergence (“self-healing“)

Chef DSL abstracts the OS-specific details via

• Resources

• Providers

Naming: kitchen metaphor

• Chef, Cookbook, Recipe, Knife, …

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 46

Page 46: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

Ingredient #3: Managing Cookbooks

27. September 2013 Slide 47

Page 47: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #3: Managing Cookbooks Back to our Example Scenario

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

where is the PostgreSQL cookbook / recipe coming from???

27. September 2013 Slide 48

Page 48: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #3: Managing Cookbooks Typical Sources for “Community Cookbooks”

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

http://community.opscode.com/cookbooks/

https://github.com/search?q=cookbook

27. September 2013 Slide 49

Page 49: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #3: Managing Cookbooks Enter Librarian

What is Librarian?

• Librarian is a Dependency Management tool for Cookbooks

• Downloads and extracts cookbooks for you

Dependencies are specified in a Cheffile

• Transitive dependencies are resolved as well

• Different cookbook sources and version constraints are respected

More information:

• https://github.com/applicationsonline/librarian-chef

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 50

Page 50: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #3: Managing Cookbooks A simple Cheffile

Cheffile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 51

Page 51: Cooking with Chef, Knife, Vagrant and Co

Ingredient #3: Managing Cookbooks – A simple Cheffile

Librarian „installs“ the cookbooks

Page 52: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

Ingredient #4: Application Cookbooks

27. September 2013 Slide 53

Page 53: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Back to our Example Scenario

Re-using Community Cookbooks is not enough

• We need a specific database and database user for our application

• We need to deploy our application’s .war file in Tomcat

• Existing Community Cookbooks don’t “know” our specific requirements!

We need to write our own “Application Cookbook” for this purpose!

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 54

Page 54: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Did you know? You can create cookbooks with a knife!

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

Ingredient #4: Application Cookbooks Enter Knife

27. September 2013 Slide 55

Page 55: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Structure of our “myapp” cookbook

Single cookbook with separate recipes for setting up the database and webapp

Goal: hide implementation details from the user

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

myapp

attributes

recipes

db.rb

web.rb

db.rb

default.rb

web.rb

27. September 2013 Slide 56

Page 56: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Structure of our “myapp” cookbook

Attributes files expose the “configuration parameters” of a cookbook and supply sensible defaults

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

myapp

attributes

recipes

db.rb

web.rb

db.rb

default.rb

web.rb

27. September 2013 Slide 57

Page 57: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Structure of our “myapp” cookbook

• myapp/attributes/db.rb

• myapp/attributes/web.rb

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 58

Page 58: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Structure of our “myapp” cookbook

The “db” recipe first installs PostgreSQL, then creates the database and database user

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

myapp

attributes

recipes

db.rb

web.rb

db.rb

default.rb

web.rb

27. September 2013 Slide 59

Page 59: Cooking with Chef, Knife, Vagrant and Co
Page 60: Cooking with Chef, Knife, Vagrant and Co

first install the PostgreSQL server

Page 61: Cooking with Chef, Knife, Vagrant and Co

installs prerequisites for interacting with PostgreSQL from within this recipe

Page 62: Cooking with Chef, Knife, Vagrant and Co

creates the database user

Page 63: Cooking with Chef, Knife, Vagrant and Co

creates the database

Page 64: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Use “myapp::db” in the Vagrantfile

Vagrantfile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 65

Page 65: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Use “myapp::db” in the Vagrantfile

Vagrantfile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

instead of “postgresql::server”

27. September 2013 Slide 66

Page 66: Cooking with Chef, Knife, Vagrant and Co

vagrant provision db

Page 67: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Extended Cheffile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 68

Page 68: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Structure of our “myapp” cookbook

The “web” recipe first installs tomcat, then configures and deploys the our web app

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

myapp

attributes

recipes

db.rb

web.rb

db.rb

default.rb

web.rb

27. September 2013 Slide 69

Page 69: Cooking with Chef, Knife, Vagrant and Co
Page 70: Cooking with Chef, Knife, Vagrant and Co

we need to install Tomcat first

Page 71: Cooking with Chef, Knife, Vagrant and Co

there is our .war file located

Page 72: Cooking with Chef, Knife, Vagrant and Co

database configuration (via context.xml)

Page 73: Cooking with Chef, Knife, Vagrant and Co

deploy the webapp to Tomcat

Page 74: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Use “myapp::web” in the Vagrantfile

Vagrantfile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 75

Page 75: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Use “myapp::web” in the Vagrantfile

Vagrantfile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

instead of the “tomcat::default“ recipe

27. September 2013 Slide 76

Page 76: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Ingredient #4: Application Cookbooks Use “myapp::web” in the Vagrantfile

Vagrantfile

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

C connect to this database host (here: ip address of the db vm)

27. September 2013 Slide 77

Page 77: Cooking with Chef, Knife, Vagrant and Co

vagrant provision web

Page 78: Cooking with Chef, Knife, Vagrant and Co

Is it running now?

Page 79: Cooking with Chef, Knife, Vagrant and Co

Or whatever is in your context… (we have borrowed the “yabe“ example from Play! here: http://www.playframework.org/documentation/1.0/samples)

Page 80: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

The DevOps Kitchen – Recap

27. September 2013 Slide 81

Page 81: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Recap: Ingredients for cooking in the DevOps Kitchen

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr

Ingr. #1 We need VMs

Ingr. #2 Provisioning

Ingr. #3 Cookbook Management

Ingr. #4 Application Cookbooks

+

+

+

27. September 2013 Slide 82

Page 82: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Fully Automated Infrastructure via Textual Specification

• No shell scripts, but specification of the target state via Chef DSL

• Crisp and concise, but not cryptic

• SCM-friendly: Kilobytes instead of Gigabytes, diffable

• Complex infrastructure available at the push-of-a-button

• Reproducible anywhere in the deployment pipeline

• Good software engineering principles applied to infrastructure

What have we achieved?

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 83

Page 83: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

We have barely touched the surface…

There’s much more of Chef, Vagrant & Co we have not seen today:

• More Vagrant features – Alternative Providers (e.g. vmware, aws, rackspace, managed-servers, etc…) – Alternative Provisioners (e.g. puppet, shell, ansible, salt, etc…) – Vagrant Plugins (e.g. vagrant-omnibus, vagrant-berkshelf, vagrant-cachier, …) – …

• More Chef concepts – Writing Custom Resources und Providers (LWRPs) – Data bags – Environments – Search – Chef Solo vs. Chef Server – …

• And most importantly…

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 84

Page 84: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013 Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 85

…if we treat infrastructure as code, then we have to test!

Linting

• foodcritic

Unit-Level Testing

• chefspec

• fauxhai

Integration Testing (from the inside)

• chef-minitest-handler

• test-kitchen

Acceptance Testing (outside-in)

• cucumber-nagios

Page 85: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Resources Some useful stuff to check out… • Code Examples from this talk

https://github.com/tknerr/oop-chef-demo

• Newer Example based on Vagrant / Chef / Berkshelf https://github.com/tknerr/sample-application-cookbook

• Bills Kitchen – all you need for cooking with Chef & Vagrant on Windows https://github.com/tknerr/bills-kitchen https://ikm.zuehlke.com/topics/Pages/Bill%27s%20Kitchen.aspx

• “Chef, Vagrant & Co” Yammer Group https://www.yammer.com/zuehlke.com/#/threads/inGroup?type=in_group&feedId=2051695

• “Test-Driven Infrastructure with Chef (2nd ed.)” – highly recommended! http://shop.oreilly.com/product/0636920030973.do

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 86

Page 86: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Resources More of the Vagrant Ecosystem… • Bindler – Manage your Vagrant Plugins via `plugins.json`

https://github.com/fgrehm/bindler

• Selection of useful Vagrant plugins https://github.com/schisamo/vagrant-omnibus – installs Chef on a “provisionerless” basebox https://github.com/riotgames/vagrant-berkshelf – resolves cookbook dependencies on `vagrant up` https://github.com/fgrehm/vagrant-cachier – transparently caches downloaded packages https://github.com/tmatilai/vagrant-proxyconf – sets the proxy inside the VM …

• Selection of Vagrant Providers https://github.com/mitchellh/vagrant-aws – provision EC2 instances in the AWS cloud with Vagrant https://github.com/tknerr/vagrant-managed-servers – provision “non-VMs” with Vagrant https://github.com/fgrehm/vagrant-lxc - provision LXC containers …

Cooking with Chef, Knife, Vagrant & Co | Torben Knerr 27. September 2013 Slide 87

Page 87: Cooking with Chef, Knife, Vagrant and Co

© Zühlke 2013

Cooking with Chef, Knife, Vagrant & Co

Torben Knerr

Discussion Time!

20. Juli 2012

Puppet, Berkshelf, Foo, Bar, Baz

DevOps, NoOps, Blablabla

Vagrant, Chef, Deployment Stuff

Holy Cloud! Slide 88