chef workshop: setup environment with chef,vagrant, and berkshelf
DESCRIPTION
How to setup environment with chef (especially local environment)TRANSCRIPT
Developers’ Workshop No.2@sakajunquality
Chef:101
Introductionto
Chef
Agenda
- Prerequisites- VirtualBox, Vagrant, and the Chef
- Part 1- Just Do It
- Part 2- Quick Insights of Chef
- Get used to the tools
- Know the basics of Chef
- Being able to use Chef
- Being able to create own Cookbook
Objectives
Before getting started ...
http://goo.gl/UrfOkghttp://goo.gl/i6X8w5
Everything in this workshop appears in ...
Prerequisites
0.1. VirtualBox
- Created by Sun microsystems
- Virtual Machine Environment
- Free
0.2. Vagrant
- Tool for building virtual enviroments
- Enhance virtual machine softwaree.g. Virtualbox
- Pluginse.g. sandbox(sahara)
0.2. Vagrant box
Virtual Machine Image for VagrantAvailable in Multi platform
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
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
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
0.2 Set up Vagrant env. 2
Create Vagrant dir$ mkdir my_saba # at any directory$ cd my_saba
Initialize Vagrant$ vagrant init
$ vim VagrantfileSet Boxfile
Set ipaddress
Set synced folder
0.2 Set up Vagrant env. 3
$ vim Vagrantfile (continue)To customize VM’s spec (like memory size) ...
0.2 Set up Vagrant env. 4
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.
0.2 SSH setting for Vagrant
Set ssh config for the env.
$ vagrant ssh-config --host my_saba >> ~/.ssh/config
Caution: >> != >
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
$ ssh my_saba
Check if ssh connection is enabled from dir other than vagrant’s
0.2 SSH connection to Vagrant
Vagrant has Provisioning
0.3. Chef
- Automation platform for infrastrucrure
- Manage Infrastracture in Codes
- 2 types of Chef- Chef Server- Chef Solo
0.3. Chef Server
Chef Server
Server(Chef Client)
Server(Chef Client)
Server(Chef Client)
WorkStation
ChefRepository
0.3. Chef Solo
Local PC
Server(Node)
Server(Node)
Server(Node)
ChefRepository
- 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
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)
Ideally Chef run on any platform
Practically depends on Cookbooksor environments...
0.3. Multiplatform
0.3. Ruby
everything is made with ruby...
Use Chef Solo
0.3. Installation of Chef
Gem install$ gem install chef$ gem install knife-solo
Part. 1
$ knife solo init first_chef
$ knife solo init [repo_name]
Chef repository ≒ Git repository
(usually maintained as git repo.)
1.1 Create Repository
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.
$ cd ./site-cookbooks/my_cookbook/
# default recipe is generated
$ vim ./recipes/default.rb
1.3 Create Recipe
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
$ cd ../../$ vim ./nodes/my_saba.json
{"run_list": [
"recipe[my_cookbook]"]
}
1.3 Let’s set up LAMP env.
$ ssh my_saba
$ sudo service iptables stop
* unlike VPS iptables is often enabled, and sometimes this causes chef failure
1.3 Before running
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
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
1.5 What my_cookbook did was
Same as run the following commands
$ yum install httpd$ yum install mysql-server$ yum install php
Comunity CookbooksCookbooks mananged in github
Opscode: Major comunity for Cookbookshttps://supermarket.getchef.com
1.6 Community Cookbooks & Berkshelf
BerkshefTool for managing Cookbooks
- Get Community Cookbooks
- Resolove cookbook’s dependencies
1.6 Community Cookbooks & 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
$ vim nodes/my_saba2.json
{"run_list": [
"recipe[apache2]","recipe[php]","recipe[mysql::server]"
]}
1.7 Use Community Cookbooks 2
1.7 Use Community Cookbooks 3
$ berks install
$ knife solo prepare my_saba
$ knife solo cook mysaba -N my_saba2
1.7 Merit of Community Cookbooks
- Maintainanced in Social Env.
- Customized for Chef
- More Automated
- More Advanced
Part 1. Summary
$ For now Chef worked
$ Automation? maybe
Part. 2
2.1 Structure of Chef
Repository (Kitchen)- Instruction and Procedure
Cookbook- Collection of Recipe
Recipe- Procedures of building
2.1 Structure of Chef
Chef Repository
Cookbook Cookbook
Recipe
Recipe
Recipe
Recipe
Recipe
Recipe
2.2 Insight of Repo (Kitchen)
$ ls -lah first_chef
nodes / roles- Instruction
site-cookbooks / cookbooks- Cookbooks directory
2.2 Insight of Repo (Kitchen)
Chef Repository
Node Cookbook
Node Recipe
Recipe
Recipe
Role
Node
Role Role
2.2 Insight of Repo (Kitchen)
2.2 Insight of Repo (Kitchen)
nodes
roles
cookbookssite-cookbooks
2.2 Insight of Repo (Kitchen)
nodes
roles
cookbookssite-cookbooks
make things complicated...
2.3 Insight of Cookbook
$ ls -lah site-cookbooks/my_cookbooks
2.3 Insight of Cookbook
resipes- directory for recipes
attributes- default attributes for recipes
templates/files/resources- templete for recipes
2.2 Insight of Cookbook
recipes
attirbutestemplates
filesresources
$ 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
$ 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
$ 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
$ vim ../nodes/my_saba3.json
{"run_list": [
"recipe[my_cookbook::make_text]"]
}
→run
2.3 Insight of Recipe 4
$ 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
recipesmake_text.rb
attirbutesmake_text.rb
Templatestext.txt.erb
2.3 Insight of Recipe 5
Noderun_list
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
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
Part 2. Summary
$ Basic Structure Chef- Nodes/Roles/Coobooks
$ Structure of Cookbook- Recipes/Attributes/Resource
Have a nice cooking!