Chef入门(二)
网址: http://it.taocms.org/07/4103.htm.html
上一篇文章Chef入门(一)我们介绍了Chef是什么、以及如何搭建Chef环境。这篇文章,我们介绍一下Chef中的一些概念以及如何编写cookbook,并在此过程中部署一个Apache环境,通过本文你可以学到:
- 创建一个cookbook,并在其中添加recipe
- 上传你的cookbook到Chef server
- 配置node的run list
- 在你的目标node上运行chef-client,执行cookbook中定义的操作。
一些概念
cookbook
cookbook是配置和策略的集合单元,它定义了一个场景,比如部署Apache环境。一个cookbook包含了很多组件来支持实现这个场景,以下是一个cookbook的目录:
-rw-r--r-- 1 weizhifeng staff 495 Jul 21 11:01 CHANGELOG.md
-rw-r--r--@ 1 weizhifeng staff 1536 Jul 21 11:01 README.md
drwxr-xr-x 2 weizhifeng staff 68 Jul 21 11:01 attributes
drwxr-xr-x 2 weizhifeng staff 68 Jul 21 11:01 definitions
drwxr-xr-x 3 weizhifeng staff 102 Jul 21 11:01 files
drwxr-xr-x 2 weizhifeng staff 68 Jul 21 11:01 libraries
-rw-r--r--@ 1 weizhifeng staff 298 Jul 21 11:01 metadata.rb
drwxr-xr-x 2 weizhifeng staff 68 Jul 21 11:01 providers
drwxr-xr-x 3 weizhifeng staff 102 Jul 26 11:05 recipes
drwxr-xr-x 2 weizhifeng staff 68 Jul 21 11:01 resources
drwxr-xr-x 3 weizhifeng staff 102 Jul 21 11:01 templates
- attributes:用来设置node的属性
- definitions:用来创建可以重用的resource集合
- files:一些需要的文件
- libraries:用来扩展chef-client或者添加一些helper到Ruby中
- metadata.rb:包含一些元数据,比如cookbook的名字、版本、支持的平台等等
- recipes:存储recipe,每个recipe指定了需要的resource以及这些resource执行的顺序
- resources:存储自定义的resource
- providers:存储自定义的provider
- templates:存储ruby模板语言描述的文件,用来解决复杂的配置场景
recipe
每个cookbook都会包含一到多个recipe(默认是default.rb)。一个recipe就是实现cookbook所描述场景的步骤。看以下这个简单的recipe:
package 'apache2' do
action :install
end
service 'apache2' do
action [ :enable, :start ]
end
cookbook_file '/var/www/index.html' do
source 'index.html'
mode '0644'
end
可以看出这个recipe分为三个步骤,分别是安装apache2、启动apache2、拷贝文件。
resource和provider
resource就是recipe中的配置项,可以是package、service、bash等等。provider就是为这些resource提供实现的程序。以编程语言来描述的话,resource定义了接口,provider提供了不同平台的实现。
实战
说了那么多概念,我们接下来在ubuntu上安装并配置Apache。
第一步:创建cookbook
在workstation中,我们通过knife来和Chef server进行交互。执行以下命令来创建一个cookbook。
$ cd ~/chef-repo
$ knife cookbook create apache-tutorial-1
执行完成之后,在~/chef-repo/cookbooks
目录下会生成名为apache-tutorial-1
的cookbook。不过cookbook都是在本地的,还没有上传到Chef server。
第二步:编写recipe
当你创建了一个cookbook,Chef会帮你创建一个默认的recipe。用你的编辑器打开~/chef-repo/cookbooks/apache-tutorial-1/recipes/default.rb
。现在让我们来写一些ruby代码来执行以下的动作:
- 安装Apache
- 启动Apache并且添加到开机启动
- 配置home page
把以下代码添加到recipe中。
package 'apache2' do
action :install
end
service 'apache2' do
action [ :enable, :start ]
end
cookbook_file '/var/www/index.html' do
source 'index.html'
mode '0644'
end
第三步:添加文件资源
recipe的最后一步我们使用了cookbook_file来拷贝home page。把以下内容添加到文件~/chef-repo/cookbooks/apache-tutorial-1/files/default/index.html
中。
<html>
<body>
<h1>Hello, world!</h1>
</body>
</html>
第四步:上传cookbook
在你的chef-repo目录中执行以下命令来上传cookbook。
$ knife cookbook upload apache-tutorial-1
第五步:创建run list
run list定义了recipe的执行顺序,当前的例子中我们的run list中只有一个recipe。打开manage.opscode.com,我们来配置run list。
然后从Available Recipes
中拖拽recipe到Current Run List
之中。然后点击Save Run List
。
第六步:运行chef-client
接下来你需要执行chef-client来从Chef server获取最新的cookbook,并且在目标node上执行。你可以自己登录到node上,然后手动执行。
$ ssh chef@your.host
$ ssh sudo chef-client
或者使用knife。
$ knife ssh your.host 'sudo chef-client' -m -x chef -P chef
-x
表示username,-P
表示password。
如果你使用的是vagrant,则执行如下命令。
$ knife ssh localhost 'sudo chef-client' -m -x vagrant -P vagrant --ssh-port 2200
Starting Chef Client, version 11.12.8
resolving cookbooks for run list: ["apache-tutorial-1"]
Synchronizing Cookbooks:
- apache-tutorial-1
Compiling Cookbooks...
Converging 3 resources
Recipe: apache-tutorial-1::default
* package[apache2] action install (up to date)
* service[apache2] action enable (up to date)
* service[apache2] action start (up to date)
* cookbook_file[/var/www/index.html] action create (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/4 resources updated in 19.554803129 seconds
等chef-client执行完成之后,你会发现Apache已经配置好并且运行了,并且把home page拷贝到了/var/www/index.html
,访问80端口应该可以看到home page的输出。
至此我们已经创建了自己的cookbook,并在目标node上执行,更多的内容可以查看官方文档。
参考
- http://docs.getchef.com/chef_overview_cookbooks.html
- http://learn.getchef.com/legacy/tutorials/create-your-first-cookbook/
- http://docs.getchef.com/chef/resources.html
网友评论已有0条评论, 我也要评论