如有錯誤歡迎指正,期待您的寶貴意見。
cloud-init 是一個能夠啟用和初始化虛擬機器的指令碼,在虛擬機器啟動的時候,對虛擬機器進行操作,他的功能很強,廣泛適用於 OpenStack。
ubuntu14.04 /etc/cloud/cloud.cfg
# The top level settings are used as module
# and system configuration.
# A set of users which may be applied and/or used by various modules
# when a 'defa ult' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
- default
# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the above $user (ubuntu)
disable_root: true
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
# Example datasource config
# datasource:
# Ec2:
# metadata_urls: [ 'blah.com' ]
# timeout: 5 # (defaults to 50 seconds)
# max_wait: 10 # (defaults to 120 seconds)
# The modules that run in the 'init' stage
cloud_init_modules:
- migrator
- seed_random
- bootcmd
- write-files
- growpart
- resizefs
- set_hostname
- update_hostname
- update_etc_hosts
- ca-certs
- rsyslog
- users-groups
- ssh
# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
- emit_upstart
- disk_setup
- mounts
- ssh-import-id
- locale
- set-passwords
- grub-dpkg
- apt-pipelining
- apt-configure
- package-update-upgrade-install
- landscape
- timezone
- puppet
- chef
- salt-minion
- mcollective
- disable-ec2-metadata
- runcmd
- byobu
# The modules that run in the 'final' stage
cloud_final_modules:
- rightscale_userdata
- scripts-vendor
- scripts-per-once
- scripts-per-boot
- scripts-per-instance
- scripts-user
- ssh-authkey-fingerprints
- keys-to-console
- phone-home
- final-message
- power-state-change
# System and/or distro specific settings
# (not accessible to handlers/transforms)
system_info:
# This will affect which distro class gets used
distro: ubuntu
# Default user name + that default users groups (if added/used)
default_user:
name: ubuntu
lock_passwd: True
gecos: Ubuntu
groups: [adm, audio, cdrom, dialout, dip, floppy, netdev, plugdev, sudo, video]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
# Other config here will be given to the distro class and/or path classes
paths:
cloud_dir: /var/lib/cloud/
templates_dir: /etc/cloud/templates/
upstart_dir: /etc/init/
package_mirrors:
- arches: [i386, amd64]
failsafe:
primary: http://archive.ubuntu.com/ubuntu
security: http://security.ubuntu.com/ubuntu
search:
primary:
- http://%(ec2_region)s.ec2.archive.ubuntu.com/ubuntu/
- http://%(availability_zone)s.clouds.archive.ubuntu.com/ubuntu/
security: []
- arches: [armhf, armel, default]
failsafe:
primary: http://ports.ubuntu.com/ubuntu-ports
security: http://ports.ubuntu.com/ubuntu-ports
ssh_svcname: ssh
1. cloud-init 資料 3
2. cloud-init 模板安裝 3
2.1. linux 3
2.1.1. Rhel6.5安裝cloud-init 3
2.1.2. 安裝驗證 4
2.1.3. 配置修改 4
2.2. AIX 5
2.2.1. Aix7.1安裝cloud-init 5
2.2.2. 安裝驗證 5
2.2.3. 配置修改 7
3. cloud-init模板製作使用 8
3.1. 模板製作 8
3.1.1. 用命令列進行轉模板 8
3.1.2. 用horizon進行轉模板 8
3.2. 模板使用 9
3.2.1. 命令列建立虛機 9
3.2.2. Horizon建立虛機 9
4. Cloud-init工作原理 11
4.1. 理論介紹 11
4.2. 執行個體講解 13
5. 部分指令碼、配置樣本 15
5.1. 修改root使用者密碼 15
5.2. 建立新使用者、ssh登陸 16
5.3. 全網域名稱、主機名稱寫入hosts檔案 16
1. cloud-init資料
啟用引擎和 cloud-init 是擷取使用者輸入以及在部署的虛擬機器上配置作業系統和軟體的方法。Cloud-init在 OpenStack 中廣泛使用。安裝在虛機內,形成模板後隨模板使用。
cloud-init用於在建立虛擬機器時通過中繼資料服務對虛擬機器基本配置,包括常見的主機名稱,使用者,密碼等等。同時使用者可以通過user_data自訂一些資料對虛擬機器進行配置,可以有設定檔cloud-config或者直接給定指令碼,支援常見的shell cmd python指令碼(第5部分會有樣本)。
程式首先判斷作業系統類型,載入對應的模組,載入服務。
註:虛機裡不可同時安裝啟用引擎和cloud-init。 2. cloud-init模板安裝 2.1. linux
以rhel6.5為例進行安裝。
Cloud中所有模組都放在/usr/lib/python2.6/site-packages/cloudinit/config 2.1.1. Rhel6.5安裝cloud-init
準備rhel6.5虛機,安裝作業系統,配置網路後擷取cloud-init的rpm安裝包(也可以在powervc /opt/ibm/powervc/images/cloud-init 下擷取)。
使用 yum、EPEL或者任何其他軟體包管理器來安裝存放庫中cloud-init所必需的依賴項。
安裝 ftp://ftp.unicamp.br/pub/linuxpatch/cloud-init-ppc64 存放庫中 rhel6 的cloud-init依賴項。
添加 EPEL yum 存放庫以獲得相關RPM。請運行以下命令以設定 Red Hat Enterprise Linux 6的存放庫。
wget http://dl.fedoraproject.org/pub/epel/6Server/ppc64/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6*.rpm
yum install 進行安裝以下依賴包:
libselinux-python
policycoreutils-python
python-argparse
python-boto
python-cheetah
python-configobj
python-jsonpatch
python-prettytable
python-requests
最後安裝cloud-init rpm安裝包:
rpm -ivh cloud-init-0.7.4-2.el6.noarch.rpm 2.1.2. 安裝驗證
執行 cloud-init init 命令:
2.1.3. 配置修改
安裝 cloud-init 之後,請使用下列值來修改 /etc/cloud/cloud.cfg 中提供的 cloud.cfg 檔案。
#add
disable_ec2_metadata: True
#set
disable_root: 0
ssh_pwauth: 1
ssh_deletekeys: 1
清除MAC資訊:
(1)第一個檔案,清空/etc/udev/rules.d/70-persistent-net.rules檔案內容。
(2)第二個檔案,清空/lib/udev/rules.d/75-persistent-net-generator.rules內容。
(3)第三個檔案,去除/etc/sysconfig/network-scripts/ifcfg-eth0中HWADDR一行。
(4)清除 /var/log/cloud-init.log 中log資訊。 2.2. AIX
以AIX7.1為例進行安裝。 2.2.1. Aix7.1安裝cloud-init
準備建立AIX7.1虛機,安裝作業系統,配置IP資訊連結外網擷取rpm安裝包。
擷取rpm包後,進行包安裝。
安裝依賴包:
bash is needed by cloud-init-0.7.5-4.1
python-argparse is needed by cloud-init-0.7.5-4.1
python-boto >= 2.7 is needed by cloud-init-0.7.5-4.1
python-cheetah is needed by cloud-init-0.7.5-4.1
python-configobj is needed by cloud-init-0.7.5-4.1
python-jsonpatch is needed by cloud-init-0.7.5-4.1
python-oauth is needed by cloud-init-0.7.5-4.1
python-prettytable is needed by cloud-init-0.7.5-4.1
python-pyserial is needed by cloud-init-0.7.5-4.1
python-PyYAML is needed by cloud-init-0.7.5-4.1
python-requests is needed by cloud-init-0.7.5-4.1
python-setuptools is needed by cloud-init-0.7.5-4.1
python-xml is needed by cloud-init-0.7.5-4.1
python-yaml is needed by cloud-init-0.7.5-4.1
/usr/bin/python is needed by cloud-init-0.7.5-4.1
rpm -ivh cloud-init-0.7.5-4.1.aix6.1.ppc.rpm
2.2.2. 安裝驗證
安裝完成後可以利用rpm -qa查看已安裝的rpm包。
如:
# rpm -qa
AIX-rpm-7.1.3.30-1
tcl-8.4.7-3
tk-8.4.7-3
expect-5.42.1-3
bash-4.2-9
python-argparse-1.2.1-1
libyaml-0.1.4-1
unzip-6.0-2
bzip2-1.0.6-2
info-5.0-2
libiconv-1.14-1
db-4.8.24-4
expat-2.1.0-1
gmp-5.1.3-1
libffi-3.0.11-1
gettext-0.17-8
openssl-1.0.1g-1
ncurses-5.9-3
readline-6.2-3
sqlite-3.7.15.2-2
zlib-1.2.5-6
gdbm-1.10-1
python-2.7.6-1
python-setuptools-0.9.8-1
python-boto-2.34.0-1
python-cheetah-2.4.4-2
python-configobj-5.0.5-1
python-jsonpointer-1.0.c1ec3df-1
python-jsonpatch-1.8-1
python-oauth-1.0.1-1
python-prettytable-0.7.2-1
python-pyserial-2.7-1
python-PyYAML-3.11-2
python-requests-2.4.3-1
python-xml-0.8.4-1
cloud-init-0.7.5-4.1
glib2-2.33.2-1
python-devel-2.7.6-1
fdupes-1.51-1
python-six-1.3.0-1
執行/opt/freeware/bin/cloud-init init 進行驗證
2.2.3. 配置修改
修改 /opt/freeware/etc/cloud/cloud.cfg
- update_hostname 刪除此行
datasource_list: ['ConfigDrive'] 刪除
disable_ec2_metadata: True 添加此行
ssh_pwauth: 1 設定為1
disable_root: 0 設定為0
設定虛機網路模式
利用 smit tcpip 命令將虛機設定為dhcp模式。
清除cloud-init 的log資訊
清除 /var/log/cloud-init-output.log 記錄檔內容
# >/var/log/cloud-init-output.log 3. cloud-init模板製作使用 3.1. 模板製作 3.1.1. 用命令列進行轉模板
curl -i 'http://172.30.128.231:8774/v2/a8314cebd08140029b149e27c2bb37cc/servers/3721184d-981c-4763-9e1d-5b741c08eb7e/action' -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "User-Agent: python-novaclient" -H "X-Auth-Project-Id: admin" -H "X-Auth-Token:d300686b417649ac8c48f72b9ef6d1d7" -d '{"finishOnboard": {"boot_volume_id": null,"os_distro": "aix"}}'
nova image-create 3721184d-981c-4763-9e1d-5b741c08eb7e cloud_dlpar_rhel7
3.1.2. 用horizon進行轉模板
利用執行個體的快照按鈕進行轉模板操作,虛機必須關機斷電進行快照操作。
如下圖所示:
3.2. 模板使用 3.2.1. 命令列建立虛機
利用 --user-data 、--key-name、-meta 等參數傳輸資料;
--user-data :指定指令碼配置,實現修改密碼等操作。
--key-name :指定金鑰組name,實現無密碼登陸
-meta :寫入中繼資料。
建立虛機命令:
nova boot powerkvm --flavor 6 --image a48c96fd-d49a-4d0d-b6d9-1ddec52b65ba --nic net-id=f4889f14-33ab-4345-b480-92db1d411a67 --key-name key1 --user-data /root/img/pass.txt --meta abc=def
3.2.2. Horizon建立虛機
部分功能使用方法如下圖所示:
eg: ssh -i cloud_key root@172.24.23.30
4. Cloud-init工作原理 4.1. 理論介紹
註:以rhel6.5為例介紹(涉及到部分檔案路徑)。
Cloudinit通過雲平台的metadata服務或ConfigDrive等資料來源拿到該userdata後,首先會對其進行切分,並分別供各模組處理。Cloud中所有模組都放在/usr/lib/python2.6/site-packages/cloudinit/config。有設定檔可知主要分為如下三大類:cloud_init_modules、cloud_config_modules、cloud_final_modules、system_info。
這裡需要特別提到的一個模組是scripts-user,該模組負責執行userdata中的user scripts內容以及其他模組(如runcmd)產生的指令碼,因此cloudinit的設定檔將其放在了cloud_final_modules階段的近乎最後。
Cloud設定檔主要分為兩部分:
1、參數、變數定義部分。
2、模組列表部分。
常見的配置包括:設定虛擬機器的hostname、hosts檔案、設定使用者名稱密碼、更新apt-get的本機快取、調整檔案系統的大小(注意不是調整分區的大小)等。
設定檔:/etc/cloud/cloud.cfg 。
cloudinit會在虛擬機器啟動的過程中分四個階段運行,按照時間順序分為:cloud-init-local, cloud-init, cloud-config, cloud-final。
cloud-init-local階段主要是運行本地的一些初始化指令碼。cloud-init, cloud-config, cloud-final階段分別執行設定檔中名為cloud_init_modules、cloud_config_modules、cloud_final_modules下的所有模組,如果模組列表為空白,則什麼都不運行。
各模組在運行時,會根據之前定義的變數/參數的值,配置虛擬機器的狀態。
update_etc_host模組,/usr/lib/python2.6/site-packages/cloudinit/config/cc_update_etc_hosts.py。該模組用來設定主機的hosts檔案,其中就用到了hostname、fqdn、manage_etc_hosts等變數的值。模組首先嘗試從cloudinit的設定檔中讀取這些變數的值,如果沒有定義,則嘗試從其他的資料來源中擷取變數的值,例如對於openstac來講,可以從metadata service(http://169.254.169.254/latest/meta-data/hostname)擷取虛擬機器的主機名稱。如果初始模板沒有配置此參數,則可以通過建立虛機時的--user-data參數擷取相應的參數值)。
4.2. 執行個體講解
以主機名稱寫入hosts檔案為例進行介紹:
一、模板初始配置
未經設定的hosts檔案
/etc/cloud/cloud.cfg設定檔裡設定
manage_etc_hosts: True
preserve_hostname: False
manage_etc_hosts設定為true,虛機啟動的時候cloud_init_modules階段運行update_etc_hos模組,擷取變數值和相應的參數值。模板檔案/etc/cloud/templates/hosts.redhat.tmpl被利用,
重寫hosts檔案如下:
二、以自訂指令碼添加
如果出事模板裡沒有設定,可以通過nova boot建立虛機的時候指定--user-data參數擷取配置值。
nova boot testhostname --flavor 6 --image d892728d-3652-49ca-a943-e8006b0cbbb2 --nic net-id=f4889f14-33ab-4345-b480-92db1d411a67 --user-data /root/img/pass.txt
或通過horizon頁面建立虛機時指定指令碼如下:
#cloud-config
chpasswd:
list: |
root:123456
expire: False
ssh_pwauth: True
manage_etc_hosts: True
preserve_hostname: False
將全網域名稱、主機名稱成功寫入hosts檔案:
5. 部分指令碼、配置樣本
可以有設定檔cloud-config或者直接以指令碼形式給出. 5.1. 修改root使用者密碼
#cloud-config
chpasswd:
list: |
root:123456
expire: False
ssh_pwauth: True
5.2. 建立新使用者、ssh登陸
#!/bin/bash
echo "one test about user data" >>userdata
chmod 777 userdata
useradd -m me
echo -e 'me\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
passwd me <<EOF
abcabc
abcabc
EOF
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
service ssh restart
5.3. 全網域名稱、主機名稱寫入hosts檔案
#cloud-config
chpasswd:
list: |
root:123456
expire: False
ssh_pwauth: True
manage_etc_hosts: True
preserve_hostname: False