Chef - 客户端设置

为了使 Chef 节点与 Chef 服务器通信,您需要在节点上设置 Chef 客户端。

Chef 客户端

这是 Chef 节点的关键组件之一,它从 Chef 服务器检索Cookbook(食谱)并在节点上执行它们。它也被称为 Chef 配置器。

在这里,我们将使用 Vagrant 来管理虚拟机。Vagrant 还可以配置 Shell 脚本、Chef 和 Puppet 等配置器,以使虚拟机进入所需状态。在我们的例子中,我们将使用 Vagrant 来管理虚拟机,使用 VirtualBox 和 Chef 客户端作为配置器。

步骤 1 − 从 https://www.virtualbox.org/wiki/downlod

下载并安装 VirtualBox

步骤 2 − 从 http://downloads.vagrantup.com 下载并安装 Vagrant

步骤 3 − 安装 Vagrant Omnibus 插件以使 Vagrant 能够在 VM 上安装 Chef 客户端。

$ vagrant plugin install vagrant-omnibus 

创建和启动虚拟机

步骤 1 − 我们可以从 Opscode vagrant repo 下载所需的 Vagrant 框。从以下 URL 下载 opscode-ubuntu-12.04 框 https://opscode-vmbento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box

步骤 2 − 获得 Vagrant 文件后,下载编辑 Vagrant 文件所需的路径。

vipin@laptop:~/chef-repo $ subl Vagrantfile 
Vagrant.configure("2") do |config| 
   config.vm.box = "opscode-ubuntu-12.04" 
   config.vm.box_url = https://opscode-vm-bento.s3.amazonaws.com/ 
   vagrant/opscode_ubuntu-12.04_provisionerless.box 
   config.omnibus.chef_version = :latest  
   config.vm.provision :chef_client do |chef| 
      chef.provisioning_path = "/etc/chef" 
      chef.chef_server_url = "https://api.opscode.com/ 
      organizations/<YOUR_ORG>" 
      chef.validation_key_path = "/.chef/<YOUR_ORG>-validator.pem"
      chef.validation_client_name = "<YOUR_ORG>-validator" 
      chef.node_name = "server" 
   end 
end 

在上述程序中,您需要使用正确或所需的组织名称更新 <YOUR_ORG> 名称。

步骤 3 − 配置后的下一步是启动 vagrant box。为此,您需要移动到 Vagrant box 所在的位置并运行以下命令。

$ vagrant up

步骤 4 − 机器启动后,您可以使用以下命令登录机器。

$ vagrant ssh

在上述命令中,vagrantfile 是用 Ruby 领域特定语言 (DSL) 编写的,用于配置 vagrant 虚拟机。

在 vagrant 文件中,我们有配置对象。Vagrant 将使用此配置对象来配置 VM。

Vagrant.configure("2") do |config| 
……. 
End

在配置块中,您将告诉 vagrant 使用哪个 VM 映像来启动节点。

config.vm.box = "opscode-ubuntu-12.04" 
config.vm.box_url = https://opscode-vm-bento.s3.amazonaws.com/ 
   vagrant/opscode_ubuntu-12.04_provisionerless.box

在下一步中,您将告诉 Vagrant 下载 omnibus 插件。

config.omnibus.chef_version = :latest

选择要启动的 VM 盒后,配置如何使用 Chef 调配盒。

config.vm.provision :chef_client do |chef|
…..
End

在此步骤中,您需要设置有关如何将虚拟节点连接到 Chef 服务器的说明。您需要告诉 Vagrant 您需要将所有 Chef 内容存储在节点上的位置。

chef.provisioning_path = "/etc/chef"