Chef - 單機設定



Chef-Solo 是一款開源工具,可在本地執行,並允許使用 Chef Cookbook 配置虛擬機器,而無需複雜的 Chef 客戶端和伺服器配置。它有助於在自行建立的伺服器上執行 Cookbook。

在本地機器上執行 Chef-Solo 之前,需要在本地機器上安裝以下兩個檔案。

  • Solo.rb - 此檔案告訴 Chef 在哪裡可以找到 Cookbook、角色和資料包。

  • Node.json - 此檔案設定執行列表和任何節點特定的屬性(如果需要)。

solo.rb 配置

以下是配置 solo.rb 的步驟。

步驟 1 - 在 Chef 倉庫中建立一個 solo.rb 檔案。

current_dir       = File.expand_path(File.dirname(__FILE__)) 
file_cache_path   "#{current_dir}" 
cookbook_path     "#{current_dir}/cookbooks" 
role_path         "#{current_dir}/roles" 
data_bag_path     "#{current_dir}/data_bags" 

步驟 2 - 將檔案新增到 git 倉庫。

$ git add solo.rb 

步驟 3 - 在 Chef 倉庫中建立一個 node.json 檔案,內容如下。

{ 
   "run_list": [ "recipe[ntp]" ] 
} 

步驟 4 - 使用 knife 獲取 Chef 倉庫中的 ntp cookbook。

vipin@laptop:~/chef-repo $ knife cookbook site install ntp 
Installing ntp to /Users/mma/work/chef-repo/cookbooks 
…TRUNCATED OUTPUT… 
Cookbook ntp version 1.3.0 successfully installed 

步驟 5 - 將 node.json 檔案新增到 Git。

$ git add node.json 

步驟 6 - 提交併將檔案推送到 git 倉庫。

vipin@laptop:~/chef-repo $ git commit -m "initial setup for Chef Solo" 
vipin@laptop:~/chef-repo $ git push 
Counting objects: 4, done. 
Delta compression using up to 4 threads. 
...TRUNCATED OUTPUT... 
To git@github.com:mmarschall/chef-repo.git 
b930647..5bcfab6 master -> master 

在節點上執行 Cookbook

步驟 1 - 登入到要配置 Chef-Solo 的節點。

步驟 2 - 在機器上克隆 Chef 倉庫。

$ git clone $URL_PATH 

步驟 3 - cd 到 Chef 倉庫。

$ cd chef-repo 

最後,執行 Chef-Solo 以使節點收斂 -

$ sudo chef-solo -c solo.rb -j node.json 
[2017-20-08T22:54:13+01:00] INFO: *** Chef 11.0.0 *** 
[2017-20-08T22:54:13+01:00] INFO: Setting the run_list to 
["recipe[ntp]"] from JSON 
...TRUNCATED OUTPUT... 
[2012-12-08T22:54:16+01:00] INFO: Chef Run complete in 2.388374 
seconds 
[2012-12-08T22:54:16+01:00] INFO: Running report handlers 

solo.rb 將 Chef-Solo 配置為在其當前目錄(Chef 倉庫)中查詢其 Cookbook、角色和資料包。

Chef-Solo 從 JSON 檔案中獲取其節點配置。在我們的示例中,我們將其稱為 node.json。如果要管理多個伺服器,則每個節點都需要一個單獨的檔案。然後,Chef-Solo 只會根據在 solo.rb 和 node.json 中找到的配置資料執行 Chef 執行。

廣告

© . All rights reserved.