cloud-init 是 linux 的一個工具,當系統啓動時,cloud-init 可從 nova metadata 服務或者 config drive 中獲取 metadata,完成包括但不限於下面的定製化工做:html
設置 default localelinux
設置 hostnamecentos
添加 ssh keys到 .ssh/authorized_keys網絡
設置用戶密碼ssh
配置網絡工具
安裝軟件包spa
爲了實現 instance 定製工做,cloud-init 會按 4 個階段執行任務:code
localhtm
initip
config
final
cloud-init 安裝時會將這 4 個階段執行的任務以服務的形式註冊到系統中,好比在 systemd 的環境下,咱們可以看到這4個階段分別對應的服務:
local - cloud-init-local.service
init - cloud-init.service
config - cloud-config.service
final - cloud-final.service
做爲 cloud-init 執行的第一個階段,此時 instance 還不知道該如何配置網卡,cloud-init 的任務就是從 config drive 中獲取配置信息,而後寫入 /etc/network/interfaces 文件(若是是 centos 則寫入 /etc/sysconfig/network-scripts/ifcfg-xxx)。
若是沒有 config drive,則將全部網卡配置成 dhcp 模式。這是很是關鍵的一步,只有當網卡正確配置後,才能獲取到 metadata。
關於 local 階段下一節會經過實驗詳細分析。
正常狀況下,在這三個階段執行以前 instance 網絡已經配置好了,而且已經成功獲取到 metadata。cloud-init 的配置文件 /etc/cloud/cloud.cfg 定義了三個階段分別要執行的任務,任務以 module 形式指定。
instance 真正的定製工做就是由這些 module 完成的。module 決定作哪些定製化工做,而 metadata 則決定最終定製化的結果。
舉個例子,若是 cloud.cfg 中指定了 set_hostname
這個 module,則意味着 cloud-int 會設置 instance 的主機名,而具體設置成哪一個主機名則由 metadata 中 hostname
參數決定。
有些 module 是有默認行爲的,好比 growpart
,若是 metadata 中沒有特別指定,它會自動擴展 /
分區。
因爲篇幅限制,這裏就不一一討論每一個 module 了,具體可參看文檔 https://cloudinit.readthedocs.io/en/latest/topics/modules.html
後面咱們會討論 cloud-init 典型的使用場景,其中也會涉及經常使用 module 的示例。