Learn Chef Rally 學習筆記 part2 — 安裝及啟動 Apache

Luyo
verybuy-dev
Published in
10 min readJul 22, 2017

本文記錄 Learn Chef Rally 以下章節的學習歷程:

[Configure a package and service]

本章節的開頭寫著:

KEY POINT: Like files, packages and services are also types of resources. Chef applies resources in the order you specify.

前一章節是學習如何設定 file,這一節是學習如何設定 package。

1. 安裝 Apache

首先開一個新檔案 ~/chef-repo/webserver.rb,內容為:

package 'httpd'

對 package 這個 resource 來說預設的行為是 :install, 接著執行 chef-client 指令,跟之前不一樣的是,這邊因為要安裝套件所以需要 sudo 權限去執行:

$ sudo chef-client --local-mode webserver.rb
[2017-07-19T10:48:28+00:00] WARN: No config file found or specified on command line, using command line options.
[2017-07-19T10:48:28+00:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/yo/chef-repo.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks...
[2017-07-19T10:48:30+00:00] WARN: Node ip-172-31-18-184.ap-southeast-1.compute.internal has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/yo/chef-repo/webserver.rb
* yum_package[httpd] action install
- install version 2.4.6-45.el7.centos.4 of package httpd
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 09 seconds

這樣就安裝好 apache server 了!因為是 CentOS/RHEL 系統,可以看到 chef 會直接用 yum 去安裝套件。

檢查一下 apache 的安裝是否有符合預期:

$ ls /etc/httpd/
conf conf.d conf.modules.d logs modules run

很好,apache 跟想象中的一樣被安裝到 /etc/httpd 底下。

再執行一次一樣的 chef-client 指令看看會發生什麼事:

$ sudo chef-client --local-mode webserver.rb
(略)
Recipe: @recipe_files::/home/yo/chef-repo/webserver.rb
* yum_package[httpd] action install (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/1 resources updated in 06 seconds

因為套件是 update to date 狀態,所以不用做任何事情。

2. start 並 enable Apache service

enable 是指開機預設會自動 start 這個 server 的意思。有架過 server 的朋友應該都不陌生,因為沒有辦法精準翻譯就直接寫英文。

修改 webserver.rb

package 'httpd'service 'httpd' do
action [:enable, :start]
end

再次執行 chef-client

$ sudo chef-client --local-mode webserver.rb
(略)
Recipe: @recipe_files::/home/yo/chef-repo/webserver.rb
* yum_package[httpd] action install (up to date)
* service[httpd] action enable
- enable service service[httpd]
* service[httpd] action start
- start service service[httpd]
Running handlers:
Running handlers complete
Chef Client finished, 2/3 resources updated in 03 seconds

看到多了 enable 及 start 的資訊,用 service 指令來檢查一下:

$ service httpd status
Redirecting to /bin/systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 六 2017-07-22 11:33:14 UTC; 3min 19s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 9336 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─9336 /usr/sbin/httpd -DFOREGROUND
├─9337 /usr/sbin/httpd -DFOREGROUND
├─9338 /usr/sbin/httpd -DFOREGROUND
├─9339 /usr/sbin/httpd -DFOREGROUND
├─9340 /usr/sbin/httpd -DFOREGROUND
└─9341 /usr/sbin/httpd -DFOREGROUND

有看到 enabledactive 就沒錯了。

除了 filepackage 以外,現在又多一個 service 的 resource 類型了。

3. 加入網站首頁

現在 apache 安裝好了也跑起來了,我們再用之前學過的 file resource 來加入一個首頁的 html 檔案,一樣打開 webserver.rb 新增 file resource 的內容:

package 'httpd'service 'httpd' do
action [:enable, :start]
end
file '/var/www/html/index.html' do
content '<html>
<body>
<h1>Hello <a href="https://www.verybuy.cc">VeryBuy</a></h1>
</body>
</html>'
end

html 的內容就自由發揮囉。

再來執行一次 chef-client

$ sudo chef-client --local-mode webserver.rb
(略)
Recipe: @recipe_files::/home/yo/chef-repo/webserver.rb
* yum_package[httpd] action install (up to date)
* service[httpd] action enable (up to date)
* service[httpd] action start (up to date)
* file[/var/www/html/index.html] action create
- create new file /var/www/html/index.html
- update content in file /var/www/html/index.html from none to 95172f
--- /var/www/html/index.html 2017-07-22 11:49:46.215923116 +0000
+++ /var/www/html/.chef-index.html20170722-9362-pv4j3k 2017-07-22 11:49:46.214923159 +0000
@@ -1 +1,6 @@
+<html>
+ <body>
+ <h1>Hello <a href="https://www.verybuy.cc">VeryBuy</a></h1>
+ </body>
+</html>
- restore selinux security context
Running handlers:
Running handlers complete

打開瀏覽器,網址列輸入機器 IP 應該就會看到你的首頁囉,爽!

4. 確認網站有跑起來

如果你的機器沒有 public 的話可以用 curl 來測試:

$ curl localhost
<html>
<body>
<h1>Hello <a href="https://www.verybuy.cc">VeryBuy</a></h1>
</body>
</html>

小結

  1. 這個章節我們認識了 package 和 service 這兩種 resource,所以現在總共出現了三種 resource — file, package, 及 service,點擊連結可以看完整的 resource 文件 (但現在應該還看不太懂)。
  2. Chef 會按照 recipe 內的順序去執行動作,所以上面的範例中,會先安裝 package,再 enable 及 start service,最後再加入 file 的檔案內容。其中 enable 及 start 的部分,因為寫的是 [:enable, :start] 所以也會按照同樣的邏輯,先 enable 再 start。

測驗

What does the code package ‘httpd’ mean?

  1. It identifies a package resource.
  2. It tags the system as a web server.
  3. It is incomplete and will cause an error message.

What determines when Chef applies resources?

  1. The order of their precedence.
  2. The order of their attributes.
  3. The order they’re listed in a recipe.

When must you list a resources attributes?

  1. Immediately after the resource name.
  2. Immediately after the actions.
  3. You can list them anywhere within the resource block.

答案是 1, 3, 3。

--

--