cloud-init 原理(執行的整個過程)

1、cloud-init 工做原理

cloud-init 是 linux 的一個工具,當系統啓動時,cloud-init 可從 nova metadata 服務或者 config drive 中獲取 metadata,完成包括但不限於下面的定製化工做:python

設置默認語言環境linux

設置實例主機名ubuntu

添加 ssh keys到 .ssh/authorized_keyscentos

設置用戶密碼網絡

配置網絡python2.7

安裝軟件包ssh

爲了實現 instance 定製工做,cloud-init 會按 4 個階段執行任務:工具

localspa

initpwa

config

final

cloud-init 安裝時會將這 4 個階段執行的任務以服務的形式註冊到系統中,好比在 systemd 的環境下,咱們可以看到這4個階段分別對應的服務:

local - cloud-init-local.service

init - cloud-init.service

config - cloud-config.service

final - cloud-final.service

local 階段

做爲 cloud-init 執行的第一個階段,此時 instance 還不知道該如何配置網卡,cloud-init 的任務就是從 config drive 中獲取配置信息,而後寫入 /etc/network/interfaces 文件(若是是 centos 則寫入 /etc/sysconfig/network-scripts/ifcfg-xxx)。

若是沒有 config drive,則將全部網卡配置成 dhcp 模式。這是很是關鍵的一步,只有當網卡正確配置後,才能獲取到 metadata。

關於 local 階段下一節會經過實驗詳細分析。

init, config 和 final 階段

正常狀況下,在這三個階段執行以前 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 中沒有特別指定,它會自動擴展/分區。

2、 cloud-init 的典型應用(設置 hostanme,設置用戶初始密碼,安裝軟件)

設置 hostname

cloud-init 默認會將 instance 的名字設置爲 hostname。但這樣不太方便,有時但願可以將兩者分開,可利用 cloud-init 的set_hostname模塊實現。set_hostname它會查詢 metadata 中 hostname 信息,默認值就是 instance 的名字。咱們能夠指定本身的 hostname,方法是將下面的內容傳給 cloud-init:

#cloud-config

hostname: my1.cloudman.cc

manage_etc_hosts: true

說明以下:

cloud-init 只會讀取以#cloud-config開頭的數據,因此這一行必定要寫對。

hostname: my1.cloudman.cc告訴 cloud-init 將 hostname 設置爲 my1.cloudman.cc。

manage_etc_hosts: true告訴 cloud-init 更新 /etc/hosts 文件。

接下來的問題是:如何將這些信息傳給 cloud-init?

有三幾種方法:

① instance 部署時,直接將其粘貼到Customization Script輸入框中。

② 將其保存爲文件,instance 部署時上傳(上圖 ② 所示)。

③ 將其保存爲文件,命令行nova boot或者openstack server create部署 instance 時,使用參數--user-data傳入。

部署成功後,hostname 正確設置,/etc/hosts 也相應更新。

定製用戶初始密碼

官方的 cloud image 默認只能經過 ssh key 登陸。咱們能夠利用set-passwords模塊爲用戶設置密碼並啓用密碼登陸。須要傳入的腳本以下:

#cloud-config

chpasswd:

list: |

root:123456

ubuntu:123456

expire: false

ssh_pwauth: true

說明以下:

root 和 ubuntu 用戶密碼設置爲 123456。

ssh_pwauth啓用密碼登陸。

instance 啓動後 ssh 驗證:

ubuntu 用戶 ssh 密碼登陸成功,而且可經過密碼切換到 root。

安裝軟件

標準鏡像中不可能包含咱們須要的全部軟件,定製安裝是不可避免的。一個辦法是部署完後手動安裝,另外一個辦法是經過package-update-upgrade-install模塊讓 cloud-init 自動爲咱們安裝。

須要傳入的腳本以下:

#cloud-config

apt:

primary:

- arches: [default]

search:

-http://1.2.3.4

- https://mirrors.tuna.tsinghua.edu.cn/ubuntu

search_dns: true

packages:

- pwgen

- pastebinit

- [libpython2.7, 2.7.3-0ubuntu3.1]

說明以下:

apt指定安裝源的位置,這裏爲http://1.2.3.4。若是是 yum 源則用yum_repos模塊指定,具體用法可參看官網文檔。

packages指定須要安裝的軟件包,還能夠指定具體版本。

instance 啓動後可看到 /etc/apt/sources.list 中安裝源已經更新爲http://1.2.3.4

因爲http://1.2.3.4不是一個有效的 apt 源,安裝確定會失敗,咱們能夠在 /var/log/cloud-init.log 看到失敗的信息。

雖然失敗了,但咱們至少能夠肯定以下事情:

傳入的腳本是有效的,cloud-init 確實在嘗試安裝指定的軟件。

/var/log/cloud-init.log 會完整地記錄 cloud-init 運行的全部細節,是 debug 最重要的工具。

做者:大眼傑的世界 連接:https://www.jianshu.com/p/f4fa583f022a 來源:簡書 簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。

相關文章
相關標籤/搜索