chef workshop: setup environment with chef,vagrant, and berkshelf

71
Developers’ Workshop No.2 @sakajunquality Chef:101 Introduction to Chef

Upload: jun-sakata

Post on 07-Dec-2014

434 views

Category:

Technology


2 download

DESCRIPTION

How to setup environment with chef (especially local environment)

TRANSCRIPT

Page 1: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Developers’ Workshop No.2@sakajunquality

Chef:101

Introductionto

Chef

Page 2: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Agenda

- Prerequisites- VirtualBox, Vagrant, and the Chef

- Part 1- Just Do It

- Part 2- Quick Insights of Chef

Page 3: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

- Get used to the tools

- Know the basics of Chef

- Being able to use Chef

- Being able to create own Cookbook

Objectives

Page 4: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Before getting started ...

Page 5: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

http://goo.gl/UrfOkghttp://goo.gl/i6X8w5

Everything in this workshop appears in ...

Page 6: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Prerequisites

Page 7: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf
Page 8: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.1. VirtualBox

- Created by Sun microsystems

- Virtual Machine Environment

- Free

Page 9: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf
Page 10: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2. Vagrant

- Tool for building virtual enviroments

- Enhance virtual machine softwaree.g. Virtualbox

- Pluginse.g. sandbox(sahara)

Page 11: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2. Vagrant box

Virtual Machine Image for VagrantAvailable in Multi platform

Page 12: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2. Vagrant Usage

- Remember 5 commands

$ vagrant init # Initaialize Vagrant

$ vagrant up # Boot up

$ vagrant halt # Shutdown

$ vagrant destroy # Reset

$ vagrant ssh # use ssh conection

$ vagrant status # check statsu

Page 13: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

IP address setting- set local IP address for the env.

Synced folder- share local directories and files

Memory- Customize Mem. size of VM

0.2 Vagrant settings

Page 14: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2 Set up Vagrant env. 1

Add Box filehttp://www.vagrantbox.es

$ vagrant box add centos65 [url]this name will be used later

$ vagrant box listYou can list box files

Page 15: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2 Set up Vagrant env. 2

Create Vagrant dir$ mkdir my_saba # at any directory$ cd my_saba

Initialize Vagrant$ vagrant init

Page 16: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim VagrantfileSet Boxfile

Set ipaddress

Set synced folder

0.2 Set up Vagrant env. 3

Page 17: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim Vagrantfile (continue)To customize VM’s spec (like memory size) ...

0.2 Set up Vagrant env. 4

Page 18: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2 Set up Vagrant env. 5

Let’s start up Vagrant env.

$ vagrant up

if you set up synced folder, password will be asked.

Page 19: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2 SSH setting for Vagrant

Set ssh config for the env.

$ vagrant ssh-config --host my_saba >> ~/.ssh/config

Caution: >> != >

Page 20: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.2 SSH setting for Vagrant

In some cases...(depends on the box file or the version for Vagrant)

$ vim ~/.ssh/config

Host my_saba

Host name 127.0.0.1 192.168.96.10 # change here!!!

User vagrant

https://gist.github.com/sakajunquality/0df9d9a6e1cde5d512b7

Page 21: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ ssh my_saba

Check if ssh connection is enabled from dir other than vagrant’s

0.2 SSH connection to Vagrant

Page 22: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Vagrant has Provisioning

Page 23: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf
Page 24: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.3. Chef

- Automation platform for infrastrucrure

- Manage Infrastracture in Codes

- 2 types of Chef- Chef Server- Chef Solo

Page 25: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.3. Chef Server

Chef Server

Server(Chef Client)

Server(Chef Client)

Server(Chef Client)

WorkStation

ChefRepository

Page 26: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.3. Chef Solo

Local PC

Server(Node)

Server(Node)

Server(Node)

ChefRepository

Page 27: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

- Chef Server- Server and Client- Control Client in Server- suit for large-scale context

- Chef Solo- Local PC and Server(Node)- suit for small-scale context

0.3. Chef Server and Chef Solo

Page 28: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

AWS EC2(Amazon Linux)

0.3. Multiplatform

Chef

Sakura VPS(CentOS)

Vagrant(Ubuntu)

AWS EC2(RedHat)

Sakura VPS(Ubuntu)

AWS EC2(Fedora)

Vagrant(Scientific

Linux)

DTI VPS(CentOS)

Page 29: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Ideally Chef run on any platform

Practically depends on Cookbooksor environments...

0.3. Multiplatform

Page 30: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.3. Ruby

everything is made with ruby...

Page 31: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Use Chef Solo

Page 32: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

0.3. Installation of Chef

Gem install$ gem install chef$ gem install knife-solo

Page 33: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Part. 1

Page 34: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ knife solo init first_chef

$ knife solo init [repo_name]

Chef repository ≒ Git repository

(usually maintained as git repo.)

1.1 Create Repository

Page 35: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.2 Create Cookbook

$ knife cookbook create my_cookbook -o site-cookbooks

$ knife cookbook create [cookbook name] -o [place]

*usually user’s cookbooks will be placed in site-cookbooks dir.

Page 36: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ cd ./site-cookbooks/my_cookbook/

# default recipe is generated

$ vim ./recipes/default.rb

1.3 Create Recipe

Page 37: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.3 Let’s set up LAMP env.

$ vim ./recipes/default.rb

package "httpd" do action :installend

package "mysql-server" do action :installend

package "php" do action :installend

https://gist.github.com/sakajunquality/c245d39cdb1cb799db5b

Page 38: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ cd ../../$ vim ./nodes/my_saba.json

{"run_list": [

"recipe[my_cookbook]"]

}

1.3 Let’s set up LAMP env.

Page 39: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ ssh my_saba

$ sudo service iptables stop

* unlike VPS iptables is often enabled, and sometimes this causes chef failure

1.3 Before running

Page 40: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.4 Run Cookbook

($ bundle install # in case of bundler)

Simply run 2 commands

$ knife solo prepare my_saba

$ knife solo cook my_saba -N saba

Page 41: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.5 See what happens

$ ssh my_saba

Let’s see what happened...

$ yum list installed | grep httpd$ yum list installed | grep mysql$ yum list installed | grep php

Page 42: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.5 What my_cookbook did was

Same as run the following commands

$ yum install httpd$ yum install mysql-server$ yum install php

Page 43: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Comunity CookbooksCookbooks mananged in github

Opscode: Major comunity for Cookbookshttps://supermarket.getchef.com

1.6 Community Cookbooks & Berkshelf

Page 44: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

BerkshefTool for managing Cookbooks

- Get Community Cookbooks

- Resolove cookbook’s dependencies

1.6 Community Cookbooks & Berkshelf

Page 45: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.7 Use Community Cookbooks 1

$ vagrant destroy$ gem install berkshelf(or add to Gemfile)

$ vim Berksfilesite :opscode

cookbook 'apache2'cookbook 'php'cookbook 'mysql'

https://gist.github.com/sakajunquality/d423a6cc7b317e16ab5f

Page 46: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim nodes/my_saba2.json

{"run_list": [

"recipe[apache2]","recipe[php]","recipe[mysql::server]"

]}

1.7 Use Community Cookbooks 2

Page 47: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.7 Use Community Cookbooks 3

$ berks install

$ knife solo prepare my_saba

$ knife solo cook mysaba -N my_saba2

Page 48: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

1.7 Merit of Community Cookbooks

- Maintainanced in Social Env.

- Customized for Chef

- More Automated

- More Advanced

Page 49: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Part 1. Summary

$ For now Chef worked

$ Automation? maybe

Page 50: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Part. 2

Page 51: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf
Page 52: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.1 Structure of Chef

Repository (Kitchen)- Instruction and Procedure

Cookbook- Collection of Recipe

Recipe- Procedures of building

Page 53: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.1 Structure of Chef

Chef Repository

Cookbook Cookbook

Recipe

Recipe

Recipe

Recipe

Recipe

Recipe

Page 54: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.2 Insight of Repo (Kitchen)

$ ls -lah first_chef

Page 55: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

nodes / roles- Instruction

site-cookbooks / cookbooks- Cookbooks directory

2.2 Insight of Repo (Kitchen)

Page 56: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Chef Repository

Node Cookbook

Node Recipe

Recipe

Recipe

Role

Node

Role Role

2.2 Insight of Repo (Kitchen)

Page 57: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.2 Insight of Repo (Kitchen)

nodes

roles

cookbookssite-cookbooks

Page 58: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.2 Insight of Repo (Kitchen)

nodes

roles

cookbookssite-cookbooks

make things complicated...

Page 59: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.3 Insight of Cookbook

$ ls -lah site-cookbooks/my_cookbooks

Page 60: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.3 Insight of Cookbook

resipes- directory for recipes

attributes- default attributes for recipes

templates/files/resources- templete for recipes

Page 61: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.2 Insight of Cookbook

recipes

attirbutestemplates

filesresources

Page 62: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ cd ./site-cookbooks/my_cookbook/$ vim ./recipes/make_text.rb

template "/home/vagrant/test_dayo.txt" do source "text.txt.erb" mode "644" variables({ :name => node['text']['name'], :message => node['text']['message'] }) action :createend

2.3 Insight of Recipe 1

https://gist.github.com/sakajunquality/4c8d8bf04914f8c5c9ce

Page 63: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim ./templates/default/text.txt.erb

Hello, <%= name %>You have new message.

<%= message %>

2.3 Insight of Recipe 2

https://gist.github.com/sakajunquality/ddb1496aea99340a5648

Page 64: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim ./attributes/make_text.rb

default['text']['name'] = 'me'default['text']['message'] = 'hello, world'

https://gist.github.com/sakajunquality/4d02c89f3e80aead7eb4

2.3 Insight of Recipe 3

Page 65: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim ../nodes/my_saba3.json

{"run_list": [

"recipe[my_cookbook::make_text]"]

}

→run

2.3 Insight of Recipe 4

Page 66: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

$ vim ../nodes/my_saba4.json

{"text" : {

"name" : "hoge hoge hoge", "message" : "testtesttest"

},"run_list": [

"recipe[my_cookbook::make_text]"]

}

→run

2.3 Insight of Recipe 4

Page 67: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

recipesmake_text.rb

attirbutesmake_text.rb

Templatestext.txt.erb

2.3 Insight of Recipe 5

Noderun_list

Page 68: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Resource- chef’s classdirectory, template, package etc.

Action- methods of classcreate, install, start, restart etc.

Attribute- method’s arguments- can be set in attributes or nodes/roles

2.5 Chef terms

Page 69: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

2.5 Resource examples

package "httpd" do action :installend

template "/etc/httpd/conf.d/vhost.conf" do source vhost.conf.erb mode "644" action :createend

directory "/var/www/vhost/abcdefg" do mode "775" user "admin" action :createend

Page 70: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Part 2. Summary

$ Basic Structure Chef- Nodes/Roles/Coobooks

$ Structure of Cookbook- Recipes/Attributes/Resource

Page 71: Chef Workshop: Setup Environment with Chef,Vagrant, and Berkshelf

Have a nice cooking!