Chef 入门(一)

#Chef是什么

官方的介绍是这样的:Automation for Web-Scale IT.Chef delivers fast, scalable, flexible IT automation.简单来说Chef就是IT自动化工具,它把服务器的环境(软件、依赖库、网络等)进行抽象,以特有的配置语法(Ruby语言)对其进行管理,可以自动的进行服务器环境的初始化工作。Chef也是实践DevOpsContinuous Integration的重要工具,同类的工具还有puppet

#为什么需要Chef

Web开发者肯定对以下的情景熟悉:

以上的问题总结为一句话就是环境管理的混乱,或者说根本没有环境管理。环境管理可能是个不那么太引起开发者重视的一项事情,许多人对环境的要求只是能够跑起来,不管通过什么自虐的方式配置起来都没有问题,如果生产环境发生了变更,而开发环境没有及时变更,那么只能骂娘。其实环境管理和代码的版本控制是一样的重要,你能想象没有版本控制的开发方式吗?有人会说:我照着文档手动配置环境也很快,也不会出错啊。如果这样那么你可以关闭当前网页了。

Chef基本上做两件事:

#Chef环境

Chef环境由三个部分组成:Chef server,Workstation, Node(此Node非Node.js)。

三者之间的关系是这样的:

chef-overview

获取Chef Server

为了节省时间和资源,本文使用Enterprise Chef作为自己的Chef Server,先注册账号,然后登录并添加自己的ORGANIZATION,这里就不详细说明了,请自行摸索。

安装Workstation

##Omnibus installer

我们首先安装omnibus installer,Mac或Ubuntu用户执行如下命令:

$ curl -L https://www.opscode.com/chef/install.sh | sudo bash

以上命令会在机器上安装chef-clientRuby语言Knife等一系列工具,安装结束后输入以下命令来确认安装没有问题:

$ chef-client -v
Chef: 11.12.8

chef-client安装完成之后,会在/opt生成如下的目录结构:

/opt
   /chef
      /bin
      /embedded
         /bin
         /include
         /lib
         /share
         /ssl

注意:如果我们使用的是自行安装的开源Chef server,那么需要安装git,git安装方法这里不再叙述。

以上完成之后,我们需要获取chef-repo,如果使用的是Enterprise Chef,那么你需要下载Starter Kit

chef-starter-kit

之后拷贝到home目录下:

$ cp chef-repo ~/

获取配置文件

为了能够与Chef server通讯,我们需要登录到Enterprise Chef上获取三个配置文件:

chef-get-knife.rb

chef-get-validator.pem

chef-get-USER.pem

获取到以上三个文件之后,我们需要把它们拷贝到~/chef-repo/.chef目录下:

$ cp /path/to/knife.rb /path/to/ORGANIZATION-validator.pem /path/to/USERNAME.pem ~/chef-repo/.chef

把Ruby添加到$PATH

由于Chef中的工具都是基于Ruby语言的,所以chef-client中包含了一个稳定版本的Ruby解释器,你需要把它添加到$PATH中,以便通过命令行可以执行Ruby脚本。

$ echo 'export PATH="/opt/chef/embedded/bin:$PATH"' >> ~/.bash_profile && source ~/.bash_profile

验证chef-client

如果通过Knife可以和Chef server通信,那么代表Workstation安装成功,接下来需要验证。

  1. 进入chef-repo

    $ cd ~/chef-repo

  2. 执行以下命令

    $ knife client list

如果执行成功会返回一个可以访问Chef server的client列表。比如:

workstation
registered_node

安装Node

一个Node可以是任何可以连接上网络的电脑,你必须是它的管理员,并且拥有sudo或者root权限。官方推荐CentOS 6+,Windows Server 2008+,或者Ubuntu 12.04+。你还需要拥有它的SSH账号、密码以及端口。

虽然你可以自己折腾物理机器,不过如果只是想看看Chef有什么功能的话,Chef官方提供了一些配置好的虚拟机,君请自取:

如果你想在自己的本地折腾,那么Chef官方也提供了Vagrant方案:在你之前下载的Starter Kit中已经包含了Vagrantfile文件:

$ ll ~/chef-repo
-rw-r--r--  1 weizhifeng  staff       2405 Jul 18 06:26 README.md
-rw-r--r--  1 weizhifeng  staff       3747 Jul 18 16:24 Vagrantfile
drwxr-xr-x  4 weizhifeng  staff        136 Jul 18 06:26 cookbooks
drwxr-xr-x  3 weizhifeng  staff        102 Jul 18 06:26 roles

你可以直接执行

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 8080 => 9090 (adapter 1)
    default: 22 => 2200 (adapter 1)

来启动VM。

接下来我们需要执行knife bootstrap命令,这个命令会在VM上安装chef-client,并且chef-client会与Chef server进行交互。

$ knife bootstrap localhost --sudo -x vagrant -P vagrant --ssh-port 2222 -N node1	
Connecting to localhost
localhost Starting first Chef Client run...
....
....
localhost Starting Chef Client, version 11.12.8
localhost resolving cookbooks for run list: []
localhost Synchronizing Cookbooks:
localhost Compiling Cookbooks...
localhost [2014-07-19T13:50:55+00:00] WARN: Node node1 has an empty run list.
localhost Converging 0 resources
localhost 
localhost Running handlers:
localhost Running handlers complete
localhost 
localhost Chef Client finished, 0/0 resources updated in 76.501062463 seconds

注意:如果你运行了N个Vagrant VM,那么--ssh-port 2222可能不正确,你可以在vagrant up输出的日志中找到正确的网络转发端口。

为了确认Node是否已经bootstrap成功,访问manage.opscode.com/organizations,在Nodes标签上你应该可以看到刚才bootstrap的node名称。

chef-nodes

恭喜,你现在拥有了一个Chef环境,下一篇文章我们开始介绍Chef怎么使用。

#参考

(完)
comments powered by Disqus
Powered by GitHub  &&  Jekyll | CC BY-NC-SA