Author: Zhang Hua published in: 2014-03-14
Copyright NOTICE: You can reprint, reprint, please be sure to hyperlink form to indicate the original source and author information and this copyright notice
(http://blog.csdn.net/quqi99)
Chef is a puppet-like scripting tool for rapid deployment of software and its dependencies, and the installation steps are scripted (puppet written in an xml-based specialized grammar, chef written in Ruby), and the client obtains the script from the server side and executes it. Its structure is as follows:
Chef That 's how it works. :
Define how each client should configure itself (i.e., Recipe)on the workstation, and then upload that information to a central server
Each client connects to the hub server to see how to configure itself and then configure itself
Workstation and server, and between client and server, are certified with a PEM, and when a new client is added, You need to copy Validator.pem to the new client from the hub server, and then use this PEM to register your CLIENT.PEM for later authentication concepts: Resource and provider
Resource is what chef provides to you to describe a part of the system you want to configure (in what state), see examples:
Package "Vim" do
action:install
end
This is a resource, and it wants to express the desire to vim install (in the installed state)
It has a resource type (package)
Have a name (VIM)
There may be some optional arguments (not in this case).
There is an action (install) (actually describes a state that is similar to the ensure in puppet and does not go to install every time)
Here package is a resource type, listing a few of the more commonly used resource:
Directory
Execute
Execute "ssh-keygen"
do Command "ssh-keygen-t dsa-f/root/.ssh/id_rsa-n \" "
if file.exists?" /root/.ssh/id_rsa ")
action:nothing
End
File
File "/tmp/something" do
owner "root"
Group "root"
mode "0755"
action:create
content just Test "
End
Group
# Add group cyops and add root to it
Group "Cyops" do
system True
the "root"
end
Package
Script
Service
Service "NTPD" do
Action[:enable,:start]
End
Template, the following upload the Config.conf.erb file on the server to the client, rename it config.conf and make a variable substitution (the variable in the template file is written as: <%= @config_var%>)
Template "/tmp/config.conf" do
source "Config.conf.erb"
variables (
: Config_var => node[:configs][ : Config_var]
) End
to find template files in order:
.../template/host-client1.chefdemo.com /config.conf.erb ...
/template/centos-6.5/config.conf.erb ...
/template/centos/config.conf.erb
.../template/default/config.conf.erb
User
User "random" do
comment "Random user"
UID 1000
gid "users" home
"/home/random"
Shell "/bin/zsh"
action "Create" # Create is the default action, so you can omit this line
end
The concept of Provider may be more abstract, like the resource example above, the reason we don't care how Vim is installed (Apt,yum ...) is because there is Provider that is to say Provider is responsible for the abstraction of the Resource to the actual command ( As the example above may be : Yum-y install vim) concept: Recipe
Simply put a number of Resource written together is Recipe, the client will Recipe inside the resouce in order (important) The application of one article to itself:
It's a combination of resource.
Apply sequentially
Can contain other recipe, example: Include_recipe "NTP::d efault" Concept: node and role
Role can be used to describe how a server wants to be configured ( configured as a Web server , MySQL server , even a forum )
It has a run_list that contains the recipe and role that is required to configure a server to look like this (role can contain role)
Node very well understood. , each one was Chef Managed Servers (Run chef-client) is a node
Here's an example to help understand that there are two recipe:ntp::d efault and MySQL::d efault
Package "NTP"
does action [: Install] End
# The following resource is referred to as: Install NTP resource
service "NTPD"
do Action[:enable,:start] End
# This resource is referred to as: Start NTP resource
package "Mysql-server" do
action: Install End
# behind this resource is referred to as: Install Mysql-server resource
service "Mysql-server" do
Action:start
We create a role called Ntp_and_mysql and add these two recipe to the inside, and the corresponding commands are
# Knife Role Create Ntp_and_mysql
This command will use VIM to open a file for you to edit this role, modify it to do so and then save the exit,
{
"override_attributes": {
},
"Chef_type": "Role",
"env_run_lists": {
},
"Json_class": " Chef::role ",
" name ":" Ntp_and_mysql ",
" run_list ": [
" RECIPE[NTP::d efault] ",
" Recipe[mysql:: Default]
, "
default_attributes": {
},
"description": ""
}
And then apply this role to a node (in fact, add the recipe of this role runlist to node Runlist)
# Knife Node Run list add client1.chefdemo.com ' role[ntp_and_mysql] '
Finally client1.chefdemo.com this node will expand it to 4 resource (in order)
Installation of NTP resource
Resource to start NTP
Installation of Mysql-server Resource
Start the Mysql-server resource
Then the provider turns it into the corresponding command, and the last node has to do is:
Installing NTP
Start NTP
Install Mysql-server
Start Mysql-server concept: Cookbook
Cookbook is actually Recipe and so some things to pack , like the previous NTP::d EFAULT,NTP is a cookbook
Cookbook's directory structure looks like this
tree/var/chef/cookbooks/ntp/
/var/chef/cookbooks/ntp/
├──attributes
├──definitions
├──files
│ └──default
├──libraries
├──metadata.rb
├──providers
├──readme.md
├── Recipes
│ ├──default.rb
│ └──ntp.rb
├──resources
└──templates
└──default
└──ntp.conf.erb
directories, 5 files
a command to generate a cookbook directory structure: Rake New_cookbook cookbook=test
Concept: Databag
Because the recipe that created the user used Databag, so here's a quick one.
Data Bag provides a way to define global information , see examples directly
First we create a data Bag
# Knife Data Bag Create admin
This command creates a databag on the chef-server that can store information inside
Mkdir-p/var/chef/data_bags/admin
vim/var/chef/data-bags/admin/quqi.json
{
"id": "Quqi",
"shell" : "/bin/bash",
"comment": "Quqi",
"action": "Create",
}
and upload it to the service side:
Cd/var/chef
Knife Data bag from File admin Quqi.json
There are two ways to access this information in recipe now: Data_bag and Data_bag_item
Data_bag
Admin user has Quqi.json this data file that Data_bag (' admin ') equals ["Quqi"]
Data_bag_item
Concept: Attribute
Property (Attributes) is the node (Node) information , such as IP address, hostname, loaded kernel module, the version of the programming language available in the system, and more. New properties can be added to the node in many ways.
There are four types of properties, arranged in order of precedence from highest to lowest, which are:
Automatic
Override
Normal
Default
The cookbook properties file can be found in the Attributes subdirectory of cookbook. They operate in the context of the node object and use node's method to set the value of the property:
default["Apache" ["dir"] = "/etc/apache2"
The use of the node object here is implied, and the following is equivalent to the above:
node.default["Apache" ["dir"] = "/etc/apache2"
Concept: LWRP
LWRP (Lightweightresources and Providers), customizing resource and provider. Install OpenStack with chef Environment Preparation </