introduction to infrastructure as code & automation / introduction to chef

88
Chef Fundamentals by Chef Software, Inc. is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License . Intro to Infrastructure as Code Introduction to Chef All Things Open – October 2014 https://github.com/nathenharvey/ato-2014

Upload: all-things-open

Post on 10-Jul-2015

260 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef Fundamentals by Chef Software, Inc. is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Intro to Infrastructure as Code Introduction to Chef All Things Open – October 2014 https://github.com/nathenharvey/ato-2014

Page 2: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Nathen Harvey • Community Director at Chef • Co-host of the Food Fight Show • Co-organizer of DevOpsDC meetup • Occasional farmer – http://bit.ly/farmer-nathen

• @nathenharvey • [email protected]

Page 3: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Hello! • System Administrator?

Page 4: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Hello! • System Administrator? • Developer?

Page 5: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Hello! • System Administrator? • Developer? • Ruby developer?

Page 6: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Hello! • System Administrator? • Developer? • Ruby developer?

• DevOp?

Page 7: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Hello! • System Administrator? • Developer? • Ruby developer?

• DevOp? • Business Person?

Page 8: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Are you experienced? • Experience with Infrastructure as Code or Configuration Management?

Page 9: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Are you experienced? • Experience with Infrastructure as Code or Configuration Management?

• Experience with Chef?

Page 10: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Which version control system do your use?

Page 11: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Which version control system do your use?

• cp foo foo.bak

Page 12: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Which version control system do your use?

• cp foo foo.bak • cp foo{,.`date +%Y%m%d%H%M`}

Page 13: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Which version control system do your use?

• cp foo foo.bak • cp foo{,.`date +%Y%m%d%H%M`} • cp foo{,.`date +%Y%m%d%H%M`-`$USER`}

Page 14: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Infrastructure as Code

Page 15: Introduction to Infrastructure as Code & Automation / Introduction to Chef

The Sys Admin’s Journey • ssh

Page 16: Introduction to Infrastructure as Code & Automation / Introduction to Chef

The Sys Admin’s Journey • ssh • Store notes in ~/server.txt

Page 17: Introduction to Infrastructure as Code & Automation / Introduction to Chef

The Sys Admin’s Journey • ssh • Store notes in ~/server.txt • Move notes to the wiki

Page 18: Introduction to Infrastructure as Code & Automation / Introduction to Chef

The Sys Admin’s Journey • ssh • Store notes in ~/server.txt • Move notes to the wiki • Write some scripts (setup.sh, fixit.sh, etc.)

Page 19: Introduction to Infrastructure as Code & Automation / Introduction to Chef

The Sys Admin’s Journey • ssh • Store notes in ~/server.txt • Move notes to the wiki • Write some scripts (setup.sh, fixit.sh, etc.) • Golden images and snapshots

Page 20: Introduction to Infrastructure as Code & Automation / Introduction to Chef

The Sys Admin’s Journey • ssh • Store notes in ~/server.txt • Move notes to the wiki • Write some scripts (setup.sh, fixit.sh, etc.) • Golden images and snapshots • Policy-driven configuration management

Page 21: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Benefits of Automation

Page 22: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Dimensions of Scale

Page 23: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Automation Platform • Creates a dependable view of your entire network’s state.

• Can handle complex dependencies among the nodes of your network.

• Is fault tolerant. • Is secure. • Can handle multiple platforms • Can manage cloud resources • Provides a foundation for innovation

Page 24: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Infrastructure as Code • Programmatically provision and configure components

Page 25: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Infrastructure as Code • Treat like any other code base

Page 26: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Infrastructure as Code • Reconstruct business from code repository, data backup, and compute resources

Page 27: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Infrastructure as Code • Programmatically provision and configure components

• Treat like any other code base

• Reconstruct business from code repository, data backup, and compute resources

Page 28: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Policy-based • You capture the policy for your infrastructure in code

• A program ensures each node in your infrastructure complies with the policy

• A control loop keeps the system stable and allows for change when policy is updated

Page 29: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Sample Infrastructure

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

Page 30: Introduction to Infrastructure as Code & Automation / Introduction to Chef

New Compliance Mandate!

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

Page 31: Introduction to Infrastructure as Code & Automation / Introduction to Chef

New Compliance Mandate!

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

•  Move SSH off of port 22 •  Let’s put it on 2022

Page 32: Introduction to Infrastructure as Code & Automation / Introduction to Chef

6 Golden Images to Update

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

1

3

4

5

6

2

/etc/ssh/sshd_config --- a/sshd_config +++ b/sshd_config -Port 22 +Port 2202

Page 33: Introduction to Infrastructure as Code & Automation / Introduction to Chef

12 Instances to replace

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

1

3

2

5 6 7 4

8

10

9

11

12

•  Launch •  Delete •  Repeat •  Typically manually

Page 34: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Done in maintenance window

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

1

3

2

5 6 7 4

8

10

9

11

12

•  High stakes •  Late hours •  Risky change

Page 35: Introduction to Infrastructure as Code & Automation / Introduction to Chef

New configurations required?

Graphite Nagios

JBoss

Memcache

Postgres Slaves

Postgres Master

Do the new instances have new IP Addresses? * Not all connections shown

Page 36: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef Fast, scalable, flexible IT automation

Page 37: Introduction to Infrastructure as Code & Automation / Introduction to Chef

What is Chef • Open source framework for managing complexity in your infrastructure through policy-driven automation code

• A community of professionals • A company

Page 38: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef

https://www.getchef.com/chef/

Page 39: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef Server – Policy & State

Page 40: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Desired Configuration

Node

Chef Server

chef-client

What policy should I follow?

Page 41: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Desired Configuration

Node

Chef Server

chef-client

What policy should I follow?

"recipe[ntp::client]""recipe[users]"

"role[webserver]"

Page 42: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Desired Configuration

Chef Server

chef-client

What policy should I follow?

"recipe[ntp::client]""recipe[users]"

"role[webserver]"

Page 43: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef Server – Policy & State

Page 44: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA Proxy

Page 45: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA ProxyChef Server

Page 46: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA ProxyChef Server

Webservers?

Page 47: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA ProxyChef Server

Webservers?

Page 48: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA ProxyChef Server

Webservers?

{ "web01" : { "hostname" : "web01", "ipaddress" : "10.1.1.1" }, "web02" : { "hostname" : "web02", "ipaddress" : "10.1.1.2" }, "web03" : { "hostname" : "web03", "ipaddress" : "10.1.1.3" }, "web04" : { "hostname" : "web04", "ipaddress" : "10.1.1.4" }

Page 49: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA ProxyChef Server

Webservers?

{ "web01" : { "hostname" : "web01", "ipaddress" : "10.1.1.1" }, "web02" : { "hostname" : "web02", "ipaddress" : "10.1.1.2" }, "web03" : { "hostname" : "web03", "ipaddress" : "10.1.1.3" }, "web04" : { "hostname" : "web04", "ipaddress" : "10.1.1.4" }

Page 50: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA ProxyChef Server

Webservers?

{ "web01" : { "hostname" : "web01", "ipaddress" : "10.1.1.1" }, "web02" : { "hostname" : "web02", "ipaddress" : "10.1.1.2" }, "web03" : { "hostname" : "web03", "ipaddress" : "10.1.1.3" }, "web04" : { "hostname" : "web04", "ipaddress" : "10.1.1.4" }

pool_members

{ "web01" : { "hostname" : "web01", "ipaddress" : "10.1.1.1" }, "web02" : { "hostname" : "web02", "ipaddress" : "10.1.1.2" }, "web03" : { "hostname" : "web03", "ipaddress" : "10.1.1.3" }, "web04" : { "hostname" : "web04", "ipaddress" : "10.1.1.4" }

Page 51: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA Proxyhaproxy.cfg

server web01 10.1.1.1 weight 1 maxconn 1 checkserver web02 10.1.1.2 weight 1 maxconn 1 checkserver web03 10.1.1.3 weight 1 maxconn 1 checkserver web04 10.1.1.4 weight 1 maxconn 1 check

pool_members

{ "web01" : { "hostname" : "web01", "ipaddress" : "10.1.1.1" }, "web02" : { "hostname" : "web02", "ipaddress" : "10.1.1.2" }, "web03" : { "hostname" : "web03", "ipaddress" : "10.1.1.3" }, "web04" : { "hostname" : "web04", "ipaddress" : "10.1.1.4" }

Page 52: Introduction to Infrastructure as Code & Automation / Introduction to Chef

HA Proxy Configuration

Webservers

HA Proxy

Page 53: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Building your policy Resources and Recipes

Page 54: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Resources • Piece of the system and its desired state • Package that should be installed • Service that should be running • File that should be generated • Cron job that should be configured • User that should be managed • And more

• docs.getchef.com/chef/resources.html

Page 55: Introduction to Infrastructure as Code & Automation / Introduction to Chef

OPEN IN EDITOR:

SAVE FILE!

Hello, Chef!

file "/tmp/hello_chef.txt" do content "Hello, Chef"

mode "0777" end

~/hello_chef.rb

Page 56: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$Recipe: (chef-apply cookbook)::(chef-apply recipe)

* file[/tmp/hello_chef.txt] action create

- create new file /tmp/hello_chef.txt

- update content in file /tmp/hello_chef.txt from none to 79c290

--- /tmp/hello_chef.txt 2014-10-22 19:59:04.000000000 -0400

+++ /tmp/.hello_chef.txt20141022-23075-19aelx1 2014-10-22 19:59:04.000000000 -0400

@@ -1 +1,2 @@

+Hello, Chef

- change mode from '' to '0777'

Apply the policy sudo chef-apply hello_chef.rb

Page 57: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Resources • Describe the desired state • Do not need to tell Chef how to get there

• What happens when you re-apply the policy?

Page 58: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$Recipe: (chef-apply cookbook)::(chef-apply recipe)

* file[/tmp/hello_chef.txt] action create (up to date)

Apply the policy sudo chef-apply hello_chef.rb

Page 59: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Resources • A piece of the system • Its desired state

file "/tmp/hello_chef.txt" do content "Hello, Chef"

mode "0777"

end

Page 60: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$

Change the state of the system echo “Hello, #ato2014” > /tmp/hello_chef.txt

Page 61: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$Recipe: (chef-apply cookbook)::(chef-apply recipe)

* file[/tmp/hello_chef.txt] action create

- update content in file /tmp/hello_chef.txt from e453df to 79c290

--- /tmp/hello_chef.txt 2014-10-22 20:00:20.000000000 -0400

+++ /tmp/.hello_chef.txt20141022-23340-17a7m5t 2014-10-22 20:00:50.000000000 -0400

@@ -1,2 +1,2 @@

-“Hello, #ato2014”

+Hello, Chef

Apply the policy sudo chef-apply hello_chef.rb

Page 62: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Resources – Test and Repair • Resources use a test and repair model

• Resource currently in the desired state? • Yes – Do nothing • No – Bring the resource into the desired state (repair)

Page 63: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Built-in Resources • package • template • service • cron • directory • mount

• user • group • registry_key • remote_directory • route • and many more…

docs.getchef.com/chef/resources.html

Page 64: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Recipes • Policy is defined as a collection of resources in recipes. There are lots of abstractions on top of this but resources are the basic building blocks.

Page 65: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Sample Recipe package "httpd" template "/etc/httpd/conf/httpd.conf" do source "httpd.conf.erb" owner "root" group "root" mode "0644" notifies :restart, "service[httpd]" end service "httpd" do action [:start, :enable] end file "/var/www/html/index.html" do content "Hello, ATO!" end

Page 66: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Test-driven Infrastructure Change policy with confidence

Page 67: Introduction to Infrastructure as Code & Automation / Introduction to Chef

New policy mandate • Apache should listen on port 81, not the default port!

• Verify policy changes BEFORE applying the changes to production

Page 68: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Questions to ask when testing • Did chef-client complete successfully? • Did the recipe put the node in the desired state? • Are the resources properly defined? • Does the code following our style guide?

Page 69: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef client success status • Requirements to verify chef-client success: • A place to store the cookbook artifact

Page 70: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef client success status • Requirements to verify chef-client success: • A place to store the cookbook artifact • A chef-client with access to the cookbook

Page 71: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Chef client success status • Requirements to verify chef-client success: • A place to store the cookbook artifact • A chef-client with access to the cookbook • A target server running the same OS as production

Page 72: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Test Kitchen • Test harness to execute code on one or more platforms

• Driver plugins to allow your code to run on various cloud and virtualization providers

• Includes support for many testing frameworks

• Included with ChefDK

Page 73: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Test Kitchen Lifecycle • kitchen create • kitchen list • kitchen converge • kitchen login • kitchen destroy

Page 74: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Questions to ask when testing ü  Did chef-client complete successfully? • Did the recipe put the node in the desired state? • Are the resources properly defined? • Does the code following our style guide?

Page 75: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$kitchen@localhost's password:

Manually Inspect with kitchen login kitchen login

Page 76: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$kitchen@localhost's password:

Manually Inspect with kitchen login kitchen login

kitchen

Page 77: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$kitchen@localhost's password: Last login: Wed Sep 24 04:30:29 2014 from 172.17.42.1

Manually Inspect with kitchen login kitchen login

kitchen

Page 78: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$

Hello, ATO!

Manually Inspect with kitchen login curl http://localhost

Page 79: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Serverspec • Write RSpec tests to verify your servers • Not dependent on Chef • Defines many resource types • package, service, user, etc.

• Works well with Test Kitchen • http://serverspec.org/

Page 80: Introduction to Infrastructure as Code & Automation / Introduction to Chef

OPEN IN EDITOR:

SAVE FILE!

Serverspec Test

require 'serverspec' set :backend, :exec describe "apahce" do it "has httpd package installed" do expect(package('httpd')).to be_installed end it "is listening on port 80" do expect(port(80)).to be_listening end it "displays our home page" do expect(command("curl http://localhost").stdout).to match /ATO/ end end

test/integration/default/serverspec/default_spec.rb

Page 81: Introduction to Infrastructure as Code & Automation / Introduction to Chef

$-----> Setting up Busser

Creating BUSSER_ROOT in /tmp/busser

Creating busser binstub

Plugin serverspec installed (version 0.2.6)

-----> Running postinstall for serverspec plugin

Finished setting up <default-centos-64> (0m32.59s).

-----> Verifying <default-centos-64>...

Suite path directory /tmp/busser/suites does not exist, skipping.

Uploading /tmp/busser/suites/serverspec/default_spec.rb (mode=0664)

-----> Running serverspec test suite

/opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /tmp/busser/suites/serverspec/default_spec.rb --color --format documentation

apache

is installed

Finished in 0.29547 seconds

1 example, 0 failures

Finished verifying <default-centos-64> (0m4.44s).

-----> Kitchen is finished. (1m25.74s)

Verify the kitchen kitchen verify

Page 82: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Questions to ask when testing ü  Did chef-client complete successfully? ü  Did the recipe put the node in the desired state?

• Are the resources properly defined? • Does the code following our style guide?

Page 83: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Now for our new mandate • Update the tests • Watch them fail • Update the policy • See tests pass • Roll-out changes to production

Page 84: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Wrapping Up

Page 85: Introduction to Infrastructure as Code & Automation / Introduction to Chef

We’ve only scratched the surface

https://www.getchef.com/chef/

Page 86: Introduction to Infrastructure as Code & Automation / Introduction to Chef

Build Anything • Simple internal applications • Complex external applications

• Workstations • Hadoop clusters •  IaaS infrastructure • PaaS infrastructure • SaaS applications • Storage systems • You name it

http://www.flickr.com/photos/hyku/245010680/

Page 87: Introduction to Infrastructure as Code & Automation / Introduction to Chef

And Manage it Simply • Automatically reconfigure everything

• Linux, Windows, Unixes, BSDs

• Load balancers • Metrics collection systems

• Monitoring systems • Cloud migrations become trivial

http://www.flickr.com/photos/helico/404640681/

Page 88: Introduction to Infrastructure as Code & Automation / Introduction to Chef

What questions do you have? • Ask me anything! • @nathenharvey • [email protected] • Thank you!